comparison tests/test_vector.nim @ 13:a571db114152

add: vector/matrix modules, tests, nim config script
author Sam <sam@basx.dev>
date Wed, 28 Dec 2022 11:42:21 +0700
parents
children 617c6dcddbe2
comparison
equal deleted inserted replaced
12:9e5fe647ff91 13:a571db114152
1 import random
2 import math
3
4 import vector
5 import matrix
6
7
8 proc echoInfo(v: Vec) =
9 echo v
10 echo " Length: ", v.length
11 echo " Normlized: ", v.normalized
12 echo " negated: ", -v
13
14 proc echoAdd[T, U](v1: T, v2: U) =
15 echo v1, " + ", v2, " = ", v1 + v2
16 proc echoSub[T, U](v1: T, v2: U) =
17 echo v1, " - ", v2, " = ", v1 - v2
18 proc echoMul[T, U](v1: T, v2: U) =
19 echo v1, " * ", v2, " = ", v1 * v2
20 proc echoDiv[T, U](v1: T, v2: U) =
21 echo v1, " / ", v2, " = ", v1 / v2
22 proc echoDot[T, U](v1: T, v2: U) =
23 echo v1, " o ", v2, " = ", v1.dot(v2)
24 proc echoCross[T, U](v1: T, v2: U) =
25 echo v1, " x ", v2, " = ", v1.cross(v2)
26
27 proc randVec2I(): auto = newVec2(rand(1 .. 10), rand(1 .. 10))
28 proc randVec2F(): auto = newVec2(rand(10'f) + 0.01, rand(10'f) + 0.01)
29 proc randVec3I(): auto = newVec3(rand(1 .. 10), rand(1 .. 10), rand(1 .. 10))
30 proc randVec3F(): auto = newVec3(rand(10'f) + 0.01, rand(10'f) + 0.01, rand(10'f) + 0.01)
31 proc randVec4I(): auto = newVec4(rand(1 .. 10), rand(1 .. 10), rand(1 .. 10), rand(1 .. 10))
32 proc randVec4F(): auto = newVec4(rand(10'f) + 0.01, rand(10'f) + 0.01, rand(10'f) + 0.01, rand(10'f) + 0.01)
33
34
35 proc testVector() =
36 echoInfo(randVec2I())
37 echoInfo(randVec2F())
38 echoInfo(randVec3I())
39 echoInfo(randVec3F())
40 echoInfo(randVec4I())
41 echoInfo(randVec4F())
42
43 # test math operations vector-vector
44 echoAdd(randVec2I(), randVec2I())
45 echoAdd(randVec2F(), randVec2F())
46 echoAdd(randVec3I(), randVec3I())
47 echoAdd(randVec3F(), randVec3F())
48 echoAdd(randVec4I(), randVec4I())
49 echoAdd(randVec4F(), randVec4F())
50 echoSub(randVec2I(), randVec2I())
51 echoSub(randVec2F(), randVec2F())
52 echoSub(randVec3I(), randVec3I())
53 echoSub(randVec3F(), randVec3F())
54 echoSub(randVec4I(), randVec4I())
55 echoSub(randVec4F(), randVec4F())
56 echoMul(randVec2I(), randVec2I())
57 echoMul(randVec2F(), randVec2F())
58 echoMul(randVec3I(), randVec3I())
59 echoMul(randVec3F(), randVec3F())
60 echoMul(randVec4I(), randVec4I())
61 echoMul(randVec4F(), randVec4F())
62 echoDiv(randVec2I(), randVec2I())
63 echoDiv(randVec2F(), randVec2F())
64 echoDiv(randVec3I(), randVec3I())
65 echoDiv(randVec3F(), randVec3F())
66 echoDiv(randVec4I(), randVec4I())
67 echoDiv(randVec4F(), randVec4F())
68 echoDot(randVec2I(), randVec2I())
69 echoDot(randVec2F(), randVec2F())
70 echoDot(randVec3I(), randVec3I())
71 echoDot(randVec3F(), randVec3F())
72 echoDot(randVec4I(), randVec4I())
73 echoDot(randVec4F(), randVec4F())
74 echoCross(randVec3I(), randVec3I())
75 echoCross(randVec3F(), randVec3F())
76
77
78 # test math operations vector-scalar
79 echoAdd(randVec2I(), rand(1 .. 10))
80 echoAdd(randVec2F(), rand(10'f))
81 echoAdd(randVec3I(), rand(1 .. 10))
82 echoAdd(randVec3F(), rand(10'f))
83 echoAdd(randVec4I(), rand(1 .. 10))
84 echoAdd(randVec4F(), rand(10'f))
85 echoSub(randVec2I(), rand(1 .. 10))
86 echoSub(randVec2F(), rand(10'f))
87 echoSub(randVec3I(), rand(1 .. 10))
88 echoSub(randVec3F(), rand(10'f))
89 echoSub(randVec4I(), rand(1 .. 10))
90 echoSub(randVec4F(), rand(10'f))
91 echoMul(randVec2I(), rand(1 .. 10))
92 echoMul(randVec2F(), rand(10'f))
93 echoMul(randVec3I(), rand(1 .. 10))
94 echoMul(randVec3F(), rand(10'f))
95 echoMul(randVec4I(), rand(1 .. 10))
96 echoMul(randVec4F(), rand(10'f))
97 echoDiv(randVec2I(), rand(1 .. 10))
98 echoDiv(randVec2F(), rand(10'f))
99 echoDiv(randVec3I(), rand(1 .. 10))
100 echoDiv(randVec3F(), rand(10'f))
101 echoDiv(randVec4I(), rand(1 .. 10))
102 echoDiv(randVec4F(), rand(10'f))
103
104 # test math operations scalar-vector
105 echoAdd(rand(1 .. 10), randVec2I())
106 echoAdd(rand(10'f), randVec2F())
107 echoAdd(rand(1 .. 10), randVec3I())
108 echoAdd(rand(10'f), randVec3F())
109 echoAdd(rand(1 .. 10), randVec4I())
110 echoAdd(rand(10'f), randVec4F())
111 echoSub(rand(1 .. 10), randVec2I())
112 echoSub(rand(10'f), randVec2F())
113 echoSub(rand(1 .. 10), randVec3I())
114 echoSub(rand(10'f), randVec3F())
115 echoSub(rand(1 .. 10), randVec4I())
116 echoSub(rand(10'f), randVec4F())
117 echoMul(rand(1 .. 10), randVec2I())
118 echoMul(rand(10'f), randVec2F())
119 echoMul(rand(1 .. 10), randVec3I())
120 echoMul(rand(10'f), randVec3F())
121 echoMul(rand(1 .. 10), randVec4I())
122 echoMul(rand(10'f), randVec4F())
123 echoDiv(rand(1 .. 10), randVec2I())
124 echoDiv(rand(10'f), randVec2F())
125 echoDiv(rand(1 .. 10), randVec3I())
126 echoDiv(rand(10'f), randVec3F())
127 echoDiv(rand(1 .. 10), randVec4I())
128 echoDiv(rand(10'f), randVec4F())
129
130 # test attribute syntax sugar
131 echo "float2int ", to[int](randVec2F())
132 echo "int2float ", to[float](randVec2I())
133 echo "float2int ", to[int](randVec3F())
134 echo "int2float ", to[float](randVec3I())
135 echo "float2int ", to[int](randVec3F())
136 echo "int2float ", to[float](randVec3I())
137
138 echo "V2I.xx: ", randVec2I().xx
139 echo "V2I.yx: ", randVec2I().xy
140 echo "V2F.xx: ", randVec2F().xx
141 echo "V2F.yx: ", randVec2F().yx
142 echo "V2I.rr: ", randVec2I().rr
143 echo "V2I.gr: ", randVec2I().gr
144 echo "V2F.rr: ", randVec2F().rr
145 echo "V2F.gr: ", randVec2F().gr
146
147 echo "V3I.yyy: ", randVec3I().yyy
148 echo "V3I.yxz: ", randVec3I().xyz
149 echo "V3F.yyy: ", randVec3F().yyy
150 echo "V3F.yxz: ", randVec3F().yxz
151 echo "V3I.ggg: ", randVec3I().ggg
152 echo "V3I.grb: ", randVec3I().grb
153 echo "V3F.ggg: ", randVec3F().ggg
154 echo "V3F.grb: ", randVec3F().grb
155
156 echo "V4I.zzzz: ", randVec4I().zzzz
157 echo "V4I.yxzw: ", randVec4I().xyzw
158 echo "V4F.zzzz: ", randVec4F().zzzz
159 echo "V4F.yxzw: ", randVec4F().yxzw
160 echo "V4I.bbbb: ", randVec4I().bbbb
161 echo "V4I.grba: ", randVec4I().grba
162 echo "V4F.bbbb: ", randVec4F().bbbb
163 echo "V4F.grba: ", randVec4F().grba
164
165 echo "X: ", X
166 echo "Y: ", Y
167 echo "Z: ", Z
168 echo "X: ", Xi
169 echo "Y: ", Yi
170 echo "Z: ", Zi
171
172
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()
254 testVector()
255 testMatrix()