boolmap - boolmap_test.go
1 package boolmap
2
3 import "testing"
4
5 type testMap interface {
6 Get(p uint64) byte
7 Set(p uint64, d byte)
8 }
9
10 type testSlice interface {
11 Get(p uint) byte
12 Set(p uint, d byte)
13 }
14
15 var (
16 _ testMap = &Map{}
17 _ testSlice = &Slice{}
18 )
19
20 func TestBoolMap(t *testing.T) {
21 m := NewMap()
22 tests := []struct {
23 position uint64
24 value bool
25 mapPosition uint64
26 mapValue byte
27 }{
28 {0, true, 0, 1},
29 {1, true, 0, 3},
30 {2, true, 0, 7},
31 {7, true, 0, 135},
32 {8, true, 1, 1},
33 {8, false, 1, 0},
34 {1, false, 0, 133},
35 }
36 for n, test := range tests {
37 m.SetBool(test.position, test.value)
38 if m[test.mapPosition] != test.mapValue {
39 t.Errorf("test %d: expecting value %d, got %d", n+1, test.mapValue, m[test.mapPosition])
40 } else if test.mapValue == 0 {
41 if _, ok := m[test.mapPosition]; ok {
42 t.Errorf("test %d: map entry should have been removed on a zero value", n+1)
43 }
44 }
45 }
46 }
47
48 func TestBoolSlice(t *testing.T) {
49 s := NewSlice()
50 tests := []struct {
51 position uint
52 value bool
53 slicePosition uint
54 mapValue byte
55 }{
56 {0, true, 0, 1},
57 {1, true, 0, 3},
58 {2, true, 0, 7},
59 {7, true, 0, 135},
60 {8, true, 1, 1},
61 {8, false, 1, 0},
62 {1, false, 0, 133},
63 }
64 for n, test := range tests {
65 s.SetBool(test.position, test.value)
66 if (*s)[test.slicePosition] != test.mapValue {
67 t.Errorf("test %d: expecting value %d, got %d", n+1, test.mapValue, (*s)[test.slicePosition])
68 }
69 }
70 }
71
72 func BenchmarkBoolMap(b *testing.B) {
73 m := NewMap()
74 for n := 0; n < b.N; n++ {
75 for i := uint64(0); i < 100; i++ {
76 m.SetBool(i, true)
77 }
78 }
79 }
80
81 func BenchmarkNonBoolMap(b *testing.B) {
82 m := make(map[uint64]bool)
83 for n := 0; n < b.N; n++ {
84 for i := uint64(0); i < 100; i++ {
85 m[i] = true
86 }
87 }
88 }