Skip to content

Commit

Permalink
fix(NODE-3640): Fix Int32 constructor to coerce its argument to int32 (
Browse files Browse the repository at this point in the history
  • Loading branch information
gjchong25 committed Oct 25, 2021
1 parent 6894bae commit d388f1e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/int_32.ts
Expand Up @@ -25,7 +25,7 @@ export class Int32 {
value = value.valueOf();
}

this.value = +value;
this.value = +value | 0;
}

/**
Expand Down
40 changes: 32 additions & 8 deletions test/node/int_32_tests.js
Expand Up @@ -9,32 +9,56 @@ describe('Int32', function () {
const hexValue = 0x2a;
const octalValue = 0o52;
const value = 42;
const upperBoundValue = 0x7fffffff;
const lowerBoundValue = -0x80000000;
const outOfUpperBoundValue = 0x80000000;
const outOfLowerBoundValue = -0x80000001;

it('Primitive number', function (done) {
it('should accept primitive numbers', function (done) {
expect(new Int32(value).valueOf()).to.equal(value);
done();
});

it('Number object', function (done) {
it('should accept number objects', function (done) {
expect(new Int32(new Number(value)).valueOf()).to.equal(value);
done();
});

it('String Hex', function (done) {
it('should accept string Hex', function (done) {
expect(new Int32(strHexValue).valueOf()).to.equal(value);
done();
});

it('Hex', function (done) {
it('should accept hex', function (done) {
expect(new Int32(hexValue).valueOf()).to.equal(value);
done();
});

it('Octal', function (done) {
it('should accept octal', function (done) {
expect(new Int32(octalValue).valueOf()).to.equal(value);
done();
});

it('should accept int32 minimum input of -0x80000000', function (done) {
expect(new Int32(lowerBoundValue).valueOf()).to.equal(lowerBoundValue);
done();
});

it('should accept int32 maximum input of 0x7fffffff', function (done) {
expect(new Int32(upperBoundValue).valueOf()).to.equal(upperBoundValue);
done();
});

it('should truncate the input bits to int32 for inputs smaller than -0x80000000', function (done) {
expect(new Int32(outOfLowerBoundValue).valueOf()).to.equal(0x7fffffff);
done();
});

it('should truncate the input bits to int32 for inputs larger than 0x7fffffff', function (done) {
expect(new Int32(outOfUpperBoundValue).valueOf()).to.equal(-0x80000000);
done();
});

it('should equal zero', function () {
const prop = 'key';
const zero = BSON.serialize({ [prop]: new Int32(0) }).toString();
Expand All @@ -45,7 +69,7 @@ describe('Int32', function () {
});
});

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

describe('toString', () => {
it('should serialize to a string', () => {
const testNumber = Math.floor(Math.random() * 0xffffffff);
const testNumber = 0x7fffffff;
const int32 = new Int32(testNumber);
expect(int32.toString()).to.equal(testNumber.toString());
});
Expand All @@ -67,7 +91,7 @@ describe('Int32', function () {

for (const radix of testRadices) {
it(`should support radix argument: ${radix}`, () => {
const testNumber = Math.floor(Math.random() * 0xffffffff);
const testNumber = 0x7fffffff;
const int32 = new Int32(testNumber);
expect(int32.toString(radix)).to.equal(testNumber.toString(radix));
});
Expand Down

0 comments on commit d388f1e

Please sign in to comment.