-
Notifications
You must be signed in to change notification settings - Fork 2
/
sortedmap_score_test.go
153 lines (135 loc) · 3.3 KB
/
sortedmap_score_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
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
// Copyright (c) 2018-2019 Burak Sezer. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package sorted
import (
"bytes"
"math/rand"
"testing"
"time"
)
func Test_SortedMapSetWithScore(t *testing.T) {
sm := NewSortedMapWithScore(0, nil)
defer sm.Close()
r := rand.New(rand.NewSource(time.Now().UnixNano()))
keys := []string{}
check := make(map[int]struct{})
for i := 0; i < 100; i++ {
key := r.Int()
if _, ok := check[key]; ok {
// Find a new key.
i--
continue
}
err := sm.Set(bkey(key), bval(key), uint64(i))
if err != nil {
t.Fatalf("Expected nil. Got %v", err)
}
keys = append(keys, string(bkey(key)))
check[key] = struct{}{}
}
idx := 0
// Sorted by iteration order.
sm.Range(func(key, value []byte) bool {
if keys[idx] != string(key) {
t.Fatalf("Expected %s. Got: %s for id: %d", keys[idx], string(key), idx)
}
idx++
return true
})
}
func Test_SortedMapWithScoreGet(t *testing.T) {
sm := NewSortedMapWithScore(0, nil)
defer sm.Close()
r := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := 0; i < 100; i++ {
score := r.Uint64()
err := sm.Set(bkey(i), bval(i), score)
if err != nil {
t.Fatalf("Expected nil. Got %v", err)
}
}
for i := 0; i < 100; i++ {
val, err := sm.Get(bkey(i))
if err != nil {
t.Fatalf("Expected nil. Got %v", err)
}
if !bytes.Equal(val, bval(i)) {
t.Fatalf("Values are differend for %s", string(bval(i)))
}
}
}
func Test_SortedMapWithScoreDelete(t *testing.T) {
sm := NewSortedMapWithScore(0, nil)
defer sm.Close()
r := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := 0; i < 100; i++ {
score := r.Uint64()
err := sm.Set(bkey(i), bval(i), score)
if err != nil {
t.Fatalf("Expected nil. Got %v", err)
}
}
for i := 0; i < 100; i++ {
err := sm.Delete(bkey(i))
if err != nil {
t.Fatalf("Expected nil. Got %v", err)
}
}
for i := 0; i < 100; i++ {
ok := sm.Check(bkey(i))
if ok {
t.Fatalf("Expected false. Got true for %s", string(bkey(i)))
}
}
}
func Test_SortedMapWithScoreRange(t *testing.T) {
sm := NewSortedMapWithScore(0, nil)
defer sm.Close()
r := rand.New(rand.NewSource(time.Now().UnixNano()))
keys := make(map[string]struct{})
for i := 0; i < 100; i++ {
err := sm.Set(bkey(i), bval(i), r.Uint64())
if err != nil {
t.Fatalf("Expected nil. Got %v", err)
}
key := string(bkey(i))
keys[key] = struct{}{}
}
sm.Range(func(key, value []byte) bool {
if _, ok := keys[string(key)]; !ok {
t.Fatalf("Invalid hkey: %s", string(key))
}
return true
})
}
func Test_SortedMapWithScoreSubMap(t *testing.T) {
sm := NewSortedMapWithScore(0, nil)
defer sm.Close()
r := rand.New(rand.NewSource(time.Now().UnixNano()))
keys := []string{}
check := make(map[int]struct{})
for i := 0; i < 100; i++ {
key := r.Int()
if _, ok := check[key]; ok {
// Find a new key.
i--
continue
}
err := sm.Set(bkey(key), bval(key), uint64(i))
if err != nil {
t.Fatalf("Expected nil. Got %v", err)
}
keys = append(keys, string(bkey(key)))
check[key] = struct{}{}
}
idx := 23
// Sorted by iteration order.
sm.SubMap(23, 45, func(key, value []byte) bool {
if keys[idx] != string(key) {
t.Fatalf("Expected %s. Got: %s for id: %d", keys[idx], string(key), idx)
}
idx++
return true
})
}