]> git.lizzy.rs Git - dragonfireclient.git/blob - builtin/common/tests/vector_spec.lua
Fix vector.from_string returning a table without vector metatable
[dragonfireclient.git] / builtin / common / tests / vector_spec.lua
1 _G.vector = {}
2 dofile("builtin/common/vector.lua")
3
4 describe("vector", function()
5         describe("new()", function()
6                 it("constructs", function()
7                         assert.same({x = 0, y = 0, z = 0}, vector.new())
8                         assert.same({x = 1, y = 2, z = 3}, vector.new(1, 2, 3))
9                         assert.same({x = 3, y = 2, z = 1}, vector.new({x = 3, y = 2, z = 1}))
10
11                         assert.is_true(vector.check(vector.new()))
12                         assert.is_true(vector.check(vector.new(1, 2, 3)))
13                         assert.is_true(vector.check(vector.new({x = 3, y = 2, z = 1})))
14
15                         local input = vector.new({ x = 3, y = 2, z = 1 })
16                         local output = vector.new(input)
17                         assert.same(input, output)
18                         assert.equal(input, output)
19                         assert.is_false(rawequal(input, output))
20                         assert.equal(input, input:new())
21                 end)
22
23                 it("throws on invalid input", function()
24                         assert.has.errors(function()
25                                 vector.new({ x = 3 })
26                         end)
27
28                         assert.has.errors(function()
29                                 vector.new({ d = 3 })
30                         end)
31                 end)
32         end)
33
34         it("zero()", function()
35                 assert.same({x = 0, y = 0, z = 0}, vector.zero())
36                 assert.same(vector.new(), vector.zero())
37                 assert.equal(vector.new(), vector.zero())
38                 assert.is_true(vector.check(vector.zero()))
39         end)
40
41         it("copy()", function()
42                 local v = vector.new(1, 2, 3)
43                 assert.same(v, vector.copy(v))
44                 assert.same(vector.new(v), vector.copy(v))
45                 assert.equal(vector.new(v), vector.copy(v))
46                 assert.is_true(vector.check(vector.copy(v)))
47         end)
48
49         it("indexes", function()
50                 local some_vector = vector.new(24, 42, 13)
51                 assert.equal(24, some_vector[1])
52                 assert.equal(24, some_vector.x)
53                 assert.equal(42, some_vector[2])
54                 assert.equal(42, some_vector.y)
55                 assert.equal(13, some_vector[3])
56                 assert.equal(13, some_vector.z)
57
58                 some_vector[1] = 100
59                 assert.equal(100, some_vector.x)
60                 some_vector.x = 101
61                 assert.equal(101, some_vector[1])
62
63                 some_vector[2] = 100
64                 assert.equal(100, some_vector.y)
65                 some_vector.y = 102
66                 assert.equal(102, some_vector[2])
67
68                 some_vector[3] = 100
69                 assert.equal(100, some_vector.z)
70                 some_vector.z = 103
71                 assert.equal(103, some_vector[3])
72         end)
73
74         it("direction()", function()
75                 local a = vector.new(1, 0, 0)
76                 local b = vector.new(1, 42, 0)
77                 assert.equal(vector.new(0, 1, 0), vector.direction(a, b))
78                 assert.equal(vector.new(0, 1, 0), a:direction(b))
79         end)
80
81         it("distance()", function()
82                 local a = vector.new(1, 0, 0)
83                 local b = vector.new(3, 42, 9)
84                 assert.is_true(math.abs(43 - vector.distance(a, b)) < 1.0e-12)
85                 assert.is_true(math.abs(43 - a:distance(b)) < 1.0e-12)
86                 assert.equal(0, vector.distance(a, a))
87                 assert.equal(0, b:distance(b))
88         end)
89
90         it("length()", function()
91                 local a = vector.new(0, 0, -23)
92                 assert.equal(0, vector.length(vector.new()))
93                 assert.equal(23, vector.length(a))
94                 assert.equal(23, a:length())
95         end)
96
97         it("normalize()", function()
98                 local a = vector.new(0, 0, -23)
99                 assert.equal(vector.new(0, 0, -1), vector.normalize(a))
100                 assert.equal(vector.new(0, 0, -1), a:normalize())
101                 assert.equal(vector.new(), vector.normalize(vector.new()))
102         end)
103
104         it("floor()", function()
105                 local a = vector.new(0.1, 0.9, -0.5)
106                 assert.equal(vector.new(0, 0, -1), vector.floor(a))
107                 assert.equal(vector.new(0, 0, -1), a:floor())
108         end)
109
110         it("round()", function()
111                 local a = vector.new(0.1, 0.9, -0.5)
112                 assert.equal(vector.new(0, 1, -1), vector.round(a))
113                 assert.equal(vector.new(0, 1, -1), a:round())
114         end)
115
116         it("apply()", function()
117                 local i = 0
118                 local f = function(x)
119                         i = i + 1
120                         return x + i
121                 end
122                 local a = vector.new(0.1, 0.9, -0.5)
123                 assert.equal(vector.new(1, 1, 0), vector.apply(a, math.ceil))
124                 assert.equal(vector.new(1, 1, 0), a:apply(math.ceil))
125                 assert.equal(vector.new(0.1, 0.9, 0.5), vector.apply(a, math.abs))
126                 assert.equal(vector.new(0.1, 0.9, 0.5), a:apply(math.abs))
127                 assert.equal(vector.new(1.1, 2.9, 2.5), vector.apply(a, f))
128                 assert.equal(vector.new(4.1, 5.9, 5.5), a:apply(f))
129         end)
130
131         it("equals()", function()
132                 local function assertE(a, b)
133                         assert.is_true(vector.equals(a, b))
134                 end
135                 local function assertNE(a, b)
136                         assert.is_false(vector.equals(a, b))
137                 end
138
139                 assertE({x = 0, y = 0, z = 0}, {x = 0, y = 0, z = 0})
140                 assertE({x = -1, y = 0, z = 1}, {x = -1, y = 0, z = 1})
141                 assertE({x = -1, y = 0, z = 1}, vector.new(-1, 0, 1))
142                 local a = {x = 2, y = 4, z = -10}
143                 assertE(a, a)
144                 assertNE({x = -1, y = 0, z = 1}, a)
145
146                 assert.equal(vector.new(1, 2, 3), vector.new(1, 2, 3))
147                 assert.is_true(vector.new(1, 2, 3):equals(vector.new(1, 2, 3)))
148                 assert.not_equal(vector.new(1, 2, 3), vector.new(1, 2, 4))
149                 assert.is_true(vector.new(1, 2, 3) == vector.new(1, 2, 3))
150                 assert.is_false(vector.new(1, 2, 3) == vector.new(1, 3, 3))
151         end)
152
153         it("metatable is same", function()
154                 local a = vector.new()
155                 local b = vector.new(1, 2, 3)
156
157                 assert.equal(true, vector.check(a))
158                 assert.equal(true, vector.check(b))
159
160                 assert.equal(vector.metatable, getmetatable(a))
161                 assert.equal(vector.metatable, getmetatable(b))
162                 assert.equal(vector.metatable, a.metatable)
163         end)
164
165         it("sort()", function()
166                 local a = vector.new(1, 2, 3)
167                 local b = vector.new(0.5, 232, -2)
168                 local sorted = {vector.new(0.5, 2, -2), vector.new(1, 232, 3)}
169                 assert.same(sorted, {vector.sort(a, b)})
170                 assert.same(sorted, {a:sort(b)})
171         end)
172
173         it("angle()", function()
174                 assert.equal(math.pi, vector.angle(vector.new(-1, -2, -3), vector.new(1, 2, 3)))
175                 assert.equal(math.pi/2, vector.new(0, 1, 0):angle(vector.new(1, 0, 0)))
176         end)
177
178         it("dot()", function()
179                 assert.equal(-14, vector.dot(vector.new(-1, -2, -3), vector.new(1, 2, 3)))
180                 assert.equal(0, vector.new():dot(vector.new(1, 2, 3)))
181         end)
182
183         it("cross()", function()
184                 local a = vector.new(-1, -2, 0)
185                 local b = vector.new(1, 2, 3)
186                 assert.equal(vector.new(-6, 3, 0), vector.cross(a, b))
187                 assert.equal(vector.new(-6, 3, 0), a:cross(b))
188         end)
189
190         it("offset()", function()
191                 assert.same({x = 41, y = 52, z = 63}, vector.offset(vector.new(1, 2, 3), 40, 50, 60))
192                 assert.equal(vector.new(41, 52, 63), vector.offset(vector.new(1, 2, 3), 40, 50, 60))
193                 assert.equal(vector.new(41, 52, 63), vector.new(1, 2, 3):offset(40, 50, 60))
194         end)
195
196         it("is()", function()
197                 local some_table1 = {foo = 13, [42] = 1, "bar", 2}
198                 local some_table2 = {1, 2, 3}
199                 local some_table3 = {x = 1, 2, 3}
200                 local some_table4 = {1, 2, z = 3}
201                 local old = {x = 1, y = 2, z = 3}
202                 local real = vector.new(1, 2, 3)
203
204                 assert.is_false(vector.check(nil))
205                 assert.is_false(vector.check(1))
206                 assert.is_false(vector.check(true))
207                 assert.is_false(vector.check("foo"))
208                 assert.is_false(vector.check(some_table1))
209                 assert.is_false(vector.check(some_table2))
210                 assert.is_false(vector.check(some_table3))
211                 assert.is_false(vector.check(some_table4))
212                 assert.is_false(vector.check(old))
213                 assert.is_true(vector.check(real))
214                 assert.is_true(real:check())
215         end)
216
217         it("global pairs", function()
218                 local out = {}
219                 local vec = vector.new(10, 20, 30)
220                 for k, v in pairs(vec) do
221                         out[k] = v
222                 end
223                 assert.same({x = 10, y = 20, z = 30}, out)
224         end)
225
226         it("abusing works", function()
227                 local v = vector.new(1, 2, 3)
228                 v.a = 1
229                 assert.equal(1, v.a)
230
231                 local a_is_there = false
232                 for key, value in pairs(v) do
233                         if key == "a" then
234                                 a_is_there = true
235                                 assert.equal(value, 1)
236                                 break
237                         end
238                 end
239                 assert.is_true(a_is_there)
240         end)
241
242         it("add()", function()
243                 local a = vector.new(1, 2, 3)
244                 local b = vector.new(1, 4, 3)
245                 local c = vector.new(2, 6, 6)
246                 assert.equal(c, vector.add(a, {x = 1, y = 4, z = 3}))
247                 assert.equal(c, vector.add(a, b))
248                 assert.equal(c, a:add(b))
249                 assert.equal(c, a + b)
250                 assert.equal(c, b + a)
251         end)
252
253         it("subtract()", function()
254                 local a = vector.new(1, 2, 3)
255                 local b = vector.new(2, 4, 3)
256                 local c = vector.new(-1, -2, 0)
257                 assert.equal(c, vector.subtract(a, {x = 2, y = 4, z = 3}))
258                 assert.equal(c, vector.subtract(a, b))
259                 assert.equal(c, a:subtract(b))
260                 assert.equal(c, a - b)
261                 assert.equal(c, -b + a)
262         end)
263
264         it("multiply()", function()
265                 local a = vector.new(1, 2, 3)
266                 local b = vector.new(2, 4, 3)
267                 local c = vector.new(2, 8, 9)
268                 local s = 2
269                 local d = vector.new(2, 4, 6)
270                 assert.equal(c, vector.multiply(a, {x = 2, y = 4, z = 3}))
271                 assert.equal(c, vector.multiply(a, b))
272                 assert.equal(d, vector.multiply(a, s))
273                 assert.equal(d, a:multiply(s))
274                 assert.equal(d, a * s)
275                 assert.equal(d, s * a)
276                 assert.equal(-a, -1 * a)
277         end)
278
279         it("divide()", function()
280                 local a = vector.new(1, 2, 3)
281                 local b = vector.new(2, 4, 3)
282                 local c = vector.new(0.5, 0.5, 1)
283                 local s = 2
284                 local d = vector.new(0.5, 1, 1.5)
285                 assert.equal(c, vector.divide(a, {x = 2, y = 4, z = 3}))
286                 assert.equal(c, vector.divide(a, b))
287                 assert.equal(d, vector.divide(a, s))
288                 assert.equal(d, a:divide(s))
289                 assert.equal(d, a / s)
290                 assert.equal(d, 1/s * a)
291                 assert.equal(-a, a / -1)
292         end)
293
294         it("to_string()", function()
295                 local v = vector.new(1, 2, 3.14)
296                 assert.same("(1, 2, 3.14)", vector.to_string(v))
297                 assert.same("(1, 2, 3.14)", v:to_string())
298                 assert.same("(1, 2, 3.14)", tostring(v))
299         end)
300
301         it("from_string()", function()
302                 local v = vector.new(1, 2, 3.14)
303                 assert.is_true(vector.check(vector.from_string("(1, 2, 3.14)")))
304                 assert.same({v, 13}, {vector.from_string("(1, 2, 3.14)")})
305                 assert.same({v, 12}, {vector.from_string("(1,2 ,3.14)")})
306                 assert.same({v, 12}, {vector.from_string("(1,2,3.14,)")})
307                 assert.same({v, 11}, {vector.from_string("(1 2 3.14)")})
308                 assert.same({v, 15}, {vector.from_string("( 1, 2, 3.14 )")})
309                 assert.same({v, 15}, {vector.from_string(" ( 1, 2, 3.14) ")})
310                 assert.same({vector.new(), 8}, {vector.from_string("(0,0,0) ( 1, 2, 3.14) ")})
311                 assert.same({v, 22}, {vector.from_string("(0,0,0) ( 1, 2, 3.14) ", 8)})
312                 assert.same({v, 22}, {vector.from_string("(0,0,0) ( 1, 2, 3.14) ", 9)})
313                 assert.same(nil, vector.from_string("nothing"))
314         end)
315
316         -- This function is needed because of floating point imprecision.
317         local function almost_equal(a, b)
318                 if type(a) == "number" then
319                         return math.abs(a - b) < 0.00000000001
320                 end
321                 return vector.distance(a, b) < 0.000000000001
322         end
323
324         describe("rotate_around_axis()", function()
325                 it("rotates", function()
326                         assert.True(almost_equal({x = -1, y = 0, z = 0},
327                                 vector.rotate_around_axis({x = 1, y = 0, z = 0}, {x = 0, y = 1, z = 0}, math.pi)))
328                         assert.True(almost_equal({x = 0, y = 1, z = 0},
329                                 vector.rotate_around_axis({x = 0, y = 0, z = 1}, {x = 1, y = 0, z = 0}, math.pi / 2)))
330                         assert.True(almost_equal({x = 4, y = 1, z = 1},
331                                 vector.rotate_around_axis({x = 4, y = 1, z = 1}, {x = 4, y = 1, z = 1}, math.pi / 6)))
332                 end)
333                 it("keeps distance to axis", function()
334                         local rotate1 = {x = 1, y = 3, z = 1}
335                         local axis1 = {x = 1, y = 3, z = 2}
336                         local rotated1 = vector.rotate_around_axis(rotate1, axis1, math.pi / 13)
337                         assert.True(almost_equal(vector.distance(axis1, rotate1), vector.distance(axis1, rotated1)))
338                         local rotate2 = {x = 1, y = 1, z = 3}
339                         local axis2 = {x = 2, y = 6, z = 100}
340                         local rotated2 = vector.rotate_around_axis(rotate2, axis2, math.pi / 23)
341                         assert.True(almost_equal(vector.distance(axis2, rotate2), vector.distance(axis2, rotated2)))
342                         local rotate3 = {x = 1, y = -1, z = 3}
343                         local axis3 = {x = 2, y = 6, z = 100}
344                         local rotated3 = vector.rotate_around_axis(rotate3, axis3, math.pi / 2)
345                         assert.True(almost_equal(vector.distance(axis3, rotate3), vector.distance(axis3, rotated3)))
346                 end)
347                 it("rotates back", function()
348                         local rotate1 = {x = 1, y = 3, z = 1}
349                         local axis1 = {x = 1, y = 3, z = 2}
350                         local rotated1 = vector.rotate_around_axis(rotate1, axis1, math.pi / 13)
351                         rotated1 = vector.rotate_around_axis(rotated1, axis1, -math.pi / 13)
352                         assert.True(almost_equal(rotate1, rotated1))
353                         local rotate2 = {x = 1, y = 1, z = 3}
354                         local axis2 = {x = 2, y = 6, z = 100}
355                         local rotated2 = vector.rotate_around_axis(rotate2, axis2, math.pi / 23)
356                         rotated2 = vector.rotate_around_axis(rotated2, axis2, -math.pi / 23)
357                         assert.True(almost_equal(rotate2, rotated2))
358                         local rotate3 = {x = 1, y = -1, z = 3}
359                         local axis3 = {x = 2, y = 6, z = 100}
360                         local rotated3 = vector.rotate_around_axis(rotate3, axis3, math.pi / 2)
361                         rotated3 = vector.rotate_around_axis(rotated3, axis3, -math.pi / 2)
362                         assert.True(almost_equal(rotate3, rotated3))
363                 end)
364                 it("is right handed", function()
365                         local v_before1 = {x = 0, y = 1, z = -1}
366                         local v_after1 = vector.rotate_around_axis(v_before1, {x = 1, y = 0, z = 0}, math.pi / 4)
367                         assert.True(almost_equal(vector.normalize(vector.cross(v_after1, v_before1)), {x = 1, y = 0, z = 0}))
368
369                         local v_before2 = {x = 0, y = 3, z = 4}
370                         local v_after2 = vector.rotate_around_axis(v_before2, {x = 1, y = 0, z = 0},  2 * math.pi / 5)
371                         assert.True(almost_equal(vector.normalize(vector.cross(v_after2, v_before2)), {x = 1, y = 0, z = 0}))
372
373                         local v_before3 = {x = 1, y = 0, z = -1}
374                         local v_after3 = vector.rotate_around_axis(v_before3, {x = 0, y = 1, z = 0}, math.pi / 4)
375                         assert.True(almost_equal(vector.normalize(vector.cross(v_after3, v_before3)), {x = 0, y = 1, z = 0}))
376
377                         local v_before4 = {x = 3, y = 0, z = 4}
378                         local v_after4 = vector.rotate_around_axis(v_before4, {x = 0, y = 1, z = 0}, 2 * math.pi / 5)
379                         assert.True(almost_equal(vector.normalize(vector.cross(v_after4, v_before4)), {x = 0, y = 1, z = 0}))
380
381                         local v_before5 = {x = 1, y = -1, z = 0}
382                         local v_after5 = vector.rotate_around_axis(v_before5, {x = 0, y = 0, z = 1}, math.pi / 4)
383                         assert.True(almost_equal(vector.normalize(vector.cross(v_after5, v_before5)), {x = 0, y = 0, z = 1}))
384
385                         local v_before6 = {x = 3, y = 4, z = 0}
386                         local v_after6 = vector.rotate_around_axis(v_before6, {x = 0, y = 0, z = 1}, 2 * math.pi / 5)
387                         assert.True(almost_equal(vector.normalize(vector.cross(v_after6, v_before6)), {x = 0, y = 0, z = 1}))
388                 end)
389         end)
390
391         describe("rotate()", function()
392                 it("rotates", function()
393                         assert.True(almost_equal({x = -1, y = 0, z = 0},
394                                 vector.rotate({x = 1, y = 0, z = 0}, {x = 0, y = math.pi, z = 0})))
395                         assert.True(almost_equal({x = 0, y = -1, z = 0},
396                                 vector.rotate({x = 1, y = 0, z = 0}, {x = 0, y = 0, z = math.pi / 2})))
397                         assert.True(almost_equal({x = 1, y = 0, z = 0},
398                                 vector.rotate({x = 1, y = 0, z = 0}, {x = math.pi / 123, y = 0, z = 0})))
399                 end)
400                 it("is counterclockwise", function()
401                         local v_before1 = {x = 0, y = 1, z = -1}
402                         local v_after1 = vector.rotate(v_before1, {x = math.pi / 4, y = 0, z = 0})
403                         assert.True(almost_equal(vector.normalize(vector.cross(v_after1, v_before1)), {x = 1, y = 0, z = 0}))
404
405                         local v_before2 = {x = 0, y = 3, z = 4}
406                         local v_after2 = vector.rotate(v_before2, {x = 2 * math.pi / 5, y = 0, z = 0})
407                         assert.True(almost_equal(vector.normalize(vector.cross(v_after2, v_before2)), {x = 1, y = 0, z = 0}))
408
409                         local v_before3 = {x = 1, y = 0, z = -1}
410                         local v_after3 = vector.rotate(v_before3, {x = 0, y = math.pi / 4, z = 0})
411                         assert.True(almost_equal(vector.normalize(vector.cross(v_after3, v_before3)), {x = 0, y = 1, z = 0}))
412
413                         local v_before4 = {x = 3, y = 0, z = 4}
414                         local v_after4 = vector.rotate(v_before4, {x = 0, y = 2 * math.pi / 5, z = 0})
415                         assert.True(almost_equal(vector.normalize(vector.cross(v_after4, v_before4)), {x = 0, y = 1, z = 0}))
416
417                         local v_before5 = {x = 1, y = -1, z = 0}
418                         local v_after5 = vector.rotate(v_before5, {x = 0, y = 0, z = math.pi / 4})
419                         assert.True(almost_equal(vector.normalize(vector.cross(v_after5, v_before5)), {x = 0, y = 0, z = 1}))
420
421                         local v_before6 = {x = 3, y = 4, z = 0}
422                         local v_after6 = vector.rotate(v_before6, {x = 0, y = 0, z = 2 * math.pi / 5})
423                         assert.True(almost_equal(vector.normalize(vector.cross(v_after6, v_before6)), {x = 0, y = 0, z = 1}))
424                 end)
425         end)
426
427         it("dir_to_rotation()", function()
428                 -- Comparing rotations (pitch, yaw, roll) is hard because of certain ambiguities,
429                 -- e.g. (pi, 0, pi) looks exactly the same as (0, pi, 0)
430                 -- So instead we convert the rotation back to vectors and compare these.
431                 local function forward_at_rot(rot)
432                         return vector.rotate(vector.new(0, 0, 1), rot)
433                 end
434                 local function up_at_rot(rot)
435                         return vector.rotate(vector.new(0, 1, 0), rot)
436                 end
437                 local rot1 = vector.dir_to_rotation({x = 1, y = 0, z = 0}, {x = 0, y = 1, z = 0})
438                 assert.True(almost_equal({x = 1, y = 0, z = 0}, forward_at_rot(rot1)))
439                 assert.True(almost_equal({x = 0, y = 1, z = 0}, up_at_rot(rot1)))
440                 local rot2 = vector.dir_to_rotation({x = 1, y = 1, z = 0}, {x = 0, y = 0, z = 1})
441                 assert.True(almost_equal({x = 1/math.sqrt(2), y = 1/math.sqrt(2), z = 0}, forward_at_rot(rot2)))
442                 assert.True(almost_equal({x = 0, y = 0, z = 1}, up_at_rot(rot2)))
443                 for i = 1, 1000 do
444                         local rand_vec = vector.new(math.random(), math.random(), math.random())
445                         if vector.length(rand_vec) ~= 0 then
446                                 local rot_1 = vector.dir_to_rotation(rand_vec)
447                                 local rot_2 = {
448                                         x = math.atan2(rand_vec.y, math.sqrt(rand_vec.z * rand_vec.z + rand_vec.x * rand_vec.x)),
449                                         y = -math.atan2(rand_vec.x, rand_vec.z),
450                                         z = 0
451                                 }
452                                 assert.True(almost_equal(rot_1, rot_2))
453                         end
454                 end
455
456         end)
457 end)