aboutsummaryrefslogtreecommitdiffstats
path: root/common/bitutil/bitutil_test.go
blob: 93647031ef84e456df11011a4926b9953533ce00 (plain) (blame)
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
// Copyright 2013 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Adapted from: https://golang.org/src/crypto/cipher/xor_test.go

package bitutil

import (
    "bytes"
    "testing"
)

// Tests that bitwise XOR works for various alignments.
func TestXOR(t *testing.T) {
    for alignP := 0; alignP < 2; alignP++ {
        for alignQ := 0; alignQ < 2; alignQ++ {
            for alignD := 0; alignD < 2; alignD++ {
                p := make([]byte, 1023)[alignP:]
                q := make([]byte, 1023)[alignQ:]

                for i := 0; i < len(p); i++ {
                    p[i] = byte(i)
                }
                for i := 0; i < len(q); i++ {
                    q[i] = byte(len(q) - i)
                }
                d1 := make([]byte, 1023+alignD)[alignD:]
                d2 := make([]byte, 1023+alignD)[alignD:]

                XORBytes(d1, p, q)
                safeXORBytes(d2, p, q)
                if !bytes.Equal(d1, d2) {
                    t.Error("not equal", d1, d2)
                }
            }
        }
    }
}

// Tests that bitwise AND works for various alignments.
func TestAND(t *testing.T) {
    for alignP := 0; alignP < 2; alignP++ {
        for alignQ := 0; alignQ < 2; alignQ++ {
            for alignD := 0; alignD < 2; alignD++ {
                p := make([]byte, 1023)[alignP:]
                q := make([]byte, 1023)[alignQ:]

                for i := 0; i < len(p); i++ {
                    p[i] = byte(i)
                }
                for i := 0; i < len(q); i++ {
                    q[i] = byte(len(q) - i)
                }
                d1 := make([]byte, 1023+alignD)[alignD:]
                d2 := make([]byte, 1023+alignD)[alignD:]

                ANDBytes(d1, p, q)
                safeANDBytes(d2, p, q)
                if !bytes.Equal(d1, d2) {
                    t.Error("not equal")
                }
            }
        }
    }
}

// Tests that bitwise OR works for various alignments.
func TestOR(t *testing.T) {
    for alignP := 0; alignP < 2; alignP++ {
        for alignQ := 0; alignQ < 2; alignQ++ {
            for alignD := 0; alignD < 2; alignD++ {
                p := make([]byte, 1023)[alignP:]
                q := make([]byte, 1023)[alignQ:]

                for i := 0; i < len(p); i++ {
                    p[i] = byte(i)
                }
                for i := 0; i < len(q); i++ {
                    q[i] = byte(len(q) - i)
                }
                d1 := make([]byte, 1023+alignD)[alignD:]
                d2 := make([]byte, 1023+alignD)[alignD:]

                ORBytes(d1, p, q)
                safeORBytes(d2, p, q)
                if !bytes.Equal(d1, d2) {
                    t.Error("not equal")
                }
            }
        }
    }
}

// Tests that bit testing works for various alignments.
func TestTest(t *testing.T) {
    for align := 0; align < 2; align++ {
        // Test for bits set in the bulk part
        p := make([]byte, 1023)[align:]
        p[100] = 1

        if TestBytes(p) != safeTestBytes(p) {
            t.Error("not equal")
        }
        // Test for bits set in the tail part
        q := make([]byte, 1023)[align:]
        q[len(q)-1] = 1

        if TestBytes(q) != safeTestBytes(q) {
            t.Error("not equal")
        }
    }
}

// Benchmarks the potentially optimized XOR performance.
func BenchmarkFastXOR1KB(b *testing.B) { benchmarkFastXOR(b, 1024) }
func BenchmarkFastXOR2KB(b *testing.B) { benchmarkFastXOR(b, 2048) }
func BenchmarkFastXOR4KB(b *testing.B) { benchmarkFastXOR(b, 4096) }

func benchmarkFastXOR(b *testing.B, size int) {
    p, q := make([]byte, size), make([]byte, size)

    for i := 0; i < b.N; i++ {
        XORBytes(p, p, q)
    }
}

// Benchmarks the baseline XOR performance.
func BenchmarkBaseXOR1KB(b *testing.B) { benchmarkBaseXOR(b, 1024) }
func BenchmarkBaseXOR2KB(b *testing.B) { benchmarkBaseXOR(b, 2048) }
func BenchmarkBaseXOR4KB(b *testing.B) { benchmarkBaseXOR(b, 4096) }

func benchmarkBaseXOR(b *testing.B, size int) {
    p, q := make([]byte, size), make([]byte, size)

    for i := 0; i < b.N; i++ {
        safeXORBytes(p, p, q)
    }
}

// Benchmarks the potentially optimized AND performance.
func BenchmarkFastAND1KB(b *testing.B) { benchmarkFastAND(b, 1024) }
func BenchmarkFastAND2KB(b *testing.B) { benchmarkFastAND(b, 2048) }
func BenchmarkFastAND4KB(b *testing.B) { benchmarkFastAND(b, 4096) }

func benchmarkFastAND(b *testing.B, size int) {
    p, q := make([]byte, size), make([]byte, size)

    for i := 0; i < b.N; i++ {
        ANDBytes(p, p, q)
    }
}

// Benchmarks the baseline AND performance.
func BenchmarkBaseAND1KB(b *testing.B) { benchmarkBaseAND(b, 1024) }
func BenchmarkBaseAND2KB(b *testing.B) { benchmarkBaseAND(b, 2048) }
func BenchmarkBaseAND4KB(b *testing.B) { benchmarkBaseAND(b, 4096) }

func benchmarkBaseAND(b *testing.B, size int) {
    p, q := make([]byte, size), make([]byte, size)

    for i := 0; i < b.N; i++ {
        safeANDBytes(p, p, q)
    }
}

// Benchmarks the potentially optimized OR performance.
func BenchmarkFastOR1KB(b *testing.B) { benchmarkFastOR(b, 1024) }
func BenchmarkFastOR2KB(b *testing.B) { benchmarkFastOR(b, 2048) }
func BenchmarkFastOR4KB(b *testing.B) { benchmarkFastOR(b, 4096) }

func benchmarkFastOR(b *testing.B, size int) {
    p, q := make([]byte, size), make([]byte, size)

    for i := 0; i < b.N; i++ {
        ORBytes(p, p, q)
    }
}

// Benchmarks the baseline OR performance.
func BenchmarkBaseOR1KB(b *testing.B) { benchmarkBaseOR(b, 1024) }
func BenchmarkBaseOR2KB(b *testing.B) { benchmarkBaseOR(b, 2048) }
func BenchmarkBaseOR4KB(b *testing.B) { benchmarkBaseOR(b, 4096) }

func benchmarkBaseOR(b *testing.B, size int) {
    p, q := make([]byte, size), make([]byte, size)

    for i := 0; i < b.N; i++ {
        safeORBytes(p, p, q)
    }
}

// Benchmarks the potentially optimized bit testing performance.
func BenchmarkFastTest1KB(b *testing.B) { benchmarkFastTest(b, 1024) }
func BenchmarkFastTest2KB(b *testing.B) { benchmarkFastTest(b, 2048) }
func BenchmarkFastTest4KB(b *testing.B) { benchmarkFastTest(b, 4096) }

func benchmarkFastTest(b *testing.B, size int) {
    p := make([]byte, size)
    for i := 0; i < b.N; i++ {
        TestBytes(p)
    }
}

// Benchmarks the baseline bit testing performance.
func BenchmarkBaseTest1KB(b *testing.B) { benchmarkBaseTest(b, 1024) }
func BenchmarkBaseTest2KB(b *testing.B) { benchmarkBaseTest(b, 2048) }
func BenchmarkBaseTest4KB(b *testing.B) { benchmarkBaseTest(b, 4096) }

func benchmarkBaseTest(b *testing.B, size int) {
    p := make([]byte, size)
    for i := 0; i < b.N; i++ {
        safeTestBytes(p)
    }
}