boolmap - crumbmap_test.go
1 package boolmap
2
3 import "testing"
4
5 var (
6 _ testMap = &CrumbMap{}
7 _ testSlice = &CrumbSlice{}
8 )
9
10 func TestCrumbMap(t *testing.T) {
11 tests := []struct {
12 val byte
13 pos uint64
14 repeat bool
15 }{
16 {3, 0, false},
17 {2, 0, false},
18 {1, 0, false},
19 {0, 0, false},
20 {3, 0, true},
21 {2, 1, true},
22 {1, 2, true},
23 {0, 3, true},
24 {0, 4, true},
25 {1, 5, true},
26 {2, 6, true},
27 {3, 7, true},
28 }
29
30 m := NewCrumbMap()
31
32 for n, test := range tests {
33 m.Set(test.pos, test.val)
34 if g := m.Get(test.pos); g != test.val {
35 t.Errorf("test %d-1: expecting %d, got %d", n+1, test.val, g)
36 }
37 }
38 for n, test := range tests {
39 if !test.repeat {
40 continue
41 }
42 if g := m.Get(test.pos); g != test.val {
43 t.Errorf("test %d-2: expecting %d, got %d", n+1, test.val, g)
44 }
45 }
46 }
47
48 func TestCrumbSlice(t *testing.T) {
49 tests := []struct {
50 val byte
51 pos uint
52 repeat bool
53 }{
54 {3, 0, false},
55 {2, 0, false},
56 {1, 0, false},
57 {0, 0, false},
58 {3, 0, true},
59 {2, 1, true},
60 {1, 2, true},
61 {0, 3, true},
62 {0, 4, true},
63 {1, 5, true},
64 {2, 6, true},
65 {3, 7, true},
66 }
67
68 s := NewCrumbSlice()
69
70 for n, test := range tests {
71 s.Set(test.pos, test.val)
72 if g := s.Get(test.pos); g != test.val {
73 t.Errorf("test %d-1: expecting %d, got %d", n+1, test.val, g)
74 }
75 }
76 for n, test := range tests {
77 if !test.repeat {
78 continue
79 }
80 if g := s.Get(test.pos); g != test.val {
81 t.Errorf("test %d-2: expecting %d, got %d", n+1, test.val, g)
82 }
83 }
84 }
85
86 func BenchmarkCrumbMap(b *testing.B) {
87 m := NewCrumbMap()
88 for n := 0; n < b.N; n++ {
89 for i := uint64(0); i < 100; i++ {
90 m.Set(i, byte(i&3))
91 }
92 }
93 }
94
95 func BenchmarkNonCrumbMap(b *testing.B) {
96 m := make(map[uint64]byte)
97 for n := 0; n < b.N; n++ {
98 for i := uint64(0); i < 100; i++ {
99 m[i] = byte(i & 3)
100 }
101 }
102 }