From b35ca8cd4a655b628e6be95570918017bc8133bc Mon Sep 17 00:00:00 2001 From: Grace Chong Date: Fri, 22 Oct 2021 13:23:43 -0400 Subject: [PATCH 1/5] (NODE-3640) Fix Int32 constructor to coerce its argument to int32 --- src/int_32.ts | 2 +- test/node/int_32_tests.js | 28 ++++++++++++++++++++++++++-- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/int_32.ts b/src/int_32.ts index 4e58a012..d64f444f 100644 --- a/src/int_32.ts +++ b/src/int_32.ts @@ -25,7 +25,7 @@ export class Int32 { value = value.valueOf(); } - this.value = +value; + this.value = +value | 0; } /** diff --git a/test/node/int_32_tests.js b/test/node/int_32_tests.js index 1d50d354..951ec799 100644 --- a/test/node/int_32_tests.js +++ b/test/node/int_32_tests.js @@ -9,6 +9,10 @@ describe('Int32', function () { const hexValue = 0x2a; const octalValue = 0o52; const value = 42; + const upperBoundValue = 2 ** 31 - 1; + const lowerBoundValue = -(2 ** 31); + const outOfUpperBoundValue = 2 ** 40; + const outOfLowerBoundValue = -(2 ** 40); it('Primitive number', function (done) { expect(new Int32(value).valueOf()).to.equal(value); @@ -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('should equal zero', function (done) { + expect(new Int32(outOfLowerBoundValue).valueOf()).to.equal(0); + done(); + }); + + it('should equal zero', 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(); @@ -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()); }); @@ -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)); }); From fb92619414a79a1ba484e3ce75443a0dab7d3210 Mon Sep 17 00:00:00 2001 From: Grace Chong Date: Fri, 22 Oct 2021 13:36:07 -0400 Subject: [PATCH 2/5] (NODE-3640) fix test wording --- test/node/int_32_tests.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/node/int_32_tests.js b/test/node/int_32_tests.js index 951ec799..f0549b39 100644 --- a/test/node/int_32_tests.js +++ b/test/node/int_32_tests.js @@ -49,12 +49,12 @@ describe('Int32', function () { done(); }); - it('should equal zero', function (done) { + it('Outside lower bound', function (done) { expect(new Int32(outOfLowerBoundValue).valueOf()).to.equal(0); done(); }); - it('should equal zero', function (done) { + it('Outside upper bound', function (done) { expect(new Int32(outOfUpperBoundValue).valueOf()).to.equal(0); done(); }); From cc3bf382e8512cf3fe335c494c5cbcf824d2244e Mon Sep 17 00:00:00 2001 From: Grace Chong Date: Fri, 22 Oct 2021 14:05:49 -0400 Subject: [PATCH 3/5] fix(NODE-3640): remove unsupported exponentiation operator from tests --- test/node/int_32_tests.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/node/int_32_tests.js b/test/node/int_32_tests.js index f0549b39..bb8affbd 100644 --- a/test/node/int_32_tests.js +++ b/test/node/int_32_tests.js @@ -9,10 +9,10 @@ describe('Int32', function () { const hexValue = 0x2a; const octalValue = 0o52; const value = 42; - const upperBoundValue = 2 ** 31 - 1; - const lowerBoundValue = -(2 ** 31); - const outOfUpperBoundValue = 2 ** 40; - const outOfLowerBoundValue = -(2 ** 40); + 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); From 5f637798dbd1ba44de41dcd27af5a52507151645 Mon Sep 17 00:00:00 2001 From: Grace Chong Date: Fri, 22 Oct 2021 15:46:05 -0400 Subject: [PATCH 4/5] test: fix test wording for better understanding and change bounds to be narrower --- test/node/int_32_tests.js | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/test/node/int_32_tests.js b/test/node/int_32_tests.js index bb8affbd..24caad7f 100644 --- a/test/node/int_32_tests.js +++ b/test/node/int_32_tests.js @@ -3,7 +3,7 @@ const BSON = require('../register-bson'); const Int32 = BSON.Int32; -describe('Int32', function () { +describe.only('Int32', function () { context('Constructor', function () { const strHexValue = '0x2a'; const hexValue = 0x2a; @@ -11,51 +11,51 @@ describe('Int32', function () { const value = 42; const upperBoundValue = 0x7fffffff; const lowerBoundValue = -0x80000000; - const outOfUpperBoundValue = 0x10000000000; - const outOfLowerBoundValue = -0x10000000000; + 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('Lower bound', function (done) { + it('should accept int32 minimum input of -0x80000000', function (done) { expect(new Int32(lowerBoundValue).valueOf()).to.equal(lowerBoundValue); done(); }); - it('Upper bound', function (done) { + it('should accept int32 maximum input of 0x7fffffff', 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); + it('should truncate the input bits to int32 for too small inputs', function (done) { + expect(new Int32(outOfLowerBoundValue).valueOf()).to.equal(0x7fffffff); done(); }); - it('Outside upper bound', function (done) { - expect(new Int32(outOfUpperBoundValue).valueOf()).to.equal(0); + it('should truncate the input bits to int32 for too large inputs', function (done) { + expect(new Int32(outOfUpperBoundValue).valueOf()).to.equal(-0x80000000); done(); }); @@ -69,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 From ea310106c903f63bab1268978b6070b97a53f99e Mon Sep 17 00:00:00 2001 From: Grace Chong Date: Mon, 25 Oct 2021 10:05:03 -0400 Subject: [PATCH 5/5] test: fix wording and remove .only --- test/node/int_32_tests.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/node/int_32_tests.js b/test/node/int_32_tests.js index 24caad7f..27d7adfc 100644 --- a/test/node/int_32_tests.js +++ b/test/node/int_32_tests.js @@ -3,7 +3,7 @@ const BSON = require('../register-bson'); const Int32 = BSON.Int32; -describe.only('Int32', function () { +describe('Int32', function () { context('Constructor', function () { const strHexValue = '0x2a'; const hexValue = 0x2a; @@ -49,12 +49,12 @@ describe.only('Int32', function () { done(); }); - it('should truncate the input bits to int32 for too small inputs', function (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 too large inputs', function (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(); });