| /* Copyright (c) 2015, Brandon Jones, Colin MacKenzie IV. |
| |
| Permission is hereby granted, free of charge, to any person obtaining a copy |
| of this software and associated documentation files (the "Software"), to deal |
| in the Software without restriction, including without limitation the rights |
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| copies of the Software, and to permit persons to whom the Software is |
| furnished to do so, subject to the following conditions: |
| |
| The above copyright notice and this permission notice shall be included in |
| all copies or substantial portions of the Software. |
| |
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| THE SOFTWARE. */ |
| |
| describe("mat2d", function() { |
| var mat2d = require("../../src/gl-matrix/mat2d.js"); |
| |
| var out, matA, matB, identity, result; |
| |
| beforeEach(function() { |
| matA = [1, 2, |
| 3, 4, |
| 5, 6]; |
| |
| oldA = [1, 2, |
| 3, 4, |
| 5, 6]; |
| |
| matB = [7, 8, |
| 9, 10, |
| 11, 12]; |
| |
| oldB = [7, 8, |
| 9, 10, |
| 11, 12]; |
| |
| out = [0, 0, |
| 0, 0, |
| 0, 0]; |
| |
| identity = [1, 0, |
| 0, 1, |
| 0, 0]; |
| }); |
| |
| describe("create", function() { |
| beforeEach(function() { result = mat2d.create(); }); |
| it("should return a 6 element array initialized to a 2x3 identity matrix", function() { expect(result).toBeEqualish(identity); }); |
| }); |
| |
| describe("clone", function() { |
| beforeEach(function() { result = mat2d.clone(matA); }); |
| it("should return a 6 element array initialized to the values in matA", function() { expect(result).toBeEqualish(matA); }); |
| }); |
| |
| describe("copy", function() { |
| beforeEach(function() { result = mat2d.copy(out, matA); }); |
| it("should place values into out", function() { expect(out).toBeEqualish(matA); }); |
| it("should return out", function() { expect(result).toBe(out); }); |
| }); |
| |
| describe("identity", function() { |
| beforeEach(function() { result = mat2d.identity(out); }); |
| it("should place values into out", function() { expect(result).toBeEqualish(identity); }); |
| it("should return out", function() { expect(result).toBe(out); }); |
| }); |
| |
| describe("invert", function() { |
| describe("with a separate output matrix", function() { |
| beforeEach(function() { result = mat2d.invert(out, matA); }); |
| |
| it("should place values into out", function() { expect(out).toBeEqualish([ -2, 1, 1.5, -0.5, 1, -2 ]); }); |
| it("should return out", function() { expect(result).toBe(out); }); |
| it("should not modify matA", function() { expect(matA).toBeEqualish(oldA); }); |
| }); |
| |
| describe("when matA is the output matrix", function() { |
| beforeEach(function() { result = mat2d.invert(matA, matA); }); |
| |
| it("should place values into matA", function() { expect(matA).toBeEqualish([ -2, 1, 1.5, -0.5, 1, -2 ]); }); |
| it("should return matA", function() { expect(result).toBe(matA); }); |
| }); |
| }); |
| |
| describe("determinant", function() { |
| beforeEach(function() { result = mat2d.determinant(matA); }); |
| |
| it("should return the determinant", function() { expect(result).toEqual(-2); }); |
| }); |
| |
| describe("multiply", function() { |
| it("should have an alias called 'mul'", function() { expect(mat2d.mul).toEqual(mat2d.multiply); }); |
| |
| describe("with a separate output matrix", function() { |
| beforeEach(function() { result = mat2d.multiply(out, matA, matB); }); |
| |
| it("should place values into out", function() { expect(out).toBeEqualish([31, 46, 39, 58, 52, 76]); }); |
| it("should return out", function() { expect(result).toBe(out); }); |
| it("should not modify matA", function() { expect(matA).toBeEqualish(oldA); }); |
| it("should not modify matB", function() { expect(matB).toBeEqualish(oldB); }); |
| }); |
| |
| describe("when matA is the output matrix", function() { |
| beforeEach(function() { result = mat2d.multiply(matA, matA, matB); }); |
| |
| it("should place values into matA", function() { expect(matA).toBeEqualish([31, 46, 39, 58, 52, 76]); }); |
| it("should return matA", function() { expect(result).toBe(matA); }); |
| it("should not modify matB", function() { expect(matB).toBeEqualish(oldB); }); |
| }); |
| |
| describe("when matB is the output matrix", function() { |
| beforeEach(function() { result = mat2d.multiply(matB, matA, matB); }); |
| |
| it("should place values into matB", function() { expect(matB).toBeEqualish([31, 46, 39, 58, 52, 76]); }); |
| it("should return matB", function() { expect(result).toBe(matB); }); |
| it("should not modify matA", function() { expect(matA).toBeEqualish(oldA); }); |
| }); |
| }); |
| |
| describe("rotate", function() { |
| describe("with a separate output matrix", function() { |
| beforeEach(function() { result = mat2d.rotate(out, matA, Math.PI * 0.5); }); |
| |
| it("should place values into out", function() { expect(out).toBeEqualish([3, 4, -1, -2, 5, 6]); }); |
| it("should return out", function() { expect(result).toBe(out); }); |
| it("should not modify matA", function() { expect(matA).toBeEqualish(oldA); }); |
| }); |
| |
| describe("when matA is the output matrix", function() { |
| beforeEach(function() { result = mat2d.rotate(matA, matA, Math.PI * 0.5); }); |
| |
| it("should place values into matA", function() { expect(matA).toBeEqualish([3, 4, -1, -2, 5, 6]); }); |
| it("should return matA", function() { expect(result).toBe(matA); }); |
| }); |
| }); |
| |
| describe("scale", function() { |
| var vecA; |
| beforeEach(function() { vecA = [2, 3]; }); |
| |
| describe("with a separate output matrix", function() { |
| beforeEach(function() { result = mat2d.scale(out, matA, vecA); }); |
| |
| it("should place values into out", function() { expect(out).toBeEqualish([2, 4, 9, 12, 5, 6]); }); |
| it("should return out", function() { expect(result).toBe(out); }); |
| it("should not modify matA", function() { expect(matA).toBeEqualish(oldA); }); |
| }); |
| |
| describe("when matA is the output matrix", function() { |
| beforeEach(function() { result = mat2d.scale(matA, matA, vecA); }); |
| |
| it("should place values into matA", function() { expect(matA).toBeEqualish([2, 4, 9, 12, 5, 6]); }); |
| it("should return matA", function() { expect(result).toBe(matA); }); |
| }); |
| }); |
| |
| describe("translate", function() { |
| var vecA; |
| beforeEach(function() { vecA = [2, 3]; }); |
| |
| describe("with a separate output matrix", function() { |
| beforeEach(function() { result = mat2d.translate(out, matA, vecA); }); |
| |
| it("should place values into out", function() { expect(out).toBeEqualish([1, 2, 3, 4, 16, 22]); }); |
| it("should return out", function() { expect(result).toBe(out); }); |
| it("should not modify matA", function() { expect(matA).toBeEqualish(oldA); }); |
| }); |
| |
| describe("when matA is the output matrix", function() { |
| beforeEach(function() { result = mat2d.translate(matA, matA, vecA); }); |
| |
| it("should place values into matA", function() { expect(matA).toBeEqualish([1, 2, 3, 4, 16, 22]); }); |
| it("should return matA", function() { expect(result).toBe(matA); }); |
| }); |
| }); |
| |
| describe("str", function() { |
| beforeEach(function() { result = mat2d.str(matA); }); |
| |
| it("should return a string representation of the matrix", function() { expect(result).toEqual("mat2d(1, 2, 3, 4, 5, 6)"); }); |
| }); |
| |
| describe("frob", function() { |
| beforeEach(function() { result = mat2d.frob(matA); }); |
| it("should return the Frobenius Norm of the matrix", function() { expect(result).toEqual( Math.sqrt(Math.pow(1, 2) + Math.pow(2, 2) + Math.pow(3, 2) + Math.pow(4, 2) + Math.pow(5, 2) + Math.pow(6, 2) + 1)); }); |
| }); |
| |
| }); |