-
Notifications
You must be signed in to change notification settings - Fork 0
/
dict_example_test.go
126 lines (110 loc) · 1.96 KB
/
dict_example_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
package godino
import (
"fmt"
"sort"
)
func ExampleDict_Clear() {
d := Dict[int, string]{1: "foo"}
d.Clear()
fmt.Println(d.Items())
// Output: []
}
func ExampleDict_Copy() {
d := Dict[int, string]{1: "foo"}
d2 := d.Copy()
fmt.Println(d2.Items())
// Output: [{1 foo}]
}
func ExampleDict_Get() {
d := Dict[int, string]{1: "foo"}
fmt.Println(d.Get(1))
fmt.Println(d.Get(2))
fmt.Println(d.Get(2, "bar"))
// Output:
// foo
//
// bar
}
func ExampleDict_Has() {
d := Dict[int, string]{1: "foo"}
fmt.Println(d.Has(1))
fmt.Println(d.Has(2))
// Output:
// true
// false
}
func ExampleDict_Items() {
d := Dict[int, string]{
1: "foo",
2: "bar",
3: "baz",
}
items := d.Items()
sort.Slice(items, func(i, j int) bool { return items[i].Key < items[j].Key })
fmt.Println(items)
// Output:
// [{1 foo} {2 bar} {3 baz}]
}
func ExampleDict_Keys() {
d := Dict[int, string]{
1: "foo",
2: "bar",
3: "baz",
}
keys := d.Keys()
sort.Slice(keys, func(i, j int) bool { return keys[i] < keys[j] })
fmt.Println(keys)
// Output:
// [1 2 3]
}
func ExampleDict_Pop() {
d := Dict[int, string]{
1: "foo",
2: "bar",
3: "baz",
}
popped, ok := d.Pop(1)
fmt.Println(popped, ok)
fmt.Println(d.Has(1))
fmt.Println(len(d))
popped2, ok2 := d.Pop(4, "fallback")
fmt.Println(popped2, ok2)
popped3, ok3 := d.Pop(5)
fmt.Println(popped3, ok3)
// Output:
// foo true
// false
// 2
// fallback false
// false
}
func ExampleDict_SetDefault() {
d := Dict[int, string]{
1: "foo",
2: "bar",
}
d2 := Dict[int, string]{
2: "baz",
3: "bar",
}
d.Update(d2)
items := d.Items()
sort.Slice(items, func(i, j int) bool {
return items[i].Key < items[j].Key
})
fmt.Println(items)
// Output:
// [{1 foo} {2 baz} {3 bar}]
}
func ExampleDict_Values() {
d := Dict[int, string]{
1: "foo",
2: "bar",
3: "baz",
}
values := d.Values()
sort.Slice(values, func(i, j int) bool { return values[i] < values[j] })
fmt.Println(values)
// Output:
// [bar baz foo]
}