Mercurial > games > semicongine
comparison tests/test_vector.nim @ 16:617c6dcddbe2
add: matrix multiplications, tests
| author | Sam <sam@basx.dev> | 
|---|---|
| date | Fri, 30 Dec 2022 15:56:17 +0700 | 
| parents | a571db114152 | 
| children | 547f3a374271 | 
   comparison
  equal
  deleted
  inserted
  replaced
| 15:dde536a70483 | 16:617c6dcddbe2 | 
|---|---|
| 1 import random | 1 import random | 
| 2 import math | 2 import math | 
| 3 | 3 | 
| 4 import vector | 4 import vector | 
| 5 import matrix | |
| 6 | 5 | 
| 7 | 6 | 
| 8 proc echoInfo(v: Vec) = | 7 proc echoInfo(v: Vec) = | 
| 9 echo v | 8 echo v | 
| 10 echo " Length: ", v.length | 9 echo " Length: ", v.length | 
| 168 echo "X: ", Xi | 167 echo "X: ", Xi | 
| 169 echo "Y: ", Yi | 168 echo "Y: ", Yi | 
| 170 echo "Z: ", Zi | 169 echo "Z: ", Zi | 
| 171 | 170 | 
| 172 | 171 | 
| 173 template withAllIntegerMats(stuff: untyped) = | |
| 174 stuff(Mat22[int32]) | |
| 175 stuff(Mat23[int32]) | |
| 176 stuff(Mat32[int32]) | |
| 177 stuff(Mat33[int32]) | |
| 178 stuff(Mat34[int32]) | |
| 179 stuff(Mat43[int32]) | |
| 180 stuff(Mat44[int32]) | |
| 181 stuff(Mat22[int64]) | |
| 182 stuff(Mat23[int64]) | |
| 183 stuff(Mat32[int64]) | |
| 184 stuff(Mat33[int64]) | |
| 185 stuff(Mat34[int64]) | |
| 186 stuff(Mat43[int64]) | |
| 187 stuff(Mat44[int64]) | |
| 188 | |
| 189 template withAllFloatMats(stuff: untyped) = | |
| 190 stuff(Mat22[float32]) | |
| 191 stuff(Mat23[float32]) | |
| 192 stuff(Mat32[float32]) | |
| 193 stuff(Mat33[float32]) | |
| 194 stuff(Mat34[float32]) | |
| 195 stuff(Mat43[float32]) | |
| 196 stuff(Mat44[float32]) | |
| 197 stuff(Mat22[float64]) | |
| 198 stuff(Mat23[float64]) | |
| 199 stuff(Mat32[float64]) | |
| 200 stuff(Mat33[float64]) | |
| 201 stuff(Mat34[float64]) | |
| 202 stuff(Mat43[float64]) | |
| 203 stuff(Mat44[float64]) | |
| 204 | |
| 205 template withAllMats(stuff: untyped) = | |
| 206 stuff(Mat22[int]) | |
| 207 stuff(Mat23[int]) | |
| 208 stuff(Mat32[int]) | |
| 209 stuff(Mat33[int]) | |
| 210 stuff(Mat34[int]) | |
| 211 stuff(Mat43[int]) | |
| 212 stuff(Mat44[int]) | |
| 213 stuff(Mat22[float]) | |
| 214 stuff(Mat23[float]) | |
| 215 stuff(Mat32[float]) | |
| 216 stuff(Mat33[float]) | |
| 217 stuff(Mat34[float]) | |
| 218 stuff(Mat43[float]) | |
| 219 stuff(Mat44[float]) | |
| 220 | |
| 221 template testTranspose(t: typedesc) = | |
| 222 echo "testTranspose: ", t | |
| 223 let m = t().randomized() | |
| 224 assert m == m.transposed().transposed() | |
| 225 | |
| 226 template testAssignI(t: typedesc) = | |
| 227 echo "testAssignI: ", t | |
| 228 var m = t() | |
| 229 for i in 0 ..< t.data.len: | |
| 230 m[rand(0 ..< m.rowCount), rand(0 ..< m.columnCount)] = rand(0'i32 .. 100'i32) | |
| 231 | |
| 232 template testAssignF(t: typedesc) = | |
| 233 echo "testAssignF: ", t | |
| 234 var m = t() | |
| 235 for i in 0 ..< t.data.len: | |
| 236 m[rand(0 ..< m.rowCount), rand(0 ..< m.columnCount)] = rand(100'f) | |
| 237 | |
| 238 template testRowCols(t: typedesc) = | |
| 239 echo "testRowCols: ", t | |
| 240 var m = t().randomized() | |
| 241 for i in 0 ..< m.rowCount: | |
| 242 echo m.row(i) | |
| 243 for i in 0 ..< m.columnCount: | |
| 244 echo m.col(i) | |
| 245 | |
| 246 | |
| 247 proc testMatrix() = | |
| 248 withAllMats(testTranspose) | |
| 249 withAllIntegerMats(testAssignI) | |
| 250 withAllFloatMats(testAssignF) | |
| 251 withAllMats(testRowCols) | |
| 252 | |
| 253 randomize() | 172 randomize() | 
| 254 testVector() | 173 testVector() | 
| 255 testMatrix() | 
