|
1 | 1 | 'use strict';
|
2 | 2 |
|
3 | 3 | const BSON = require('../register-bson');
|
4 |
| -const { getNodeMajor, isBrowser } = require('./tools/utils'); |
5 | 4 | const Double = BSON.Double;
|
6 | 5 |
|
7 |
| -describe('Double', function () { |
8 |
| - describe('Constructor', function () { |
9 |
| - var value = 42.3456; |
| 6 | +describe('BSON Double Precision', function () { |
| 7 | + context('class Double', function () { |
| 8 | + describe('constructor()', function () { |
| 9 | + const value = 42.3456; |
10 | 10 |
|
11 |
| - it('Primitive number', function (done) { |
12 |
| - expect(new Double(value).valueOf()).to.equal(value); |
13 |
| - done(); |
14 |
| - }); |
15 |
| - |
16 |
| - it('Number object', function (done) { |
17 |
| - expect(new Double(new Number(value)).valueOf()).to.equal(value); |
18 |
| - done(); |
19 |
| - }); |
20 |
| - }); |
| 11 | + it('Primitive number', function (done) { |
| 12 | + expect(new Double(value).valueOf()).to.equal(value); |
| 13 | + done(); |
| 14 | + }); |
21 | 15 |
|
22 |
| - describe('toString', () => { |
23 |
| - it('should serialize to a string', () => { |
24 |
| - const testNumber = Math.random() * Number.MAX_VALUE; |
25 |
| - const double = new Double(testNumber); |
26 |
| - expect(double.toString()).to.equal(testNumber.toString()); |
| 16 | + it('Number object', function (done) { |
| 17 | + expect(new Double(new Number(value)).valueOf()).to.equal(value); |
| 18 | + done(); |
| 19 | + }); |
27 | 20 | });
|
28 | 21 |
|
29 |
| - const testRadices = [2, 8, 10, 16, 22]; |
30 |
| - |
31 |
| - for (const radix of testRadices) { |
32 |
| - it(`should support radix argument: ${radix}`, () => { |
| 22 | + describe('#toString()', () => { |
| 23 | + it('should serialize to a string', () => { |
33 | 24 | const testNumber = Math.random() * Number.MAX_VALUE;
|
34 | 25 | const double = new Double(testNumber);
|
35 |
| - expect(double.toString(radix)).to.equal(testNumber.toString(radix)); |
| 26 | + expect(double.toString()).to.equal(testNumber.toString()); |
36 | 27 | });
|
37 |
| - } |
38 |
| - }); |
39 |
| - |
40 |
| - describe('specialValues', () => { |
41 |
| - function twiceSerialized(value) { |
42 |
| - let serializedDouble = BSON.serialize({ d: value }); |
43 |
| - let deserializedDouble = BSON.deserialize(serializedDouble, { promoteValues: true }); |
44 |
| - let newVal = deserializedDouble.d; |
45 |
| - return newVal; |
46 |
| - } |
47 |
| - |
48 |
| - it('inf', () => { |
49 |
| - let value = Infinity; |
50 |
| - let newVal = twiceSerialized(value); |
51 |
| - expect(value).to.equal(newVal); |
52 |
| - }); |
53 | 28 |
|
54 |
| - it('-inf', () => { |
55 |
| - let value = -Infinity; |
56 |
| - let newVal = twiceSerialized(value); |
57 |
| - expect(value).to.equal(newVal); |
58 |
| - }); |
| 29 | + const testRadices = [2, 8, 10, 16, 22]; |
59 | 30 |
|
60 |
| - it('NaN', () => { |
61 |
| - let value = NaN; |
62 |
| - let newVal = twiceSerialized(value); |
63 |
| - expect(Number.isNaN(newVal)).to.equal(true); |
64 |
| - }); |
65 |
| - |
66 |
| - it('NaN with payload', function () { |
67 |
| - if (!isBrowser() && getNodeMajor() < 10) { |
68 |
| - this.skip(); |
| 31 | + for (const radix of testRadices) { |
| 32 | + it(`should support radix argument: ${radix}`, () => { |
| 33 | + const testNumber = Math.random() * Number.MAX_VALUE; |
| 34 | + const double = new Double(testNumber); |
| 35 | + expect(double.toString(radix)).to.equal(testNumber.toString(radix)); |
| 36 | + }); |
69 | 37 | }
|
70 |
| - let buffer = Buffer.from('120000000000F87F', 'hex'); |
71 |
| - |
72 |
| - const dv = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength); |
73 |
| - let value = dv.getFloat64(0, true); |
74 |
| - |
75 |
| - let serializedDouble = BSON.serialize({ d: value }); |
76 |
| - expect(serializedDouble.subarray(7, 15)).to.deep.equal(buffer); |
77 |
| - let { d: newVal } = BSON.deserialize(serializedDouble, { promoteValues: true }); |
78 |
| - expect(Number.isNaN(newVal)).to.equal(true); |
79 | 38 | });
|
| 39 | + }); |
80 | 40 |
|
81 |
| - it('0', () => { |
82 |
| - let value = 0; |
83 |
| - let orig = new Double(value); |
84 |
| - let newVal = twiceSerialized(orig); |
85 |
| - expect(value).to.equal(newVal); |
| 41 | + function serializeThenDeserialize(value) { |
| 42 | + const serializedDouble = BSON.serialize({ d: value }); |
| 43 | + const deserializedDouble = BSON.deserialize(serializedDouble, { promoteValues: false }); |
| 44 | + return deserializedDouble.d; |
| 45 | + } |
| 46 | + |
| 47 | + const testCases = [ |
| 48 | + { name: 'Infinity', input: new Double(Infinity) }, |
| 49 | + { name: '-Infinity', input: new Double(-Infinity) }, |
| 50 | + { name: 'Number.EPSILON', input: new Double(Number.EPSILON) }, |
| 51 | + { name: 'Zero', input: new Double(0) }, |
| 52 | + { name: 'Double (Negative Zero)', input: new Double(-0) } |
| 53 | + ]; |
| 54 | + |
| 55 | + for (const { name, input } of testCases) { |
| 56 | + it(`should return Double from serialize-deserialize roundtrip when value is: ${name}`, () => { |
| 57 | + const outcome = serializeThenDeserialize(input); |
| 58 | + expect(outcome.value).to.equal(input.value); |
| 59 | + expect(Object.is(outcome.value, input.value)).to.be.true; |
86 | 60 | });
|
| 61 | + } |
87 | 62 |
|
88 |
| - it('-0', () => { |
89 |
| - let value = -0; |
90 |
| - let orig = new Double(value); |
91 |
| - let newVal = twiceSerialized(orig); |
92 |
| - expect(Object.is(newVal, -0)).to.be.true; |
93 |
| - }); |
| 63 | + it('should preserve NaN value in serialize-deserialize roundtrip', () => { |
| 64 | + const value = NaN; |
| 65 | + const newVal = serializeThenDeserialize(value); |
| 66 | + expect(Number.isNaN(newVal.value)).to.equal(true); |
| 67 | + }); |
94 | 68 |
|
95 |
| - // TODO (NODE-4335): -0 should be serialized as double |
96 |
| - it.skip('-0 serializes as Double', () => { |
97 |
| - let value = -0; |
98 |
| - let serializedDouble = BSON.serialize({ d: value }); |
99 |
| - let type = serializedDouble[5]; |
100 |
| - expect(type).to.not.equal(BSON.BSON_DATA_NUMBER); |
| 69 | + context('NaN with Payload', function () { |
| 70 | + const NanPayloadBuffer = Buffer.from('120000000000F87F', 'hex'); |
| 71 | + const NanPayloadDV = new DataView( |
| 72 | + NanPayloadBuffer.buffer, |
| 73 | + NanPayloadBuffer.byteOffset, |
| 74 | + NanPayloadBuffer.byteLength |
| 75 | + ); |
| 76 | + const NanPayloadDouble = NanPayloadDV.getFloat64(0, true); |
| 77 | + const serializedNanPayloadDouble = BSON.serialize({ d: NanPayloadDouble }); |
| 78 | + |
| 79 | + it('should keep payload in serialize-deserialize roundtrip', function () { |
| 80 | + expect(serializedNanPayloadDouble.subarray(7, 15)).to.deep.equal(NanPayloadBuffer); |
101 | 81 | });
|
102 | 82 |
|
103 |
| - it('Number.EPSILON', () => { |
104 |
| - let value = Number.EPSILON; |
105 |
| - let newVal = twiceSerialized(value); |
106 |
| - expect(value).to.equal(newVal); |
| 83 | + it('should preserve NaN value in serialize-deserialize roundtrip', function () { |
| 84 | + const { d: newVal } = BSON.deserialize(serializedNanPayloadDouble, { promoteValues: true }); |
| 85 | + expect(Number.isNaN(newVal)).to.equal(true); |
107 | 86 | });
|
108 | 87 | });
|
| 88 | + it('NODE-4335: does not preserve -0 in serialize-deserialize roundtrip if JS number is used', () => { |
| 89 | + // TODO (NODE-4335): -0 should be serialized as double |
| 90 | + const value = -0; |
| 91 | + const serializedDouble = BSON.serialize({ d: value }); |
| 92 | + const type = serializedDouble[4]; |
| 93 | + expect(type).to.not.equal(BSON.BSON_DATA_NUMBER); |
| 94 | + expect(type).to.equal(BSON.BSON_DATA_INT); |
| 95 | + }); |
109 | 96 | });
|
0 commit comments