Skip to content
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

fix(NODE-3640): Fix Int32 constructor to coerce its argument to int32 #466

Merged
merged 5 commits into from
Oct 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/int_32.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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