1 /* Copyright (c) 2013, Brandon Jones, Colin MacKenzie IV. All rights reserved.
3 Redistribution and use in source and binary forms, with or without modification,
4 are permitted provided that the following conditions are met:
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.
12 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
13 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
14 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
15 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
16 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
17 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
18 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
19 ANY 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
21 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
23 describe("mat2d", function() {
24 var out, matA, matB, identity, result;
26 beforeEach(function() {
52 describe("create", function() {
53 beforeEach(function() { result = mat2d.create(); });
54 it("should return a 6 element array initialized to a 2x3 identity matrix", function() { expect(result).toBeEqualish(identity); });
57 describe("clone", function() {
58 beforeEach(function() { result = mat2d.clone(matA); });
59 it("should return a 6 element array initialized to the values in matA", function() { expect(result).toBeEqualish(matA); });
62 describe("copy", function() {
63 beforeEach(function() { result = mat2d.copy(out, matA); });
64 it("should place values into out", function() { expect(out).toBeEqualish(matA); });
65 it("should return out", function() { expect(result).toBe(out); });
68 describe("identity", function() {
69 beforeEach(function() { result = mat2d.identity(out); });
70 it("should place values into out", function() { expect(result).toBeEqualish(identity); });
71 it("should return out", function() { expect(result).toBe(out); });
74 describe("invert", function() {
75 describe("with a separate output matrix", function() {
76 beforeEach(function() { result = mat2d.invert(out, matA); });
78 it("should place values into out", function() { expect(out).toBeEqualish([ -2, 1, 1.5, -0.5, 1, -2 ]); });
79 it("should return out", function() { expect(result).toBe(out); });
80 it("should not modify matA", function() { expect(matA).toBeEqualish(oldA); });
83 describe("when matA is the output matrix", function() {
84 beforeEach(function() { result = mat2d.invert(matA, matA); });
86 it("should place values into matA", function() { expect(matA).toBeEqualish([ -2, 1, 1.5, -0.5, 1, -2 ]); });
87 it("should return matA", function() { expect(result).toBe(matA); });
91 describe("determinant", function() {
92 beforeEach(function() { result = mat2d.determinant(matA); });
94 it("should return the determinant", function() { expect(result).toEqual(-2); });
97 describe("multiply", function() {
98 it("should have an alias called 'mul'", function() { expect(mat2d.mul).toEqual(mat2d.multiply); });
100 describe("with a separate output matrix", function() {
101 beforeEach(function() { result = mat2d.multiply(out, matA, matB); });
103 it("should place values into out", function() { expect(out).toBeEqualish([25, 28, 57, 64, 100, 112]); });
104 it("should return out", function() { expect(result).toBe(out); });
105 it("should not modify matA", function() { expect(matA).toBeEqualish(oldA); });
106 it("should not modify matB", function() { expect(matB).toBeEqualish(oldB); });
109 describe("when matA is the output matrix", function() {
110 beforeEach(function() { result = mat2d.multiply(matA, matA, matB); });
112 it("should place values into matA", function() { expect(matA).toBeEqualish([25, 28, 57, 64, 100, 112]); });
113 it("should return matA", function() { expect(result).toBe(matA); });
114 it("should not modify matB", function() { expect(matB).toBeEqualish(oldB); });
117 describe("when matB is the output matrix", function() {
118 beforeEach(function() { result = mat2d.multiply(matB, matA, matB); });
120 it("should place values into matB", function() { expect(matB).toBeEqualish([25, 28, 57, 64, 100, 112]); });
121 it("should return matB", function() { expect(result).toBe(matB); });
122 it("should not modify matA", function() { expect(matA).toBeEqualish(oldA); });
126 describe("rotate", function() {
127 describe("with a separate output matrix", function() {
128 beforeEach(function() { result = mat2d.rotate(out, matA, Math.PI * 0.5); });
130 it("should place values into out", function() { expect(out).toBeEqualish([2, -1, 4, -3, 6, -5]); });
131 it("should return out", function() { expect(result).toBe(out); });
132 it("should not modify matA", function() { expect(matA).toBeEqualish(oldA); });
135 describe("when matA is the output matrix", function() {
136 beforeEach(function() { result = mat2d.rotate(matA, matA, Math.PI * 0.5); });
138 it("should place values into matA", function() { expect(matA).toBeEqualish([2, -1, 4, -3, 6, -5]); });
139 it("should return matA", function() { expect(result).toBe(matA); });
143 describe("scale", function() {
145 beforeEach(function() { vecA = [2, 3]; });
147 describe("with a separate output matrix", function() {
148 beforeEach(function() { result = mat2d.scale(out, matA, vecA); });
150 it("should place values into out", function() { expect(out).toBeEqualish([2, 6, 6, 12, 10, 18]); });
151 it("should return out", function() { expect(result).toBe(out); });
152 it("should not modify matA", function() { expect(matA).toBeEqualish(oldA); });
155 describe("when matA is the output matrix", function() {
156 beforeEach(function() { result = mat2d.scale(matA, matA, vecA); });
158 it("should place values into matA", function() { expect(matA).toBeEqualish([2, 6, 6, 12, 10, 18]); });
159 it("should return matA", function() { expect(result).toBe(matA); });
163 describe("translate", function() {
165 beforeEach(function() { vecA = [2, 3]; });
167 describe("with a separate output matrix", function() {
168 beforeEach(function() { result = mat2d.translate(out, matA, vecA); });
170 it("should place values into out", function() { expect(out).toBeEqualish([1, 2, 3, 4, 7, 9]); });
171 it("should return out", function() { expect(result).toBe(out); });
172 it("should not modify matA", function() { expect(matA).toBeEqualish(oldA); });
175 describe("when matA is the output matrix", function() {
176 beforeEach(function() { result = mat2d.translate(matA, matA, vecA); });
178 it("should place values into matA", function() { expect(matA).toBeEqualish([1, 2, 3, 4, 7, 9]); });
179 it("should return matA", function() { expect(result).toBe(matA); });
183 describe("str", function() {
184 beforeEach(function() { result = mat2d.str(matA); });
186 it("should return a string representation of the matrix", function() { expect(result).toEqual("mat2d(1, 2, 3, 4, 5, 6)"); });