-
Notifications
You must be signed in to change notification settings - Fork 0
/
insert_four_test.go
115 lines (109 loc) · 3.81 KB
/
insert_four_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
package k2tree
import (
"bytes"
"math/rand"
"testing"
)
func TestInsertFourBits(t *testing.T) {
tt := []struct {
in []byte
inByte byte
expected []byte
expectedOut byte
}{
{
in: []byte{0x01, 0x02, 0x03},
inByte: 0xF0,
expected: []byte{0xF0, 0x10, 0x20},
expectedOut: 0x30,
},
{
in: []byte{0x1A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A},
inByte: 0xF0,
expected: []byte{0xF1, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0},
expectedOut: 0xA0,
},
{
in: []byte{0x1A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A,
0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A},
inByte: 0xF0,
expected: []byte{0xF1, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0,
0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0},
expectedOut: 0xA0,
},
{
in: []byte{0xef, 0x4, 0xf6, 0x14, 0x4f, 0x21, 0x78, 0x5a, 0x55, 0x66, 0xa1, 0x3d, 0xf8, 0x7d, 0x5c, 0x10, 0x3c, 0x3f, 0x28, 0x4c, 0x5, 0x62, 0x3f, 0xda, 0xbf, 0x11, 0xa7, 0x2, 0x8e, 0xc8, 0x4f, 0xc2},
inByte: 0xa7,
expected: []byte{0xae, 0xf0, 0x4f, 0x61, 0x44, 0xf2, 0x17, 0x85, 0xa5, 0x56, 0x6a, 0x13, 0xdf, 0x87, 0xd5, 0xc1, 0x3, 0xc3, 0xf2, 0x84, 0xc0, 0x56, 0x23, 0xfd, 0xab, 0xf1, 0x1a, 0x70, 0x28, 0xec, 0x84, 0xfc},
expectedOut: 0x20,
},
}
for i, x := range tt {
b := make([]byte, len(x.in))
copy(b, x.in)
gotOut := insertFourBits(b, x.inByte)
if !bytes.Equal(b, x.expected) {
t.Logf("Bytes not matching for test %d: \ngot\t\t %#v\nexpected\t %#v", i, b, x.expected)
t.Fail()
}
if gotOut != x.expectedOut {
t.Logf("Output byte not matching for test %d: got %#v, expected %#v", i, gotOut, x.expectedOut)
t.Fail()
}
}
}
const FuzzIterations = 1000
func TestInsertFourFuzz(t *testing.T) {
for i := 0; i < FuzzIterations; i++ {
inbyte := byte(rand.Intn(256))
orig := generateByteString((rand.Intn(256) * rand.Intn(256)) + 1)
bytestr := make([]byte, len(orig))
realbyte := make([]byte, len(orig))
copy(realbyte, orig)
copy(bytestr, orig)
out := insertFourBits(bytestr[1:], inbyte)
outreal := insertFourBitsGo(realbyte[1:], inbyte)
if !bytes.Equal(bytestr, realbyte) {
t.Logf("Mismatched test case:\n{\nin: %#v,\ninByte: %#v,\ngot: %#v,\ngotOut: %#v,\nexpected: %#v,\nexpectedOut: %#v,\n},\n",
orig, inbyte, bytestr, out, realbyte, outreal)
t.Fail()
} else if out != outreal {
t.Logf("Mismatched test case:\n{\nin: %#v,\ninByte: %#v,\ngot: %#v,\ngotOut: %#v,\nexpected: %#v,\nexpectedOut: %#v,\n},\n",
orig, inbyte, bytestr, out, realbyte, outreal)
t.Fail()
}
}
}
func generateByteString(size int) []byte {
out := make([]byte, size)
for i := 0; i < size; i++ {
b := byte(rand.Intn(256))
out[i] = b
}
return out
}
func TestInsertFourChainableFuzz(t *testing.T) {
// Checks if calling insertFourBits followed by insertFourBits and passing
// along the extra byte is equivalent.
for i := 0; i < FuzzIterations; i++ {
inbyte := byte(rand.Intn(256))
n := 16 + rand.Intn(8)
orig := generateByteString((n * 2) + 1)
bytestr := make([]byte, len(orig))
realbyte := make([]byte, len(orig))
copy(realbyte, orig)
copy(bytestr, orig)
out_a := insertFourBits(bytestr[1:n+1], inbyte)
out_b := insertFourBits(bytestr[n+1:], out_a)
outreal := insertFourBitsGo(realbyte[1:], inbyte)
if !bytes.Equal(bytestr, realbyte) {
t.Logf("Mismatched test case:\n{\nin: %#v,\ninByte: %#v,\ngot: %#v,\ngotOut: %#v,\nexpected: %#v,\nexpectedOut: %#v,\n},\n",
orig, inbyte, bytestr, out_b, realbyte, outreal)
t.Fail()
} else if out_b != outreal {
t.Logf("Mismatched test case:\n{\nin: %#v,\ninByte: %#v,\ngot: %#v,\ngotOut: %#v,\nexpected: %#v,\nexpectedOut: %#v,\n},\n",
orig, inbyte, bytestr, out_b, realbyte, outreal)
t.Fail()
}
}
}