...
1
16
17 package allocator
18
19 import (
20 "math/big"
21 "testing"
22 )
23
24 func TestCountBits(t *testing.T) {
25
26 bigN, ok := big.NewInt(0).SetString("10000000000000000000000000000000000000000000000000000000000000000", 16)
27 if !ok {
28 t.Fatal("Failed to set bigN")
29 }
30 tests := []struct {
31 n *big.Int
32 expected int
33 }{
34 {n: big.NewInt(int64(0)), expected: 0},
35 {n: big.NewInt(int64(0xffffffffff)), expected: 40},
36 {n: bigN, expected: 1},
37 }
38 for _, test := range tests {
39 actual := countBits(test.n)
40 if test.expected != actual {
41 t.Errorf("%s should have %d bits but recorded as %d", test.n, test.expected, actual)
42 }
43 }
44 }
45
46 func BenchmarkCountBits(b *testing.B) {
47 bigN, ok := big.NewInt(0).SetString("10000000000000000000000000000000000000000000000000000000000000000", 16)
48 if !ok {
49 b.Fatal("Failed to set bigN")
50 }
51 for i := 0; i < b.N; i++ {
52 countBits(bigN)
53 }
54 }
55
View as plain text