-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathfuncs_test.go
76 lines (72 loc) · 1.83 KB
/
funcs_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
package bit
import (
"testing"
)
func TestConstants(t *testing.T) {
if bitsPerWord != 32 && bitsPerWord != 64 {
t.Errorf("bitsPerWord = %v; want 32 or 64", bitsPerWord)
}
if bitsPerWord == 32 {
if MaxUint != 1<<32-1 {
t.Errorf("MaxUint = %#x; want 1<<32 - 1", uint64(MaxUint))
}
if MaxInt != 1<<31-1 {
t.Errorf("MaxInt = %#x; want 1<<31 - 1", int64(MaxInt))
}
if MinInt != -1<<31 {
t.Errorf("MinInt = %#x; want -1 << 31", int64(MinInt))
}
}
if bitsPerWord == 64 {
if MaxUint != 1<<64-1 {
t.Errorf("MaxUint = %#x; want 1<<64 - 1", uint64(MaxUint))
}
if MaxInt != 1<<63-1 {
t.Errorf("MaxInt = %#x; want 1<<63 - 1", int64(MaxInt))
}
if MinInt != -1<<63 {
t.Errorf("MinInt = %#x; want -1 << 63", int64(MinInt))
}
}
}
// Checks all words with one nonzero bit.
func TestWordOneBit(t *testing.T) {
for i := 0; i < 64; i++ {
var w uint64 = 1 << uint(i)
lead, trail, count := LeadingZeros(w), TrailingZeros(w), Count(w)
if lead != 63-i {
t.Errorf("LeadingZeros(%#x) = %d; want %d", w, lead, i)
}
if trail != i {
t.Errorf("TrailingZeros(%#x) = %d; want %d", w, trail, i)
}
if count != 1 {
t.Errorf("Count(%#x) = %d; want %d", w, count, 1)
}
}
}
func TestWordFuncs(t *testing.T) {
for _, x := range []struct {
w uint64
lead, trail, count int
}{
{0x0, 64, 64, 0},
{0xa, 60, 1, 2},
{0xffffffffffffffff, 0, 0, 64},
{0x7ffffffffffffffe, 1, 1, 62},
{0x5555555555555555, 1, 0, 32},
{0xaaaaaaaaaaaaaaaa, 0, 1, 32},
} {
w := x.w
lead, trail, count := LeadingZeros(w), TrailingZeros(w), Count(w)
if lead != x.lead {
t.Errorf("LeadingZeros(%#x) = %v; want %v", w, lead, x.lead)
}
if trail != x.trail {
t.Errorf("TrailingZeros(%#x) = %v; want %v", w, trail, x.trail)
}
if count != x.count {
t.Errorf("Count(%#x) = %v; want %v", w, count, x.count)
}
}
}