1/* Copyright (c) 2013, Brandon Jones, Colin MacKenzie IV. All rights reserved. 2 3Redistribution and use in source and binary forms, with or without modification, 4are permitted provided that the following conditions are met: 5 6 * Redistributions of source code must retain the above copyright notice, this 7 list of conditions and the following disclaimer. 8 * Redistributions in binary form must reproduce the above copyright notice, 9 this list of conditions and the following disclaimer in the documentation 10 and/or other materials provided with the distribution. 11 12THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 13ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 14WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 15DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR 16ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 17(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 18LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 19ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 20(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 21SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ 22 23describe("mat3", function() { 24 var out, matA, matB, identity, result; 25 26 beforeEach(function() { 27 matA = [1, 0, 0, 28 0, 1, 0, 29 1, 2, 1]; 30 31 matB = [1, 0, 0, 32 0, 1, 0, 33 3, 4, 1]; 34 35 out = [0, 0, 0, 36 0, 0, 0, 37 0, 0, 0]; 38 39 identity = [1, 0, 0, 40 0, 1, 0, 41 0, 0, 1]; 42 }); 43 44 describe("fromMat4", function() { 45 beforeEach(function() { 46 result = mat3.fromMat4(out, [ 1, 2, 3, 4, 47 5, 6, 7, 8, 48 9,10,11,12, 49 13,14,15,16]); }); 50 51 it("should return out", function() { expect(result).toBe(out); }); 52 53 it("should calculate proper mat3", function() { 54 expect(out).toBeEqualish([ 1, 2, 3, 55 5, 6, 7, 56 9,10,11]); 57 }); 58 }); 59 60 describe("create", function() { 61 beforeEach(function() { result = mat3.create(); }); 62 it("should return a 9 element array initialized to a 3x3 identity matrix", function() { expect(result).toBeEqualish(identity); }); 63 }); 64 65 describe("clone", function() { 66 beforeEach(function() { result = mat3.clone(matA); }); 67 it("should return a 9 element array initialized to the values in matA", function() { expect(result).toBeEqualish(matA); }); 68 }); 69 70 describe("copy", function() { 71 beforeEach(function() { result = mat3.copy(out, matA); }); 72 it("should place values into out", function() { expect(out).toBeEqualish(matA); }); 73 it("should return out", function() { expect(result).toBe(out); }); 74 }); 75 76 describe("identity", function() { 77 beforeEach(function() { result = mat3.identity(out); }); 78 it("should place values into out", function() { expect(result).toBeEqualish(identity); }); 79 it("should return out", function() { expect(result).toBe(out); }); 80 }); 81 82 describe("transpose", function() { 83 describe("with a separate output matrix", function() { 84 beforeEach(function() { result = mat3.transpose(out, matA); }); 85 86 it("should place values into out", function() { 87 expect(out).toBeEqualish([ 88 1, 0, 1, 89 0, 1, 2, 90 0, 0, 1 91 ]); 92 }); 93 it("should return out", function() { expect(result).toBe(out); }); 94 it("should not modify matA", function() { 95 expect(matA).toBeEqualish([ 96 1, 0, 0, 97 0, 1, 0, 98 1, 2, 1 99 ]); 100 }); 101 }); 102 103 describe("when matA is the output matrix", function() { 104 beforeEach(function() { result = mat3.transpose(matA, matA); }); 105 106 it("should place values into matA", function() { 107 expect(matA).toBeEqualish([ 108 1, 0, 1, 109 0, 1, 2, 110 0, 0, 1 111 ]); 112 }); 113 it("should return matA", function() { expect(result).toBe(matA); }); 114 }); 115 }); 116 117 describe("invert", function() { 118 describe("with a separate output matrix", function() { 119 beforeEach(function() { result = mat3.invert(out, matA); }); 120 121 it("should place values into out", function() { 122 expect(out).toBeEqualish([ 123 1, 0, 0, 124 0, 1, 0, 125 -1, -2, 1 126 ]); 127 }); 128 it("should return out", function() { expect(result).toBe(out); }); 129 it("should not modify matA", function() { 130 expect(matA).toBeEqualish([ 131 1, 0, 0, 132 0, 1, 0, 133 1, 2, 1 134 ]); 135 }); 136 }); 137 138 describe("when matA is the output matrix", function() { 139 beforeEach(function() { result = mat3.invert(matA, matA); }); 140 141 it("should place values into matA", function() { 142 expect(matA).toBeEqualish([ 143 1, 0, 0, 144 0, 1, 0, 145 -1, -2, 1 146 ]); 147 }); 148 it("should return matA", function() { expect(result).toBe(matA); }); 149 }); 150 }); 151 152 describe("adjoint", function() { 153 describe("with a separate output matrix", function() { 154 beforeEach(function() { result = mat3.adjoint(out, matA); }); 155 156 it("should place values into out", function() { 157 expect(out).toBeEqualish([ 158 1, 0, 0, 159 0, 1, 0, 160 -1, -2, 1 161 ]); 162 }); 163 it("should return out", function() { expect(result).toBe(out); }); 164 it("should not modify matA", function() { 165 expect(matA).toBeEqualish([ 166 1, 0, 0, 167 0, 1, 0, 168 1, 2, 1 169 ]); 170 }); 171 }); 172 173 describe("when matA is the output matrix", function() { 174 beforeEach(function() { result = mat3.adjoint(matA, matA); }); 175 176 it("should place values into matA", function() { 177 expect(matA).toBeEqualish([ 178 1, 0, 0, 179 0, 1, 0, 180 -1, -2, 1 181 ]); 182 }); 183 it("should return matA", function() { expect(result).toBe(matA); }); 184 }); 185 }); 186 187 describe("determinant", function() { 188 beforeEach(function() { result = mat3.determinant(matA); }); 189 190 it("should return the determinant", function() { expect(result).toEqual(1); }); 191 }); 192 193 describe("multiply", function() { 194 it("should have an alias called 'mul'", function() { expect(mat3.mul).toEqual(mat3.multiply); }); 195 196 describe("with a separate output matrix", function() { 197 beforeEach(function() { result = mat3.multiply(out, matA, matB); }); 198 199 it("should place values into out", function() { 200 expect(out).toBeEqualish([ 201 1, 0, 0, 202 0, 1, 0, 203 4, 6, 1 204 ]); 205 }); 206 it("should return out", function() { expect(result).toBe(out); }); 207 it("should not modify matA", function() { 208 expect(matA).toBeEqualish([ 209 1, 0, 0, 210 0, 1, 0, 211 1, 2, 1 212 ]); 213 }); 214 it("should not modify matB", function() { 215 expect(matB).toBeEqualish([ 216 1, 0, 0, 217 0, 1, 0, 218 3, 4, 1 219 ]); 220 }); 221 }); 222 223 describe("when matA is the output matrix", function() { 224 beforeEach(function() { result = mat3.multiply(matA, matA, matB); }); 225 226 it("should place values into matA", function() { 227 expect(matA).toBeEqualish([ 228 1, 0, 0, 229 0, 1, 0, 230 4, 6, 1 231 ]); 232 }); 233 it("should return matA", function() { expect(result).toBe(matA); }); 234 it("should not modify matB", function() { 235 expect(matB).toBeEqualish([ 236 1, 0, 0, 237 0, 1, 0, 238 3, 4, 1 239 ]); 240 }); 241 }); 242 243 describe("when matB is the output matrix", function() { 244 beforeEach(function() { result = mat3.multiply(matB, matA, matB); }); 245 246 it("should place values into matB", function() { 247 expect(matB).toBeEqualish([ 248 1, 0, 0, 249 0, 1, 0, 250 4, 6, 1 251 ]); 252 }); 253 it("should return matB", function() { expect(result).toBe(matB); }); 254 it("should not modify matA", function() { 255 expect(matA).toBeEqualish([ 256 1, 0, 0, 257 0, 1, 0, 258 1, 2, 1 259 ]); 260 }); 261 }); 262 }); 263 264 describe("str", function() { 265 beforeEach(function() { result = mat3.str(matA); }); 266 267 it("should return a string representation of the matrix", function() { expect(result).toEqual("mat3(1, 0, 0, 0, 1, 0, 1, 2, 1)"); }); 268 }); 269});