-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkv_test.go
101 lines (81 loc) · 1.88 KB
/
kv_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
package kv
import (
"fmt"
"testing"
"time"
)
// testing Set Get HasKey & delet functions
func Test_runAll(t *testing.T) {
fmt.Println("================= sync mod ===================")
intmap := New[int, int]()
fmt.Println(intmap)
intmap.Set(1, 123)
intval, _ := intmap.Get(1)
if intval != 123 {
t.Errorf("key must be %d", 123)
}
if ok := intmap.HasKey(1); ok != true {
t.Error("'1' key must be exist")
}
//
strmap := New[string, string]()
strmap.Set("hi", "hello")
sval, _ := strmap.Get("hi")
if sval != "hello" {
t.Errorf("value must be %s", "hello")
}
if ok := strmap.HasKey("hi"); ok != true {
t.Error("'hi' key must be exist")
}
fmt.Println("All Functions pass")
fmt.Println("=============== concurrent mod ==============")
}
var m = New[int, int]()
// testing Set and HasKey function in parallel
func Test_Set_HasKey(t *testing.T) {
for i := 0; i < 1000000; i++ {
if ok := m.HasKey(i); ok == true {
t.Errorf("%d key must be not exist", i)
}
m.Set(i, i*2)
if ok := m.HasKey(i); ok == false {
t.Errorf("%d key must be exist", i)
}
}
fmt.Println("Set func Pass")
fmt.Println("HasKey func Pass")
}
// testing Get function in parallel
func Test_Get(t *testing.T) {
for i := 0; i < 1000000; i++ {
time.Sleep(time.Nanosecond * 1)
val, ok := m.Get(i)
if !ok {
t.Error("not found value")
return
}
if val != i*2 {
t.Errorf("value must be %d", i*2)
return
}
}
fmt.Println("Get func Pass ")
}
// testing Delet function in parallel
func Test_Delete(t *testing.T) {
for i := 0; i < 1000000; i++ {
time.Sleep(time.Nanosecond * 1)
m.Delete(i)
}
//if len(m.data) != 0 {
// t.Errorf("len data must be %d not %d\n", 0, len(m.data))
//}
fmt.Println("Delete functions Pass")
}
/*
// bechmark
func Bench_Set(t *testing.B) {}
func Bench_Get(t *testing.B) {}
func Bench_Delete(t *testing.B) {}
func Bench_HasKey(t *testing.B) {}
*/