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..27d7adfc 100644 --- a/test/node/int_32_tests.js +++ b/test/node/int_32_tests.js @@ -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(); @@ -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 @@ -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)); });