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
|
package dict
import (
"fmt"
"slices"
"strings"
"testing"
)
func TestDict_Sort(t *testing.T) {
d := NewDict[string]()
d.Set("foo", "foo")
d.Set("bar", "bar")
d.Set("baz", "baz")
d.Sort(func(a, b Entry[string]) int {
return strings.Compare(a.Key, b.Key)
})
expected := []Entry[string]{
{"bar", "bar"},
{"baz", "baz"},
{"foo", "foo"},
}
verifyMap(t, d)
if !slices.Equal(d.s, expected) {
t.Fatalf("expected %v but got %v", expected, d.s)
}
}
func verifyMap(t *testing.T, dict *Dict[string]) {
if len(dict.m) != len(dict.s) {
t.Fatalf("length missmatch")
}
for i, s := range dict.s {
v, ok := dict.m[s.Key]
if !ok {
t.Fatalf("unknown key %s", s)
}
if v != i {
t.Errorf("expected %d but got %d\n", i, v)
}
}
}
func BenchmarkDict_Get(b *testing.B) {
d := NewDict[int]()
for i := 0; i < 1000; i++ {
d.Set(fmt.Sprintf("key_%d", i), i)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = d.Get("key_500")
}
}
func BenchmarkDict_GetBytes(b *testing.B) {
d := NewDict[int]()
for i := 0; i < 1000; i++ {
d.Set(fmt.Sprintf("key_%d", i), i)
}
keyBytes := []byte("key_500")
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = d.GetBytes(keyBytes)
}
}
|