-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstringtable_test.go
122 lines (117 loc) · 2.69 KB
/
stringtable_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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package stringtable
import (
"fmt"
"testing"
)
func TestSanity(t *testing.T) {
st := StringTable{}
m := make(map[string]int)
m2 := make(map[int]string)
sliceSize := 0
for i := 0; i < 1000; i++ {
str := fmt.Sprintf("str%d", i)
index := st.Put(str)
m[str] = index
m2[index] = str
sliceSize++
}
if st.Len() != len(m) {
t.Errorf("Wrong len: %d", st.Len())
}
// Can read whats written
for k, v := range m {
if st.Get(v) != k {
t.Errorf("Got %s for %d but expected %s", st.Get(v), v, k)
}
}
// Add another gives same index
for i := 0; i < 1000; i++ {
str := fmt.Sprintf("str%d", i)
index := st.Put(str)
if index != m[str] {
t.Errorf("Second put failure for %s: %d", str, index)
}
}
// Remove odd ones
for i := 0; i < 1000; i++ {
if i%2 != 0 {
str := fmt.Sprintf("str%d", i)
st.Remove(str)
}
}
if len(st.t) != sliceSize {
t.Errorf("Wrong slice size, expected %d got %d", sliceSize, len(st.t))
}
// Must be same amount
if st.Len() != len(m) {
t.Errorf("Wrong len after remove: %d", st.Len())
}
// Can read whats written
for k, v := range m {
if st.Get(v) != k {
t.Errorf("Got %s for %d but expected %s", st.Get(v), v, k)
}
}
// Remove odd ones
for i := 0; i < 1000; i++ {
if i%2 != 0 {
str := fmt.Sprintf("str%d", i)
st.Remove(str)
delete(m2, m[str])
delete(m, str)
}
}
// Must be half amount
if st.Len() != len(m) {
t.Errorf("Wrong len after remove: %d", st.Len())
}
if len(st.t) != sliceSize {
t.Errorf("Wrong slice size, expected %d got %d", sliceSize, len(st.t))
}
// Add new ones
for i := 0; i < 1000; i++ {
str := fmt.Sprintf("str2 %d", i)
index := st.Put(str)
m[str] = index
m2[index] = str
}
sliceSize += 500
// size of slice must be correct
if len(st.t) != sliceSize {
t.Errorf("Wrong slice size, expected %d got %d", sliceSize, len(st.t))
}
}
func benchmarkAdd(b *testing.B, k int) {
strs := make([]string, 0, k)
for i := 0; i < k; i++ {
strs = append(strs, fmt.Sprintf("String %d", i))
}
for i := 0; i < b.N; i++ {
st := StringTable{}
for _, str := range strs {
st.Put(str)
}
for _, str := range strs {
st.Put(str)
}
}
}
func benchmarkAddMap(b *testing.B, k int) {
strs := make([]string, 0, k)
for i := 0; i < k; i++ {
strs = append(strs, fmt.Sprintf("String %d", i))
}
for i := 0; i < b.N; i++ {
st := make(map[string]int)
for _, str := range strs {
st[str] = 1
}
for _, str := range strs {
st[str] = 1
}
}
}
func BenchmarkAdd10000(b *testing.B) { benchmarkAdd(b, 10000) }
func BenchmarkAdd1000(b *testing.B) { benchmarkAdd(b, 1000) }
func BenchmarkAddMap10000(b *testing.B) { benchmarkAddMap(b, 10000) }
func BenchmarkAddMap1000(b *testing.B) { benchmarkAddMap(b, 1000) }