|
3 | 3 | const expect = require('chai').expect; |
4 | 4 | const Runtime = require('../../src/Runtime'); |
5 | 5 |
|
| 6 | +const runtime = new Runtime(); |
| 7 | + |
6 | 8 | describe('Runtime.evaluateEquals', function() { |
7 | 9 | it('should compare same loose equals on non-float', function() { |
8 | | - const runtime = new Runtime(); |
9 | | - |
10 | 10 | expect(runtime.evaluateEquals(1, true)).to.be.true; |
11 | 11 | }); |
12 | 12 |
|
13 | 13 | it('should compare different loose equals on non-float', function() { |
14 | | - const runtime = new Runtime(); |
15 | | - |
16 | 14 | expect(runtime.evaluateEquals(0, true)).to.be.false; |
17 | 15 | }); |
18 | 16 |
|
19 | 17 | it('should compare same strict equals on non-float', function() { |
20 | | - const runtime = new Runtime(); |
21 | | - |
22 | 18 | expect(runtime.evaluateEquals('foo', 'foo', true)).to.be.true; |
23 | 19 | }); |
24 | 20 |
|
25 | 21 | it('should compare different types strict equals on non-float', function() { |
26 | | - const runtime = new Runtime(); |
27 | | - |
28 | 22 | expect(runtime.evaluateEquals(true, 1, true)).to.be.false; |
29 | 23 | }); |
30 | 24 |
|
31 | 25 | it('should compare different values strict equals on non-float', function() { |
32 | | - const runtime = new Runtime(); |
33 | | - |
34 | 26 | expect(runtime.evaluateEquals('foo', 'bar', true)).to.be.false; |
35 | 27 | }); |
| 28 | + |
| 29 | + // cases adapted from: http://floating-point-gui.de/errors/NearlyEqualsTest.java |
| 30 | + describe('with numeric values', function() { |
| 31 | + [ |
| 32 | + [1000000, 1000001], |
| 33 | + [1000001, 1000000], |
| 34 | + [-1000000, -1000001], |
| 35 | + [-1000001, -1000000], |
| 36 | + [1.0000001, 1.0000002], |
| 37 | + [1.0000002, 1.0000001], |
| 38 | + [-1.0000001, -1.0000002], |
| 39 | + [-1.0000002, -1.0000001], |
| 40 | + [0.000000001000001, 0.000000001000002], |
| 41 | + [0.000000001000002, 0.000000001000001], |
| 42 | + [-0.000000001000001, -0.000000001000002], |
| 43 | + [-0.000000001000002, -0.000000001000001], |
| 44 | + [0.0, 0.0], |
| 45 | + [0.0, -0.0], |
| 46 | + [-0.0, -0.0], |
| 47 | + [0, Math.pow(10, -40), 0.01], |
| 48 | + [Math.pow(10, -40), 0, 0.01], |
| 49 | + [0, -Math.pow(10, -40), 0.01], |
| 50 | + [-Math.pow(10, -40), 0, 0.01], |
| 51 | + [Number.MAX_VALUE, Number.MAX_VALUE], |
| 52 | + [Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY], |
| 53 | + [Number.NEGATIVE_INFINITY, Number.NEGATIVE_INFINITY], |
| 54 | + [10 * Number.MIN_VALUE, 10 * -Number.MIN_VALUE], |
| 55 | + [Number.MIN_VALUE, Number.MIN_VALUE], |
| 56 | + [Number.MIN_VALUE, -Number.MIN_VALUE], |
| 57 | + [-Number.MIN_VALUE, Number.MIN_VALUE], |
| 58 | + [Number.MIN_VALUE, 0], |
| 59 | + [0, Number.MIN_VALUE], |
| 60 | + [-Number.MIN_VALUE, 0], |
| 61 | + [0, -Number.MIN_VALUE] |
| 62 | + ].forEach((c) => { |
| 63 | + const epsilon = c[2] || 0.00001; |
| 64 | + |
| 65 | + it(`should compare ${c[0]} to be equal to ${c[1]} with epsilon ${epsilon}`, function() { |
| 66 | + runtime.epsilon = epsilon; |
| 67 | + expect(runtime.evaluateEquals(...c)).to.be.true; |
| 68 | + }); |
| 69 | + }); |
| 70 | + [ |
| 71 | + [10000, 10001], |
| 72 | + [10001, 10000], |
| 73 | + [-10000, -10001], |
| 74 | + [-10001, -10000], |
| 75 | + [1.0002, 1.0001], |
| 76 | + [1.0001, 1.0002], |
| 77 | + [-1.0001, -1.0002], |
| 78 | + [-1.0002, -1.0001], |
| 79 | + [Number.MAX_VALUE, -Number.MAX_VALUE], |
| 80 | + [-Number.MAX_VALUE, Number.MAX_VALUE], |
| 81 | + [Number.MAX_VALUE, Number.MAX_VALUE / 2], |
| 82 | + [Number.MAX_VALUE, -Number.MAX_VALUE / 2], |
| 83 | + [-Number.MAX_VALUE, Number.MAX_VALUE / 2], |
| 84 | + [Number.NEGATIVE_INFINITY, Number.POSITIVE_INFINITY], |
| 85 | + [Number.POSITIVE_INFINITY, Number.MAX_VALUE], |
| 86 | + [Number.NEGATIVE_INFINITY, -Number.MAX_VALUE], |
| 87 | + [Number.NaN, Number.NaN], |
| 88 | + [Number.NaN, 0.0], |
| 89 | + [-0.0, Number.NaN], |
| 90 | + [Number.NaN, -0.0], |
| 91 | + [0.0, Number.NaN], |
| 92 | + [Number.NaN, Number.POSITIVE_INFINITY], |
| 93 | + [Number.POSITIVE_INFINITY, Number.NaN], |
| 94 | + [Number.NaN, Number.NEGATIVE_INFINITY], |
| 95 | + [Number.NEGATIVE_INFINITY, Number.NaN], |
| 96 | + [Number.NaN, Number.MAX_VALUE], |
| 97 | + [Number.MAX_VALUE, Number.NaN], |
| 98 | + [Number.NaN, -Number.MAX_VALUE], |
| 99 | + [-Number.MAX_VALUE, Number.NaN], |
| 100 | + [Number.NaN, Number.MIN_VALUE], |
| 101 | + [Number.MIN_VALUE, Number.NaN], |
| 102 | + [Number.NaN, -Number.MIN_VALUE], |
| 103 | + [-Number.MIN_VALUE, Number.NaN], |
| 104 | + [1.000000001, -1.0], |
| 105 | + [-1.0, 1.000000001], |
| 106 | + [-1.000000001, 1.0], |
| 107 | + [1.0, -1.000000001] |
| 108 | + ].forEach((c) => { |
| 109 | + it(`should compare ${c[0]} to be not equal to ${c[1]} with epsilon 0.00001`, function() { |
| 110 | + runtime.epsilon = 0.00001; |
| 111 | + expect(runtime.evaluateEquals(...c)).to.be.false; |
| 112 | + }); |
| 113 | + }); |
| 114 | + }); |
36 | 115 | }); |
0 commit comments