-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgomap_test.go
166 lines (146 loc) · 3.08 KB
/
gomap_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
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
All right reserved:https://github.com/hunterhug/gomap at 2020
Attribution-NonCommercial-NoDerivatives 4.0 International
You can use it for education only but can't make profits for any companies and individuals!
*/
package gomap
import (
"fmt"
"math/rand"
"testing"
"time"
)
func TestNew(t *testing.T) {
rw := make(map[string]interface{})
// loop times
var num = 10000
randNum := 100000000
rand.Seed(time.Now().Unix())
// 1. new a map
m := New()
m = NewAVLMap()
for i := 0; i < num; i++ {
key := fmt.Sprintf("%d", rand.Int63n(int64(randNum)))
//fmt.Println("add key:", key)
// 2. put key pairs
xx := key + fmt.Sprintf("_%v", rand.Int63n(int64(randNum)))
m.Put(key, xx)
rw[key] = xx
if m.Check() {
//fmt.Println("is a rb tree,len:", m.Len())
} else {
fmt.Println("add")
return
// check rb tree
}
}
if m.Check() {
fmt.Println("is a rb tree,len:", m.Len())
}
for k, v := range rw {
vv, ok := m.Get(k)
if !ok {
fmt.Println("err")
return
}
if vv != v {
fmt.Println("1 err", vv, v)
return
}
}
// 8. delete many
for i := 0; i < num; i++ {
key := fmt.Sprintf("%d", rand.Int63n(int64(randNum)))
//fmt.Println("delete key:", key)
m.Delete(key)
delete(rw, key)
if m.Check() {
//fmt.Println("is a rb tree,len:", m.Len())
} else {
return
// check rb tree
}
}
for k, v := range rw {
vv, ok := m.Get(k)
if !ok {
fmt.Println("err")
return
}
if vv != v {
fmt.Println("err", vv, v)
return
}
}
// 3. can iterator
//iterator := m.Iterator()
//for iterator.HasNext() {
// k, v := iterator.Next()
// fmt.Printf("Iterator key:%s,value %v\n", k, v)
//}
// 4. get key
key := "9"
value, exist := m.Get(key)
if exist {
fmt.Printf("%s exist, value is:%s\n", key, value)
} else {
fmt.Printf("%s not exist\n", key)
}
// 5. get int will err
_, _, err := m.GetInt(key)
if err != nil {
fmt.Println(err.Error())
}
// 6. check is a rb tree
if m.Check() {
fmt.Println("is a rb tree,len:", m.Len())
}
// 7. delete '9' then find '9'
m.Delete(key)
value, exist = m.Get(key)
if exist {
fmt.Printf("%s exist, value is:%s\n", key, value)
} else {
fmt.Printf("%s not exist\n", key)
}
// 8. delete many
for i := 0; i < num; i++ {
key := fmt.Sprintf("%d", rand.Int63n(int64(randNum)))
//fmt.Println("delete key:", key)
xx := key + fmt.Sprintf("_%v", rand.Int63n(int64(randNum)))
m.Put(key, xx)
rw[key] = xx
if m.Check() {
// //fmt.Println("is a rb tree,len:", m.Len())
} else {
// check rb tree
}
key = fmt.Sprintf("%d", rand.Int63n(int64(randNum)))
m.Delete(key)
delete(rw, key)
if m.Check() {
// //fmt.Println("is a rb tree,len:", m.Len())
} else {
return
// check rb tree
}
}
for k, v := range rw {
vv, ok := m.Get(k)
if !ok {
fmt.Println("err")
return
}
if vv != v {
fmt.Println("err", vv, v)
return
}
}
// 9. key list
//fmt.Printf("keyList:%#v,len:%d\n", m.KeyList(), m.Len())
//fmt.Printf("keySortList:%#v,len:%d\n", m.KeySortedList(), m.Len())
// 10. check is a rb tree
if m.Check() {
fmt.Println("is a rb tree,len:", m.Len())
}
}