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"
"codeberg.org/snonux/algorithms/ds"
"codeberg.org/snonux/algorithms/sort"
)
const factor int = 10
const minLength int = 1
const maxLength int = 10000
// Store here so that Go isn't optimizing the benchmark away.
var benchResult interface{}
func TestElementary(t *testing.T) {
for i := minLength; i <= maxLength; i *= factor {
test[int,int](NewElementary[int,int](), i, t)
}
}
func TestBST(t *testing.T) {
for i := minLength; i <= maxLength; i *= factor {
test[int,int](NewBST[int,int](), i, t)
}
}
func TestRedBlackBST(t *testing.T) {
for i := minLength; i <= maxLength; i *= factor {
test[int,int](NewRedBlackBST[int,int](), i, t)
}
}
func TestHash(t *testing.T) {
for i := minLength; i <= maxLength; i *= factor {
test[int,int](NewHash[int,int](i*2), i, t)
}
}
func TestGoMap(t *testing.T) {
for i := minLength; i <= maxLength; i *= factor {
test[int,int](NewGoMap[int,int](), i, t)
}
}
func test[K ds.Integer, V ds.Number](s Set[K,V], l int, t *testing.T) {
keys := ds.NewRandomArrayList[K](l, l)
randoms := ds.NewRandomArrayList[V](l, -1)
mapping := make(map[K]V, l)
get := func(key K, del bool) V {
var val V
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 K) V { return get(key, false) }
testDel := func(key K) V { return get(key, true) }
testPut := func(key K, val V) {
s.Put(key, val)
mapping[key] = val
//t.Log("Put", key, val)
testGet(key)
}
//t.Log("Put random key-values", l)
var prevKey K
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[int,int]()
for i := minLength; i <= maxLength; i *= factor {
benchmark[int,int](s, i, t)
}
}
func BenchmarkBST(t *testing.B) {
s := NewBST[int,int]()
for i := minLength; i <= maxLength; i *= factor {
benchmark[int,int](s, i, t)
}
}
func BenchmarkRedBlackBST(t *testing.B) {
s := NewRedBlackBST[int,int]()
for i := minLength; i <= maxLength; i *= factor {
benchmark[int,int](s, i, t)
}
}
func BenchmarkHash(t *testing.B) {
s := NewHash[int,int](maxLength * 2)
for i := minLength; i <= maxLength; i *= factor {
benchmark[int,int](s, i, t)
}
}
func BenchmarkGoMap(t *testing.B) {
s := NewGoMap[int,int]()
for i := minLength; i <= maxLength; i *= factor {
benchmark[int,int](s, i, t)
}
}
func benchmark[K ds.Integer, V ds.Number](s Set[K,V], l int, b *testing.B) {
list := ds.NewRandomArrayList[K](l, -1)
b.Run(fmt.Sprintf("random(%d)", l), func(b *testing.B) {
b.ResetTimer()
for i, a := range list {
s.Put(a, V(i))
}
for _, a := range list {
benchResult, _ = s.Get(a)
}
})
}
|