|
2 | 2 |
|
3 | 3 | const BSON = require('../register-bson');
|
4 | 4 | const Double = BSON.Double;
|
5 |
| -const inspect = require('util').inspect; |
6 | 5 |
|
7 | 6 | describe('BSON Double Precision', function () {
|
8 | 7 | context('class Double', function () {
|
@@ -38,26 +37,158 @@ describe('BSON Double Precision', function () {
|
38 | 37 |
|
39 | 38 | describe('.toExtendedJSON()', () => {
|
40 | 39 | const tests = [
|
41 |
| - { input: new Double(0), output: { $numberDouble: '0.0' } }, |
42 |
| - { input: new Double(-0), output: { $numberDouble: '-0.0' } }, |
43 |
| - { input: new Double(3), output: { $numberDouble: '3.0' } }, |
44 |
| - { input: new Double(-3), output: { $numberDouble: '-3.0' } }, |
45 |
| - { input: new Double(3.4), output: { $numberDouble: '3.4' } }, |
46 |
| - { input: new Double(Number.EPSILON), output: { $numberDouble: '2.220446049250313e-16' } }, |
47 |
| - { input: new Double(12345e7), output: { $numberDouble: '123450000000.0' } }, |
48 |
| - { input: new Double(12345e-1), output: { $numberDouble: '1234.5' } }, |
49 |
| - { input: new Double(-12345e-1), output: { $numberDouble: '-1234.5' } }, |
50 |
| - { input: new Double(Infinity), output: { $numberDouble: 'Infinity' } }, |
51 |
| - { input: new Double(-Infinity), output: { $numberDouble: '-Infinity' } }, |
52 |
| - { input: new Double(NaN), output: { $numberDouble: 'NaN' } } |
| 40 | + { |
| 41 | + title: 'returns "0.0" when input is a number 0', |
| 42 | + input: 0, |
| 43 | + output: { $numberDouble: '0.0' } |
| 44 | + }, |
| 45 | + { |
| 46 | + title: 'returns "-0.0" when input is a number -0', |
| 47 | + input: -0, |
| 48 | + output: { $numberDouble: '-0.0' } |
| 49 | + }, |
| 50 | + { |
| 51 | + title: 'returns "0.0" when input is a string "-0.0"', |
| 52 | + input: '-0.0', |
| 53 | + output: { $numberDouble: '-0.0' } |
| 54 | + }, |
| 55 | + { |
| 56 | + title: 'returns "3.0" when input is a number 3', |
| 57 | + input: 3, |
| 58 | + output: { $numberDouble: '3.0' } |
| 59 | + }, |
| 60 | + { |
| 61 | + title: 'returns "-3.0" when input is a number -3', |
| 62 | + input: -3, |
| 63 | + output: { $numberDouble: '-3.0' } |
| 64 | + }, |
| 65 | + { |
| 66 | + title: 'returns "3.4" when input is a number 3.4', |
| 67 | + input: 3.4, |
| 68 | + output: { $numberDouble: '3.4' } |
| 69 | + }, |
| 70 | + { |
| 71 | + title: 'returns "2.220446049250313e-16" when input is Number.EPSILON', |
| 72 | + input: Number.EPSILON, |
| 73 | + output: { $numberDouble: '2.220446049250313e-16' } |
| 74 | + }, |
| 75 | + { |
| 76 | + title: 'returns "123450000000.0" when input is a number 12345e7', |
| 77 | + input: 12345e7, |
| 78 | + output: { $numberDouble: '123450000000.0' } |
| 79 | + }, |
| 80 | + { |
| 81 | + title: 'returns "1234.5" when input is a number 12345e-1', |
| 82 | + input: 12345e-1, |
| 83 | + output: { $numberDouble: '1234.5' } |
| 84 | + }, |
| 85 | + { |
| 86 | + title: 'returns "-1234.5" when input is a number -12345e-1', |
| 87 | + input: -12345e-1, |
| 88 | + output: { $numberDouble: '-1234.5' } |
| 89 | + }, |
| 90 | + { |
| 91 | + title: 'returns "Infinity" when input is a number Infinity', |
| 92 | + input: Infinity, |
| 93 | + output: { $numberDouble: 'Infinity' } |
| 94 | + }, |
| 95 | + { |
| 96 | + title: 'returns "-Infinity" when input is a number -Infinity', |
| 97 | + input: -Infinity, |
| 98 | + output: { $numberDouble: '-Infinity' } |
| 99 | + }, |
| 100 | + { |
| 101 | + title: 'returns "NaN" when input is a number NaN', |
| 102 | + input: NaN, |
| 103 | + output: { $numberDouble: 'NaN' } |
| 104 | + }, |
| 105 | + { |
| 106 | + title: 'returns "1.7976931348623157e+308" when input is a number Number.MAX_VALUE', |
| 107 | + input: Number.MAX_VALUE, |
| 108 | + output: { $numberDouble: '1.7976931348623157e+308' } |
| 109 | + }, |
| 110 | + { |
| 111 | + title: 'returns "5e-324" when input is a number Number.MIN_VALUE', |
| 112 | + input: Number.MIN_VALUE, |
| 113 | + output: { $numberDouble: '5e-324' } |
| 114 | + }, |
| 115 | + { |
| 116 | + title: 'returns "-1.7976931348623157e+308" when input is a number -Number.MAX_VALUE', |
| 117 | + input: -Number.MAX_VALUE, |
| 118 | + output: { $numberDouble: '-1.7976931348623157e+308' } |
| 119 | + }, |
| 120 | + { |
| 121 | + title: 'returns "-5e-324" when input is a number -Number.MIN_VALUE', |
| 122 | + input: -Number.MIN_VALUE, |
| 123 | + output: { $numberDouble: '-5e-324' } |
| 124 | + }, |
| 125 | + { |
| 126 | + // Reference: https://docs.oracle.com/cd/E19957-01/806-3568/ncg_math.html |
| 127 | + // min positive normal number |
| 128 | + title: |
| 129 | + 'returns "2.2250738585072014e-308" when input is a number the minimum positive normal value', |
| 130 | + input: '2.2250738585072014e-308', |
| 131 | + output: { $numberDouble: '2.2250738585072014e-308' } |
| 132 | + }, |
| 133 | + { |
| 134 | + // max subnormal number |
| 135 | + title: |
| 136 | + 'returns "2.225073858507201e-308" when input is a number the maximum positive subnormal value', |
| 137 | + input: '2.225073858507201e-308', |
| 138 | + output: { $numberDouble: '2.225073858507201e-308' } |
| 139 | + }, |
| 140 | + { |
| 141 | + // min positive subnormal number (NOTE: JS does not output same input string, but numeric values are equal) |
| 142 | + title: 'returns "5e-324" when input is a number the minimum positive subnormal value', |
| 143 | + input: '4.9406564584124654e-324', |
| 144 | + output: { $numberDouble: '5e-324' } |
| 145 | + }, |
| 146 | + { |
| 147 | + // https://262.ecma-international.org/13.0/#sec-number.prototype.tofixed |
| 148 | + // Note: calling toString on this integer returns 1000000000000000100, so toFixed is more precise |
| 149 | + // This test asserts we do not change _current_ behavior, however preserving this value is not |
| 150 | + // something that is possible in BSON, if a future version of this library were to emit |
| 151 | + // "1000000000000000100.0" instead, it would not be incorrect from a BSON/MongoDB/Double precision perspective, |
| 152 | + // it would just constrain the string output to what is possible with 8 bytes of floating point precision |
| 153 | + title: |
| 154 | + 'returns "1000000000000000128.0" when input is an int-like number beyond 8-byte double precision', |
| 155 | + input: '1000000000000000128', |
| 156 | + output: { $numberDouble: '1000000000000000128.0' } |
| 157 | + } |
53 | 158 | ];
|
54 | 159 |
|
55 | 160 | for (const test of tests) {
|
56 | 161 | const input = test.input;
|
57 | 162 | const output = test.output;
|
58 |
| - const title = `returns ${inspect(output)} when Double is ${input}`; |
| 163 | + const title = test.title; |
59 | 164 | it(title, () => {
|
60 |
| - expect(output).to.deep.equal(input.toExtendedJSON({ relaxed: false })); |
| 165 | + const inputAsDouble = new Double(input); |
| 166 | + expect(inputAsDouble.toExtendedJSON({ relaxed: false })).to.deep.equal(output); |
| 167 | + if (!Number.isNaN(inputAsDouble.value)) { |
| 168 | + expect(Number(inputAsDouble.toExtendedJSON({ relaxed: false }).$numberDouble)).to.equal( |
| 169 | + inputAsDouble.value |
| 170 | + ); |
| 171 | + } |
| 172 | + }); |
| 173 | + |
| 174 | + it(`preserves the byte wise value of ${input} (${typeof input}) after stringification`, () => { |
| 175 | + // Asserts the same bytes can be reconstructed from the generated string, |
| 176 | + // sometimes the string changes "4.9406564584124654e-324" -> "5e-324" |
| 177 | + // but both represent the same ieee754 double bytes |
| 178 | + const ejsonDoubleString = new Double(input).toExtendedJSON().$numberDouble; |
| 179 | + const bytesFromInput = (() => { |
| 180 | + const b = Buffer.alloc(8); |
| 181 | + b.writeDoubleBE(Number(input)); |
| 182 | + return b.toString('hex'); |
| 183 | + })(); |
| 184 | + |
| 185 | + const bytesFromOutput = (() => { |
| 186 | + const b = Buffer.alloc(8); |
| 187 | + b.writeDoubleBE(Number(ejsonDoubleString)); |
| 188 | + return b.toString('hex'); |
| 189 | + })(); |
| 190 | + |
| 191 | + expect(bytesFromOutput).to.equal(bytesFromInput); |
61 | 192 | });
|
62 | 193 | }
|
63 | 194 | });
|
|
0 commit comments