Debian Codegraph @

closure-library/closure/goog/math/vec2_test.js

raw
1 // Copyright 2006 The Closure Library Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS-IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 goog.provide('goog.math.Vec2Test');
16 goog.setTestOnly('goog.math.Vec2Test');
17
18 goog.require('goog.math.Vec2');
19 goog.require('goog.testing.jsunit');
20
21 function assertVectorEquals(a, b) {
22 assertTrue(b + ' should be equal to ' + a, goog.math.Vec2.equals(a, b));
23 }
24
25
26 function testVec2() {
27 var v = new goog.math.Vec2(3.14, 2.78);
28 assertEquals(3.14, v.x);
29 assertEquals(2.78, v.y);
30 }
31
32
33 function testRandomUnit() {
34 var a = goog.math.Vec2.randomUnit();
35 assertRoughlyEquals(1.0, a.magnitude(), 1e-10);
36 }
37
38
39 function testRandom() {
40 var a = goog.math.Vec2.random();
41 assertTrue(a.magnitude() <= 1.0);
42 }
43
44
45 function testClone() {
46 var a = new goog.math.Vec2(1, 2);
47 var b = a.clone();
48
49 assertEquals(a.x, b.x);
50 assertEquals(a.y, b.y);
51 }
52
53
54 function testMagnitude() {
55 var a = new goog.math.Vec2(0, 10);
56 var b = new goog.math.Vec2(3, 4);
57
58 assertEquals(10, a.magnitude());
59 assertEquals(5, b.magnitude());
60 }
61
62
63 function testSquaredMagnitude() {
64 var a = new goog.math.Vec2(-3, -4);
65 assertEquals(25, a.squaredMagnitude());
66 }
67
68
69 function testScaleFactor() {
70 var a = new goog.math.Vec2(1, 2);
71 var scaled = a.scale(0.5);
72
73 assertTrue(
74 'The type of the return value should be goog.math.Vec2',
75 scaled instanceof goog.math.Vec2);
76 assertVectorEquals(new goog.math.Vec2(0.5, 1), a);
77 }
78
79
80 function testScaleXY() {
81 var a = new goog.math.Vec2(10, 15);
82 var scaled = a.scale(2, 3);
83 assertEquals('The function should return the target instance', a, scaled);
84 assertTrue(
85 'The type of the return value should be goog.math.Vec2',
86 scaled instanceof goog.math.Vec2);
87 assertVectorEquals(new goog.math.Vec2(20, 45), a);
88 }
89
90
91 function testInvert() {
92 var a = new goog.math.Vec2(3, 4);
93 a.invert();
94
95 assertEquals(-3, a.x);
96 assertEquals(-4, a.y);
97 }
98
99
100 function testNormalize() {
101 var a = new goog.math.Vec2(5, 5);
102 a.normalize();
103 assertRoughlyEquals(1.0, a.magnitude(), 1e-10);
104 }
105
106
107 function testAdd() {
108 var a = new goog.math.Vec2(1, -1);
109 a.add(new goog.math.Vec2(3, 3));
110 assertVectorEquals(new goog.math.Vec2(4, 2), a);
111 }
112
113
114 function testSubtract() {
115 var a = new goog.math.Vec2(1, -1);
116 a.subtract(new goog.math.Vec2(3, 3));
117 assertVectorEquals(new goog.math.Vec2(-2, -4), a);
118 }
119
120
121 function testRotate() {
122 var a = new goog.math.Vec2(1, -1);
123 a.rotate(Math.PI / 2);
124 assertRoughlyEquals(1, a.x, 0.000001);
125 assertRoughlyEquals(1, a.y, 0.000001);
126 a.rotate(-Math.PI);
127 assertRoughlyEquals(-1, a.x, 0.000001);
128 assertRoughlyEquals(-1, a.y, 0.000001);
129 }
130
131
132 function testRotateAroundPoint() {
133 var a = goog.math.Vec2.rotateAroundPoint(
134 new goog.math.Vec2(1, -1), new goog.math.Vec2(1, 0), Math.PI / 2);
135 assertRoughlyEquals(2, a.x, 0.000001);
136 assertRoughlyEquals(0, a.y, 0.000001);
137 }
138
139
140 function testEquals() {
141 var a = new goog.math.Vec2(1, 2);
142
143 assertFalse(a.equals(null));
144 assertFalse(a.equals({}));
145 assertFalse(a.equals(new goog.math.Vec2(1, 3)));
146 assertFalse(a.equals(new goog.math.Vec2(2, 2)));
147
148 assertTrue(a.equals(a));
149 assertTrue(a.equals(new goog.math.Vec2(1, 2)));
150 }
151
152
153 function testSum() {
154 var a = new goog.math.Vec2(0.5, 0.25);
155 var b = new goog.math.Vec2(0.5, 0.75);
156
157 var c = goog.math.Vec2.sum(a, b);
158 assertVectorEquals(new goog.math.Vec2(1, 1), c);
159 }
160
161
162 function testDifference() {
163 var a = new goog.math.Vec2(0.5, 0.25);
164 var b = new goog.math.Vec2(0.5, 0.75);
165
166 var c = goog.math.Vec2.difference(a, b);
167 assertVectorEquals(new goog.math.Vec2(0, -0.5), c);
168 }
169
170
171 function testDistance() {
172 var a = new goog.math.Vec2(3, 4);
173 var b = new goog.math.Vec2(-3, -4);
174
175 assertEquals(10, goog.math.Vec2.distance(a, b));
176 }
177
178
179 function testSquaredDistance() {
180 var a = new goog.math.Vec2(3, 4);
181 var b = new goog.math.Vec2(-3, -4);
182
183 assertEquals(100, goog.math.Vec2.squaredDistance(a, b));
184 }
185
186
187 function testVec2Equals() {
188 assertTrue(goog.math.Vec2.equals(null, null));
189 assertFalse(goog.math.Vec2.equals(null, new goog.math.Vec2()));
190
191 var a = new goog.math.Vec2(1, 3);
192 assertTrue(goog.math.Vec2.equals(a, a));
193 assertTrue(goog.math.Vec2.equals(a, new goog.math.Vec2(1, 3)));
194 assertFalse(goog.math.Vec2.equals(1, new goog.math.Vec2(3, 1)));
195 }
196
197
198 function testDot() {
199 var a = new goog.math.Vec2(0, 5);
200 var b = new goog.math.Vec2(3, 0);
201 assertEquals(0, goog.math.Vec2.dot(a, b));
202
203 var c = new goog.math.Vec2(-5, -5);
204 var d = new goog.math.Vec2(0, 7);
205 assertEquals(-35, goog.math.Vec2.dot(c, d));
206 }
207
208 function testDeterminant() {
209 var a = new goog.math.Vec2(0, 5);
210 var b = new goog.math.Vec2(0, 10);
211 assertEquals(0, goog.math.Vec2.determinant(a, b));
212
213 var c = new goog.math.Vec2(0, 5);
214 var d = new goog.math.Vec2(10, 0);
215 assertEquals(-50, goog.math.Vec2.determinant(c, d));
216
217 var e = new goog.math.Vec2(-5, -5);
218 var f = new goog.math.Vec2(0, 7);
219 assertEquals(-35, goog.math.Vec2.determinant(e, f));
220 }
221
222 function testLerp() {
223 var a = new goog.math.Vec2(0, 0);
224 var b = new goog.math.Vec2(10, 10);
225
226 for (var i = 0; i <= 10; i++) {
227 var c = goog.math.Vec2.lerp(a, b, i / 10);
228 assertEquals(i, c.x);
229 assertEquals(i, c.y);
230 }
231 }
232
233 function testRescaled() {
234 const a = new goog.math.Vec2(1, 2);
235 const b = goog.math.Vec2.rescaled(a, 3);
236 assertVectorEquals(new goog.math.Vec2(3, 6), b);
237
238 const c = goog.math.Vec2.rescaled(a, 3, -2);
239 assertVectorEquals(new goog.math.Vec2(3, -4), c);
240 assertVectorEquals(new goog.math.Vec2(1, 2), a);
241 }
242
243 function testToString() {
244 testEquals('(0, 0)', new goog.math.Vec2(0, 0).toString());
245 }