-
Notifications
You must be signed in to change notification settings - Fork 0
/
leb128_test.go
174 lines (143 loc) · 4.36 KB
/
leb128_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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Copyright 2017 Equim. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package leb128
import (
"fmt"
"strings"
"testing"
)
var (
uleb128Spec = map[uint64]string{
0: "00000000",
0x7f: "01111111",
0x80: "00000001 10000000",
0xff: "00000001 11111111",
2333: "00010010 10011101",
0xef17: "00000011 11011110 10010111",
624485: "00100110 10001110 11100101",
0xffff: "00000011 11111111 11111111",
18446744073709551615: "00000001 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111",
}
sleb128Spec = map[int64]string{
-9223372036854775808: "01111111 10000000 10000000 10000000 10000000 10000000 10000000 10000000 10000000 10000000",
-624485: "01011001 11110001 10011011",
^0x40: "01111111 10111111",
^0x3f: "01000000",
-1: "01111111",
0: "00000000",
1: "00000001",
0x3f: "00111111",
0x40: "00000000 11000000",
0xef17: "00000011 11011110 10010111",
9223372036854775807: "00000000 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111",
}
errorSpec = map[string]uint8{
"11111111": 0,
"10000000 10000000": 0,
"01111111 10000000 10000000 10000000 10000000 10000000 10000000 10000000 10000000 10000000 11111111": 0,
}
)
// bytesToBinary encodes b into a base 2 string in MSB to LSB order.
func bytesToBinary(b []byte) string {
l := len(b)
parts := make([]string, l)
for i, v := range b {
parts[l-i-1] = fmt.Sprintf("%08b", v)
}
return strings.Join(parts, " ")
}
// binaryToBytes decodes base 2 string in MSB to LSB order into bytes.
func binaryToBytes(s string) []byte {
parts := strings.Split(s, " ")
l := len(parts)
b := make([]byte, l)
for i, v := range parts {
fmt.Sscanf(v, "%b", &b[l-i-1])
}
return b
}
func TestEncodeUleb128(t *testing.T) {
count := 0
for input, expected := range uleb128Spec {
var b []byte
if count%2 == 0 {
b = AppendUleb128([]byte("magic"), input)[5:]
} else {
b = AppendUleb128(nil, input)
}
if actual := bytesToBinary(b); actual != expected {
t.Fatalf("\nInput: %v\nExpected: %v\n Got: %v\n", input, expected, actual)
}
count++
}
}
func TestEncodeSleb128(t *testing.T) {
count := 0
for input, expected := range sleb128Spec {
var b []byte
if count%2 == 0 {
b = AppendSleb128([]byte("equim"), input)[5:]
} else {
b = AppendSleb128(nil, input)
}
if actual := bytesToBinary(b); actual != expected {
t.Fatalf("\nInput: %v\nExpected: %v\n Got: %v\n", input, expected, actual)
}
count++
}
}
func TestDecodeUleb128(t *testing.T) {
count := 0
for expected, binStr := range uleb128Spec {
input := binaryToBytes(binStr)
var actual uint64
var n uint8
if count%2 == 0 {
actual, n = DecodeUleb128(append(input, []byte("magic")...))
} else {
actual, n = DecodeUleb128(input)
}
if expectedN := len(input); int(n) != expectedN {
t.Fatalf("\nInput: %v\nExpected: n == %v\n Got: n == %v\n", input, expectedN, n)
}
if actual != expected {
t.Fatalf("\nInput: %v\nExpected: %v\n Got: %v\n", input, expected, actual)
}
count++
}
for binStr, expected := range errorSpec {
input := binaryToBytes(binStr)
_, actual := DecodeUleb128(input)
if actual != expected {
t.Fatalf("\nInput: %v\nExpected: %v\n Got: %v\n", input, expected, actual)
}
}
}
func TestDecodeSleb128(t *testing.T) {
count := 0
for expected, binStr := range sleb128Spec {
input := binaryToBytes(binStr)
var actual int64
var n uint8
if count%2 == 0 {
actual, n = DecodeSleb128(append(input, []byte("equim")...))
} else {
actual, n = DecodeSleb128(input)
}
if expectedN := len(input); int(n) != expectedN {
t.Fatalf("\nInput: %v\nExpected: n == %v\n Got: n == %v\n", input, expectedN, n)
}
if actual != expected {
t.Fatalf("\nInput: %v\nExpected: %v\n Got: %v\n", input, expected, actual)
}
count++
}
for binStr, expected := range errorSpec {
input := binaryToBytes(binStr)
_, actual := DecodeSleb128(input)
if actual != expected {
t.Fatalf("\nInput: %v\nExpected: %v\n Got: %v\n", input, expected, actual)
}
}
}