From 13c93b674d460faad634854e5471f0cb645c6438 Mon Sep 17 00:00:00 2001 From: Scott Loughmiller Date: Mon, 9 Sep 2013 13:09:38 -0700 Subject: [PATCH 1/4] add unit tests to reproduce int64 bugs --- package.json | 6 +++ test/binarypack.nodeunit.js | 77 +++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 test/binarypack.nodeunit.js diff --git a/package.json b/package.json index 29869d4..4ddadfc 100644 --- a/package.json +++ b/package.json @@ -14,6 +14,12 @@ }, "dependencies" : { "buffercursor" : ">=0.0.3" + }, + "devDependencies": { + "nodeunit" : ">=0.8.0" + }, + "scripts": { + "test": "nodeunit test/binarypack.nodeunit.js" } } diff --git a/test/binarypack.nodeunit.js b/test/binarypack.nodeunit.js new file mode 100644 index 0000000..12233b4 --- /dev/null +++ b/test/binarypack.nodeunit.js @@ -0,0 +1,77 @@ +/*jslint node: true */ + +"use strict"; + +var binarypack = require('../lib/binarypack.js'); + +function testPackUnpack(test, data) { + console.log('packing: ', data); + var buffer = binarypack.pack(data); + console.log('unpacking'); + test.strictEqual(binarypack.unpack(buffer), data); +} + + +exports.integers = { + testInt32 : function (test) { + + var i, + int32Max = Math.pow(2, 31) - 1; + + for (i=1; i<=int32Max; i*=2) { + + // test positive + testPackUnpack(test, i); + + // test negative + testPackUnpack(test, i * -1); + + // test + 1 + testPackUnpack(test, i + 1); + + // test - 1 + testPackUnpack(test, i - 1); + + // test negative + 1 + testPackUnpack(test, (i + 1) * -1); + + // test negative - 1 + testPackUnpack(test, (i - 1) * -1); + } + + testPackUnpack(test, int32Max); + + testPackUnpack(test, int32Max * -1); + + test.done(); + }, + + testUInt32 : function (test) { + + testPackUnpack(test, Math.pow(2, 31)); + + testPackUnpack(test, Math.pow(2, 32) - 1); + + test.done(); + }, + + testInt64 : function (test) { + testPackUnpack(test, Math.pow(2, 31) * -1); + + testPackUnpack(test, Math.pow(2, 32)); + + testPackUnpack(test, Math.pow(2, 32) * -1); + + testPackUnpack(test, Math.pow(2, 48)); + + testPackUnpack(test, Math.pow(2, 48) * -1); + + // largest integer available in javascript + testPackUnpack(test, Math.pow(2, 53)); + + testPackUnpack(test, Math.pow(2, 53) * -1); + + test.done(); + } + +}; From 5a369c97a0e589481e89ed6ab910cfeabcacecc2 Mon Sep 17 00:00:00 2001 From: Scott Loughmiller Date: Mon, 9 Sep 2013 13:15:50 -0700 Subject: [PATCH 2/4] readUInt64 - refence buffer array inside object returned from slice --- lib/binarypack.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/binarypack.js b/lib/binarypack.js index 208545b..a858988 100644 --- a/lib/binarypack.js +++ b/lib/binarypack.js @@ -102,14 +102,14 @@ BinaryPack.Unpacker.prototype.unpack = function(){ } BinaryPack.Unpacker.prototype.readUInt64 = function(){ var bytes = this.cursor.slice(8); - return ((((((bytes[0] * 256 + - bytes[1]) * 256 + - bytes[2]) * 256 + - bytes[3]) * 256 + - bytes[4]) * 256 + - bytes[5]) * 256 + - bytes[6]) * 256 + - bytes[7]; + return ((((((bytes.buffer[0] * 256 + + bytes.buffer[1]) * 256 + + bytes.buffer[2]) * 256 + + bytes.buffer[3]) * 256 + + bytes.buffer[4]) * 256 + + bytes.buffer[5]) * 256 + + bytes.buffer[6]) * 256 + + bytes.buffer[7]; } BinaryPack.Unpacker.prototype.readInt64 = function(){ From 8ca8fe7271b3361556e0ca48a198b2db07e0d7c1 Mon Sep 17 00:00:00 2001 From: Scott Loughmiller Date: Mon, 9 Sep 2013 13:17:24 -0700 Subject: [PATCH 3/4] readInt64 should call readUInt64, not itself recursively --- lib/binarypack.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/binarypack.js b/lib/binarypack.js index a858988..1772d48 100644 --- a/lib/binarypack.js +++ b/lib/binarypack.js @@ -113,7 +113,7 @@ BinaryPack.Unpacker.prototype.readUInt64 = function(){ } BinaryPack.Unpacker.prototype.readInt64 = function(){ - var uint64 = this.readInt64(); + var uint64 = this.readUInt64(); return (uint64 < Math.pow(2, 63) ) ? uint64 : uint64 - Math.pow(2, 64); } From 03ccc029e13fa9942f4ece46e1f72ba50a01ba1e Mon Sep 17 00:00:00 2001 From: Scott Loughmiller Date: Tue, 14 Jan 2014 16:11:03 -0800 Subject: [PATCH 4/4] use patched buffercursor --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 4ddadfc..b502714 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,7 @@ "node": ">=0.6.0" }, "dependencies" : { - "buffercursor" : ">=0.0.3" + "buffercursor" : "git+https://github.com/loughmiller/node-buffercursor.git#master" }, "devDependencies": { "nodeunit" : ">=0.8.0"