Skip to content

Commit

Permalink
(decoding) test coverage - reached 100% coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
dmonad committed Mar 10, 2019
1 parent b8b5187 commit 85c134a
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
8 changes: 5 additions & 3 deletions decoding.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,12 @@ export const peekUint16 = decoder =>
* @param {Decoder} decoder
* @return {number} An unsigned integer.
*/
export const peekUint32 = decoder =>
(decoder.arr[decoder.pos] +
export const peekUint32 = decoder => (
decoder.arr[decoder.pos] +
(decoder.arr[decoder.pos + 1] << 8) +
(decoder.arr[decoder.pos + 2] << 16) +
(decoder.arr[decoder.pos + 3] << 24)) >>> 0
(decoder.arr[decoder.pos + 3] << 24)
) >>> 0

/**
* Read unsigned integer (32bit) with variable length.
Expand All @@ -180,6 +181,7 @@ export const readVarUint = decoder => {
if (r < 1 << 7) {
return num >>> 0 // return unsigned number!
}
/* istanbul ignore if */
if (len > 35) {
throw new Error('Integer out of range!')
}
Expand Down
40 changes: 37 additions & 3 deletions encoding.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,13 @@ function test (testname, write, read, val, doLog = true) {
}

const testVarString = (s) => {
let encoder = encoding.createEncoder()
const encoder = encoding.createEncoder()
encoding.writeVarString(encoder, s)
let reader = decoding.createDecoder(encoding.toBuffer(encoder))
let result = decoding.readVarString(reader)
const decoder = decoding.createDecoder(encoding.toBuffer(encoder))
const peeked = decoding.peekVarString(decoder)
const result = decoding.readVarString(decoder)
t.compareStrings(s, result)
t.compareStrings(s, peeked)
}

/**
Expand All @@ -87,6 +89,11 @@ export const testRepeatVarUintEncoding = tc => {
test(`varUint of ${n}`, encoding.writeVarUint, decoding.readVarUint, n, false)
}

export const testRepeatPeekVarUintEncoding = tc => {
const n = prng.int31(tc.prng, 0, (1 << 28) - 1)
test(`varUint of ${n}`, encoding.writeVarUint, decoding.peekVarUint, n, false)
}

export const testStringEncoding = tc => {
testVarString('hello')
testVarString('test!')
Expand Down Expand Up @@ -185,6 +192,20 @@ export const testSetOnOverflow = tc => {
t.assert(buffer.createUint8ArrayFromArrayBuffer(buf2)[initialLen + 1] === 7)
}

export const testCloneDecoder = tc => {
const encoder = encoding.createEncoder()
encoding.writeUint8(encoder, 12132)
encoding.writeVarUint(encoder, 329840128734)
encoding.writeVarString(encoder, 'dtrnuiaednudiaendturinaedt nduiaen dturinaed ')
const buf = encoding.toBuffer(encoder)
const decoder = decoding.createDecoder(buf)
decoding.skip8(decoder)
const decoder2 = decoding.clone(decoder)
const payload1 = decoding.readTail(decoder)
const payload2 = decoding.readTail(decoder2)
t.compare(payload1, payload2)
}

export const testWriteBinaryEncoder = tc => {
const encoder = encoding.createEncoder()
encoding.writeUint16(encoder, 4)
Expand All @@ -196,3 +217,16 @@ export const testWriteBinaryEncoder = tc => {
t.assert(decoding.readVarUint(decoder) === 143095)
t.assert(decoding.readUint16(decoder) === 4)
}

export const testOverflowStringDecoding = tc => {
const gen = tc.prng
const encoder = encoding.createEncoder()
let longStr = ''
while (longStr.length < 11000) {
longStr += prng.utf16String(gen, 100000)
}
encoding.writeVarString(encoder, longStr)
const buf = encoding.toBuffer(encoder)
const decoder = decoding.createDecoder(buf)
t.assert(longStr === decoding.readVarString(decoder))
}

0 comments on commit 85c134a

Please sign in to comment.