Skip to content

Commit ffc5137

Browse files
sync variable-length-quantity (#563)
1 parent 72913ca commit ffc5137

File tree

4 files changed

+205
-90
lines changed

4 files changed

+205
-90
lines changed

exercises/practice/variable-length-quantity/.meta/example.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ local function decode(bytes)
88
table.insert(values, n)
99
n = 0
1010
end
11-
assert((i ~= #bytes) or (byte & 0x80 == 0), 'incomplete byte sequence')
11+
assert((i ~= #bytes) or (byte & 0x80 == 0), 'incomplete sequence')
1212
end
1313
return values
1414
end
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
local map = function(t, f)
2+
local mapped = {}
3+
for i, v in ipairs(t) do
4+
mapped[i] = f(v)
5+
end
6+
return mapped
7+
end
8+
9+
local function render_integers(integers)
10+
return table.concat(map(integers, function(integer)
11+
return string.format("0x%x", integer)
12+
end), ', ')
13+
end
14+
15+
return {
16+
module_name = 'vlq',
17+
18+
generate_test = function(case)
19+
if case.expected.error then
20+
local template = [[
21+
assert.has_error(function()
22+
vlq.%s({ %s })
23+
end)]]
24+
return template:format(case.property, render_integers(case.input.integers))
25+
else
26+
local template = [[
27+
assert.are.same({ %s }, vlq.%s({ %s }))]]
28+
return template:format(render_integers(case.expected), case.property, render_integers(case.input.integers))
29+
end
30+
end
31+
}
Lines changed: 51 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,103 @@
1-
# This is an auto-generated file. Regular comments will be removed when this
2-
# file is regenerated. Regenerating will not touch any manually added keys,
3-
# so comments can be added in a "comment" key.
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
411

512
[35c9db2e-f781-4c52-b73b-8e76427defd0]
6-
description = "zero"
13+
description = "Encode a series of integers, producing a series of bytes. -> zero"
714

815
[be44d299-a151-4604-a10e-d4b867f41540]
9-
description = "arbitrary single byte"
16+
description = "Encode a series of integers, producing a series of bytes. -> arbitrary single byte"
17+
18+
[890bc344-cb80-45af-b316-6806a6971e81]
19+
description = "Encode a series of integers, producing a series of bytes. -> asymmetric single byte"
1020

1121
[ea399615-d274-4af6-bbef-a1c23c9e1346]
12-
description = "largest single byte"
22+
description = "Encode a series of integers, producing a series of bytes. -> largest single byte"
1323

1424
[77b07086-bd3f-4882-8476-8dcafee79b1c]
15-
description = "smallest double byte"
25+
description = "Encode a series of integers, producing a series of bytes. -> smallest double byte"
1626

1727
[63955a49-2690-4e22-a556-0040648d6b2d]
18-
description = "arbitrary double byte"
28+
description = "Encode a series of integers, producing a series of bytes. -> arbitrary double byte"
29+
30+
[4977d113-251b-4d10-a3ad-2f5a7756bb58]
31+
description = "Encode a series of integers, producing a series of bytes. -> asymmetric double byte"
1932

2033
[29da7031-0067-43d3-83a7-4f14b29ed97a]
21-
description = "largest double byte"
34+
description = "Encode a series of integers, producing a series of bytes. -> largest double byte"
2235

2336
[3345d2e3-79a9-4999-869e-d4856e3a8e01]
24-
description = "smallest triple byte"
37+
description = "Encode a series of integers, producing a series of bytes. -> smallest triple byte"
2538

2639
[5df0bc2d-2a57-4300-a653-a75ee4bd0bee]
27-
description = "arbitrary triple byte"
40+
description = "Encode a series of integers, producing a series of bytes. -> arbitrary triple byte"
41+
42+
[6731045f-1e00-4192-b5ae-98b22e17e9f7]
43+
description = "Encode a series of integers, producing a series of bytes. -> asymmetric triple byte"
2844

2945
[f51d8539-312d-4db1-945c-250222c6aa22]
30-
description = "largest triple byte"
46+
description = "Encode a series of integers, producing a series of bytes. -> largest triple byte"
3147

3248
[da78228b-544f-47b7-8bfe-d16b35bbe570]
33-
description = "smallest quadruple byte"
49+
description = "Encode a series of integers, producing a series of bytes. -> smallest quadruple byte"
3450

3551
[11ed3469-a933-46f1-996f-2231e05d7bb6]
36-
description = "arbitrary quadruple byte"
52+
description = "Encode a series of integers, producing a series of bytes. -> arbitrary quadruple byte"
53+
54+
[b45ef770-cbba-48c2-bd3c-c6362679516e]
55+
description = "Encode a series of integers, producing a series of bytes. -> asymmetric quadruple byte"
3756

3857
[d5f3f3c3-e0f1-4e7f-aad0-18a44f223d1c]
39-
description = "largest quadruple byte"
58+
description = "Encode a series of integers, producing a series of bytes. -> largest quadruple byte"
4059

4160
[91a18b33-24e7-4bfb-bbca-eca78ff4fc47]
42-
description = "smallest quintuple byte"
61+
description = "Encode a series of integers, producing a series of bytes. -> smallest quintuple byte"
4362

4463
[5f34ff12-2952-4669-95fe-2d11b693d331]
45-
description = "arbitrary quintuple byte"
64+
description = "Encode a series of integers, producing a series of bytes. -> arbitrary quintuple byte"
65+
66+
[9be46731-7cd5-415c-b960-48061cbc1154]
67+
description = "Encode a series of integers, producing a series of bytes. -> asymmetric quintuple byte"
4668

4769
[7489694b-88c3-4078-9864-6fe802411009]
48-
description = "maximum 32-bit integer input"
70+
description = "Encode a series of integers, producing a series of bytes. -> maximum 32-bit integer input"
4971

5072
[f9b91821-cada-4a73-9421-3c81d6ff3661]
51-
description = "two single-byte values"
73+
description = "Encode a series of integers, producing a series of bytes. -> two single-byte values"
5274

5375
[68694449-25d2-4974-ba75-fa7bb36db212]
54-
description = "two multi-byte values"
76+
description = "Encode a series of integers, producing a series of bytes. -> two multi-byte values"
5577

5678
[51a06b5c-de1b-4487-9a50-9db1b8930d85]
57-
description = "many multi-byte values"
79+
description = "Encode a series of integers, producing a series of bytes. -> many multi-byte values"
5880

5981
[baa73993-4514-4915-bac0-f7f585e0e59a]
60-
description = "one byte"
82+
description = "Decode a series of bytes, producing a series of integers. -> one byte"
6183

6284
[72e94369-29f9-46f2-8c95-6c5b7a595aee]
63-
description = "two bytes"
85+
description = "Decode a series of bytes, producing a series of integers. -> two bytes"
6486

6587
[df5a44c4-56f7-464e-a997-1db5f63ce691]
66-
description = "three bytes"
88+
description = "Decode a series of bytes, producing a series of integers. -> three bytes"
6789

6890
[1bb58684-f2dc-450a-8406-1f3452aa1947]
69-
description = "four bytes"
91+
description = "Decode a series of bytes, producing a series of integers. -> four bytes"
7092

7193
[cecd5233-49f1-4dd1-a41a-9840a40f09cd]
72-
description = "maximum 32-bit integer"
94+
description = "Decode a series of bytes, producing a series of integers. -> maximum 32-bit integer"
7395

7496
[e7d74ba3-8b8e-4bcb-858d-d08302e15695]
75-
description = "incomplete sequence causes error"
97+
description = "Decode a series of bytes, producing a series of integers. -> incomplete sequence causes error"
7698

7799
[aa378291-9043-4724-bc53-aca1b4a3fcb6]
78-
description = "incomplete sequence causes error, even if value is zero"
100+
description = "Decode a series of bytes, producing a series of integers. -> incomplete sequence causes error, even if value is zero"
79101

80102
[a91e6f5a-c64a-48e3-8a75-ce1a81e0ebee]
81-
description = "multiple values"
103+
description = "Decode a series of bytes, producing a series of integers. -> multiple values"

exercises/practice/variable-length-quantity/variable-length-quantity_spec.lua

Lines changed: 122 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,137 @@
1-
local vlq = require 'variable-length-quantity'
1+
local vlq = require('variable-length-quantity')
22

3-
-- LuaFormatter off
43
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)
108

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)
1612

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)
2216

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)
2820

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)
3524

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)
4128

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)
4732

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)
5336

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)
5964

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)
6797
end)
6898

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)
73136
end)
74137
end)
75-
-- LuaFormatter on

0 commit comments

Comments
 (0)