Skip to content

Commit 877df34

Browse files
committed
test(NODE-5594): add tests to check rounding behaviour
1 parent fa5fcd9 commit 877df34

File tree

2 files changed

+54
-4
lines changed

2 files changed

+54
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { Suite } from '../suite';
2+
import * as BSON from '../../../../';
3+
4+
export function getObjectIdSerializationSuite(): Suite {
5+
const objectIdArray = Array.from({ length: 1000 }, () => ({ _id: new BSON.ObjectId() }));
6+
7+
const suite = new Suite('ObjectId serialization');
8+
return suite.task({
9+
name: `objectIdSerialization`,
10+
data: objectIdArray,
11+
fn: objectIdArray => {
12+
for (const oid of objectIdArray) BSON.serialize(oid);
13+
},
14+
iterations: 100,
15+
resultUnit: 'megabytes_per_second',
16+
transform: (runtimeMS: number) => {
17+
return BSON.serialize(objectIdArray).byteLength / 1024 ** 2 / (runtimeMS / 1000);
18+
}
19+
});
20+
}

test/node/decimal128.test.ts

+34-4
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ describe('Decimal128', function () {
468468

469469
it('fromString from string round', function (done) {
470470
// Create decimal from string value 10E-6177
471-
let result = Decimal128.fromString('10E-6177');
471+
const result = Decimal128.fromString('10E-6177');
472472
const bytes = Buffer.from(
473473
[
474474
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
@@ -478,9 +478,6 @@ describe('Decimal128', function () {
478478

479479
expect(bytes).to.deep.equal(result.bytes);
480480

481-
result = Decimal128.fromString('37.499999999999999196428571428571375');
482-
expect(result.toString()).to.deep.equal('37.49999999999999919642857142857138');
483-
484481
// // Create decimal from string value 15E-6177
485482
// result = Decimal128.fromString('15E-6177');
486483
// bytes = Buffer.from(
@@ -1306,6 +1303,39 @@ describe('Decimal128', function () {
13061303
});
13071304
}
13081305
});
1306+
1307+
context('when the input has more than 34 significant digits', function () {
1308+
it('does not throw an error', function () {
1309+
expect(() =>
1310+
Decimal128.fromStringWithRounding('37.499999999999999196428571428571375')
1311+
).to.not.throw();
1312+
});
1313+
context('when the digit to round is >= 5', function () {
1314+
it('rounds up correctly', function () {
1315+
const result = Decimal128.fromStringWithRounding(
1316+
'37.499999999999999196428571428571375'
1317+
);
1318+
expect(result.toString()).to.deep.equal('37.49999999999999919642857142857138');
1319+
});
1320+
});
1321+
context('when the digit to round is < 5', function () {
1322+
it('rounds down correctly', function () {
1323+
const result = Decimal128.fromStringWithRounding(
1324+
'37.499999999999999196428571428571374'
1325+
);
1326+
expect(result.toString()).to.deep.equal('37.49999999999999919642857142857137');
1327+
});
1328+
});
1329+
1330+
context('when the digit to round is 9', function () {
1331+
it('rounds up and carries correctly', function () {
1332+
const result = Decimal128.fromStringWithRounding(
1333+
'37.4999999999999999196428571428571399'
1334+
);
1335+
expect(result.toString()).to.deep.equal('37.49999999999999991964285714285714');
1336+
});
1337+
});
1338+
});
13091339
});
13101340
});
13111341
});

0 commit comments

Comments
 (0)