-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfast_xor_test.go
56 lines (51 loc) · 1.11 KB
/
fast_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
package byte_utils
import (
"bytes"
"math/rand"
"testing"
)
func randomBytes(size, seed int) []byte {
rng := rand.New(rand.NewSource(int64(seed)))
toReturn := make([]byte, size)
for i := range toReturn {
toReturn[i] = byte(rng.Int())
}
return toReturn
}
func TestXor(t *testing.T) {
x := randomBytes(1337, 7)
y := randomBytes(len(x), 8)
expectedResult := make([]byte, len(x))
for i := range x {
expectedResult[i] = x[i] ^ y[i]
}
dst := make([]byte, len(x))
SimpleXor(dst, x, y)
if !bytes.Equal(dst, expectedResult) {
t.Logf("SimpleXor produced incorrect results.\n")
t.FailNow()
}
FastXor(dst, dst, y)
if !bytes.Equal(dst, x) {
t.Logf("FastXor failed to restore original input.\n")
t.FailNow()
}
}
func BenchmarkFastXor(b *testing.B) {
size := 1024 * 1024
x := randomBytes(size, 1337)
y := randomBytes(size, 1338)
dst := make([]byte, size)
for n := 0; n < b.N; n++ {
FastXor(dst, x, y)
}
}
func BenchmarkSimpleXor(b *testing.B) {
size := 1024 * 1024
x := randomBytes(size, 1337)
y := randomBytes(size, 1338)
dst := make([]byte, size)
for n := 0; n < b.N; n++ {
SimpleXor(dst, x, y)
}
}