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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
package search
import (
"fmt"
"testing"
"github.com/snonux/algorithms/ds"
"github.com/snonux/algorithms/sort"
)
const factor int = 10
const minLength int = 1
const maxLength int = 10000
// Store results here to avoid compiler optimizations
var benchResult int
func TestElementary(t *testing.T) {
for i := minLength; i <= maxLength; i *= factor {
test(NewElementary(), i, t)
}
}
func TestBST(t *testing.T) {
for i := minLength; i <= maxLength; i *= factor {
test(NewBST(), i, t)
}
}
func TestRedBlackBST(t *testing.T) {
for i := minLength; i <= maxLength; i *= factor {
test(NewRedBlackBST(), i, t)
}
}
func TestHash(t *testing.T) {
for i := minLength; i <= maxLength; i *= factor {
test(NewHash(i*2), i, t)
}
}
func TestGoMap(t *testing.T) {
for i := minLength; i <= maxLength; i *= factor {
test(NewGoMap(), i, t)
}
}
func test(s Put, l int, t *testing.T) {
keys := ds.NewRandomArrayList(l, l)
randoms := ds.NewRandomArrayList(l, -1)
mapping := make(map[int]int, l)
get := func(key int, del bool) int {
var val int
var err error
switch {
case del:
defer delete(mapping, key)
val, err = s.Del(key)
//t.Log("Del", key, val, err)
default:
val, err = s.Get(key)
//t.Log("Get", key, val, err)
}
if mVal, ok := mapping[key]; ok {
if err != nil {
t.Error("Could not get element", key, val, mVal, err)
}
if mVal != val {
t.Error("Got wrong value for element", key, val, mVal)
}
return val
}
if err == nil {
t.Error("Got element but expected not to", key, val)
}
return val
}
testGet := func(key int) int { return get(key, false) }
testDel := func(key int) int { return get(key, true) }
testPut := func(key, val int) {
s.Put(key, val)
mapping[key] = val
//t.Log("Put", key, val)
testGet(key)
}
//t.Log("Put random key-values", l)
var prevKey int
for _, key := range sort.Shuffle(keys) {
testPut(key, randoms[key])
testGet(prevKey)
prevKey = key
}
//t.Log("Del random key-values", l)
for _, key := range sort.Shuffle(keys) {
testDel(key)
testGet(prevKey)
prevKey = key
}
if !s.Empty() {
t.Error("Expected set to be empty", l)
}
}
func BenchmarkElementary(t *testing.B) {
s := NewElementary()
for i := minLength; i <= maxLength; i *= factor {
benchmark(s, i, t)
}
}
func BenchmarkBST(t *testing.B) {
s := NewBST()
for i := minLength; i <= maxLength; i *= factor {
benchmark(s, i, t)
}
}
func BenchmarkRedBlackBST(t *testing.B) {
s := NewRedBlackBST()
for i := minLength; i <= maxLength; i *= factor {
benchmark(s, i, t)
}
}
func BenchmarkHash(t *testing.B) {
s := NewHash(maxLength * 2)
for i := minLength; i <= maxLength; i *= factor {
benchmark(s, i, t)
}
}
func BenchmarkGoMap(t *testing.B) {
s := NewGoMap()
for i := minLength; i <= maxLength; i *= factor {
benchmark(s, i, t)
}
}
func benchmark(s Put, l int, b *testing.B) {
list := ds.NewRandomArrayList(l, -1)
b.Run(fmt.Sprintf("random(%d)", l), func(b *testing.B) {
b.ResetTimer()
for i, a := range list {
s.Put(a, i)
}
for _, a := range list {
benchResult, _ = s.Get(a)
}
})
}
|