fixed varUint encoding issue

This commit is contained in:
Kevin Jahns 2017-07-30 22:16:59 +02:00
parent 003fa735a0
commit f31ec9a8b8
2 changed files with 17 additions and 8 deletions

View File

@ -20,28 +20,28 @@ export class BinaryEncoder {
this.data[pos] = num & bits8 this.data[pos] = num & bits8
} }
writeUint16 (num) { writeUint16 (num) {
this.data.push(num & bits8, (num >> 8) & bits8) this.data.push(num & bits8, (num >>> 8) & bits8)
} }
setUint16 (pos, num) { setUint16 (pos, num) {
this.data[pos] = num & bits8 this.data[pos] = num & bits8
this.data[pos + 1] = (num >> 8) & bits8 this.data[pos + 1] = (num >>> 8) & bits8
} }
writeUint32 (num) { writeUint32 (num) {
for (let i = 0; i < 4; i++) { for (let i = 0; i < 4; i++) {
this.data.push(num & bits8) this.data.push(num & bits8)
num >>= 8 num >>>= 8
} }
} }
setUint32 (pos, num) { setUint32 (pos, num) {
for (let i = 0; i < 4; i++) { for (let i = 0; i < 4; i++) {
this.data[pos + i] = num & bits8 this.data[pos + i] = num & bits8
num >>= 8 num >>>= 8
} }
} }
writeVarUint (num) { writeVarUint (num) {
while (num >= 0b10000000) { while (num >= 0b10000000) {
this.data.push(0b10000000 | (bits7 & num)) this.data.push(0b10000000 | (bits7 & num))
num >>= 7 num >>>= 7
} }
this.data.push(bits7 & num) this.data.push(bits7 & num)
} }
@ -95,7 +95,7 @@ export class BinaryDecoder {
num = num | ((r & bits7) << len) num = num | ((r & bits7) << len)
len += 7 len += 7
if (r < 1 << 7) { if (r < 1 << 7) {
return num return num >>> 0 // return unsigned number!
} }
if (len > 35) { if (len > 35) {
throw new Error('Integer out of range!') throw new Error('Integer out of range!')

View File

@ -2,7 +2,6 @@ import { test } from 'cutest'
import Chance from 'chance' import Chance from 'chance'
import Y from '../src/y.js' import Y from '../src/y.js'
import { BinaryEncoder, BinaryDecoder } from '../src/Encoding.js' import { BinaryEncoder, BinaryDecoder } from '../src/Encoding.js'
import { applyRandomTests } from '../../y-array/test/testGeneration.js'
function testEncoding (t, write, read, val) { function testEncoding (t, write, read, val) {
let encoder = new BinaryEncoder() let encoder = new BinaryEncoder()
@ -22,6 +21,7 @@ test('varUint 1 byte', async function varUint1 (t) {
test('varUint 2 bytes', async function varUint2 (t) { test('varUint 2 bytes', async function varUint2 (t) {
testEncoding(t, writeVarUint, readVarUint, 1 << 9 | 3) testEncoding(t, writeVarUint, readVarUint, 1 << 9 | 3)
testEncoding(t, writeVarUint, readVarUint, 1 << 9 | 3)
}) })
test('varUint 3 bytes', async function varUint3 (t) { test('varUint 3 bytes', async function varUint3 (t) {
testEncoding(t, writeVarUint, readVarUint, 1 << 17 | 1 << 9 | 3) 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) 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) { 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})) 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 writeVarString = (encoder, val) => encoder.writeVarString(val)
const readVarString = decoder => decoder.readVarString() const readVarString = decoder => decoder.readVarString()