Upstream version 11.40.271.0
[platform/framework/web/crosswalk.git] / src / third_party / trace-viewer / third_party / gl-matrix / spec / gl-matrix / mat2d-spec.js
1 /* Copyright (c) 2013, Brandon Jones, Colin MacKenzie IV. All rights reserved.
2
3 Redistribution and use in source and binary forms, with or without modification,
4 are 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
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. */
22
23 describe("mat2d", function() {
24     var out, matA, matB, identity, result;
25
26     beforeEach(function() {
27         matA = [1, 2,
28                 3, 4,
29                 5, 6];
30
31         oldA = [1, 2,
32                 3, 4,
33                 5, 6];
34
35         matB = [7, 8,
36                 9, 10,
37                 11, 12];
38
39         oldB = [7, 8,
40                 9, 10,
41                 11, 12];
42
43         out =  [0, 0,
44                 0, 0,
45                 0, 0];
46
47         identity = [1, 0,
48                     0, 1,
49                     0, 0];
50     });
51
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); });
55     });
56
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); });
60     });
61
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); });
66     });
67
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); });
72     });
73
74     describe("invert", function() {
75         describe("with a separate output matrix", function() {
76             beforeEach(function() { result = mat2d.invert(out, matA); });
77             
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); });
81         });
82
83         describe("when matA is the output matrix", function() {
84             beforeEach(function() { result = mat2d.invert(matA, matA); });
85             
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); });
88         });
89     });
90
91     describe("determinant", function() {
92         beforeEach(function() { result = mat2d.determinant(matA); });
93         
94         it("should return the determinant", function() { expect(result).toEqual(-2); });
95     });
96
97     describe("multiply", function() {
98         it("should have an alias called 'mul'", function() { expect(mat2d.mul).toEqual(mat2d.multiply); });
99
100         describe("with a separate output matrix", function() {
101             beforeEach(function() { result = mat2d.multiply(out, matA, matB); });
102             
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); });
107         });
108
109         describe("when matA is the output matrix", function() {
110             beforeEach(function() { result = mat2d.multiply(matA, matA, matB); });
111             
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); });
115         });
116
117         describe("when matB is the output matrix", function() {
118             beforeEach(function() { result = mat2d.multiply(matB, matA, matB); });
119             
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); });
123         });
124     });
125
126     describe("rotate", function() {
127         describe("with a separate output matrix", function() {
128             beforeEach(function() { result = mat2d.rotate(out, matA, Math.PI * 0.5); });
129             
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); });
133         });
134
135         describe("when matA is the output matrix", function() {
136             beforeEach(function() { result = mat2d.rotate(matA, matA, Math.PI * 0.5); });
137             
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); });
140         });
141     });
142
143     describe("scale", function() {
144         var vecA;
145         beforeEach(function() { vecA = [2, 3]; });
146
147         describe("with a separate output matrix", function() {
148             beforeEach(function() { result = mat2d.scale(out, matA, vecA); });
149             
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); });
153         });
154
155         describe("when matA is the output matrix", function() {
156             beforeEach(function() { result = mat2d.scale(matA, matA, vecA); });
157             
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); });
160         });
161     });
162
163     describe("translate", function() {
164         var vecA;
165         beforeEach(function() { vecA = [2, 3]; });
166
167         describe("with a separate output matrix", function() {
168             beforeEach(function() { result = mat2d.translate(out, matA, vecA); });
169             
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); });
173         });
174
175         describe("when matA is the output matrix", function() {
176             beforeEach(function() { result = mat2d.translate(matA, matA, vecA); });
177             
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); });
180         });
181     });
182
183     describe("str", function() {
184         beforeEach(function() { result = mat2d.str(matA); });
185         
186         it("should return a string representation of the matrix", function() { expect(result).toEqual("mat2d(1, 2, 3, 4, 5, 6)"); });
187     });
188 });