forked from robskie/chd
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchd_test.go
123 lines (105 loc) · 2.56 KB
/
chd_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
123
package chd
import (
"bytes"
"strconv"
"testing"
"github.com/stretchr/testify/assert"
)
func TestMap_WriteTo(t *testing.T) {
b := NewBuilder(nil)
for i := 0; i < 10; i++ {
b.Add([]byte(strconv.Itoa(i)), []byte(strconv.Itoa(i)))
}
c, err := b.Build()
assert.NoError(t, err)
assert.Equal(t, "3", string(c.Get([]byte("3"))))
var buf bytes.Buffer
_, err = c.WriteTo(&buf)
assert.NoError(t, err)
c1 := NewMap()
c1.Read(buf.Bytes())
assert.Equal(t, "3", string(c1.Get([]byte("3"))))
}
func TestMap_Get(t *testing.T) {
b := NewBuilder(nil)
for i := 0; i < 4; i++ {
b.Add([]byte(strconv.Itoa(i)), []byte(strconv.Itoa(i)))
}
c, err := b.Build()
assert.NoError(t, err)
t.Run(`found`, func(t *testing.T) {
assert.NotNil(t, c.Get([]byte("0")))
assert.NotNil(t, c.Get([]byte("3")))
})
t.Run(`not found`, func(t *testing.T) {
assert.Nil(t, c.Get([]byte("10")))
})
t.Run(`random key`, func(t *testing.T) {
assert.NotNil(t, c.GetRandomKey())
})
t.Run(`random value`, func(t *testing.T) {
assert.NotNil(t, c.GetRandomValue())
})
}
func TestMap_GetRandomKey(t *testing.T) {
iterations := 1000
for it := 1; it < iterations; it++ {
t.Run(strconv.Itoa(it), func(t *testing.T) {
b := NewBuilder(nil)
for i := 0; i < it; i++ {
b.Add([]byte("k"+strconv.Itoa(i)), []byte("v"+strconv.Itoa(i)))
}
c, err := b.Build()
assert.NoError(t, err)
vals := map[string]struct{}{}
for i := 0; i < 10000; i++ {
val := c.GetRandomKey()
assert.NotEmpty(t, val)
vals[string(val)] = struct{}{}
}
if it == 1 {
assert.Len(t, vals, 1)
return
}
assert.True(t, len(vals) > 1)
})
}
}
func TestMap_GetRandomValue(t *testing.T) {
iterations := 1000
for it := 1; it < iterations; it++ {
t.Run(strconv.Itoa(it), func(t *testing.T) {
b := NewBuilder(nil)
for i := 0; i < it; i++ {
b.Add([]byte("k"+strconv.Itoa(i)), []byte("v"+strconv.Itoa(i)))
}
c, err := b.Build()
assert.NoError(t, err)
vals := map[string]struct{}{}
for i := 0; i < 10000; i++ {
val := c.GetRandomValue()
assert.NotEmpty(t, val)
vals[string(val)] = struct{}{}
}
if it == 1 {
assert.Len(t, vals, 1)
return
}
assert.True(t, len(vals) > 1)
})
}
}
func TestMap_Values(t *testing.T) {
b := NewBuilder(nil)
for i := 0; i <= 4; i++ {
b.Add([]byte(strconv.Itoa(i)), []byte(strconv.Itoa(i)))
}
c, err := b.Build()
assert.NoError(t, err)
values := []string{}
c.Values(func(v []byte) bool {
values = append(values, string(v))
return true
})
assert.ElementsMatch(t, []string{"0", "1", "2", "3", "4"}, values)
}