|
1 |
| -local vlq = require 'variable-length-quantity' |
| 1 | +local vlq = require('variable-length-quantity') |
2 | 2 |
|
3 |
| --- LuaFormatter off |
4 | 3 | describe('variable-length-quantity', function()
|
5 |
| - it('should decode single bytes', function() |
6 |
| - assert.are.same({ 0x00 }, vlq.decode({ 0x00 })) |
7 |
| - assert.are.same({ 0x40 }, vlq.decode({ 0x40 })) |
8 |
| - assert.are.same({ 0x7f }, vlq.decode({ 0x7f })) |
9 |
| - end) |
| 4 | + describe('encode a series of integers, producing a series of bytes.', function() |
| 5 | + it('zero', function() |
| 6 | + assert.are.same({ 0x0 }, vlq.encode({ 0x0 })) |
| 7 | + end) |
10 | 8 |
|
11 |
| - it('should decode double bytes', function() |
12 |
| - assert.are.same({ 0x80 }, vlq.decode({ 0x81, 0x00 })) |
13 |
| - assert.are.same({ 0x2000 }, vlq.decode({ 0xc0, 0x00 })) |
14 |
| - assert.are.same({ 0x3fff }, vlq.decode({ 0xff, 0x7f })) |
15 |
| - end) |
| 9 | + it('arbitrary single byte', function() |
| 10 | + assert.are.same({ 0x40 }, vlq.encode({ 0x40 })) |
| 11 | + end) |
16 | 12 |
|
17 |
| - it('should decode triple bytes', function() |
18 |
| - assert.are.same({ 0x4000 }, vlq.decode({ 0x81, 0x80, 0x00 })) |
19 |
| - assert.are.same({ 0x100000 }, vlq.decode({ 0xc0, 0x80, 0x00 })) |
20 |
| - assert.are.same({ 0x1fffff }, vlq.decode({ 0xff, 0xff, 0x7f })) |
21 |
| - end) |
| 13 | + it('asymmetric single byte', function() |
| 14 | + assert.are.same({ 0x53 }, vlq.encode({ 0x53 })) |
| 15 | + end) |
22 | 16 |
|
23 |
| - it('should decode quadruple bytes', function() |
24 |
| - assert.are.same({ 0x200000 }, vlq.decode({ 0x81, 0x80, 0x80, 0x00 })) |
25 |
| - assert.are.same({ 0x08000000 }, vlq.decode({ 0xc0, 0x80, 0x80, 0x00 })) |
26 |
| - assert.are.same({ 0x0fffffff }, vlq.decode({ 0xff, 0xff, 0xff, 0x7f })) |
27 |
| - end) |
| 17 | + it('largest single byte', function() |
| 18 | + assert.are.same({ 0x7f }, vlq.encode({ 0x7f })) |
| 19 | + end) |
28 | 20 |
|
29 |
| - it('should decode multiple values', function() |
30 |
| - assert.are.same( |
31 |
| - { 0x2000, 0x123456, 0x0fffffff, 0x00, 0x3fff, 0x4000 }, |
32 |
| - vlq.decode({ 0xc0, 0x00, 0xc8, 0xe8, 0x56, 0xff, 0xff, 0xff, 0x7f, 0x00, 0xff, 0x7f, 0x81, 0x80, 0x00 }) |
33 |
| - ) |
34 |
| - end) |
| 21 | + it('smallest double byte', function() |
| 22 | + assert.are.same({ 0x81, 0x0 }, vlq.encode({ 0x80 })) |
| 23 | + end) |
35 | 24 |
|
36 |
| - it('should encode single bytes', function() |
37 |
| - assert.are.same({ 0x00 }, vlq.encode({ 0x00 })) |
38 |
| - assert.are.same({ 0x40 }, vlq.encode({ 0x40 })) |
39 |
| - assert.are.same({ 0x7f }, vlq.encode({ 0x7f })) |
40 |
| - end) |
| 25 | + it('arbitrary double byte', function() |
| 26 | + assert.are.same({ 0xc0, 0x0 }, vlq.encode({ 0x2000 })) |
| 27 | + end) |
41 | 28 |
|
42 |
| - it('should encode double bytes', function() |
43 |
| - assert.are.same({ 0x81, 0x00 }, vlq.encode({ 0x80 })) |
44 |
| - assert.are.same({ 0xc0, 0x00 }, vlq.encode({ 0x2000 })) |
45 |
| - assert.are.same({ 0xff, 0x7f }, vlq.encode({ 0x3fff })) |
46 |
| - end) |
| 29 | + it('asymmetric double byte', function() |
| 30 | + assert.are.same({ 0x81, 0x2d }, vlq.encode({ 0xad })) |
| 31 | + end) |
47 | 32 |
|
48 |
| - it('should encode triple bytes', function() |
49 |
| - assert.are.same({ 0x81, 0x80, 0x00 }, vlq.encode({ 0x4000 })) |
50 |
| - assert.are.same({ 0xc0, 0x80, 0x00 }, vlq.encode({ 0x100000 })) |
51 |
| - assert.are.same({ 0xff, 0xff, 0x7f }, vlq.encode({ 0x1fffff })) |
52 |
| - end) |
| 33 | + it('largest double byte', function() |
| 34 | + assert.are.same({ 0xff, 0x7f }, vlq.encode({ 0x3fff })) |
| 35 | + end) |
53 | 36 |
|
54 |
| - it('should encode quadruple bytes', function() |
55 |
| - assert.are.same({ 0x81, 0x80, 0x80, 0x00 }, vlq.encode({ 0x200000 })) |
56 |
| - assert.are.same({ 0xc0, 0x80, 0x80, 0x00 }, vlq.encode({ 0x08000000 })) |
57 |
| - assert.are.same({ 0xff, 0xff, 0xff, 0x7f }, vlq.encode({ 0x0fffffff })) |
58 |
| - end) |
| 37 | + it('smallest triple byte', function() |
| 38 | + assert.are.same({ 0x81, 0x80, 0x0 }, vlq.encode({ 0x4000 })) |
| 39 | + end) |
| 40 | + |
| 41 | + it('arbitrary triple byte', function() |
| 42 | + assert.are.same({ 0xc0, 0x80, 0x0 }, vlq.encode({ 0x100000 })) |
| 43 | + end) |
| 44 | + |
| 45 | + it('asymmetric triple byte', function() |
| 46 | + assert.are.same({ 0x87, 0xab, 0x1c }, vlq.encode({ 0x1d59c })) |
| 47 | + end) |
| 48 | + |
| 49 | + it('largest triple byte', function() |
| 50 | + assert.are.same({ 0xff, 0xff, 0x7f }, vlq.encode({ 0x1fffff })) |
| 51 | + end) |
| 52 | + |
| 53 | + it('smallest quadruple byte', function() |
| 54 | + assert.are.same({ 0x81, 0x80, 0x80, 0x0 }, vlq.encode({ 0x200000 })) |
| 55 | + end) |
| 56 | + |
| 57 | + it('arbitrary quadruple byte', function() |
| 58 | + assert.are.same({ 0xc0, 0x80, 0x80, 0x0 }, vlq.encode({ 0x8000000 })) |
| 59 | + end) |
| 60 | + |
| 61 | + it('asymmetric quadruple byte', function() |
| 62 | + assert.are.same({ 0x81, 0xd5, 0xee, 0x4 }, vlq.encode({ 0x357704 })) |
| 63 | + end) |
59 | 64 |
|
60 |
| - it('should encode multiple values', function() |
61 |
| - assert.are.same({ 0x40, 0x7f }, vlq.encode({ 0x40, 0x7f })) |
62 |
| - assert.are.same({ 0x81, 0x80, 0x00, 0xc8, 0xe8, 0x56 }, vlq.encode({ 0x4000, 0x123456 })) |
63 |
| - assert.are.same( |
64 |
| - { 0xc0, 0x00, 0xc8, 0xe8, 0x56, 0xff, 0xff, 0xff, 0x7f, 0x00, 0xff, 0x7f, 0x81, 0x80, 0x00 }, |
65 |
| - vlq.encode({ 0x2000, 0x123456, 0x0fffffff, 0x00, 0x3fff, 0x4000 }) |
66 |
| - ) |
| 65 | + it('largest quadruple byte', function() |
| 66 | + assert.are.same({ 0xff, 0xff, 0xff, 0x7f }, vlq.encode({ 0xfffffff })) |
| 67 | + end) |
| 68 | + |
| 69 | + it('smallest quintuple byte', function() |
| 70 | + assert.are.same({ 0x81, 0x80, 0x80, 0x80, 0x0 }, vlq.encode({ 0x10000000 })) |
| 71 | + end) |
| 72 | + |
| 73 | + it('arbitrary quintuple byte', function() |
| 74 | + assert.are.same({ 0x8f, 0xf8, 0x80, 0x80, 0x0 }, vlq.encode({ 0xff000000 })) |
| 75 | + end) |
| 76 | + |
| 77 | + it('asymmetric quintuple byte', function() |
| 78 | + assert.are.same({ 0x88, 0xb3, 0x95, 0xc2, 0x5 }, vlq.encode({ 0x86656105 })) |
| 79 | + end) |
| 80 | + |
| 81 | + it('maximum 32-bit integer input', function() |
| 82 | + assert.are.same({ 0x8f, 0xff, 0xff, 0xff, 0x7f }, vlq.encode({ 0xffffffff })) |
| 83 | + end) |
| 84 | + |
| 85 | + it('two single-byte values', function() |
| 86 | + assert.are.same({ 0x40, 0x7f }, vlq.encode({ 0x40, 0x7f })) |
| 87 | + end) |
| 88 | + |
| 89 | + it('two multi-byte values', function() |
| 90 | + assert.are.same({ 0x81, 0x80, 0x0, 0xc8, 0xe8, 0x56 }, vlq.encode({ 0x4000, 0x123456 })) |
| 91 | + end) |
| 92 | + |
| 93 | + it('many multi-byte values', function() |
| 94 | + assert.are.same({ 0xc0, 0x0, 0xc8, 0xe8, 0x56, 0xff, 0xff, 0xff, 0x7f, 0x0, 0xff, 0x7f, 0x81, 0x80, 0x0 }, |
| 95 | + vlq.encode({ 0x2000, 0x123456, 0xfffffff, 0x0, 0x3fff, 0x4000 })) |
| 96 | + end) |
67 | 97 | end)
|
68 | 98 |
|
69 |
| - it('should raise an error when decoding an incomplete byte sequence', function() |
70 |
| - assert.has.error(function() |
71 |
| - vlq.decode({ 0x81, 0x00, 0x80 }) |
72 |
| - end, 'incomplete byte sequence') |
| 99 | + describe('decode a series of bytes, producing a series of integers.', function() |
| 100 | + it('one byte', function() |
| 101 | + assert.are.same({ 0x7f }, vlq.decode({ 0x7f })) |
| 102 | + end) |
| 103 | + |
| 104 | + it('two bytes', function() |
| 105 | + assert.are.same({ 0x2000 }, vlq.decode({ 0xc0, 0x0 })) |
| 106 | + end) |
| 107 | + |
| 108 | + it('three bytes', function() |
| 109 | + assert.are.same({ 0x1fffff }, vlq.decode({ 0xff, 0xff, 0x7f })) |
| 110 | + end) |
| 111 | + |
| 112 | + it('four bytes', function() |
| 113 | + assert.are.same({ 0x200000 }, vlq.decode({ 0x81, 0x80, 0x80, 0x0 })) |
| 114 | + end) |
| 115 | + |
| 116 | + it('maximum 32-bit integer', function() |
| 117 | + assert.are.same({ 0xffffffff }, vlq.decode({ 0x8f, 0xff, 0xff, 0xff, 0x7f })) |
| 118 | + end) |
| 119 | + |
| 120 | + it('incomplete sequence causes error', function() |
| 121 | + assert.has_error(function() |
| 122 | + vlq.decode({ 0xff }) |
| 123 | + end) |
| 124 | + end) |
| 125 | + |
| 126 | + it('incomplete sequence causes error, even if value is zero', function() |
| 127 | + assert.has_error(function() |
| 128 | + vlq.decode({ 0x80 }) |
| 129 | + end) |
| 130 | + end) |
| 131 | + |
| 132 | + it('multiple values', function() |
| 133 | + assert.are.same({ 0x2000, 0x123456, 0xfffffff, 0x0, 0x3fff, 0x4000 }, vlq.decode( |
| 134 | + { 0xc0, 0x0, 0xc8, 0xe8, 0x56, 0xff, 0xff, 0xff, 0x7f, 0x0, 0xff, 0x7f, 0x81, 0x80, 0x0 })) |
| 135 | + end) |
73 | 136 | end)
|
74 | 137 | end)
|
75 |
| --- LuaFormatter on |
|
0 commit comments