From a8b188fbb2d3ef649f47d86c8d7fe1abcd4928cc Mon Sep 17 00:00:00 2001 From: Dominic Tarr Date: Thu, 8 May 2014 23:24:07 +0200 Subject: [PATCH 1/6] update tape --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 235f29d..1f8542f 100644 --- a/package.json +++ b/package.json @@ -19,6 +19,6 @@ "author": "Chris Dickinson ", "license": "MIT", "devDependencies": { - "tape": "~0.2.2" + "tape": "~2.12.3" } -} +} \ No newline at end of file From e55b25d840d5783d23d47cd61ad8209f0a2c2e8d Mon Sep 17 00:00:00 2001 From: Dominic Tarr Date: Thu, 8 May 2014 23:46:11 +0200 Subject: [PATCH 2/6] add tests for large numbers - up to 2^51, max integer that a js number can represent --- test.js | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/test.js b/test.js index 12f2e8f..81d1dac 100644 --- a/test.js +++ b/test.js @@ -71,6 +71,41 @@ test('encode multiple byte with zero first byte', function(assert) { assert.end() }) +test('big integers', function (assert) { + + var bigs = [] + for(var i = 32; i <= 52; i++) (function (i) { + bigs.push(Math.pow(2, i) - 1) + bigs.push(Math.pow(2, i)) + })(i) + console.log(bigs) + bigs.forEach(function (n) { + var data = encode(n) + console.error(n, '->', data) + assert.equal(decode(data), n) + }) + assert.end() +}) + +test('fuzz test - big', function(assert) { + var expect + , encoded + + var MAX_INTD = Math.pow(2, 51) + var MAX_INT = Math.pow(2, 31) + + for(var i = 0, len = 100; i < len; ++i) { + expect = randint(MAX_INTD - MAX_INT) + MAX_INT + encoded = encode(expect) + var data = decode(encoded) + assert.equal(expect, data, 'fuzz test: ' + expect.toString()) + assert.equal(decode.bytesRead, encoded.length) + } + + assert.end() +}) + + function randint(range) { - return ~~(Math.random() * range) + return Math.floor(Math.random() * range) } From 5855143da8f568edca488a19b0cd2f4d740d277c Mon Sep 17 00:00:00 2001 From: Dominic Tarr Date: Thu, 8 May 2014 23:47:29 +0200 Subject: [PATCH 3/6] use divide when number is too large for bitwise operations --- encode.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/encode.js b/encode.js index 3320033..34a11a6 100644 --- a/encode.js +++ b/encode.js @@ -3,17 +3,22 @@ module.exports = encode var MSB = 0x80 , REST = 0x7F , MSBALL = ~REST + , INT = Math.pow(2, 31) function encode(num, out, offset) { out = out || [] offset = offset || 0 var oldOffset = offset - while(num & MSBALL) { - out[offset++] = (num & 0xFF) | MSB + while(num >= INT) { + out[offset++] = (num & 0xFF) | MSB + num /= 128 + } + while(num >= 0x80) { + out[offset++] = (num & 0xFF) | MSB num >>>= 7 } - out[offset] = num + out[offset] = num | 0 encode.bytesWritten = offset - oldOffset + 1 From 51da5f39f650dfd7aa912e3b39e0b518cc96a896 Mon Sep 17 00:00:00 2001 From: Dominic Tarr Date: Thu, 8 May 2014 23:49:49 +0200 Subject: [PATCH 4/6] use bitwise check instead of >= when number is small --- encode.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/encode.js b/encode.js index 34a11a6..63f5149 100644 --- a/encode.js +++ b/encode.js @@ -14,7 +14,7 @@ function encode(num, out, offset) { out[offset++] = (num & 0xFF) | MSB num /= 128 } - while(num >= 0x80) { + while(num & MSBALL) { out[offset++] = (num & 0xFF) | MSB num >>>= 7 } From 10da3a6d3357a5d3724c908c08cd2a5b695365d2 Mon Sep 17 00:00:00 2001 From: Dominic Tarr Date: Thu, 8 May 2014 23:58:25 +0200 Subject: [PATCH 5/6] test the largest numbers --- test.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/test.js b/test.js index 81d1dac..2aeb653 100644 --- a/test.js +++ b/test.js @@ -74,7 +74,7 @@ test('encode multiple byte with zero first byte', function(assert) { test('big integers', function (assert) { var bigs = [] - for(var i = 32; i <= 52; i++) (function (i) { + for(var i = 32; i <= 53; i++) (function (i) { bigs.push(Math.pow(2, i) - 1) bigs.push(Math.pow(2, i)) })(i) @@ -83,6 +83,7 @@ test('big integers', function (assert) { var data = encode(n) console.error(n, '->', data) assert.equal(decode(data), n) + assert.notEqual(decode(data), n - 1) }) assert.end() }) @@ -91,7 +92,7 @@ test('fuzz test - big', function(assert) { var expect , encoded - var MAX_INTD = Math.pow(2, 51) + var MAX_INTD = Math.pow(2, 55) var MAX_INT = Math.pow(2, 31) for(var i = 0, len = 100; i < len; ++i) { From d51a128ab25387366c8091ba3aae7e4b8f2f4285 Mon Sep 17 00:00:00 2001 From: Dominic Tarr Date: Fri, 9 May 2014 10:34:43 +0200 Subject: [PATCH 6/6] remove console.log --- test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test.js b/test.js index 2aeb653..0769851 100644 --- a/test.js +++ b/test.js @@ -78,7 +78,7 @@ test('big integers', function (assert) { bigs.push(Math.pow(2, i) - 1) bigs.push(Math.pow(2, i)) })(i) - console.log(bigs) + bigs.forEach(function (n) { var data = encode(n) console.error(n, '->', data)