Skip to content

Commit cc8f526

Browse files
committed
Sync variable-length-quantity with problem spec
1 parent 3c6e571 commit cc8f526

4 files changed

Lines changed: 314 additions & 92 deletions

File tree

exercises/practice/variable-length-quantity/.docs/instructions.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
Implement variable length quantity encoding and decoding.
44

5-
The goal of this exercise is to implement [VLQ](https://en.wikipedia.org/wiki/Variable-length_quantity) encoding/decoding.
5+
The goal of this exercise is to implement [VLQ][vlq] encoding/decoding.
66

77
In short, the goal of this encoding is to encode integer values in a way that would save bytes.
8-
Only the first 7 bits of each byte is significant (right-justified; sort of like an ASCII byte).
8+
Only the first 7 bits of each byte are significant (right-justified; sort of like an ASCII byte).
99
So, if you have a 32-bit value, you have to unpack it into a series of 7-bit bytes.
1010
Of course, you will have a variable number of bytes depending upon your integer.
1111
To indicate which is the last byte of the series, you leave bit #7 clear.
@@ -30,3 +30,5 @@ Here are examples of integers as 32-bit values, and the variable length quantiti
3030
08000000 C0 80 80 00
3131
0FFFFFFF FF FF FF 7F
3232
```
33+
34+
[vlq]: https://en.wikipedia.org/wiki/Variable-length_quantity
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{% for test in cases %}
2+
#[test]
3+
{% if loop.index != 1 -%}
4+
#[ignore]
5+
{% endif -%}
6+
fn {{ test.description | slugify | replace(from="-", to="_") }}() {
7+
{% if test.property == "encode" -%}
8+
let input = &{{ test.input.integers | json_encode() }};
9+
let output = {{ crate_name }}::{{ fn_names[0] }}(input);
10+
let expected = vec![
11+
{%- for byte in test.expected -%}
12+
0x{{ byte | to_hex }},
13+
{%- endfor -%}
14+
];
15+
{%- elif test.property == "decode" -%}
16+
let input = &[
17+
{%- for byte in test.input.integers -%}
18+
0x{{ byte | to_hex }},
19+
{%- endfor -%}
20+
];
21+
let output = {{ crate_name }}::{{ fn_names[1] }}(input);
22+
let expected = {% if test.expected is object -%}
23+
{# apparently there is no test for overflow yet -#}
24+
Err(variable_length_quantity::Error::IncompleteNumber)
25+
{%- else -%}
26+
Ok(vec!{{ test.expected | json_encode() }})
27+
{%- endif %};
28+
{%- else -%}
29+
panic!("unknown property: {{ test.property }}");
30+
{%- endif %}
31+
assert_eq!(output, expected);
32+
}
33+
{% endfor -%}
Lines changed: 88 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,88 @@
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.
11+
12+
[35c9db2e-f781-4c52-b73b-8e76427defd0]
13+
description = "Encode a series of integers, producing a series of bytes. -> zero"
14+
15+
[be44d299-a151-4604-a10e-d4b867f41540]
16+
description = "Encode a series of integers, producing a series of bytes. -> arbitrary single byte"
17+
18+
[ea399615-d274-4af6-bbef-a1c23c9e1346]
19+
description = "Encode a series of integers, producing a series of bytes. -> largest single byte"
20+
21+
[77b07086-bd3f-4882-8476-8dcafee79b1c]
22+
description = "Encode a series of integers, producing a series of bytes. -> smallest double byte"
23+
24+
[63955a49-2690-4e22-a556-0040648d6b2d]
25+
description = "Encode a series of integers, producing a series of bytes. -> arbitrary double byte"
26+
27+
[29da7031-0067-43d3-83a7-4f14b29ed97a]
28+
description = "Encode a series of integers, producing a series of bytes. -> largest double byte"
29+
30+
[3345d2e3-79a9-4999-869e-d4856e3a8e01]
31+
description = "Encode a series of integers, producing a series of bytes. -> smallest triple byte"
32+
33+
[5df0bc2d-2a57-4300-a653-a75ee4bd0bee]
34+
description = "Encode a series of integers, producing a series of bytes. -> arbitrary triple byte"
35+
36+
[f51d8539-312d-4db1-945c-250222c6aa22]
37+
description = "Encode a series of integers, producing a series of bytes. -> largest triple byte"
38+
39+
[da78228b-544f-47b7-8bfe-d16b35bbe570]
40+
description = "Encode a series of integers, producing a series of bytes. -> smallest quadruple byte"
41+
42+
[11ed3469-a933-46f1-996f-2231e05d7bb6]
43+
description = "Encode a series of integers, producing a series of bytes. -> arbitrary quadruple byte"
44+
45+
[d5f3f3c3-e0f1-4e7f-aad0-18a44f223d1c]
46+
description = "Encode a series of integers, producing a series of bytes. -> largest quadruple byte"
47+
48+
[91a18b33-24e7-4bfb-bbca-eca78ff4fc47]
49+
description = "Encode a series of integers, producing a series of bytes. -> smallest quintuple byte"
50+
51+
[5f34ff12-2952-4669-95fe-2d11b693d331]
52+
description = "Encode a series of integers, producing a series of bytes. -> arbitrary quintuple byte"
53+
54+
[7489694b-88c3-4078-9864-6fe802411009]
55+
description = "Encode a series of integers, producing a series of bytes. -> maximum 32-bit integer input"
56+
57+
[f9b91821-cada-4a73-9421-3c81d6ff3661]
58+
description = "Encode a series of integers, producing a series of bytes. -> two single-byte values"
59+
60+
[68694449-25d2-4974-ba75-fa7bb36db212]
61+
description = "Encode a series of integers, producing a series of bytes. -> two multi-byte values"
62+
63+
[51a06b5c-de1b-4487-9a50-9db1b8930d85]
64+
description = "Encode a series of integers, producing a series of bytes. -> many multi-byte values"
65+
66+
[baa73993-4514-4915-bac0-f7f585e0e59a]
67+
description = "Decode a series of bytes, producing a series of integers. -> one byte"
68+
69+
[72e94369-29f9-46f2-8c95-6c5b7a595aee]
70+
description = "Decode a series of bytes, producing a series of integers. -> two bytes"
71+
72+
[df5a44c4-56f7-464e-a997-1db5f63ce691]
73+
description = "Decode a series of bytes, producing a series of integers. -> three bytes"
74+
75+
[1bb58684-f2dc-450a-8406-1f3452aa1947]
76+
description = "Decode a series of bytes, producing a series of integers. -> four bytes"
77+
78+
[cecd5233-49f1-4dd1-a41a-9840a40f09cd]
79+
description = "Decode a series of bytes, producing a series of integers. -> maximum 32-bit integer"
80+
81+
[e7d74ba3-8b8e-4bcb-858d-d08302e15695]
82+
description = "Decode a series of bytes, producing a series of integers. -> incomplete sequence causes error"
83+
84+
[aa378291-9043-4724-bc53-aca1b4a3fcb6]
85+
description = "Decode a series of bytes, producing a series of integers. -> incomplete sequence causes error, even if value is zero"
86+
87+
[a91e6f5a-c64a-48e3-8a75-ce1a81e0ebee]
88+
description = "Decode a series of bytes, producing a series of integers. -> multiple values"

0 commit comments

Comments
 (0)