From 96a63ce58147365bbb1e62a46f3dfa966270f5a3 Mon Sep 17 00:00:00 2001 From: karai17 Date: Sun, 24 Jul 2016 11:35:48 -0300 Subject: [PATCH] Added a few more mat4 tests --- modules/mat4.lua | 4 ---- spec/mat4_spec.lua | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/modules/mat4.lua b/modules/mat4.lua index d75c6bb..9370504 100644 --- a/modules/mat4.lua +++ b/modules/mat4.lua @@ -642,8 +642,6 @@ function mat4_mt.__index(t, k) if type(k) == "number" then return t._m[k-1] end - elseif type(k) == "number" then - return t._m[k] end return rawget(mat4, k) @@ -654,8 +652,6 @@ function mat4_mt.__newindex(t, k, v) if type(k) == "number" then t._m[k-1] = v end - elseif type(k) == "number" then - t._m[k] = v end rawset(mat4, k, v) diff --git a/spec/mat4_spec.lua b/spec/mat4_spec.lua index b4afa64..7bac16e 100644 --- a/spec/mat4_spec.lua +++ b/spec/mat4_spec.lua @@ -118,6 +118,7 @@ describe("mat4:", function() 4, 8, 12, 16 } local c = mat4():mul(a, b) + local d = a * b assert.is.equal(30, c[1]) assert.is.equal(70, c[2]) assert.is.equal(110, c[3]) @@ -134,6 +135,7 @@ describe("mat4:", function() assert.is.equal(382, c[14]) assert.is.equal(614, c[15]) assert.is.equal(846, c[16]) + assert.is.equal(c, d) end) it("multiplies a matrix and a vec4", function() @@ -145,10 +147,16 @@ describe("mat4:", function() } local b = { 10, 20, 30, 40 } local c = mat4.mul_vec4({}, a, b) + local d = a * b assert.is.equal(900, c[1]) assert.is.equal(1000, c[2]) assert.is.equal(1100, c[3]) assert.is.equal(1200, c[4]) + + assert.is.equal(c[1], d[1]) + assert.is.equal(c[2], d[2]) + assert.is.equal(c[3], d[3]) + assert.is.equal(c[4], d[4]) end) it("scales a matrix", function() @@ -184,6 +192,14 @@ describe("mat4:", function() local b = mat4():invert(a) local c = mat4():mul(a, b) assert.is.equal(mat4(), c) + + local d = mat4() + d:rotate(d, math.pi/4, vec3.unit_y) + d:translate(d, vec3(4, 5, 6)) + + local e = -d + local f = mat4():mul(d, e) + assert.is.equal(mat4(), f) end) it("transposes a matrix", function() @@ -327,6 +343,26 @@ describe("mat4:", function() assert.is_true(utils.tolerance( 1 -f.far.c, 0.001)) assert.is_true(utils.tolerance( 1000-f.far.d, 0.001)) end) + + it("checks to see if data is a valid matrix (not a table)", function() + assert.is_not_true(mat4.is_mat4(0)) + end) + + it("checks to see if data is a valid matrix (invalid data)", function() + assert.is_not_true(mat4.is_mat4({})) + end) + + it("gets a string representation of a matrix", function() + local a = mat4() + local b = a:to_string() + local z = "+0.000" + local o = "+1.000" + local s = string.format( + "[ %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s ]", + o, z, z, z, z, o, z, z, z, z, o, z, z, z ,z, o + ) + assert.is.equal(s, b) + end) end) --[[