Ticket #1990: test_Math.js.patch

File test_Math.js.patch, 5.6 KB (added by historic_bruno, 11 years ago)
  • binaries/data/mods/public/simulation/components/tests/test_Math.js

     
     1/**
     2 * Tests for consistent and correct math results
     3 */
     4
     5 // +0 is different than -0, but standard equality won't test that
     6function isNegativeZero(z) { return z === 0 && 1/z === -Infinity; }
     7function isPositiveZero(z) { return z === 0 && 1/z === Infinity; }
     8
     9
     10// rounding
     11TS_ASSERT_EQUALS(0.1+0.2, 0.30000000000000004);
     12TS_ASSERT_EQUALS(0.1+0.7+0.3, 1.0999999999999999);
     13
     14// cos
     15TS_ASSERT_EQUALS(Math.cos(Math.PI/2), 0);
     16TS_ASSERT_UNEVAL_EQUALS(Math.cos(NaN), NaN);
     17TS_ASSERT_EQUALS(Math.cos(0), 1);
     18TS_ASSERT_EQUALS(Math.cos(-0), 1);
     19TS_ASSERT_UNEVAL_EQUALS(Math.cos(Infinity), NaN);
     20TS_ASSERT_UNEVAL_EQUALS(Math.cos(-Infinity), NaN);
     21
     22// sin
     23TS_ASSERT_EQUALS(Math.sin(Math.PI), 0);
     24TS_ASSERT_UNEVAL_EQUALS(Math.sin(NaN), NaN);
     25TS_ASSERT(isPositiveZero(Math.sin(0)));
     26// TS_ASSERT(isNegativeZero(Math.sin(-0))); TODO: doesn't match spec
     27TS_ASSERT_UNEVAL_EQUALS(Math.sin(Infinity), NaN);
     28TS_ASSERT_UNEVAL_EQUALS(Math.sin(-Infinity), NaN);
     29TS_ASSERT_EQUALS(Math.sin(1e-15), 7.771561172376096e-16);
     30
     31// atan
     32TS_ASSERT_UNEVAL_EQUALS(Math.atan(NaN), NaN);
     33TS_ASSERT(isPositiveZero(Math.atan(0)));
     34//TS_ASSERT(isNegativeZero(Math.atan(-0))); TODO: doesn't match spec
     35TS_ASSERT_EQUALS(Math.atan(Infinity), Math.PI/2);
     36TS_ASSERT_EQUALS(Math.atan(-Infinity), -Math.PI/2);
     37TS_ASSERT_EQUALS(Math.atan(1e-310), 1.00000000003903e-310);
     38TS_ASSERT_EQUALS(Math.atan(100), 1.5607966601078411);
     39
     40// atan2
     41TS_ASSERT_UNEVAL_EQUALS(Math.atan2(NaN, 1), NaN);
     42TS_ASSERT_UNEVAL_EQUALS(Math.atan2(1, NaN), NaN);
     43TS_ASSERT_EQUALS(Math.atan2(1, 0), Math.PI/2);
     44//TS_ASSERT_EQUALS(Math.atan2(1, -0), -Math.PI/2); TODO: doesn't match spec
     45TS_ASSERT(isPositiveZero(Math.atan2(0, 1)));
     46TS_ASSERT(isPositiveZero(Math.atan2(0, 0)));
     47TS_ASSERT_EQUALS(Math.atan2(0, -0), Math.PI);
     48TS_ASSERT_EQUALS(Math.atan2(0, -1), Math.PI);
     49TS_ASSERT(isNegativeZero(Math.atan2(-0, 1)));
     50TS_ASSERT(isNegativeZero(Math.atan2(-0, 0)));
     51TS_ASSERT_EQUALS(Math.atan2(-0, -0), -Math.PI);
     52TS_ASSERT_EQUALS(Math.atan2(-0, -1), -Math.PI);
     53TS_ASSERT_EQUALS(Math.atan2(-1, 0), -Math.PI/2);
     54TS_ASSERT_EQUALS(Math.atan2(-1, -0), -Math.PI/2);
     55TS_ASSERT(isPositiveZero(Math.atan2(1.7e308, Infinity)));
     56TS_ASSERT_EQUALS(Math.atan2(1.7e308, -Infinity), Math.PI);
     57TS_ASSERT(isNegativeZero(Math.atan2(-1.7e308, Infinity)));
     58TS_ASSERT_EQUALS(Math.atan2(-1.7e308, -Infinity), -Math.PI);
     59TS_ASSERT_EQUALS(Math.atan2(Infinity, -1.7e308), Math.PI/2);
     60TS_ASSERT_EQUALS(Math.atan2(-Infinity, 1.7e308), -Math.PI/2);
     61TS_ASSERT_EQUALS(Math.atan2(Infinity, Infinity), Math.PI/4);
     62TS_ASSERT_EQUALS(Math.atan2(Infinity, -Infinity), 3*Math.PI/4);
     63TS_ASSERT_EQUALS(Math.atan2(-Infinity, Infinity), -Math.PI/4);
     64TS_ASSERT_EQUALS(Math.atan2(-Infinity, -Infinity), -3*Math.PI/4);
     65TS_ASSERT_EQUALS(Math.atan2(1e-310, 2), 5.0000000001954e-311);
     66
     67// exp
     68TS_ASSERT_UNEVAL_EQUALS(Math.exp(NaN), NaN);
     69TS_ASSERT_EQUALS(Math.exp(0), 1);
     70TS_ASSERT_EQUALS(Math.exp(-0), 1);
     71TS_ASSERT_EQUALS(Math.exp(Infinity), Infinity);
     72TS_ASSERT(isPositiveZero(Math.exp(-Infinity)));
     73TS_ASSERT_EQUALS(Math.exp(10), 22026.465794806707);
     74
     75// log
     76TS_ASSERT_UNEVAL_EQUALS(Math.log("NaN"), NaN);
     77TS_ASSERT_UNEVAL_EQUALS(Math.log(-1), NaN);
     78TS_ASSERT_EQUALS(Math.log(0), -Infinity);
     79TS_ASSERT_EQUALS(Math.log(-0), -Infinity);
     80TS_ASSERT(isPositiveZero(Math.log(1)));
     81TS_ASSERT_EQUALS(Math.log(Infinity), Infinity);
     82TS_ASSERT_EQUALS(Math.log(Math.E), 0.9999999999999991);
     83TS_ASSERT_EQUALS(Math.log(Math.E*Math.E*Math.E), 2.999999999999999);
     84
     85// pow
     86TS_ASSERT_EQUALS(Math.pow(NaN, 0), 1);
     87TS_ASSERT_EQUALS(Math.pow(NaN, -0), 1);
     88TS_ASSERT_UNEVAL_EQUALS(Math.pow(NaN, 100), NaN);
     89TS_ASSERT_EQUALS(Math.pow(1.7e308, Infinity), Infinity);
     90TS_ASSERT_EQUALS(Math.pow(-1.7e308, Infinity), Infinity);
     91TS_ASSERT(isPositiveZero(Math.pow(1.7e308, -Infinity)));
     92TS_ASSERT(isPositiveZero(Math.pow(-1.7e308, -Infinity)));
     93TS_ASSERT_UNEVAL_EQUALS(Math.pow(1, Infinity), NaN);
     94TS_ASSERT_UNEVAL_EQUALS(Math.pow(-1, Infinity), NaN);
     95TS_ASSERT_UNEVAL_EQUALS(Math.pow(1, -Infinity), NaN);
     96TS_ASSERT_UNEVAL_EQUALS(Math.pow(-1, -Infinity), NaN);
     97TS_ASSERT(isPositiveZero(Math.pow(1e-310, Infinity)));
     98TS_ASSERT(isPositiveZero(Math.pow(-1e-310, Infinity)));
     99TS_ASSERT_EQUALS(Math.pow(1e-310, -Infinity), Infinity);
     100TS_ASSERT_EQUALS(Math.pow(-1e-310, -Infinity), Infinity);
     101TS_ASSERT_EQUALS(Math.pow(Infinity, 1e-310), Infinity);
     102TS_ASSERT(isPositiveZero(Math.pow(Infinity, -1e-310)));
     103TS_ASSERT_EQUALS(Math.pow(-Infinity, 101), -Infinity);
     104TS_ASSERT_EQUALS(Math.pow(-Infinity, 1.7e308), Infinity);
     105TS_ASSERT(isNegativeZero(Math.pow(-Infinity, -101)));
     106TS_ASSERT(isPositiveZero(Math.pow(-Infinity, -1.7e308)));
     107TS_ASSERT(isPositiveZero(Math.pow(0, 1e-310)));
     108TS_ASSERT_EQUALS(Math.pow(0, -1e-310), Infinity);
     109TS_ASSERT(isNegativeZero(Math.pow(-0, 101)));
     110TS_ASSERT(isPositiveZero(Math.pow(-0, 1e-310)));
     111TS_ASSERT_EQUALS(Math.pow(-0, -101), -Infinity);
     112TS_ASSERT_EQUALS(Math.pow(-0, -1e-310), Infinity);
     113TS_ASSERT_UNEVAL_EQUALS(Math.pow(-1.7e308, 1e-310), NaN);
     114TS_ASSERT_EQUALS(Math.pow(Math.PI, -100), 1.9275814160560185e-50);
     115
     116// sqrt
     117TS_ASSERT_UNEVAL_EQUALS(Math.sqrt(NaN), NaN);
     118TS_ASSERT_UNEVAL_EQUALS(Math.sqrt(-1e-323), NaN);
     119TS_ASSERT(isPositiveZero(Math.sqrt(0)));
     120TS_ASSERT(isNegativeZero(Math.sqrt(-0)));
     121TS_ASSERT_EQUALS(Math.sqrt(Infinity), Infinity);
     122TS_ASSERT_EQUALS(Math.sqrt(1e-323), 3.1434555694052576e-162);
     123
     124