From f31ec9a8b82fd9d87ee93eb63a6105b9d0fde8f2 Mon Sep 17 00:00:00 2001 From: Kevin Jahns Date: Sun, 30 Jul 2017 22:16:59 +0200 Subject: [PATCH] fixed varUint encoding issue --- src/Encoding.js | 12 ++++++------ test/encode-decode.js | 13 +++++++++++-- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/src/Encoding.js b/src/Encoding.js index 5faa5afe..f759a421 100644 --- a/src/Encoding.js +++ b/src/Encoding.js @@ -20,28 +20,28 @@ export class BinaryEncoder { this.data[pos] = num & bits8 } writeUint16 (num) { - this.data.push(num & bits8, (num >> 8) & bits8) + this.data.push(num & bits8, (num >>> 8) & bits8) } setUint16 (pos, num) { this.data[pos] = num & bits8 - this.data[pos + 1] = (num >> 8) & bits8 + this.data[pos + 1] = (num >>> 8) & bits8 } writeUint32 (num) { for (let i = 0; i < 4; i++) { this.data.push(num & bits8) - num >>= 8 + num >>>= 8 } } setUint32 (pos, num) { for (let i = 0; i < 4; i++) { this.data[pos + i] = num & bits8 - num >>= 8 + num >>>= 8 } } writeVarUint (num) { while (num >= 0b10000000) { this.data.push(0b10000000 | (bits7 & num)) - num >>= 7 + num >>>= 7 } this.data.push(bits7 & num) } @@ -95,7 +95,7 @@ export class BinaryDecoder { num = num | ((r & bits7) << len) len += 7 if (r < 1 << 7) { - return num + return num >>> 0 // return unsigned number! } if (len > 35) { throw new Error('Integer out of range!') diff --git a/test/encode-decode.js b/test/encode-decode.js index c919e97f..53b1550c 100644 --- a/test/encode-decode.js +++ b/test/encode-decode.js @@ -2,7 +2,6 @@ import { test } from 'cutest' import Chance from 'chance' import Y from '../src/y.js' import { BinaryEncoder, BinaryDecoder } from '../src/Encoding.js' -import { applyRandomTests } from '../../y-array/test/testGeneration.js' function testEncoding (t, write, read, val) { let encoder = new BinaryEncoder() @@ -22,6 +21,7 @@ test('varUint 1 byte', async function varUint1 (t) { test('varUint 2 bytes', async function varUint2 (t) { testEncoding(t, writeVarUint, readVarUint, 1 << 9 | 3) + testEncoding(t, writeVarUint, readVarUint, 1 << 9 | 3) }) test('varUint 3 bytes', async function varUint3 (t) { testEncoding(t, writeVarUint, readVarUint, 1 << 17 | 1 << 9 | 3) @@ -31,11 +31,20 @@ test('varUint 4 bytes', async function varUint4 (t) { testEncoding(t, writeVarUint, readVarUint, 1 << 25 | 1 << 17 | 1 << 9 | 3) }) +test('varUint of 2839012934', async function varUint2839012934 (t) { + testEncoding(t, writeVarUint, readVarUint, 2839012934) +}) + test('varUint random', async function varUintRandom (t) { - const chance = new Chance(t.getSeed() * 1000000000) + const chance = new Chance(t.getSeed() * Math.pow(Number.MAX_SAFE_INTEGER)) testEncoding(t, writeVarUint, readVarUint, chance.integer({min: 0, max: (1 << 28) - 1})) }) +test('varUint random user id', async function varUintRandomUserId (t) { + t.getSeed() // enforces that this test is repeated + testEncoding(t, writeVarUint, readVarUint, Y.utils.generateUserId()) +}) + const writeVarString = (encoder, val) => encoder.writeVarString(val) const readVarString = decoder => decoder.readVarString()