forked from gogearbox/gearbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtst_test.go
62 lines (54 loc) · 1.16 KB
/
tst_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
package gearbox
import (
"fmt"
"math/rand"
"testing"
"time"
)
// ExampleTST tests TST set and get methods
func ExampleTST() {
tst := newTST()
tst.Set("user", 1)
fmt.Println(tst.Get("user").(int))
fmt.Println(tst.Get("us"))
fmt.Println(tst.Get("user1"))
fmt.Println(tst.Get("not-existing"))
tst.Set("account", 5)
tst.Set("account", 6)
fmt.Println(tst.Get("account").(int))
tst.Set("acc@unt", 12)
fmt.Println(tst.Get("acc@unt").(int))
tst.Set("حساب", 15)
fmt.Println(tst.Get("حساب").(int))
tst.Set("", 14)
fmt.Println(tst.Get(""))
// Output:
// 1
// <nil>
// <nil>
// <nil>
// 6
// 12
// 15
// <nil>
}
// RandStringBytes generates random string from English letters
func RandStringBytes() string {
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
b := make([]byte, rand.Intn(100))
for i := range b {
b[i] = letterBytes[rand.Intn(len(letterBytes))]
}
return string(b)
}
func BenchmarkTSTLookup(b *testing.B) {
tst := newTST()
rand.Seed(time.Now().UnixNano())
for n := 0; n < rand.Intn(2000); n++ {
tst.Set(RandStringBytes(), rand.Intn(10000))
}
b.ResetTimer()
for n := 0; n < b.N; n++ {
tst.Get("user")
}
}