-
Notifications
You must be signed in to change notification settings - Fork 265
fix(NODE-3630): remove float parser and test edge cases for Double #502
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 11 commits
41b7107
1c1774e
39c0add
37c8edb
fb76f7d
e45f7d0
af47143
81d7544
f3f5e63
95b1af6
3d4b8a7
4d7ff2f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,36 +3,91 @@ | |
| const BSON = require('../register-bson'); | ||
| const Double = BSON.Double; | ||
|
|
||
| describe('Double', function () { | ||
| describe('Constructor', function () { | ||
| var value = 42.3456; | ||
| describe('BSON Double Precision', function () { | ||
| context('class Double', function () { | ||
| describe('constructor()', function () { | ||
| const value = 42.3456; | ||
|
|
||
| it('Primitive number', function (done) { | ||
| expect(new Double(value).valueOf()).to.equal(value); | ||
| done(); | ||
| it('Primitive number', function (done) { | ||
| expect(new Double(value).valueOf()).to.equal(value); | ||
| done(); | ||
| }); | ||
|
|
||
| it('Number object', function (done) { | ||
| expect(new Double(new Number(value)).valueOf()).to.equal(value); | ||
| done(); | ||
| }); | ||
| }); | ||
|
|
||
| it('Number object', function (done) { | ||
| expect(new Double(new Number(value)).valueOf()).to.equal(value); | ||
| done(); | ||
| describe('#toString()', () => { | ||
| it('should serialize to a string', () => { | ||
| const testNumber = Math.random() * Number.MAX_VALUE; | ||
| const double = new Double(testNumber); | ||
| expect(double.toString()).to.equal(testNumber.toString()); | ||
| }); | ||
|
|
||
| const testRadices = [2, 8, 10, 16, 22]; | ||
|
|
||
| for (const radix of testRadices) { | ||
| it(`should support radix argument: ${radix}`, () => { | ||
| const testNumber = Math.random() * Number.MAX_VALUE; | ||
| const double = new Double(testNumber); | ||
| expect(double.toString(radix)).to.equal(testNumber.toString(radix)); | ||
| }); | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| describe('toString', () => { | ||
| it('should serialize to a string', () => { | ||
| const testNumber = Math.random() * Number.MAX_VALUE; | ||
| const double = new Double(testNumber); | ||
| expect(double.toString()).to.equal(testNumber.toString()); | ||
| function serializeThenDeserialize(value) { | ||
| const serializedDouble = BSON.serialize({ d: value }); | ||
| const deserializedDouble = BSON.deserialize(serializedDouble, { promoteValues: false }); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what's the significance of
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is so we get the raw BSON instead of the value as a double; this lets us check for payload in the BSON buffer
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could you add a comment above this line explaining that? also @nbbeeken is there a more precise option we can use than the general promoteValues?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unfortunately Doubles do not have a dedicated promote option, promoteValues: false is the only way to get the deserializer to return instanceof Double. chaos |
||
| return deserializedDouble.d; | ||
| } | ||
|
|
||
| const testCases = [ | ||
| { name: 'Infinity', doubleVal: new Double(Infinity), testVal: Infinity }, | ||
| { name: '-Infinity', doubleVal: new Double(-Infinity), testVal: -Infinity }, | ||
| { name: 'Number.EPSILON', doubleVal: new Double(Number.EPSILON), testVal: Number.EPSILON }, | ||
| { name: 'Zero', doubleVal: new Double(0), testVal: 0 }, | ||
| { name: 'Negative Zero', doubleVal: new Double(-0), testVal: -0 }, | ||
| { name: 'NaN', doubleVal: new Double(NaN), testVal: NaN } | ||
| ]; | ||
|
|
||
| for (const { name, doubleVal, testVal } of testCases) { | ||
| it(`should preserve the input value ${name} in Double serialize-deserialize roundtrip`, () => { | ||
| const roundTrippedVal = serializeThenDeserialize(doubleVal); | ||
| expect(Object.is(doubleVal.value, testVal)).to.be.true; | ||
| expect(Object.is(roundTrippedVal.value, doubleVal.value)).to.be.true; | ||
| }); | ||
| } | ||
|
|
||
| const testRadices = [2, 8, 10, 16, 22]; | ||
| context('NaN with Payload', function () { | ||
| const NanPayloadBuffer = Buffer.from('120000000000F87F', 'hex'); | ||
| const NanPayloadDV = new DataView( | ||
| NanPayloadBuffer.buffer, | ||
| NanPayloadBuffer.byteOffset, | ||
| NanPayloadBuffer.byteLength | ||
| ); | ||
| const NanPayloadDouble = NanPayloadDV.getFloat64(0, true); | ||
| const serializedNanPayloadDouble = BSON.serialize({ d: NanPayloadDouble }); | ||
|
|
||
| for (const radix of testRadices) { | ||
| it(`should support radix argument: ${radix}`, () => { | ||
| const testNumber = Math.random() * Number.MAX_VALUE; | ||
| const double = new Double(testNumber); | ||
| expect(double.toString(radix)).to.equal(testNumber.toString(radix)); | ||
| }); | ||
| } | ||
| it('should keep payload in serialize-deserialize roundtrip', function () { | ||
| expect(serializedNanPayloadDouble.subarray(7, 15)).to.deep.equal(NanPayloadBuffer); | ||
| }); | ||
|
|
||
| it('should preserve NaN value in serialize-deserialize roundtrip', function () { | ||
| const { d: newVal } = BSON.deserialize(serializedNanPayloadDouble, { promoteValues: true }); | ||
| expect(newVal).to.be.NaN; | ||
| }); | ||
| }); | ||
|
|
||
| it('NODE-4335: does not preserve -0 in serialize-deserialize roundtrip if JS number is used', function () { | ||
| // TODO (NODE-4335): -0 should be serialized as double | ||
| // 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. | ||
| const value = -0; | ||
| const serializedDouble = BSON.serialize({ d: value }); | ||
| const type = serializedDouble[4]; | ||
| expect(type).to.not.equal(BSON.BSON_DATA_NUMBER); | ||
| expect(type).to.equal(BSON.BSON_DATA_INT); | ||
| }); | ||
| }); | ||
Uh oh!
There was an error while loading. Please reload this page.