-
Notifications
You must be signed in to change notification settings - Fork 1
/
set_example_test.go
143 lines (111 loc) · 2.22 KB
/
set_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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package collections_test
import (
"fmt"
"github.com/go-generics/collections"
)
func ExampleNewSet() {
set := collections.NewSet(0, 1)
fmt.Println(set.Has(0))
fmt.Println(set.Has(1))
fmt.Println(!set.Has(2))
// Output:
// true
// true
// true
}
func ExampleSet_Insert() {
set := collections.NewSet[int]()
set.Insert(0)
set.Insert(1)
fmt.Println(set.Has(0))
fmt.Println(set.Has(1))
fmt.Println(!set.Has(2))
// Output:
// true
// true
// true
}
func ExampleSet_Delete() {
set := collections.NewSet(0, 1)
set.Delete(1)
fmt.Println(set.Has(0))
fmt.Println(!set.Has(1))
// Output:
// true
// true
}
func ExampleSet_Each() {
set := collections.NewSet(3, 4)
sum := 0
set.Each(func(_, item int) {
sum += item
})
fmt.Println(sum)
// Output:
// 7
}
func ExampleSet_EachUntil() {
set := collections.NewSet(3, 4)
sum := 0
set.EachUntil(func(_, item int) {
sum += item
}, func(_, item int) bool {
return true
})
fmt.Println(set.Has(sum))
// Output:
// true
}
func ExampleUnion() {
ab := collections.NewSet("A", "B")
bc := collections.NewSet("B", "C")
union := collections.Union(ab, bc)
fmt.Println(union.Has("A"))
fmt.Println(union.Has("B"))
fmt.Println(union.Has("C"))
fmt.Println(union.Len())
// Output:
// true
// true
// true
// 3
}
func ExampleDifference() {
ab := collections.NewSet("A", "B")
bc := collections.NewSet("B", "C")
fmt.Println(collections.Difference(ab, bc))
fmt.Println(collections.Difference(bc, ab))
// Output:
// [A]
// [C]
}
func ExampleIntersection() {
ab := collections.NewSet("A", "B")
bc := collections.NewSet("B", "C")
intersection := collections.Intersection(ab, bc)
fmt.Println(intersection.String())
// Output
// [B]
}
func ExampleIsSubset() {
a := collections.NewSet(1.2)
b := collections.NewSet(1.2, 3.4)
fmt.Println(collections.IsSubset(a, a))
fmt.Println(collections.IsSubset(a, b))
fmt.Println(!collections.IsSubset(b, a))
// Output:
// true
// true
// true
}
func ExampleIsProperSubset() {
a := collections.NewSet(1.2)
b := collections.NewSet(1.2, 3.4)
fmt.Println(!collections.IsProperSubset(a, a))
fmt.Println(collections.IsProperSubset(a, b))
fmt.Println(!collections.IsProperSubset(b, a))
// Output:
// true
// true
// true
}