Skip to content

Commit d388f1e

Browse files
authored
fix(NODE-3640): Fix Int32 constructor to coerce its argument to int32 (#466)
1 parent 6894bae commit d388f1e

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

src/int_32.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export class Int32 {
2525
value = value.valueOf();
2626
}
2727

28-
this.value = +value;
28+
this.value = +value | 0;
2929
}
3030

3131
/**

test/node/int_32_tests.js

+32-8
Original file line numberDiff line numberDiff line change
@@ -9,32 +9,56 @@ describe('Int32', function () {
99
const hexValue = 0x2a;
1010
const octalValue = 0o52;
1111
const value = 42;
12+
const upperBoundValue = 0x7fffffff;
13+
const lowerBoundValue = -0x80000000;
14+
const outOfUpperBoundValue = 0x80000000;
15+
const outOfLowerBoundValue = -0x80000001;
1216

13-
it('Primitive number', function (done) {
17+
it('should accept primitive numbers', function (done) {
1418
expect(new Int32(value).valueOf()).to.equal(value);
1519
done();
1620
});
1721

18-
it('Number object', function (done) {
22+
it('should accept number objects', function (done) {
1923
expect(new Int32(new Number(value)).valueOf()).to.equal(value);
2024
done();
2125
});
2226

23-
it('String Hex', function (done) {
27+
it('should accept string Hex', function (done) {
2428
expect(new Int32(strHexValue).valueOf()).to.equal(value);
2529
done();
2630
});
2731

28-
it('Hex', function (done) {
32+
it('should accept hex', function (done) {
2933
expect(new Int32(hexValue).valueOf()).to.equal(value);
3034
done();
3135
});
3236

33-
it('Octal', function (done) {
37+
it('should accept octal', function (done) {
3438
expect(new Int32(octalValue).valueOf()).to.equal(value);
3539
done();
3640
});
3741

42+
it('should accept int32 minimum input of -0x80000000', function (done) {
43+
expect(new Int32(lowerBoundValue).valueOf()).to.equal(lowerBoundValue);
44+
done();
45+
});
46+
47+
it('should accept int32 maximum input of 0x7fffffff', function (done) {
48+
expect(new Int32(upperBoundValue).valueOf()).to.equal(upperBoundValue);
49+
done();
50+
});
51+
52+
it('should truncate the input bits to int32 for inputs smaller than -0x80000000', function (done) {
53+
expect(new Int32(outOfLowerBoundValue).valueOf()).to.equal(0x7fffffff);
54+
done();
55+
});
56+
57+
it('should truncate the input bits to int32 for inputs larger than 0x7fffffff', function (done) {
58+
expect(new Int32(outOfUpperBoundValue).valueOf()).to.equal(-0x80000000);
59+
done();
60+
});
61+
3862
it('should equal zero', function () {
3963
const prop = 'key';
4064
const zero = BSON.serialize({ [prop]: new Int32(0) }).toString();
@@ -45,7 +69,7 @@ describe('Int32', function () {
4569
});
4670
});
4771

48-
it('should equal fortyTwo', function () {
72+
it('should have serialization consistency across different representations of 42', function () {
4973
const prop = 'key';
5074
const fortyTwo = BSON.serialize({ [prop]: new Int32(value) }).toString();
5175
// should equal fortyTwo
@@ -58,7 +82,7 @@ describe('Int32', function () {
5882

5983
describe('toString', () => {
6084
it('should serialize to a string', () => {
61-
const testNumber = Math.floor(Math.random() * 0xffffffff);
85+
const testNumber = 0x7fffffff;
6286
const int32 = new Int32(testNumber);
6387
expect(int32.toString()).to.equal(testNumber.toString());
6488
});
@@ -67,7 +91,7 @@ describe('Int32', function () {
6791

6892
for (const radix of testRadices) {
6993
it(`should support radix argument: ${radix}`, () => {
70-
const testNumber = Math.floor(Math.random() * 0xffffffff);
94+
const testNumber = 0x7fffffff;
7195
const int32 = new Int32(testNumber);
7296
expect(int32.toString(radix)).to.equal(testNumber.toString(radix));
7397
});

0 commit comments

Comments
 (0)