|
| 1 | +import { encodeSigned, decodeSigned } from '../../src/utils/leb128'; |
| 2 | + |
| 3 | +/** |
| 4 | + * Examples from the DWARF 5 standard, section 7.6, table 7.8. |
| 5 | + * https://dwarfstd.org/doc/DWARF5.pdf |
| 6 | + */ |
| 7 | +const DWARF5TestCases = [ |
| 8 | + [2n, Buffer.from([2])], |
| 9 | + [-2n, Buffer.from([0x7e])], |
| 10 | + [127n, Buffer.from([127 + 0x80, 0])], |
| 11 | + [-127n, Buffer.from([1 + 0x80, 0x7f])], |
| 12 | + [128n, Buffer.from([0 + 0x80, 1])], |
| 13 | + [-128n, Buffer.from([0 + 0x80, 0x7f])], |
| 14 | + [129n, Buffer.from([1 + 0x80, 1])], |
| 15 | + [-129n, Buffer.from([0x7f + 0x80, 0x7e])], |
| 16 | +]; |
| 17 | + |
| 18 | +test('Encoded values should be decoded as the same values', () => { |
| 19 | + const testCases = Array.from(DWARF5TestCases.map(v => v[0] as bigint)).concat([ |
| 20 | + 0n, |
| 21 | + 1n, |
| 22 | + 27n, |
| 23 | + 100n, |
| 24 | + 1023n, |
| 25 | + 1024n, |
| 26 | + BigInt(Number.MAX_SAFE_INTEGER), |
| 27 | + BigInt(Number.MAX_SAFE_INTEGER) + 2n, |
| 28 | + BigInt(Number.MAX_SAFE_INTEGER) + 1000n, |
| 29 | + 0xcafecafecafe01n, |
| 30 | + 0xcafecafecafecafen, |
| 31 | + 0x0afecafecafecafen, |
| 32 | + ]); |
| 33 | + |
| 34 | + for (const value of testCases) { |
| 35 | + const encodedBuffer = encodeSigned(value); |
| 36 | + const decoded = decodeSigned(encodedBuffer); |
| 37 | + expect(decoded.value).toEqual(value); |
| 38 | + expect(decoded.rest).toHaveLength(0); |
| 39 | + // Negative values should also work |
| 40 | + const encodedNegBuffer = encodeSigned(-value); |
| 41 | + const decodedNeg = decodeSigned(encodedNegBuffer); |
| 42 | + expect(decodedNeg.value).toEqual(-value); |
| 43 | + expect(decodedNeg.rest).toHaveLength(0); |
| 44 | + } |
| 45 | +}); |
| 46 | + |
| 47 | +test('leb128 should work with fullnode docstring examples', () => { |
| 48 | + const tests = [ |
| 49 | + [0, Buffer.from([0x00])], |
| 50 | + [624485, Buffer.from([0xe5, 0x8e, 0x26])], |
| 51 | + [-123456, Buffer.from([0xc0, 0xbb, 0x78])], |
| 52 | + ]; |
| 53 | + |
| 54 | + for (const value of tests) { |
| 55 | + const encoded = encodeSigned(value[0] as number); |
| 56 | + expect(encoded).toEqual(value[1]); |
| 57 | + |
| 58 | + const decoded = decodeSigned(Buffer.concat([value[1] as Buffer, Buffer.from('cafe')])); |
| 59 | + expect(decoded.value).toEqual(BigInt(value[0] as number)); |
| 60 | + expect(decoded.rest).toEqual(Buffer.from('cafe')); |
| 61 | + } |
| 62 | +}); |
| 63 | + |
| 64 | +test('leb128 should work with DWARF 5 examples', () => { |
| 65 | + for (const value of DWARF5TestCases) { |
| 66 | + const encoded = encodeSigned(value[0] as bigint); |
| 67 | + expect(encoded).toEqual(value[1]); |
| 68 | + |
| 69 | + const decoded = decodeSigned(Buffer.concat([value[1] as Buffer, Buffer.from('cafe')])); |
| 70 | + expect(decoded.value).toEqual(BigInt(value[0] as bigint)); |
| 71 | + expect(decoded.rest).toEqual(Buffer.from('cafe')); |
| 72 | + } |
| 73 | +}); |
| 74 | + |
| 75 | +test('leb128 should fail if maxBytes is lower than required', () => { |
| 76 | + for (const value of DWARF5TestCases) { |
| 77 | + const expectedLen = (value[1] as Buffer).length; |
| 78 | + const val = value[0] as bigint; |
| 79 | + // Encode should throw if maxBytes is expectedLen-1 |
| 80 | + expect(() => { |
| 81 | + return encodeSigned(val, expectedLen - 1); |
| 82 | + }).toThrow(); |
| 83 | + // Decode should throw if maxBytes is expectedLen-1 |
| 84 | + const buf = Buffer.concat([value[1] as Buffer, Buffer.from('cafe')]); |
| 85 | + expect(() => { |
| 86 | + return decodeSigned(buf, expectedLen - 1); |
| 87 | + }).toThrow(); |
| 88 | + } |
| 89 | +}); |
0 commit comments