forked from mrekucci/epi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parity_test.go
51 lines (43 loc) · 1.2 KB
/
parity_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
// Copyright (c) 2015, Peter Mrekaj. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE.txt file.
package ptypes
import "testing"
const (
odd = 1
even = 0
)
type parityFn func(x uint64) uint16
func testParityFn(t *testing.T, fn parityFn, fnName string) {
for _, test := range []struct {
in uint64
want uint16
}{
{0, even},
{1, odd},
{2, odd},
{3, even},
{4, odd},
{5, even},
{6, even},
{7, odd},
{8, odd},
{9, even},
{1<<64 - 1, even},
} {
if got := fn(test.in); got != test.want {
t.Errorf("%s(%.64b) = %d; want %d", fnName, test.in, got, test.want)
}
}
}
func TestParity(t *testing.T) { testParityFn(t, Parity, "Parity") }
func TestParityAlt(t *testing.T) { testParityFn(t, ParityAlt, "ParityAlt") }
func TestParityLookup(t *testing.T) { testParityFn(t, ParityLookup, "ParityLookup") }
func benchParityFn(b *testing.B, fn parityFn) {
for i := 0; i < b.N; i++ {
fn(1<<64 - 1)
}
}
func BenchmarkParity(b *testing.B) { benchParityFn(b, Parity) }
func BenchmarkParityAlt(b *testing.B) { benchParityFn(b, ParityAlt) }
func BenchmarkParityLookup(b *testing.B) { benchParityFn(b, ParityLookup) }