-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstringset_test.go
More file actions
271 lines (218 loc) · 5.3 KB
/
stringset_test.go
File metadata and controls
271 lines (218 loc) · 5.3 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package stringset_test
import (
"fmt"
"reflect"
"sort"
"testing"
"github.com/axamon/stringset"
)
var tt = stringset.NewStringSet()
func ExampleNew() {
s := stringset.New()
fmt.Println(reflect.TypeOf(s))
// Output:
// *stringset.StringSet
}
func ExampleNewStringSet() {
s := stringset.NewStringSet("pippo", "pluto", "minnie")
slice := s.Strings()
sort.Strings(slice)
for _, element := range slice {
fmt.Println(element)
}
// Output:
// minnie
// pippo
// pluto
}
func ExampleStringSet_AddSlice() {
slice := []string{"pluto", "paperino", "pluto"}
s := stringset.New()
s.AddSlice(slice)
fmt.Println(s.Len())
// Output:
// 2
}
func BenchmarkT(b *testing.B) {
var t = stringset.NewStringSet()
var t1 = stringset.NewStringSet()
for n := 0; n < b.N; n++ {
t.Add("pippo")
t1.Add("pluto")
t.Add("pluto")
t1.Add("paperino")
t.Len()
t.Union(t1)
t.Intersect(t1)
t.Exists("pippo")
t.Delete("pippo")
t.Pop()
}
}
func BenchmarkAdd(b *testing.B) {
for n := 0; n < b.N; n++ {
go tt.Add("pippo")
}
}
func BenchmarkDelete(b *testing.B) {
for n := 0; n < b.N; n++ {
go tt.Delete("pippo")
}
}
func BenchmarkIntersect(b *testing.B) {
testSet := stringset.NewStringSet("pippo", "pluto", "paperino", "pippo", "poldo", "minnie", "paperinik", "paperoga")
testSet2 := stringset.NewStringSet("paperino", "pluto", "nonna papera")
for n := 0; n < b.N; n++ {
testSet.Intersect(testSet2)
}
}
func BenchmarkUnion(b *testing.B) {
testSet := stringset.NewStringSet("pippo", "pluto", "paperino", "pippo", "poldo", "minnie", "paperinik", "paperoga")
testSet2 := stringset.NewStringSet("paperino", "pluto", "nonna papera")
for n := 0; n < b.N; n++ {
go testSet.Union(testSet2)
}
}
func ExampleStringSet_Delete() {
testSet := stringset.NewStringSet("pippo", "pluto", "paperino", "pippo")
testSet.Delete("pluto")
slice := testSet.Strings()
sort.Strings(slice)
for _, element := range slice {
fmt.Println(element)
}
// Output:
// paperino
// pippo
}
func ExampleStringSet_Add() {
testSet := stringset.NewStringSet("pippo", "pluto", "paperino", "pippo")
testSet.Add("pluto")
testSet.Add("nonna papera")
slice := testSet.Strings()
sort.Strings(slice)
for _, element := range slice {
fmt.Println(element)
}
// Output:
// nonna papera
// paperino
// pippo
// pluto
}
func ExampleStringSet_Exists() {
testSet := stringset.NewStringSet("pippo", "pluto", "paperino", "pippo")
element := "pippo"
if ok := testSet.Exists(element); ok {
fmt.Printf("%s exists", element)
}
// Output:
// pippo exists
}
func ExampleStringSet_Strings() {
testSet := stringset.NewStringSet("pippo", "pluto", "paperino", "pippo")
slice := testSet.Strings()
sort.Strings(slice)
for _, element := range slice {
fmt.Println(element)
}
// Output:
// paperino
// pippo
// pluto
}
func ExampleStringSet_Strings_second() {
testSet := stringset.NewStringSet()
for _, element := range testSet.Strings() {
fmt.Println(element)
}
// Output:
//
}
func ExampleStringSet_Contains() {
testSet := stringset.NewStringSet("pippo", "pluto", "paperino", "pippo")
testSet2 := stringset.NewStringSet("pippo", "pluto")
if ok := testSet.Contains(testSet2); ok {
fmt.Println("Yes")
}
if ok := testSet2.Contains(testSet); !ok {
fmt.Println("No")
}
// Output:
// Yes
// No
}
func ExampleStringSet_Union() {
testSet := stringset.NewStringSet("pippo", "pluto", "paperino", "pippo")
testSet2 := stringset.NewStringSet("pippo", "pluto", "minnie")
u := testSet.Union(testSet2)
slice := u.Strings()
for _, element := range slice {
fmt.Println(element)
}
// Unordered output:
// minnie
// paperino
// pippo
// pluto
}
func ExampleStringSet_Len() {
testSet := stringset.NewStringSet("pippo", "pluto", "paperino", "pippo")
testSet2 := stringset.NewStringSet("pippo", "pluto")
testSet3 := stringset.NewStringSet()
fmt.Println(testSet.Len())
fmt.Println(testSet2.Len())
fmt.Println(testSet3.Len())
// Output:
// 3
// 2
// 0
}
func ExampleStringSet_Pop() {
testSet := stringset.NewStringSet("pippo", "pluto", "paperino", "pippo")
num := testSet.Len()
for i := 0; i <= num; i++ { //testSet.Len() cannot be used in for loops
element, _ := testSet.Pop()
fmt.Println(element)
}
empty, ok := testSet.Pop()
fmt.Println(empty, ok)
// Unordered output:
// paperino
// pippo
// pluto
//
// false
}
func ExampleStringSet_Difference() {
testSet := stringset.NewStringSet("pippo", "pluto", "paperino", "pippo")
testSet2 := stringset.NewStringSet("paperino", "pluto")
diff := testSet.Difference(testSet2)
fmt.Println(diff.Strings()[0])
// Output:
// pippo
}
func ExampleStringSet_Intersect() {
testSet := stringset.NewStringSet("pippo", "pluto", "paperino", "pippo", "poldo", "minnie")
testSet2 := stringset.NewStringSet("paperino", "pluto", "nonna papera")
inersect := testSet.Intersect(testSet2)
list := inersect.Strings()
for _, element := range list {
fmt.Println(element)
}
// Unordered output:
// paperino
// pluto
}
func ExampleStringSet_Intersect_second() {
testSet := stringset.NewStringSet("paperino", "pluto", "nonna papera")
testSet2 := stringset.NewStringSet("pippo", "pluto", "paperino", "pippo", "poldo", "minnie")
inersect := testSet.Intersect(testSet2)
list := inersect.Strings()
for _, element := range list {
fmt.Println(element)
}
// Unordered output:
// paperino
// pluto
}