Debian Codegraph @

closure-library/closure/goog/math/math_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.mathTest');
16 goog.setTestOnly('goog.mathTest');
17
18 goog.require('goog.math');
19 goog.require('goog.testing.jsunit');
20
21 function testRandomInt() {
22 assertEquals(0, goog.math.randomInt(0));
23 assertEquals(0, goog.math.randomInt(1));
24
25 var r = goog.math.randomInt(3);
26 assertTrue(0 <= r && r < 3);
27 }
28
29 function testUniformRandom() {
30 assertEquals(5.2, goog.math.uniformRandom(5.2, 5.2));
31 assertEquals(-6, goog.math.uniformRandom(-6, -6));
32
33 var r = goog.math.uniformRandom(-0.5, 0.5);
34 assertTrue(-0.5 <= r && r < 0.5);
35 }
36
37 function testClamp() {
38 assertEquals(3, goog.math.clamp(3, -5, 5));
39 assertEquals(5, goog.math.clamp(5, -5, 5));
40 assertEquals(-5, goog.math.clamp(-5, -5, 5));
41
42 assertEquals(-5, goog.math.clamp(-22, -5, 5));
43 assertEquals(5, goog.math.clamp(6, -5, 5));
44 }
45
46 function testModulo() {
47 assertEquals(0, goog.math.modulo(256, 8));
48
49 assertEquals(7, goog.math.modulo(7, 8));
50 assertEquals(7, goog.math.modulo(23, 8));
51 assertEquals(7, goog.math.modulo(-1, 8));
52
53 // Safari 5.1.7 has a bug in its JS engine where modulo is computed
54 // incorrectly when using variables. We avoid using
55 // goog.testing.ExpectedFailure here since it pulls in a bunch of
56 // extra dependencies for maintaining a DOM console.
57 var a = 1;
58 var b = -5;
59 if (a % b === 1 % -5) {
60 assertEquals(-4, goog.math.modulo(1, -5));
61 assertEquals(-4, goog.math.modulo(6, -5));
62 }
63 assertEquals(-4, goog.math.modulo(-4, -5));
64 }
65
66 function testLerp() {
67 assertEquals(0, goog.math.lerp(0, 0, 0));
68 assertEquals(3, goog.math.lerp(0, 6, 0.5));
69 assertEquals(3, goog.math.lerp(-1, 1, 2));
70 }
71
72 function testNearlyEquals() {
73 assertTrue(
74 'Numbers inside default tolerance should be equal',
75 goog.math.nearlyEquals(0.000001, 0.000001001));
76 assertFalse(
77 'Numbers outside default tolerance should be unequal',
78 goog.math.nearlyEquals(0.000001, 0.000003));
79 assertTrue(
80 'Numbers inside custom tolerance should be equal',
81 goog.math.nearlyEquals(0.001, 0.002, 0.1));
82 assertFalse(
83 'Numbers outside custom tolerance should be unequal',
84 goog.math.nearlyEquals(0.001, -0.1, 0.1));
85 assertTrue(
86 'Integer tolerance greater than one should succeed',
87 goog.math.nearlyEquals(87, 85, 3));
88 }
89
90 function testStandardAngleInRadians() {
91 assertRoughlyEquals(0, goog.math.standardAngleInRadians(2 * Math.PI), 1e-10);
92 assertRoughlyEquals(
93 Math.PI, goog.math.standardAngleInRadians(Math.PI), 1e-10);
94 assertRoughlyEquals(
95 Math.PI, goog.math.standardAngleInRadians(-1 * Math.PI), 1e-10);
96 assertRoughlyEquals(
97 Math.PI / 2, goog.math.standardAngleInRadians(-1.5 * Math.PI), 1e-10);
98 assertRoughlyEquals(
99 Math.PI, goog.math.standardAngleInRadians(5 * Math.PI), 1e-10);
100 assertEquals(0.01, goog.math.standardAngleInRadians(0.01));
101 assertEquals(0, goog.math.standardAngleInRadians(0));
102 }
103
104 function testStandardAngle() {
105 assertEquals(359.5, goog.math.standardAngle(-360.5));
106 assertEquals(0, goog.math.standardAngle(-360));
107 assertEquals(359.5, goog.math.standardAngle(-0.5));
108 assertEquals(0, goog.math.standardAngle(0));
109 assertEquals(0.5, goog.math.standardAngle(0.5));
110 assertEquals(0, goog.math.standardAngle(360));
111 assertEquals(1, goog.math.standardAngle(721));
112 }
113
114 function testToRadians() {
115 assertEquals(-Math.PI, goog.math.toRadians(-180));
116 assertEquals(0, goog.math.toRadians(0));
117 assertEquals(Math.PI, goog.math.toRadians(180));
118 }
119
120 function testToDegrees() {
121 assertEquals(-180, goog.math.toDegrees(-Math.PI));
122 assertEquals(0, goog.math.toDegrees(0));
123 assertEquals(180, goog.math.toDegrees(Math.PI));
124 }
125
126 function testAngleDx() {
127 assertRoughlyEquals(0, goog.math.angleDx(0, 0), 1e-10);
128 assertRoughlyEquals(0, goog.math.angleDx(90, 0), 1e-10);
129 assertRoughlyEquals(100, goog.math.angleDx(0, 100), 1e-10);
130 assertRoughlyEquals(0, goog.math.angleDx(90, 100), 1e-10);
131 assertRoughlyEquals(-100, goog.math.angleDx(180, 100), 1e-10);
132 assertRoughlyEquals(0, goog.math.angleDx(270, 100), 1e-10);
133 }
134
135 function testAngleDy() {
136 assertRoughlyEquals(0, goog.math.angleDy(0, 0), 1e-10);
137 assertRoughlyEquals(0, goog.math.angleDy(90, 0), 1e-10);
138 assertRoughlyEquals(0, goog.math.angleDy(0, 100), 1e-10);
139 assertRoughlyEquals(100, goog.math.angleDy(90, 100), 1e-10);
140 assertRoughlyEquals(0, goog.math.angleDy(180, 100), 1e-10);
141 assertRoughlyEquals(-100, goog.math.angleDy(270, 100), 1e-10);
142 }
143
144 function testAngle() {
145 assertRoughlyEquals(0, goog.math.angle(10, 10, 20, 10), 1e-10);
146 assertRoughlyEquals(90, goog.math.angle(10, 10, 10, 20), 1e-10);
147 assertRoughlyEquals(225, goog.math.angle(10, 10, 0, 0), 1e-10);
148 assertRoughlyEquals(270, goog.math.angle(10, 10, 10, 0), 1e-10);
149
150 // 0 is the conventional result, but mathematically this is undefined.
151 assertEquals(0, goog.math.angle(10, 10, 10, 10));
152 }
153
154 function testAngleDifference() {
155 assertEquals(10, goog.math.angleDifference(30, 40));
156 assertEquals(-10, goog.math.angleDifference(40, 30));
157 assertEquals(180, goog.math.angleDifference(10, 190));
158 assertEquals(180, goog.math.angleDifference(190, 10));
159 assertEquals(20, goog.math.angleDifference(350, 10));
160 assertEquals(-20, goog.math.angleDifference(10, 350));
161 assertEquals(100, goog.math.angleDifference(350, 90));
162 assertEquals(-80, goog.math.angleDifference(350, 270));
163 assertEquals(0, goog.math.angleDifference(15, 15));
164 }
165
166 function testSign() {
167 assertEquals(0, goog.math.sign(0));
168 assertEquals(-1, goog.math.sign(-3));
169 assertEquals(1, goog.math.sign(3));
170 assertEquals(1, goog.math.sign(0.0001));
171 assertEquals(-1, goog.math.sign(-0.0001));
172 assertEquals(1, goog.math.sign(3141592653589793));
173 }
174
175 function testSignOfSpecialFloatValues() {
176 assertEquals(-1, goog.math.sign(-Infinity));
177 assertEquals(1, goog.math.sign(Infinity));
178 assertNaN(goog.math.sign(NaN));
179 assertEquals(0, goog.math.sign(0));
180 assertFalse(goog.math.isNegativeZero(goog.math.sign(0)));
181 assertEquals(0, goog.math.sign(-0));
182 assertTrue(goog.math.isNegativeZero(goog.math.sign(-0)));
183 }
184
185 function testLongestCommonSubsequence() {
186 var func = goog.math.longestCommonSubsequence;
187
188 assertArrayEquals([2], func([1, 2], [2, 1]));
189 assertArrayEquals([1, 2], func([1, 2, 5], [2, 1, 2]));
190 assertArrayEquals(
191 [1, 2, 3, 4, 5],
192 func([1, 0, 2, 3, 8, 4, 9, 5], [8, 1, 2, 4, 3, 6, 4, 5]));
193 assertArrayEquals([1, 1, 1, 1, 1], func([1, 1, 1, 1, 1], [1, 1, 1, 1, 1]));
194 assertArrayEquals([5], func([1, 2, 3, 4, 5], [5, 4, 3, 2, 1]));
195 assertArrayEquals(
196 [1, 8, 11], func([1, 6, 8, 11, 13], [1, 3, 5, 8, 9, 11, 12]));
197 }
198
199 function testLongestCommonSubsequenceWithCustomComparator() {
200 var func = goog.math.longestCommonSubsequence;
201
202 var compareFn = function(a, b) { return a.field == b.field; };
203
204 var a1 = {field: 'a1', field2: 'hello'};
205 var a2 = {field: 'a2', field2: 33};
206 var a3 = {field: 'a3'};
207 var a4 = {field: 'a3'};
208
209 assertArrayEquals([a1, a2], func([a1, a2, a3], [a3, a1, a2], compareFn));
210 assertArrayEquals([a1, a3], func([a1, a3], [a1, a4], compareFn));
211 // testing the same arrays without compare function
212 assertArrayEquals([a1], func([a1, a3], [a1, a4]));
213 }
214
215 function testLongestCommonSubsequenceWithCustomCollector() {
216 var func = goog.math.longestCommonSubsequence;
217
218 var collectorFn = function(a, b) { return b; };
219
220 assertArrayEquals(
221 [1, 2, 4, 6, 7], func(
222 [1, 0, 2, 3, 8, 4, 9, 5], [8, 1, 2, 4, 3, 6, 4, 5],
223 null, collectorFn));
224 }
225
226 function testSum() {
227 assertEquals(
228 'sum() must return 0 if there are no arguments', 0, goog.math.sum());
229 assertEquals(
230 'sum() must return its argument if there is only one', 17,
231 goog.math.sum(17));
232 assertEquals(
233 'sum() must handle positive integers', 10, goog.math.sum(1, 2, 3, 4));
234 assertEquals(
235 'sum() must handle real numbers', -2.5, goog.math.sum(1, -2, 3, -4.5));
236 assertTrue(
237 'sum() must return NaN if one of the arguments isn\'t numeric',
238 isNaN(goog.math.sum(1, 2, 'foo', 3)));
239 }
240
241 function testAverage() {
242 assertTrue(
243 'average() must return NaN if there are no arguments',
244 isNaN(goog.math.average()));
245 assertEquals(
246 'average() must return its argument if there is only one', 17,
247 goog.math.average(17));
248 assertEquals(
249 'average() must handle positive integers', 3,
250 goog.math.average(1, 2, 3, 4, 5));
251 assertEquals(
252 'average() must handle real numbers', -0.625,
253 goog.math.average(1, -2, 3, -4.5));
254 assertTrue(
255 'average() must return NaN if one of the arguments isn\'t ' +
256 'numeric',
257 isNaN(goog.math.average(1, 2, 'foo', 3)));
258 }
259
260 function testSampleVariance() {
261 assertEquals(
262 'sampleVariance() must return 0 if there are no samples', 0,
263 goog.math.sampleVariance());
264 assertEquals(
265 'sampleVariance() must return 0 if there is only one ' +
266 'sample',
267 0, goog.math.sampleVariance(17));
268 assertRoughlyEquals(
269 'sampleVariance() must handle positive integers', 48,
270 goog.math.sampleVariance(3, 7, 7, 19), 0.0001);
271 assertRoughlyEquals(
272 'sampleVariance() must handle real numbers', 12.0138,
273 goog.math.sampleVariance(1.23, -2.34, 3.14, -4.56), 0.0001);
274 }
275
276 function testStandardDeviation() {
277 assertEquals(
278 'standardDeviation() must return 0 if there are no samples', 0,
279 goog.math.standardDeviation());
280 assertEquals(
281 'standardDeviation() must return 0 if there is only one ' +
282 'sample',
283 0, goog.math.standardDeviation(17));
284 assertRoughlyEquals(
285 'standardDeviation() must handle positive integers', 6.9282,
286 goog.math.standardDeviation(3, 7, 7, 19), 0.0001);
287 assertRoughlyEquals(
288 'standardDeviation() must handle real numbers', 3.4660,
289 goog.math.standardDeviation(1.23, -2.34, 3.14, -4.56), 0.0001);
290 }
291
292 function testIsInt() {
293 assertFalse(goog.math.isInt(12345.67));
294 assertFalse(goog.math.isInt(0.123));
295 assertFalse(goog.math.isInt(.1));
296 assertFalse(goog.math.isInt(-23.43));
297 assertFalse(goog.math.isInt(-.1));
298 assertFalse(goog.math.isInt(1e-1));
299 assertTrue(goog.math.isInt(1));
300 assertTrue(goog.math.isInt(0));
301 assertTrue(goog.math.isInt(-2));
302 assertTrue(goog.math.isInt(-2.0));
303 assertTrue(goog.math.isInt(10324231));
304 assertTrue(goog.math.isInt(1.));
305 assertTrue(goog.math.isInt(1e3));
306 }
307
308 function testIsFiniteNumber() {
309 assertFalse(goog.math.isFiniteNumber(NaN));
310 assertFalse(goog.math.isFiniteNumber(-Infinity));
311 assertFalse(goog.math.isFiniteNumber(+Infinity));
312 assertTrue(goog.math.isFiniteNumber(0));
313 assertTrue(goog.math.isFiniteNumber(1));
314 assertTrue(goog.math.isFiniteNumber(Math.PI));
315 }
316
317 function testIsNegativeZero() {
318 assertFalse(goog.math.isNegativeZero(0));
319 assertTrue(goog.math.isNegativeZero(-0));
320 assertFalse(goog.math.isNegativeZero(1));
321 assertFalse(goog.math.isNegativeZero(-1));
322 assertFalse(goog.math.isNegativeZero(-Number.MIN_VALUE));
323 }
324
325 function testLog10Floor() {
326 // The greatest floating point number that is less than 1.
327 var oneMinusEpsilon = 1 - Math.pow(2, -53);
328 for (var i = -30; i <= 30; i++) {
329 assertEquals(i, goog.math.log10Floor(parseFloat('1e' + i)));
330 assertEquals(
331 i - 1, goog.math.log10Floor(parseFloat('1e' + i) * oneMinusEpsilon));
332 }
333 assertEquals(-Infinity, goog.math.log10Floor(0));
334 assertTrue(isNaN(goog.math.log10Floor(-1)));
335 }
336
337 function testSafeFloor() {
338 assertEquals(0, goog.math.safeFloor(0));
339 assertEquals(0, goog.math.safeFloor(1e-15));
340 assertEquals(0, goog.math.safeFloor(-1e-15));
341 assertEquals(-1, goog.math.safeFloor(-3e-15));
342 assertEquals(4, goog.math.safeFloor(5 - 3e-15));
343 assertEquals(5, goog.math.safeFloor(5 - 1e-15));
344 assertEquals(-5, goog.math.safeFloor(-5 - 1e-15));
345 assertEquals(-6, goog.math.safeFloor(-5 - 3e-15));
346 assertEquals(3, goog.math.safeFloor(2.91, 0.1));
347 assertEquals(2, goog.math.safeFloor(2.89, 0.1));
348 // Tests some real life examples with the default epsilon value.
349 assertEquals(0, goog.math.safeFloor(Math.log(1000) / Math.LN10 - 3));
350 assertEquals(21, goog.math.safeFloor(Math.log(1e+21) / Math.LN10));
351 }
352
353 function testSafeCeil() {
354 assertEquals(0, goog.math.safeCeil(0));
355 assertEquals(0, goog.math.safeCeil(1e-15));
356 assertEquals(0, goog.math.safeCeil(-1e-15));
357 assertEquals(1, goog.math.safeCeil(3e-15));
358 assertEquals(6, goog.math.safeCeil(5 + 3e-15));
359 assertEquals(5, goog.math.safeCeil(5 + 1e-15));
360 assertEquals(-4, goog.math.safeCeil(-5 + 3e-15));
361 assertEquals(-5, goog.math.safeCeil(-5 + 1e-15));
362 assertEquals(3, goog.math.safeCeil(3.09, 0.1));
363 assertEquals(4, goog.math.safeCeil(3.11, 0.1));
364 }