This repository has been archived by the owner on Jul 12, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
byteslice_test.go
249 lines (226 loc) · 6.25 KB
/
byteslice_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
243
244
245
246
247
248
249
package byteslice
import (
"reflect"
"testing"
)
var (
values []byte
)
var tcReverse = []struct {
name string
data []byte
result []byte
}{
{"empty byte slice", []byte{}, []byte{}},
{"one element byte slice", []byte{0x01}, []byte{0x01}},
{"even length byte slice", []byte{0x01, 0x10}, []byte{0x10, 0x01}},
{"odd length byte slice", []byte{0x01, 0xFF, 0x10}, []byte{0x10, 0xFF, 0x01}},
}
func TestReverse(t *testing.T) {
for _, tc := range tcReverse {
t.Run(tc.name, func(t *testing.T) {
values = Reverse(tc.data)
if !reflect.DeepEqual(values, tc.result) {
t.Errorf("Reverse(%x) was %x, should be %x",
tc.data,
values,
tc.result)
}
})
}
}
func BenchmarkReverse(b *testing.B) {
for _, tc := range tcReverse {
b.Run(tc.name, func(b *testing.B) {
for i := 0; i < b.N; i++ {
values = Reverse(tc.data)
}
})
}
}
var tcLShift = []struct {
name string
data []byte
shift uint64
result []byte
}{
{"no shift to the left", []byte{0xDA, 0x99, 0xBA}, 0, []byte{0xDA, 0x99, 0xBA}},
{"low shift to the left", []byte{0xDA, 0x99, 0xBA}, 1, []byte{0xB5, 0x33, 0x74}},
{"middle shift to the left", []byte{0xDA, 0x99, 0xBA}, 8, []byte{0x99, 0xBA, 0x00}},
{"high shift to the left", []byte{0xDA, 0x99, 0xBA}, 16, []byte{0xBA, 0x00, 0x00}},
{"overflow shift to the left", []byte{0xDA, 0x99, 0xBA}, 24, []byte{0x00, 0x00, 0x00}},
}
func TestLShift(t *testing.T) {
for _, tc := range tcLShift {
t.Run(tc.name, func(t *testing.T) {
values = LShift(tc.data, tc.shift)
if !reflect.DeepEqual(values, tc.result) {
t.Errorf("LShift(%x, %v) was %x, should be %x",
tc.data, tc.shift,
values,
tc.result)
}
})
}
}
func BenchmarkLShift(b *testing.B) {
for _, tc := range tcLShift {
b.Run(tc.name, func(b *testing.B) {
for i := 0; i < b.N; i++ {
values = LShift(tc.data, tc.shift)
}
})
}
}
var tcRShift = []struct {
name string
data []byte
shift uint64
result []byte
}{
{"no shift to the right", []byte{0xDA, 0x99, 0xBA}, 0, []byte{0xDA, 0x99, 0xBA}},
{"low shift to the right", []byte{0xDA, 0x99, 0xBA}, 1, []byte{0x6D, 0x4C, 0xDD}},
{"middle shift to the right", []byte{0xDA, 0x99, 0xBA}, 8, []byte{0x00, 0xDA, 0x99}},
{"high shift to the right", []byte{0xDA, 0x99, 0xBA}, 16, []byte{0x00, 0x00, 0xDA}},
{"overflow shift to the right", []byte{0xDA, 0x99, 0xBA}, 24, []byte{0x00, 0x00, 0x00}},
}
func TestRShift(t *testing.T) {
for _, tc := range tcRShift {
t.Run(tc.name, func(t *testing.T) {
values = RShift(tc.data, tc.shift)
if !reflect.DeepEqual(values, tc.result) {
t.Errorf("RShift(%x, %v) was %x, should be %x",
tc.data, tc.shift,
values,
tc.result)
}
})
}
}
func BenchmarkRShift(b *testing.B) {
for _, tc := range tcRShift {
b.Run(tc.name, func(b *testing.B) {
for i := 0; i < b.N; i++ {
values = RShift(tc.data, tc.shift)
}
})
}
}
var tcLPad = []struct {
name string
data []byte
length int
filler byte
result []byte
}{
{"lpad to two elements on an smaller array", []byte{0xDA}, 2, 0x00, []byte{0x00, 0xDA}},
{"lpad to two elements with a filler on an smaller array", []byte{0xDA}, 2, 0x00, []byte{0x00, 0xDA}},
{"lpad to two elements on an empty array", []byte{}, 2, 0x11, []byte{0x11, 0x11}},
{"lpad to one element on an larger array", []byte{0xDA, 0x99, 0xBA}, 1, 0x11, []byte{0xDA, 0x99, 0xBA}},
{"lpad to negative element size change nothing", []byte{0xDA}, -1, 0x11, []byte{0xDA}},
}
func TestLPad(t *testing.T) {
for _, tc := range tcLPad {
t.Run(tc.name, func(t *testing.T) {
values = LPad(tc.data, tc.length, tc.filler)
if !reflect.DeepEqual(values, tc.result) {
t.Errorf("LPad(%x, %v, %v) was %x, should be %x",
tc.data, tc.length, tc.filler,
values,
tc.result)
}
})
}
}
func BenchmarkLPad(b *testing.B) {
for _, tc := range tcLPad {
b.Run(tc.name, func(b *testing.B) {
for i := 0; i < b.N; i++ {
values = LPad(tc.data, tc.length, tc.filler)
}
})
}
}
var tcRPad = []struct {
name string
data []byte
length int
filler byte
result []byte
}{
{"rpad to two elements on an smaller array", []byte{0xDA}, 2, 0x00, []byte{0xDA, 0x00}},
{"lpad to two elements with a filler on an smaller array", []byte{0xDA}, 2, 0x00, []byte{0xDA, 0x00}},
{"rpad to two elements on an empty array", []byte{}, 2, 0x11, []byte{0x11, 0x11}},
{"rpad to one element on an larger array", []byte{0xDA, 0x99, 0xBA}, 1, 0x11, []byte{0xDA, 0x99, 0xBA}},
{"rpad to negative element size change nothing", []byte{0xDA}, -1, 0x00, []byte{0xDA}},
}
func TestRPad(t *testing.T) {
for _, tc := range tcRPad {
t.Run(tc.name, func(t *testing.T) {
values = RPad(tc.data, tc.length, tc.filler)
if !reflect.DeepEqual(values, tc.result) {
t.Errorf("RPad(%x, %v, %v) was %x, should be %x",
tc.data, tc.length, tc.filler,
values,
tc.result)
}
})
}
}
func BenchmarkRPad(b *testing.B) {
for _, tc := range tcRPad {
b.Run(tc.name, func(b *testing.B) {
for i := 0; i < b.N; i++ {
values = RPad(tc.data, tc.length, tc.filler)
}
})
}
}
func TestUnsetError(t *testing.T) {
val, err := Unset([]byte{0x00, 0x00}, []byte{0x00})
if err == nil || val != nil {
t.Errorf("Unset with two byte slices of different size needs to return an error and no values")
}
}
func TestSetError(t *testing.T) {
val, err := Set([]byte{0x00, 0x00}, []byte{0x00})
if err == nil || val != nil {
t.Errorf("Set with two byte slices of different size needs to return an error and no values")
}
}
func TestToggleError(t *testing.T) {
val, err := Toggle([]byte{0x00, 0x00}, []byte{0x00})
if err == nil || val != nil {
t.Errorf("Toggle with two byte slices of different size needs to return an error and no values")
}
}
var tcFlip = []struct {
name string
data []byte
result []byte
}{
{"not empty array", []byte{0xDA, 0x99, 0xBA}, []byte{0x25, 0x66, 0x45}},
{"empty array", []byte{}, []byte{}},
}
func TestFlip(t *testing.T) {
for _, tc := range tcFlip {
t.Run(tc.name, func(t *testing.T) {
values = Flip(tc.data)
if !reflect.DeepEqual(values, tc.result) {
t.Errorf("Flip(%x) was %x, should be %x",
tc.data,
values,
tc.result)
}
})
}
}
func BenchmarkFlip(b *testing.B) {
for _, tc := range tcFlip {
b.Run(tc.name, func(b *testing.B) {
for i := 0; i < b.N; i++ {
values = Flip(tc.data)
}
})
}
}