1 |
|
2 | var assert = require('assert'),
|
3 | approx = require('../tools/approx');
|
4 |
|
5 | describe('approx', function() {
|
6 |
|
7 | it('should test equality of positive values', function() {
|
8 | approx.equal(1/3, 0.33333333);
|
9 | approx.equal(2, 2.000001);
|
10 | approx.equal(2, 1.999999);
|
11 | assert.throws(function () {approx.equal(2, 2.001)}, assert.AssertionError);
|
12 | assert.throws(function () {approx.equal(2, 1.999)}, assert.AssertionError);
|
13 | });
|
14 |
|
15 | it('should test equality of negative values', function() {
|
16 | approx.equal(-2, -2.000001);
|
17 | approx.equal(-2, -1.999999);
|
18 | assert.throws(function () {approx.equal(-2, -2.001)}, assert.AssertionError);
|
19 | assert.throws(function () {approx.equal(-2, -1.999)}, assert.AssertionError);
|
20 | });
|
21 |
|
22 | it('should test equality of very large values', function() {
|
23 | approx.equal(2e100, 2.000001e100);
|
24 | approx.equal(2e100, 1.999999e100);
|
25 | assert.throws(function () {approx.equal(2e100, 2.001e100)}, assert.AssertionError);
|
26 | assert.throws(function () {approx.equal(2e100, 1.999e100)}, assert.AssertionError);
|
27 | });
|
28 |
|
29 | it('should test equality of very small values', function() {
|
30 | approx.equal(2e-100, 2.000001e-100);
|
31 | approx.equal(2e-100, 1.999999e-100);
|
32 | assert.throws(function () {approx.equal(2e-100, 2.001e-100)}, assert.AssertionError);
|
33 | assert.throws(function () {approx.equal(2e-100, 1.999e-100)}, assert.AssertionError);
|
34 | });
|
35 |
|
36 | it('should test equality of NaN numbers', function() {
|
37 |
|
38 | var a = NaN;
|
39 | var b = NaN;
|
40 | approx.equal(a, b);
|
41 | assert.throws(function () {approx.equal(NaN, 3)}, assert.AssertionError);
|
42 | assert.throws(function () {approx.equal(NaN, 'nonumber')}, assert.AssertionError);
|
43 | });
|
44 |
|
45 | it('should test equality when one of the values is zero', function() {
|
46 |
|
47 | approx.equal(0, 1e-15);
|
48 | approx.equal(1e-15, 0);
|
49 | assert.throws(function () {approx.equal(0, 0.001)}, assert.AssertionError);
|
50 | });
|
51 |
|
52 | it('should test deep equality of arrays and objects', function() {
|
53 | approx.deepEqual({
|
54 | a: [1, 2, 3],
|
55 | b: [{c:4, d:5}]
|
56 | }, {
|
57 | a: [1.000001, 1.99999999, 3.000005],
|
58 | b: [{c:3.999999981, d:5.0000023}]
|
59 | });
|
60 |
|
61 | assert.throws(function () {approx.deepEqual({
|
62 | a: [1, 2, 3],
|
63 | b: [{c:4, d:5}]
|
64 | }, {
|
65 | a: [1.000001, 1.99999999, 3.000005],
|
66 | b: [{c:3.1, d:5.0000023}]
|
67 | })}, assert.AssertionError);
|
68 |
|
69 | assert.throws(function () {approx.deepEqual({
|
70 | a: [1, 2, 3],
|
71 | b: [{c:4, d:5}]
|
72 | }, {
|
73 | a: [1.001, 1.99999999, 3.000005],
|
74 | b: [{c:3, d:5.0000023}]
|
75 | })}, assert.AssertionError);
|
76 | });
|
77 |
|
78 | });
|