-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert_test.go
242 lines (214 loc) · 4.91 KB
/
convert_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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package radix
import (
"reflect"
"testing"
)
func TestConvertEmpty(t *testing.T) {
out, err := Convert(nil, 16, 2)
if out != nil {
t.Error("out must be nil")
}
if err != nil {
t.Error("err must be nil")
}
}
func TestConvertErrors(t *testing.T) {
out, err := Convert(nil, 1, 2)
if out != nil {
t.Error("out must be nil")
}
if err == nil {
t.Error("err must be not nil")
} else if err.Error() != "inrx must be greater than 1" {
t.Error("err must be invalid inrx")
}
out, err = Convert(nil, 2, 1)
if out != nil {
t.Error("out must be nil")
}
if err == nil {
t.Error("err must be not nil")
} else if err.Error() != "outrx must be greater than 1" {
t.Error("err must be invalid outrx")
}
out, err = Convert([]int{1, 2, 3}, 2, 2)
if out != nil {
t.Error("out must be nil")
}
if err == nil {
t.Error("err must be not nil")
} else if err.Error() != "in[1]: 2 must be less than inrx: 2" {
t.Error("err must be digit is greater than inrx")
}
out, err = Convert([]int{1, -1, 3}, 2, 2)
if out != nil {
t.Error("out must be nil")
}
if err == nil {
t.Error("err must be not nil")
} else if err.Error() != "in[1]: -1 must be greater or equal 0" {
t.Error("err must be digit is negative")
}
}
func TestConvertBytesEmpty(t *testing.T) {
out, err := ConvertBytes(nil, 58)
if out != nil {
t.Error("out must be nil")
}
if err != nil {
t.Error("err must be nil")
}
}
func TestConvertBytesErrors(t *testing.T) {
out, err := ConvertBytes(nil, 1)
if out != nil {
t.Error("out must be nil")
}
if err == nil {
t.Error("err must be not nil")
} else if err.Error() != "outrx must be greater than 1" {
t.Error("err must be invalid outrx")
}
out, err = ConvertBytes(nil, 257)
if out != nil {
t.Error("out must be nil")
}
if err == nil {
t.Error("err must be not nil")
} else if err.Error() != "outrx must be less than 256" {
t.Error("err must be invalid outrx")
}
}
func TestConvertBytesVsConvert(t *testing.T) {
bin := generateBytes(64, 256)
iin := generateInts(64, 256)
rxs := []int{2, 8, 10, 11, 29, 58, 256}
for _, rx := range rxs {
bout, err := ConvertBytes(bin, rx)
if err != nil {
t.Fatal(err)
}
iout, err := Convert(iin, 256, rx)
if err != nil {
t.Fatal(err)
}
if !cmpBytesVsInts(bout, iout) {
t.Errorf("failure for radix: %v", rx)
}
}
}
func cmpBytesVsInts(bytes []byte, ints []int) bool {
if len(bytes) != len(ints) {
return false
}
for i := 0; i < len(bytes); i++ {
if int(bytes[i]) != ints[i] {
return false
}
}
return true
}
func BenchmarkConvertExtraSmall(b *testing.B) {
input := generateInts(1, 256)
b.ResetTimer()
for i := 0; i < b.N; i++ {
Convert(input, 256, 58)
}
}
func BenchmarkConvertBytesExtraSmall(b *testing.B) {
input := generateBytes(1, 256)
b.ResetTimer()
for i := 0; i < b.N; i++ {
ConvertBytes(input, 58)
}
}
func BenchmarkConvertSmall(b *testing.B) {
input := generateInts(20, 256)
b.ResetTimer()
for i := 0; i < b.N; i++ {
Convert(input, 256, 58)
}
}
func BenchmarkConvertBytesSmall(b *testing.B) {
input := generateBytes(20, 256)
b.ResetTimer()
for i := 0; i < b.N; i++ {
ConvertBytes(input, 58)
}
}
func BenchmarkConvertMedium(b *testing.B) {
input := generateInts(1024, 256)
b.ResetTimer()
for i := 0; i < b.N; i++ {
Convert(input, 256, 58)
}
}
func BenchmarkConvertBytesMedium(b *testing.B) {
input := generateBytes(1024, 256)
b.ResetTimer()
for i := 0; i < b.N; i++ {
ConvertBytes(input, 58)
}
}
func BenchmarkConvertLarge(b *testing.B) {
input := generateInts(16*1024, 256)
b.ResetTimer()
for i := 0; i < b.N; i++ {
Convert(input, 256, 58)
}
}
func BenchmarkConvertBytesLarge(b *testing.B) {
input := generateBytes(16*1024, 256)
b.ResetTimer()
for i := 0; i < b.N; i++ {
ConvertBytes(input, 58)
}
}
func generateInts(n int, rx int) (out []int) {
out = make([]int, n)
for i := 0; i < n; i++ {
out[i] = (rx - 1) - i%rx
}
return
}
func generateBytes(n int, rx int) (out []byte) {
out = make([]byte, n)
for i := 0; i < n; i++ {
out[i] = byte((rx - 1) - i%rx)
}
return
}
func TestGenerateInts(t *testing.T) {
tests := []struct {
len int
radix int
want []int
}{
{len: 1, radix: 10, want: []int{9}},
{len: 4, radix: 1000, want: []int{999, 998, 997, 996}},
{len: 10, radix: 5, want: []int{4, 3, 2, 1, 0, 4, 3, 2, 1, 0}},
}
for _, test := range tests {
res := generateInts(test.len, test.radix)
if !reflect.DeepEqual(res, test.want) {
t.Errorf("generateInts(1, 10): %v not equal %v", res, test.want)
}
}
}
func TestGenerateBytes(t *testing.T) {
tests := []struct {
len int
radix int
want []byte
}{
{len: 1, radix: 10, want: []byte{9}},
{len: 4, radix: 256, want: []byte{255, 254, 253, 252}},
{len: 10, radix: 5, want: []byte{4, 3, 2, 1, 0, 4, 3, 2, 1, 0}},
}
for _, test := range tests {
res := generateBytes(test.len, test.radix)
if !reflect.DeepEqual(res, test.want) {
t.Errorf("generateInts(1, 10): %v not equal %v", res, test.want)
}
}
}