1
exercism fetch go variable-length-quantity

cases_test.go

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: d6a62f7 variable-length-quantity: Fix canonical-data.json formatting
// Problem Specifications Version: 1.0.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},
	},
}

variable_length_quantity_test.go

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
package variablelengthquantity

import (
	"bytes"
	"reflect"
	"testing"
)

func TestDecodeVarint(t *testing.T) {
	for i, tc := range decodeTestCases {
		o, err := DecodeVarint(tc.input)
		if err != nil {
			var _ error = err
			if tc.output != nil {
				t.Fatalf("FAIL: case %d | %s\nexpected %#v got error: %q\n", i, tc.description, tc.output, err)
			}
		} else if tc.output == nil {
			t.Fatalf("FAIL: case %d | %s\nexpected error, got %#v\n", i, tc.description, o)
		} else if !reflect.DeepEqual(o, tc.output) {
			t.Fatalf("FAIL: case %d | %s\nexpected\t%#v\ngot\t\t%#v\n", i, tc.description, tc.output, o)
		}
		t.Logf("PASS: case %d | %s\n", i, tc.description)
	}
}

func TestEncodeVarint(t *testing.T) {
	for i, tc := range encodeTestCases {
		if encoded := EncodeVarint(tc.input); bytes.Compare(encoded, tc.output) != 0 {
			t.Fatalf("FAIL: case %d | %s\nexpected\t%#v\ngot\t\t%#v\n", i, tc.description, tc.output, encoded)
		}
		t.Logf("PASS: case %d | %s\n", i, tc.description)
	}
}