-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain_test.go
294 lines (280 loc) · 6.54 KB
/
main_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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package main
import (
"io/ioutil"
"os"
"testing"
"github.com/stretchr/testify/require"
)
func TestParseByteOffset(t *testing.T) {
tests := []struct {
name string
in string
wantByteOffset uint64
wantBitOffset uint8
wantErr string
}{
{
name: "valid string",
in: "122@0",
wantByteOffset: 122,
wantBitOffset: 0,
wantErr: "",
},
{
name: "valid string",
in: "0@0",
wantByteOffset: 0,
wantBitOffset: 0,
wantErr: "",
},
{
name: "invalid bit offset",
in: "122@8",
wantByteOffset: 0,
wantBitOffset: 0,
wantErr: "bit offset must be between 0 and 7, was 8",
},
{
name: "invalid bit offset",
in: "122@-1",
wantByteOffset: 0,
wantBitOffset: 0,
wantErr: "invalid bit offset: strconv.ParseUint: parsing \"-1\": invalid syntax",
},
{
name: "invalid byte offset",
in: "lol@lol",
wantByteOffset: 0,
wantBitOffset: 0,
wantErr: "invalid byte offset: strconv.ParseUint: parsing \"lol\": invalid syntax",
},
{
name: "invalid bit offset",
in: "1@lol",
wantByteOffset: 0,
wantBitOffset: 0,
wantErr: "invalid bit offset: strconv.ParseUint: parsing \"lol\": invalid syntax",
},
{
name: "invalid string",
in: "@",
wantByteOffset: 0,
wantBitOffset: 0,
wantErr: "invalid byte offset: strconv.ParseUint: parsing \"\": invalid syntax",
},
{
name: "invalid string",
in: "@@@",
wantByteOffset: 0,
wantBitOffset: 0,
wantErr: "invalid offset string, too many '@' symbols found",
},
{
name: "invalid string",
in: "122:0",
wantByteOffset: 0,
wantBitOffset: 0,
wantErr: "invalid offset string, no '@' symbols found",
},
{
name: "invalid string",
in: " 122@0 ",
wantByteOffset: 0,
wantBitOffset: 0,
wantErr: `invalid byte offset: strconv.ParseUint: parsing " 122": invalid syntax`,
},
{
name: "invalid byte offset string",
in: "-1@0",
wantByteOffset: 0,
wantBitOffset: 0,
wantErr: `invalid byte offset: strconv.ParseUint: parsing "-1": invalid syntax`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotByteOffset, gotBitOffset, gotErr := parseByteOffset(tt.in)
require.Equal(t, tt.wantByteOffset, gotByteOffset)
require.Equal(t, tt.wantBitOffset, gotBitOffset)
if tt.wantErr != "" {
require.Contains(t, tt.wantErr, gotErr.Error())
} else {
require.NoError(t, gotErr)
}
})
}
}
func TestToggleNthBit(t *testing.T) {
tests := []struct {
name string
inByte byte
inNthBit uint8
want byte
}{
{
name: "0th bit",
inByte: 0b00000000,
inNthBit: 0,
want: 0b00000001,
},
{
name: "1st bit",
inByte: 0b00000000,
inNthBit: 1,
want: 0b00000010,
},
{
name: "2nd bit",
inByte: 0b00000000,
inNthBit: 2,
want: 0b00000100,
},
{
name: "3rd bit",
inByte: 0b00000000,
inNthBit: 3,
want: 0b00001000,
},
{
name: "4th bit",
inByte: 0b00000000,
inNthBit: 4,
want: 0b00010000,
},
{
name: "5th bit",
inByte: 0b00000000,
inNthBit: 5,
want: 0b00100000,
},
{
name: "6th bit",
inByte: 0b00000000,
inNthBit: 6,
want: 0b01000000,
},
{
name: "7th bit",
inByte: 0b00000000,
inNthBit: 7,
want: 0b10000000,
},
{
name: "8th bit",
inByte: 0b00000000,
inNthBit: 8,
want: 0b00000000,
},
{
name: "9th bit",
inByte: 0b00000000,
inNthBit: 9,
want: 0b00000000,
},
{
name: "32nd bit",
inByte: 0b00000000,
inNthBit: 32,
want: 0b00000000,
},
{
name: "64th bit",
inByte: 0b00000000,
inNthBit: 64,
want: 0b00000000,
},
{
name: "65th bit",
inByte: 0b00000000,
inNthBit: 65,
want: 0b00000000,
},
{
name: "66th bit",
inByte: 0b00000000,
inNthBit: 66,
want: 0b00000000,
},
{
name: "4th bit (from 0 to 1) in non-zero data",
inByte: 0b01010101,
inNthBit: 4,
want: 0b01000101,
},
{
name: "4th bit (from 1 to 0) in non-zero data",
inByte: 0b10101010,
inNthBit: 4,
want: 0b10111010,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := toggleNthBit(tt.inByte, tt.inNthBit)
require.Equal(t, tt.want, got)
})
}
}
func TestFlipBitAtOffset(t *testing.T) {
tests := []struct {
name string
inByteOffset uint64
inBitOffset uint8
in []byte
want []byte
wantErr string
}{
{
name: "regular flip",
inByteOffset: 0,
inBitOffset: 0,
in: []byte{0b00000001, 0b00000000, 0b00000000, 0b00000000},
want: []byte{0b00000000, 0b00000000, 0b00000000, 0b00000000},
wantErr: "",
},
{
name: "mid file flip",
inByteOffset: 2,
inBitOffset: 4,
in: []byte{0b00000000, 0b00000000, 0b00000000, 0b00000000},
want: []byte{0b00000000, 0b00000000, 0b00010000, 0b00000000},
wantErr: "",
},
{
name: "end of file flip",
inByteOffset: 3,
inBitOffset: 7,
in: []byte{0b00000000, 0b00000000, 0b00000000, 0b00000000},
want: []byte{0b00000000, 0b00000000, 0b00000000, 0b10000000},
wantErr: "",
},
}
dname, err := ioutil.TempDir(os.TempDir(), "")
require.NoError(t, err)
t.Cleanup(func() { os.RemoveAll(dname) })
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// prepare the target file with the data
f, err := ioutil.TempFile(dname, "")
require.NoError(t, err)
fname := f.Name()
t.Cleanup(func() { os.Remove(fname) })
_, err = f.Write(tt.in)
require.NoError(t, err)
require.NoError(t, f.Close())
// reopen the target file and do the test
f, err = os.OpenFile(fname, os.O_RDWR, 0)
require.NoError(t, err)
gotErr := flipBitAtOffset(f, int64(tt.inByteOffset), tt.inBitOffset)
if tt.wantErr != "" {
require.Contains(t, tt.wantErr, gotErr.Error())
return
}
require.NoError(t, gotErr, "should have flipped bit")
require.NoError(t, f.Close(), "should be able to close file")
got, err := ioutil.ReadFile(fname)
require.NoError(t, err, "should be able to read back the file")
require.Equal(t, tt.want, got)
})
}
}