-
Notifications
You must be signed in to change notification settings - Fork 0
/
xor_test.go
113 lines (93 loc) · 1.71 KB
/
xor_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
package afec
import (
"testing"
"github.com/stretchr/testify/require"
)
func Test_xor(t *testing.T) {
var group = make([][]byte, 3)
group[0] = []byte{1, 2, 3, 4, 5}
group[1] = []byte{1, 1, 1, 0}
group[2] = []byte{2, 2, 2, 2}
{
var parity = []byte{1, 1, 1, 1, 1}
for _, b := range group {
parity = xor(b, parity)
}
t.Log(parity) // [3 0 1 7 4]
}
{
var parity = []byte{0, 0, 0, 0, 0}
for _, b := range group {
parity = xor(b, parity)
}
t.Log(parity) // [2 1 0 6 5]
}
}
func Test_cpyclr(t *testing.T) {
var gen = func(len int, cap int) []byte {
b := make([]byte, len, len+cap)
for i := 0; i < len; i++ {
b[i] = 1
}
return b
}
var suits = []struct {
src, dst []byte
}{
{
src: []byte{1, 2, 3},
dst: []byte{1, 1, 1},
},
{
src: []byte{1, 2, 3},
dst: []byte{1, 1},
},
{
src: []byte{1, 2},
dst: []byte{1, 1, 1},
},
{
src: []byte{1, 2, 3},
dst: gen(2, 1),
},
{
src: []byte{1, 2, 3},
dst: gen(1, 1),
},
{
src: []byte{1, 2, 3},
dst: gen(1, 4),
},
{
src: []byte{1, 2, 3},
dst: gen(0, 5),
},
{
src: []byte{1, 2, 3},
dst: gen(4, 1),
},
}
for _, suit := range suits {
dst := cpyclr(suit.src, suit.dst)
require.Equal(t, suit.src, dst)
require.True(t, isEmpty(dst[len(dst):cap(dst)]))
}
}
func Test_swap(t *testing.T) {
{
var a = []byte{1, 2, 3}
var b = []byte{1, 1, 1, 1}
a1, b1 := swap(a, b)
require.Equal(t, []byte{1, 1, 1, 1}, a1)
require.Equal(t, []byte{1, 2, 3}, b1)
}
{
var a = make([]byte, 2, 4)
a[0], a[1] = 9, 9
var _ = append(a, 11, 11)
var b = []byte{1, 2, 3, 4}
rawswap(a[:4], b)
require.Equal(t, []byte{1, 2, 3, 4}, a[:4])
require.Equal(t, []byte{9, 9, 11, 11}, b)
}
}