Debian Codegraph @

examples/set-gen/sets/set_test.go

raw
1 /*
2 Copyright 2014 The Kubernetes Authors.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 package sets
18
19 import (
20 "reflect"
21 "testing"
22 )
23
24 func TestStringSet(t *testing.T) {
25 s := String{}
26 s2 := String{}
27 if len(s) != 0 {
28 t.Errorf("Expected len=0: %d", len(s))
29 }
30 s.Insert("a", "b")
31 if len(s) != 2 {
32 t.Errorf("Expected len=2: %d", len(s))
33 }
34 s.Insert("c")
35 if s.Has("d") {
36 t.Errorf("Unexpected contents: %#v", s)
37 }
38 if !s.Has("a") {
39 t.Errorf("Missing contents: %#v", s)
40 }
41 s.Delete("a")
42 if s.Has("a") {
43 t.Errorf("Unexpected contents: %#v", s)
44 }
45 s.Insert("a")
46 if s.HasAll("a", "b", "d") {
47 t.Errorf("Unexpected contents: %#v", s)
48 }
49 if !s.HasAll("a", "b") {
50 t.Errorf("Missing contents: %#v", s)
51 }
52 s2.Insert("a", "b", "d")
53 if s.IsSuperset(s2) {
54 t.Errorf("Unexpected contents: %#v", s)
55 }
56 s2.Delete("d")
57 if !s.IsSuperset(s2) {
58 t.Errorf("Missing contents: %#v", s)
59 }
60 }
61
62 func TestStringSetDeleteMultiples(t *testing.T) {
63 s := String{}
64 s.Insert("a", "b", "c")
65 if len(s) != 3 {
66 t.Errorf("Expected len=3: %d", len(s))
67 }
68
69 s.Delete("a", "c")
70 if len(s) != 1 {
71 t.Errorf("Expected len=1: %d", len(s))
72 }
73 if s.Has("a") {
74 t.Errorf("Unexpected contents: %#v", s)
75 }
76 if s.Has("c") {
77 t.Errorf("Unexpected contents: %#v", s)
78 }
79 if !s.Has("b") {
80 t.Errorf("Missing contents: %#v", s)
81 }
82
83 }
84
85 func TestNewStringSet(t *testing.T) {
86 s := NewString("a", "b", "c")
87 if len(s) != 3 {
88 t.Errorf("Expected len=3: %d", len(s))
89 }
90 if !s.Has("a") || !s.Has("b") || !s.Has("c") {
91 t.Errorf("Unexpected contents: %#v", s)
92 }
93 }
94
95 func TestStringSetList(t *testing.T) {
96 s := NewString("z", "y", "x", "a")
97 if !reflect.DeepEqual(s.List(), []string{"a", "x", "y", "z"}) {
98 t.Errorf("List gave unexpected result: %#v", s.List())
99 }
100 }
101
102 func TestStringSetDifference(t *testing.T) {
103 a := NewString("1", "2", "3")
104 b := NewString("1", "2", "4", "5")
105 c := a.Difference(b)
106 d := b.Difference(a)
107 if len(c) != 1 {
108 t.Errorf("Expected len=1: %d", len(c))
109 }
110 if !c.Has("3") {
111 t.Errorf("Unexpected contents: %#v", c.List())
112 }
113 if len(d) != 2 {
114 t.Errorf("Expected len=2: %d", len(d))
115 }
116 if !d.Has("4") || !d.Has("5") {
117 t.Errorf("Unexpected contents: %#v", d.List())
118 }
119 }
120
121 func TestStringSetSymmetricDifference(t *testing.T) {
122 a := NewString("1", "2", "3")
123 b := NewString("1", "2", "4", "5")
124 c := a.SymmetricDifference(b)
125 d := b.SymmetricDifference(a)
126 if !c.Equal(NewString("3", "4", "5")) {
127 t.Errorf("Unexpected contents: %#v", c.List())
128 }
129 if !d.Equal(NewString("3", "4", "5")) {
130 t.Errorf("Unexpected contents: %#v", d.List())
131 }
132 }
133
134 func TestStringSetHasAny(t *testing.T) {
135 a := NewString("1", "2", "3")
136
137 if !a.HasAny("1", "4") {
138 t.Errorf("expected true, got false")
139 }
140
141 if a.HasAny("0", "4") {
142 t.Errorf("expected false, got true")
143 }
144 }
145
146 func TestStringSetEquals(t *testing.T) {
147 // Simple case (order doesn't matter)
148 a := NewString("1", "2")
149 b := NewString("2", "1")
150 if !a.Equal(b) {
151 t.Errorf("Expected to be equal: %v vs %v", a, b)
152 }
153
154 // It is a set; duplicates are ignored
155 b = NewString("2", "2", "1")
156 if !a.Equal(b) {
157 t.Errorf("Expected to be equal: %v vs %v", a, b)
158 }
159
160 // Edge cases around empty sets / empty strings
161 a = NewString()
162 b = NewString()
163 if !a.Equal(b) {
164 t.Errorf("Expected to be equal: %v vs %v", a, b)
165 }
166
167 b = NewString("1", "2", "3")
168 if a.Equal(b) {
169 t.Errorf("Expected to be not-equal: %v vs %v", a, b)
170 }
171
172 b = NewString("1", "2", "")
173 if a.Equal(b) {
174 t.Errorf("Expected to be not-equal: %v vs %v", a, b)
175 }
176
177 // Check for equality after mutation
178 a = NewString()
179 a.Insert("1")
180 if a.Equal(b) {
181 t.Errorf("Expected to be not-equal: %v vs %v", a, b)
182 }
183
184 a.Insert("2")
185 if a.Equal(b) {
186 t.Errorf("Expected to be not-equal: %v vs %v", a, b)
187 }
188
189 a.Insert("")
190 if !a.Equal(b) {
191 t.Errorf("Expected to be equal: %v vs %v", a, b)
192 }
193
194 a.Delete("")
195 if a.Equal(b) {
196 t.Errorf("Expected to be not-equal: %v vs %v", a, b)
197 }
198 }
199
200 func TestStringUnion(t *testing.T) {
201 tests := []struct {
202 s1 String
203 s2 String
204 expected String
205 }{
206 {
207 NewString("1", "2", "3", "4"),
208 NewString("3", "4", "5", "6"),
209 NewString("1", "2", "3", "4", "5", "6"),
210 },
211 {
212 NewString("1", "2", "3", "4"),
213 NewString(),
214 NewString("1", "2", "3", "4"),
215 },
216 {
217 NewString(),
218 NewString("1", "2", "3", "4"),
219 NewString("1", "2", "3", "4"),
220 },
221 {
222 NewString(),
223 NewString(),
224 NewString(),
225 },
226 }
227
228 for _, test := range tests {
229 union := test.s1.Union(test.s2)
230 if union.Len() != test.expected.Len() {
231 t.Errorf("Expected union.Len()=%d but got %d", test.expected.Len(), union.Len())
232 }
233
234 if !union.Equal(test.expected) {
235 t.Errorf("Expected union.Equal(expected) but not true. union:%v expected:%v", union.List(), test.expected.List())
236 }
237 }
238 }
239
240 func TestStringIntersection(t *testing.T) {
241 tests := []struct {
242 s1 String
243 s2 String
244 expected String
245 }{
246 {
247 NewString("1", "2", "3", "4"),
248 NewString("3", "4", "5", "6"),
249 NewString("3", "4"),
250 },
251 {
252 NewString("1", "2", "3", "4"),
253 NewString("1", "2", "3", "4"),
254 NewString("1", "2", "3", "4"),
255 },
256 {
257 NewString("1", "2", "3", "4"),
258 NewString(),
259 NewString(),
260 },
261 {
262 NewString(),
263 NewString("1", "2", "3", "4"),
264 NewString(),
265 },
266 {
267 NewString(),
268 NewString(),
269 NewString(),
270 },
271 }
272
273 for _, test := range tests {
274 intersection := test.s1.Intersection(test.s2)
275 if intersection.Len() != test.expected.Len() {
276 t.Errorf("Expected intersection.Len()=%d but got %d", test.expected.Len(), intersection.Len())
277 }
278
279 if !intersection.Equal(test.expected) {
280 t.Errorf("Expected intersection.Equal(expected) but not true. intersection:%v expected:%v", intersection.List(), test.expected.List())
281 }
282 }
283 }