summaryrefslogtreecommitdiff
path: root/go/variable-length-quantity/cases_test.go
blob: 71d71a6a598a4af7fd9557c3c60fc1a63d746783 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package variablelengthquantity

// Source: exercism/problem-specifications
// Commit: 48dc5e6 variable-length-quantity: Apply new "input" policy
// Problem Specifications Version: 1.1.0

// Encode a series of integers, producing a series of bytes.
var encodeTestCases = []struct {
	description string
	input       []uint32
	output      []byte
}{
	{
		"zero",
		[]uint32{0x0},
		[]byte{0x0},
	},
	{
		"arbitrary single byte",
		[]uint32{0x40},
		[]byte{0x40},
	},
	{
		"largest single byte",
		[]uint32{0x7f},
		[]byte{0x7f},
	},
	{
		"smallest double byte",
		[]uint32{0x80},
		[]byte{0x81, 0x0},
	},
	{
		"arbitrary double byte",
		[]uint32{0x2000},
		[]byte{0xc0, 0x0},
	},
	{
		"largest double byte",
		[]uint32{0x3fff},
		[]byte{0xff, 0x7f},
	},
	{
		"smallest triple byte",
		[]uint32{0x4000},
		[]byte{0x81, 0x80, 0x0},
	},
	{
		"arbitrary triple byte",
		[]uint32{0x100000},
		[]byte{0xc0, 0x80, 0x0},
	},
	{
		"largest triple byte",
		[]uint32{0x1fffff},
		[]byte{0xff, 0xff, 0x7f},
	},
	{
		"smallest quadruple byte",
		[]uint32{0x200000},
		[]byte{0x81, 0x80, 0x80, 0x0},
	},
	{
		"arbitrary quadruple byte",
		[]uint32{0x8000000},
		[]byte{0xc0, 0x80, 0x80, 0x0},
	},
	{
		"largest quadruple byte",
		[]uint32{0xfffffff},
		[]byte{0xff, 0xff, 0xff, 0x7f},
	},
	{
		"smallest quintuple byte",
		[]uint32{0x10000000},
		[]byte{0x81, 0x80, 0x80, 0x80, 0x0},
	},
	{
		"arbitrary quintuple byte",
		[]uint32{0xff000000},
		[]byte{0x8f, 0xf8, 0x80, 0x80, 0x0},
	},
	{
		"maximum 32-bit integer input",
		[]uint32{0xffffffff},
		[]byte{0x8f, 0xff, 0xff, 0xff, 0x7f},
	},
	{
		"two single-byte values",
		[]uint32{0x40, 0x7f},
		[]byte{0x40, 0x7f},
	},
	{
		"two multi-byte values",
		[]uint32{0x4000, 0x123456},
		[]byte{0x81, 0x80, 0x0, 0xc8, 0xe8, 0x56},
	},
	{
		"many multi-byte values",
		[]uint32{0x2000, 0x123456, 0xfffffff, 0x0, 0x3fff, 0x4000},
		[]byte{0xc0, 0x0, 0xc8, 0xe8, 0x56, 0xff, 0xff, 0xff, 0x7f, 0x0, 0xff, 0x7f, 0x81, 0x80, 0x0},
	},
}

// Decode a series of bytes, producing a series of integers.
var decodeTestCases = []struct {
	description string
	input       []byte
	output      []uint32 // nil slice indicates error expected.
}{

	{
		"one byte",
		[]byte{0x7f},
		[]uint32{0x7f},
	},
	{
		"two bytes",
		[]byte{0xc0, 0x0},
		[]uint32{0x2000},
	},
	{
		"three bytes",
		[]byte{0xff, 0xff, 0x7f},
		[]uint32{0x1fffff},
	},
	{
		"four bytes",
		[]byte{0x81, 0x80, 0x80, 0x0},
		[]uint32{0x200000},
	},
	{
		"maximum 32-bit integer",
		[]byte{0x8f, 0xff, 0xff, 0xff, 0x7f},
		[]uint32{0xffffffff},
	},
	{
		"incomplete sequence causes error",
		[]byte{0xff},
		[]uint32(nil),
	},
	{
		"incomplete sequence causes error, even if value is zero",
		[]byte{0x80},
		[]uint32(nil),
	},
	{
		"multiple values",
		[]byte{0xc0, 0x0, 0xc8, 0xe8, 0x56, 0xff, 0xff, 0xff, 0x7f, 0x0, 0xff, 0x7f, 0x81, 0x80, 0x0},
		[]uint32{0x2000, 0x123456, 0xfffffff, 0x0, 0x3fff, 0x4000},
	},
}