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 3 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
28 changes: 26 additions & 2 deletions test/node/int_32_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ describe('Int32', function () {
const hexValue = 0x2a;
const octalValue = 0o52;
const value = 42;
const upperBoundValue = 0x7fffffff;
const lowerBoundValue = -0x80000000;
const outOfUpperBoundValue = 0x10000000000;
const outOfLowerBoundValue = -0x10000000000;

it('Primitive number', function (done) {
expect(new Int32(value).valueOf()).to.equal(value);
Expand All @@ -35,6 +39,26 @@ describe('Int32', function () {
done();
});

it('Lower bound', function (done) {
expect(new Int32(lowerBoundValue).valueOf()).to.equal(lowerBoundValue);
done();
});

it('Upper bound', function (done) {
expect(new Int32(upperBoundValue).valueOf()).to.equal(upperBoundValue);
done();
});

it('Outside lower bound', function (done) {
expect(new Int32(outOfLowerBoundValue).valueOf()).to.equal(0);
done();
});

it('Outside upper bound', function (done) {
expect(new Int32(outOfUpperBoundValue).valueOf()).to.equal(0);
done();
});

it('should equal zero', function () {
const prop = 'key';
const zero = BSON.serialize({ [prop]: new Int32(0) }).toString();
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