This repository has been archived by the owner on Jun 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 53
/
xor_test.go
132 lines (123 loc) · 2.46 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package stun
import (
"math/rand"
"testing"
)
func TestXORSafe(t *testing.T) {
dst := make([]byte, 8)
a := []byte{1, 2, 3, 4, 5, 6, 7, 8}
b := []byte{8, 7, 7, 6, 6, 3, 4, 1}
safeXORBytes(dst, a, b)
safeXORBytes(dst, dst, a)
for i, v := range dst {
if b[i] != v {
t.Error(b[i], "!=", v)
}
}
}
func TestXORSafeBSmaller(t *testing.T) {
dst := make([]byte, 5)
a := []byte{1, 2, 3, 4, 5, 6, 7, 8}
b := []byte{8, 7, 7, 6, 6}
safeXORBytes(dst, a, b)
safeXORBytes(dst, dst, a)
for i, v := range dst {
if b[i] != v {
t.Error(b[i], "!=", v)
}
}
}
func TestXORFast(t *testing.T) {
if !supportsUnaligned {
t.Skip("No support for unaligned operations.")
}
dst := make([]byte, 8)
a := []byte{1, 2, 3, 4, 5, 6, 7, 8}
b := []byte{8, 7, 7, 6, 6, 3, 4, 1}
xorBytes(dst, a, b)
xorBytes(dst, dst, a)
for i, v := range dst {
if b[i] != v {
t.Error(b[i], "!=", v)
}
}
}
func TestXORFastBSmaller(t *testing.T) {
if !supportsUnaligned {
t.Skip("No support for unaligned operations.")
}
dst := make([]byte, 5)
a := []byte{1, 2, 3, 4, 5, 6, 7, 8}
b := []byte{8, 7, 7, 6, 6}
xorBytes(dst, a, b)
xorBytes(dst, dst, a)
for i, v := range dst {
if b[i] != v {
t.Error(b[i], "!=", v)
}
}
}
func TestXORFallback(t *testing.T) {
if !supportsUnaligned {
t.Skip("No support for unaligned operations.")
}
defer func() {
supportsUnaligned = true
}()
supportsUnaligned = false
dst := make([]byte, 5)
a := []byte{1, 2, 3, 4, 5, 6, 7, 8}
b := []byte{8, 7, 7, 6, 6}
xorBytes(dst, a, b)
xorBytes(dst, dst, a)
for i, v := range dst {
if b[i] != v {
t.Error(b[i], "!=", v)
}
}
}
func BenchmarkXOR(b *testing.B) {
rand.Seed(666)
a := make([]byte, 1024)
c := make([]byte, 1024)
rand.Read(a)
rand.Read(c)
b.SetBytes(1024)
b.RunParallel(func(pb *testing.PB) {
dst := make([]byte, len(a))
for pb.Next() {
xorBytes(dst, a, c)
}
})
}
func BenchmarkXORSafe(b *testing.B) {
rand.Seed(666)
a := make([]byte, 1024)
c := make([]byte, 1024)
rand.Read(a)
rand.Read(c)
b.SetBytes(1024)
b.RunParallel(func(pb *testing.PB) {
dst := make([]byte, len(a))
for pb.Next() {
safeXORBytes(dst, a, c)
}
})
}
func BenchmarkXORFast(b *testing.B) {
if !supportsUnaligned {
b.Skip("No support for unaligned operations.")
}
rand.Seed(666)
a := make([]byte, 1024)
c := make([]byte, 1024)
rand.Read(a)
rand.Read(c)
b.SetBytes(1024)
b.RunParallel(func(pb *testing.PB) {
dst := make([]byte, len(a))
for pb.Next() {
fastXORBytes(dst, a, c)
}
})
}