Skip to content

Commit 81d7544

Browse files
double test refactoring and PR fixes
1 parent af47143 commit 81d7544

File tree

2 files changed

+71
-89
lines changed

2 files changed

+71
-89
lines changed

test/node/bson_corpus.spec.test.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
const Buffer = require('buffer').Buffer;
55
const BSON = require('../register-bson');
6-
const { getNodeMajor, isBrowser } = require('./tools/utils');
76
const BSONError = BSON.BSONError;
87
const EJSON = BSON.EJSON;
98

@@ -122,10 +121,6 @@ describe('BSON Corpus', function () {
122121
describe('valid-bson', function () {
123122
for (const v of valid) {
124123
it(v.description, function () {
125-
if (v.description === 'NaN with payload' && !isBrowser() && getNodeMajor() < 10) {
126-
this.skip();
127-
}
128-
129124
if (
130125
v.description === 'All BSON types' &&
131126
scenario._filename === 'multi-type-deprecated'

test/node/double_tests.js

Lines changed: 71 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,109 +1,96 @@
11
'use strict';
22

33
const BSON = require('../register-bson');
4-
const { getNodeMajor, isBrowser } = require('./tools/utils');
54
const Double = BSON.Double;
65

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;
1010

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+
});
2115

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+
});
2720
});
2821

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', () => {
3324
const testNumber = Math.random() * Number.MAX_VALUE;
3425
const double = new Double(testNumber);
35-
expect(double.toString(radix)).to.equal(testNumber.toString(radix));
26+
expect(double.toString()).to.equal(testNumber.toString());
3627
});
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-
});
5328

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];
5930

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+
});
6937
}
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);
7938
});
39+
});
8040

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;
8660
});
61+
}
8762

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+
});
9468

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);
10181
});
10282

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);
10786
});
10887
});
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+
});
10996
});

0 commit comments

Comments
 (0)