|
3 | 3 | const BSON = require('../register-bson');
|
4 | 4 | const Double = BSON.Double;
|
5 | 5 |
|
6 |
| -describe('Double', function () { |
7 |
| - describe('Constructor', function () { |
8 |
| - var value = 42.3456; |
| 6 | +describe('BSON Double Precision', function () { |
| 7 | + context('class Double', function () { |
| 8 | + describe('constructor()', function () { |
| 9 | + const value = 42.3456; |
9 | 10 |
|
10 |
| - it('Primitive number', function (done) { |
11 |
| - expect(new Double(value).valueOf()).to.equal(value); |
12 |
| - done(); |
| 11 | + it('Primitive number', function () { |
| 12 | + expect(new Double(value).valueOf()).to.equal(value); |
| 13 | + }); |
| 14 | + |
| 15 | + it('Number object', function () { |
| 16 | + expect(new Double(new Number(value)).valueOf()).to.equal(value); |
| 17 | + }); |
13 | 18 | });
|
14 | 19 |
|
15 |
| - it('Number object', function (done) { |
16 |
| - expect(new Double(new Number(value)).valueOf()).to.equal(value); |
17 |
| - done(); |
| 20 | + describe('#toString()', () => { |
| 21 | + it('should serialize to a string', () => { |
| 22 | + const testNumber = Math.random() * Number.MAX_VALUE; |
| 23 | + const double = new Double(testNumber); |
| 24 | + expect(double.toString()).to.equal(testNumber.toString()); |
| 25 | + }); |
| 26 | + |
| 27 | + const testRadices = [2, 8, 10, 16, 22]; |
| 28 | + |
| 29 | + for (const radix of testRadices) { |
| 30 | + it(`should support radix argument: ${radix}`, () => { |
| 31 | + const testNumber = Math.random() * Number.MAX_VALUE; |
| 32 | + const double = new Double(testNumber); |
| 33 | + expect(double.toString(radix)).to.equal(testNumber.toString(radix)); |
| 34 | + }); |
| 35 | + } |
18 | 36 | });
|
19 | 37 | });
|
20 | 38 |
|
21 |
| - describe('toString', () => { |
22 |
| - it('should serialize to a string', () => { |
23 |
| - const testNumber = Math.random() * Number.MAX_VALUE; |
24 |
| - const double = new Double(testNumber); |
25 |
| - expect(double.toString()).to.equal(testNumber.toString()); |
| 39 | + function serializeThenDeserialize(value) { |
| 40 | + const serializedDouble = BSON.serialize({ d: value }); |
| 41 | + const deserializedDouble = BSON.deserialize(serializedDouble, { promoteValues: false }); |
| 42 | + return deserializedDouble.d; |
| 43 | + } |
| 44 | + |
| 45 | + const testCases = [ |
| 46 | + { name: 'Infinity', doubleVal: new Double(Infinity), testVal: Infinity }, |
| 47 | + { name: '-Infinity', doubleVal: new Double(-Infinity), testVal: -Infinity }, |
| 48 | + { name: 'Number.EPSILON', doubleVal: new Double(Number.EPSILON), testVal: Number.EPSILON }, |
| 49 | + { name: 'Zero', doubleVal: new Double(0), testVal: 0 }, |
| 50 | + { name: 'Negative Zero', doubleVal: new Double(-0), testVal: -0 }, |
| 51 | + { name: 'NaN', doubleVal: new Double(NaN), testVal: NaN } |
| 52 | + ]; |
| 53 | + |
| 54 | + for (const { name, doubleVal, testVal } of testCases) { |
| 55 | + it(`should preserve the input value ${name} in Double serialize-deserialize roundtrip`, () => { |
| 56 | + const roundTrippedVal = serializeThenDeserialize(doubleVal); |
| 57 | + expect(Object.is(doubleVal.value, testVal)).to.be.true; |
| 58 | + expect(Object.is(roundTrippedVal.value, doubleVal.value)).to.be.true; |
26 | 59 | });
|
| 60 | + } |
27 | 61 |
|
28 |
| - const testRadices = [2, 8, 10, 16, 22]; |
| 62 | + context('NaN with Payload', function () { |
| 63 | + const NanPayloadBuffer = Buffer.from('120000000000F87F', 'hex'); |
| 64 | + const NanPayloadDV = new DataView( |
| 65 | + NanPayloadBuffer.buffer, |
| 66 | + NanPayloadBuffer.byteOffset, |
| 67 | + NanPayloadBuffer.byteLength |
| 68 | + ); |
| 69 | + const NanPayloadDouble = NanPayloadDV.getFloat64(0, true); |
| 70 | + // Using promoteValues: false (returning raw BSON) in order to be able to check that payload remains intact |
| 71 | + const serializedNanPayloadDouble = BSON.serialize({ d: NanPayloadDouble }); |
29 | 72 |
|
30 |
| - for (const radix of testRadices) { |
31 |
| - it(`should support radix argument: ${radix}`, () => { |
32 |
| - const testNumber = Math.random() * Number.MAX_VALUE; |
33 |
| - const double = new Double(testNumber); |
34 |
| - expect(double.toString(radix)).to.equal(testNumber.toString(radix)); |
35 |
| - }); |
36 |
| - } |
| 73 | + it('should keep payload in serialize-deserialize roundtrip', function () { |
| 74 | + expect(serializedNanPayloadDouble.subarray(7, 15)).to.deep.equal(NanPayloadBuffer); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should preserve NaN value in serialize-deserialize roundtrip', function () { |
| 78 | + const { d: newVal } = BSON.deserialize(serializedNanPayloadDouble, { promoteValues: true }); |
| 79 | + expect(newVal).to.be.NaN; |
| 80 | + }); |
| 81 | + }); |
| 82 | + |
| 83 | + it('NODE-4335: does not preserve -0 in serialize-deserialize roundtrip if JS number is used', function () { |
| 84 | + // TODO (NODE-4335): -0 should be serialized as double |
| 85 | + // This test is demonstrating the behavior of -0 being serialized as an int32 something we do NOT want to unintentionally change, but may want to change in the future, which the above ticket serves to track. |
| 86 | + const value = -0; |
| 87 | + const serializedDouble = BSON.serialize({ d: value }); |
| 88 | + const type = serializedDouble[4]; |
| 89 | + expect(type).to.not.equal(BSON.BSON_DATA_NUMBER); |
| 90 | + expect(type).to.equal(BSON.BSON_DATA_INT); |
37 | 91 | });
|
38 | 92 | });
|
0 commit comments