-
Notifications
You must be signed in to change notification settings - Fork 1
/
mapsafe_test.go
73 lines (67 loc) · 1.57 KB
/
mapsafe_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
package shoset_test
import (
"fmt"
"strconv"
"testing"
"time"
"github.com/ditrit/shoset"
)
// TestMapSafeCRUD : test MapSafe crud functions
func TestMapSafeCRUD(t *testing.T) {
m := shoset.NewMapSafe()
m.Set("a", 23).Set("b", 43).Set("c", 11)
fmt.Printf("Initial state : ")
m.Iterate(
func(key string, val interface{}) {
fmt.Printf(" - %s: %d\n", key, val.(int))
},
)
m.Set("a", 454433)
fmt.Printf("After update : ")
m.Iterate(
func(key string, val interface{}) {
fmt.Printf(" - %s: %d\n", key, val.(int))
},
)
m.Delete("b")
fmt.Printf("After delete : ")
m.Iterate(
func(key string, val interface{}) {
fmt.Printf(" - %s: %d\n", key, val.(int))
},
)
}
// TestFold : test MapSafe folding using closure
func TestFold(t *testing.T) {
m := shoset.NewMapSafe()
m.Set("a", 23).Set("b", 43).Set("c", 11)
str := ""
m.Iterate(
func(key string, val interface{}) {
str = fmt.Sprintf("%s, %d", str, val.(int))
fmt.Printf(" - %s: %d\n", key, val.(int))
})
fmt.Printf("strValue : %s\n", str)
}
// TestConcurrency
func TestConcurrency(t *testing.T) {
m := shoset.NewMapSafe()
for i := 0; i < 10; i++ {
m.Set(strconv.Itoa(i), i)
}
fmt.Printf("test Concurrency\n")
go m.Iterate(
func(key string, val interface{}) {
time.Sleep(time.Millisecond * time.Duration(10))
fmt.Printf("%s, %d\n", key, val.(int))
})
time.Sleep(time.Millisecond * time.Duration(20))
fmt.Printf("after Iterate\n")
m.Set("a", 101)
m.Set("b", 102)
fmt.Printf("after Set\n")
m.Iterate(
func(key string, val interface{}) {
fmt.Printf("%s, %d\n", key, val.(int))
})
}