-
Notifications
You must be signed in to change notification settings - Fork 0
/
map_test.go
112 lines (94 loc) · 1.87 KB
/
map_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
package readonly_test
import (
"fmt"
"testing"
"github.com/psyhatter/readonly"
)
func ExampleMap_IsNil() {
m1, m2 := readonly.NewMap[string, int](nil), readonly.NewMap(map[string]int{})
fmt.Println(m1.IsNil(), m2.IsNil())
// Output:
// true false
}
func ExampleMap_Len() {
m := readonly.NewMap(map[string]int{"1": 1, "2": 2})
fmt.Println(m.Len())
// Output:
// 2
}
func ExampleMap_Has() {
m := readonly.NewMap(map[string]int{"1": 1, "2": 2})
fmt.Println(m.Has("1"))
fmt.Println(m.Has("2"))
fmt.Println(m.Has("3"))
// Output:
// true
// true
// false
}
func ExampleMap_Get() {
m := readonly.NewMap(map[string]int{"1": 1, "2": 2})
fmt.Println(m.Get("1"))
fmt.Println(m.Get("2"))
fmt.Println(m.Get("3"))
// Output:
// 1
// 2
// 0
}
func ExampleMap_Get2() {
m := readonly.NewMap(map[string]int{"1": 1, "2": 2})
fmt.Println(m.Get2("1"))
fmt.Println(m.Get2("2"))
fmt.Println(m.Get2("3"))
// Output:
// 1 true
// 2 true
// 0 false
}
func ExampleMap_Range() {
m := readonly.NewMap(map[string]int{"1": 1, "2": 2})
m.Range(func(k string, v int) (next bool) {
fmt.Println(k, v)
return true
})
m.Range(func(k string, v int) (next bool) {
fmt.Println("this happened") // will be printed only once.
return false
})
m.Range(nil) // do nothing.
// Unordered output:
// 1 1
// 2 2
// this happened
}
var m = func() map[int]int {
m := make(map[int]int, limit)
for i := 0; i < limit; i++ {
m[i] = i
}
return m
}()
func BenchmarkMap_Range(b *testing.B) {
b.Run("built-in", func(b *testing.B) {
m := m
b.ResetTimer()
for i := 0; i < b.N; i++ {
var count int
for key, val := range m {
count += key + val
}
}
})
b.Run("readonly", func(b *testing.B) {
m := readonly.NewMap(m)
b.ResetTimer()
for i := 0; i < b.N; i++ {
var count int
m.Range(func(key, val int) (next bool) {
count += key + val
return true
})
}
})
}