xref: /aosp_15_r20/external/licenseclassifier/internal/sets/stringset_test.go (revision 46c4c49da23cae783fa41bf46525a6505638499a)
1*46c4c49dSIbrahim Kanouche// Copyright 2017 Google Inc.
2*46c4c49dSIbrahim Kanouche//
3*46c4c49dSIbrahim Kanouche// Licensed under the Apache License, Version 2.0 (the "License");
4*46c4c49dSIbrahim Kanouche// you may not use this file except in compliance with the License.
5*46c4c49dSIbrahim Kanouche// You may obtain a copy of the License at
6*46c4c49dSIbrahim Kanouche//
7*46c4c49dSIbrahim Kanouche//	http://www.apache.org/licenses/LICENSE-2.0
8*46c4c49dSIbrahim Kanouche//
9*46c4c49dSIbrahim Kanouche// Unless required by applicable law or agreed to in writing, software
10*46c4c49dSIbrahim Kanouche// distributed under the License is distributed on an "AS IS" BASIS,
11*46c4c49dSIbrahim Kanouche// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*46c4c49dSIbrahim Kanouche// See the License for the specific language governing permissions and
13*46c4c49dSIbrahim Kanouche// limitations under the License.
14*46c4c49dSIbrahim Kanouchepackage sets
15*46c4c49dSIbrahim Kanouche
16*46c4c49dSIbrahim Kanoucheimport (
17*46c4c49dSIbrahim Kanouche	"sort"
18*46c4c49dSIbrahim Kanouche	"testing"
19*46c4c49dSIbrahim Kanouche)
20*46c4c49dSIbrahim Kanouche
21*46c4c49dSIbrahim Kanouchefunc checkSameStringSet(t *testing.T, set *StringSet, unique []string) {
22*46c4c49dSIbrahim Kanouche	// Check that lengths are the same.
23*46c4c49dSIbrahim Kanouche	want := len(unique)
24*46c4c49dSIbrahim Kanouche	got := set.Len()
25*46c4c49dSIbrahim Kanouche
26*46c4c49dSIbrahim Kanouche	if got != want {
27*46c4c49dSIbrahim Kanouche		t.Errorf("NewStringSet(%v) want length %v, got %v", unique, want, got)
28*46c4c49dSIbrahim Kanouche	}
29*46c4c49dSIbrahim Kanouche
30*46c4c49dSIbrahim Kanouche	// Check that all strings are present in set.
31*46c4c49dSIbrahim Kanouche	for _, s := range unique {
32*46c4c49dSIbrahim Kanouche		want := true
33*46c4c49dSIbrahim Kanouche		got := set.Contains(s)
34*46c4c49dSIbrahim Kanouche
35*46c4c49dSIbrahim Kanouche		if got != want {
36*46c4c49dSIbrahim Kanouche			t.Errorf("Contains(%v) want %v, got %v", s, want, got)
37*46c4c49dSIbrahim Kanouche		}
38*46c4c49dSIbrahim Kanouche	}
39*46c4c49dSIbrahim Kanouche
40*46c4c49dSIbrahim Kanouche	// Check that all elements are present in strings.
41*46c4c49dSIbrahim Kanouche	sort.Strings(unique)
42*46c4c49dSIbrahim Kanouche
43*46c4c49dSIbrahim Kanouche	for i, got := range set.Sorted() {
44*46c4c49dSIbrahim Kanouche		want := unique[i]
45*46c4c49dSIbrahim Kanouche
46*46c4c49dSIbrahim Kanouche		if got != want {
47*46c4c49dSIbrahim Kanouche			t.Errorf("Sorted(%d) want %v, got %v", i, want, got)
48*46c4c49dSIbrahim Kanouche		}
49*46c4c49dSIbrahim Kanouche	}
50*46c4c49dSIbrahim Kanouche}
51*46c4c49dSIbrahim Kanouche
52*46c4c49dSIbrahim Kanouchefunc TestNewStringSet(t *testing.T) {
53*46c4c49dSIbrahim Kanouche	empty := NewStringSet()
54*46c4c49dSIbrahim Kanouche	want := 0
55*46c4c49dSIbrahim Kanouche	got := empty.Len()
56*46c4c49dSIbrahim Kanouche
57*46c4c49dSIbrahim Kanouche	if got != want {
58*46c4c49dSIbrahim Kanouche		t.Errorf("NewStringSet() want length %v, got %v", want, got)
59*46c4c49dSIbrahim Kanouche	}
60*46c4c49dSIbrahim Kanouche
61*46c4c49dSIbrahim Kanouche	unique := []string{"a", "b", "c"}
62*46c4c49dSIbrahim Kanouche	set := NewStringSet(unique...)
63*46c4c49dSIbrahim Kanouche	checkSameStringSet(t, set, unique)
64*46c4c49dSIbrahim Kanouche
65*46c4c49dSIbrahim Kanouche	// Append an already-present element.
66*46c4c49dSIbrahim Kanouche	nonUnique := append(unique, unique[0])
67*46c4c49dSIbrahim Kanouche	set = NewStringSet(nonUnique...)
68*46c4c49dSIbrahim Kanouche
69*46c4c49dSIbrahim Kanouche	// Non-unique unique should collapse to one.
70*46c4c49dSIbrahim Kanouche	want = len(unique)
71*46c4c49dSIbrahim Kanouche	got = set.Len()
72*46c4c49dSIbrahim Kanouche
73*46c4c49dSIbrahim Kanouche	if got != want {
74*46c4c49dSIbrahim Kanouche		t.Errorf("NewStringSet(%v) want length %v, got %v", nonUnique, want, got)
75*46c4c49dSIbrahim Kanouche	}
76*46c4c49dSIbrahim Kanouche}
77*46c4c49dSIbrahim Kanouche
78*46c4c49dSIbrahim Kanouchefunc TestStringSet_Copy(t *testing.T) {
79*46c4c49dSIbrahim Kanouche	// Check both copies represent the same set.
80*46c4c49dSIbrahim Kanouche	base := []string{"a", "b", "c"}
81*46c4c49dSIbrahim Kanouche	orig := NewStringSet(base...)
82*46c4c49dSIbrahim Kanouche	cpy := orig.Copy()
83*46c4c49dSIbrahim Kanouche	checkSameStringSet(t, orig, base)
84*46c4c49dSIbrahim Kanouche	checkSameStringSet(t, cpy, base)
85*46c4c49dSIbrahim Kanouche
86*46c4c49dSIbrahim Kanouche	// Check the two copies are independent.
87*46c4c49dSIbrahim Kanouche	more := []string{"d"}
88*46c4c49dSIbrahim Kanouche	orig.Insert(more...)
89*46c4c49dSIbrahim Kanouche	more = append(base, more...)
90*46c4c49dSIbrahim Kanouche	checkSameStringSet(t, orig, more)
91*46c4c49dSIbrahim Kanouche	checkSameStringSet(t, cpy, base)
92*46c4c49dSIbrahim Kanouche}
93*46c4c49dSIbrahim Kanouche
94*46c4c49dSIbrahim Kanouchefunc TestStringSet_Insert(t *testing.T) {
95*46c4c49dSIbrahim Kanouche	unique := []string{"a", "b", "c"}
96*46c4c49dSIbrahim Kanouche	set := NewStringSet(unique...)
97*46c4c49dSIbrahim Kanouche
98*46c4c49dSIbrahim Kanouche	// Insert existing element, which should basically be a no-op.
99*46c4c49dSIbrahim Kanouche	set.Insert(unique[0])
100*46c4c49dSIbrahim Kanouche	checkSameStringSet(t, set, unique)
101*46c4c49dSIbrahim Kanouche
102*46c4c49dSIbrahim Kanouche	// Actually insert new unique elements.
103*46c4c49dSIbrahim Kanouche	additional := []string{"d", "e"}
104*46c4c49dSIbrahim Kanouche	longer := append(unique, additional...)
105*46c4c49dSIbrahim Kanouche	set.Insert(additional...)
106*46c4c49dSIbrahim Kanouche	checkSameStringSet(t, set, longer)
107*46c4c49dSIbrahim Kanouche}
108*46c4c49dSIbrahim Kanouche
109*46c4c49dSIbrahim Kanouchefunc TestStringSet_Delete(t *testing.T) {
110*46c4c49dSIbrahim Kanouche	unique := []string{"a", "b", "c"}
111*46c4c49dSIbrahim Kanouche	set := NewStringSet(unique...)
112*46c4c49dSIbrahim Kanouche
113*46c4c49dSIbrahim Kanouche	// Delete non-existent element, which should basically be a no-op.
114*46c4c49dSIbrahim Kanouche	set.Delete("z")
115*46c4c49dSIbrahim Kanouche	checkSameStringSet(t, set, unique)
116*46c4c49dSIbrahim Kanouche
117*46c4c49dSIbrahim Kanouche	// Actually delete existing elements.
118*46c4c49dSIbrahim Kanouche	set.Delete(unique[1:]...)
119*46c4c49dSIbrahim Kanouche	checkSameStringSet(t, set, unique[:1])
120*46c4c49dSIbrahim Kanouche}
121*46c4c49dSIbrahim Kanouche
122*46c4c49dSIbrahim Kanouchefunc TestStringSet_Intersect(t *testing.T) {
123*46c4c49dSIbrahim Kanouche	input1 := []string{"a", "c", "d", "e", "f"}
124*46c4c49dSIbrahim Kanouche	input2 := []string{"b", "c", "e"}
125*46c4c49dSIbrahim Kanouche
126*46c4c49dSIbrahim Kanouche	// Check Intersect(nil) returns an empty set.
127*46c4c49dSIbrahim Kanouche	setA := NewStringSet(input1...)
128*46c4c49dSIbrahim Kanouche	got := setA.Intersect(nil)
129*46c4c49dSIbrahim Kanouche	checkSameStringSet(t, got, []string{})
130*46c4c49dSIbrahim Kanouche	// Check that the receiver is unchanged.
131*46c4c49dSIbrahim Kanouche	checkSameStringSet(t, setA, input1)
132*46c4c49dSIbrahim Kanouche
133*46c4c49dSIbrahim Kanouche	// Check Intersect returns the correct result.
134*46c4c49dSIbrahim Kanouche	setB := NewStringSet(input2...)
135*46c4c49dSIbrahim Kanouche	got = setA.Intersect(setB)
136*46c4c49dSIbrahim Kanouche	want := []string{"c", "e"}
137*46c4c49dSIbrahim Kanouche	checkSameStringSet(t, got, want)
138*46c4c49dSIbrahim Kanouche	// Also check the sources are unchanged.
139*46c4c49dSIbrahim Kanouche	checkSameStringSet(t, setA, input1)
140*46c4c49dSIbrahim Kanouche	checkSameStringSet(t, setB, input2)
141*46c4c49dSIbrahim Kanouche
142*46c4c49dSIbrahim Kanouche	// Reverse the inputs and verify Intersect produces the same results.
143*46c4c49dSIbrahim Kanouche	setA = NewStringSet(input2...)
144*46c4c49dSIbrahim Kanouche	setB = NewStringSet(input1...)
145*46c4c49dSIbrahim Kanouche	got = setA.Intersect(setB)
146*46c4c49dSIbrahim Kanouche	checkSameStringSet(t, got, want)
147*46c4c49dSIbrahim Kanouche	// Check the sources are again unchanged.
148*46c4c49dSIbrahim Kanouche	checkSameStringSet(t, setA, input2)
149*46c4c49dSIbrahim Kanouche	checkSameStringSet(t, setB, input1)
150*46c4c49dSIbrahim Kanouche}
151*46c4c49dSIbrahim Kanouche
152*46c4c49dSIbrahim Kanouchefunc TestStringSet_Disjoint(t *testing.T) {
153*46c4c49dSIbrahim Kanouche	input1 := []string{"a", "c", "d", "e", "f"}
154*46c4c49dSIbrahim Kanouche	input2 := []string{"b", "c", "e"}
155*46c4c49dSIbrahim Kanouche	input3 := []string{"x", "y", "z"}
156*46c4c49dSIbrahim Kanouche
157*46c4c49dSIbrahim Kanouche	// Check that sets are always disjoint with the empty set or nil
158*46c4c49dSIbrahim Kanouche	setA := NewStringSet(input1...)
159*46c4c49dSIbrahim Kanouche	emptySet := NewStringSet()
160*46c4c49dSIbrahim Kanouche
161*46c4c49dSIbrahim Kanouche	if disjoint := setA.Disjoint(nil); !disjoint {
162*46c4c49dSIbrahim Kanouche		t.Errorf("Disjoint(%s, %v) want %v, got %v", setA, nil, true, disjoint)
163*46c4c49dSIbrahim Kanouche	}
164*46c4c49dSIbrahim Kanouche
165*46c4c49dSIbrahim Kanouche	if disjoint := setA.Disjoint(emptySet); !disjoint {
166*46c4c49dSIbrahim Kanouche		t.Errorf("Disjoint(%s, %s) want %v, got %v", setA, emptySet, true, disjoint)
167*46c4c49dSIbrahim Kanouche	}
168*46c4c49dSIbrahim Kanouche
169*46c4c49dSIbrahim Kanouche	if disjoint := emptySet.Disjoint(setA); !disjoint {
170*46c4c49dSIbrahim Kanouche		t.Errorf("Disjoint(%s, %s) want %v, got %v", emptySet, setA, true, disjoint)
171*46c4c49dSIbrahim Kanouche	}
172*46c4c49dSIbrahim Kanouche
173*46c4c49dSIbrahim Kanouche	if disjoint := emptySet.Disjoint(emptySet); !disjoint {
174*46c4c49dSIbrahim Kanouche		t.Errorf("Disjoint(%s, %s) want %v, got %v", emptySet, emptySet, true, disjoint)
175*46c4c49dSIbrahim Kanouche	}
176*46c4c49dSIbrahim Kanouche
177*46c4c49dSIbrahim Kanouche	// Also check the sources are unchanged.
178*46c4c49dSIbrahim Kanouche	checkSameStringSet(t, setA, input1)
179*46c4c49dSIbrahim Kanouche	checkSameStringSet(t, emptySet, []string{})
180*46c4c49dSIbrahim Kanouche
181*46c4c49dSIbrahim Kanouche	// Check two non-empty, non-nil disjoint sets.
182*46c4c49dSIbrahim Kanouche	setC := NewStringSet(input3...)
183*46c4c49dSIbrahim Kanouche
184*46c4c49dSIbrahim Kanouche	if disjoint := setA.Disjoint(setC); !disjoint {
185*46c4c49dSIbrahim Kanouche		t.Errorf("Disjoint(%s, %s) want %v, got %v", setA, setC, true, disjoint)
186*46c4c49dSIbrahim Kanouche	}
187*46c4c49dSIbrahim Kanouche
188*46c4c49dSIbrahim Kanouche	// Also check the sources are unchanged.
189*46c4c49dSIbrahim Kanouche	checkSameStringSet(t, setA, input1)
190*46c4c49dSIbrahim Kanouche	checkSameStringSet(t, setC, input3)
191*46c4c49dSIbrahim Kanouche
192*46c4c49dSIbrahim Kanouche	// Check that two intersecting sets are not Disjoint.
193*46c4c49dSIbrahim Kanouche	setB := NewStringSet(input2...)
194*46c4c49dSIbrahim Kanouche
195*46c4c49dSIbrahim Kanouche	if disjoint := setA.Disjoint(setB); disjoint {
196*46c4c49dSIbrahim Kanouche		t.Errorf("Disjoint(%s, %s) want %v, got %v", setA, setB, false, disjoint)
197*46c4c49dSIbrahim Kanouche	}
198*46c4c49dSIbrahim Kanouche
199*46c4c49dSIbrahim Kanouche	// Also check the sources are unchanged.
200*46c4c49dSIbrahim Kanouche	checkSameStringSet(t, setA, input1)
201*46c4c49dSIbrahim Kanouche	checkSameStringSet(t, setB, input2)
202*46c4c49dSIbrahim Kanouche}
203*46c4c49dSIbrahim Kanouche
204*46c4c49dSIbrahim Kanouchefunc TestStringSet_Difference(t *testing.T) {
205*46c4c49dSIbrahim Kanouche	input1 := []string{"a", "c", "d", "e", "f"}
206*46c4c49dSIbrahim Kanouche	input2 := []string{"b", "c", "e"}
207*46c4c49dSIbrahim Kanouche	input3 := []string{"x", "y", "z"}
208*46c4c49dSIbrahim Kanouche
209*46c4c49dSIbrahim Kanouche	// Check Difference(nil) returns a copy of the receiver.
210*46c4c49dSIbrahim Kanouche	setA := NewStringSet(input1...)
211*46c4c49dSIbrahim Kanouche	got := setA.Difference(nil)
212*46c4c49dSIbrahim Kanouche	checkSameStringSet(t, got, input1)
213*46c4c49dSIbrahim Kanouche	// Check that the receiver is unchanged.
214*46c4c49dSIbrahim Kanouche	checkSameStringSet(t, setA, input1)
215*46c4c49dSIbrahim Kanouche
216*46c4c49dSIbrahim Kanouche	// Check A - A returns the empty set.
217*46c4c49dSIbrahim Kanouche	got = setA.Difference(setA)
218*46c4c49dSIbrahim Kanouche
219*46c4c49dSIbrahim Kanouche	if !got.Empty() {
220*46c4c49dSIbrahim Kanouche		t.Errorf("Difference(%s, %s).Empty() want %v, got %v",
221*46c4c49dSIbrahim Kanouche			setA, setA, true, false)
222*46c4c49dSIbrahim Kanouche	}
223*46c4c49dSIbrahim Kanouche
224*46c4c49dSIbrahim Kanouche	checkSameStringSet(t, got, []string{})
225*46c4c49dSIbrahim Kanouche	// Check that the receiver is unchanged.
226*46c4c49dSIbrahim Kanouche	checkSameStringSet(t, setA, input1)
227*46c4c49dSIbrahim Kanouche
228*46c4c49dSIbrahim Kanouche	// Check A - C simply returns elements in A if A and C are disjoint.
229*46c4c49dSIbrahim Kanouche	setC := NewStringSet(input3...)
230*46c4c49dSIbrahim Kanouche	got = setA.Difference(setC)
231*46c4c49dSIbrahim Kanouche	checkSameStringSet(t, got, input1)
232*46c4c49dSIbrahim Kanouche	// Also check the sources are unchanged.
233*46c4c49dSIbrahim Kanouche	checkSameStringSet(t, setA, input1)
234*46c4c49dSIbrahim Kanouche	checkSameStringSet(t, setC, input3)
235*46c4c49dSIbrahim Kanouche
236*46c4c49dSIbrahim Kanouche	// Check A - B returns elements in A not in B.
237*46c4c49dSIbrahim Kanouche	setB := NewStringSet(input2...)
238*46c4c49dSIbrahim Kanouche	got = setA.Difference(setB)
239*46c4c49dSIbrahim Kanouche	want := []string{"a", "d", "f"}
240*46c4c49dSIbrahim Kanouche	checkSameStringSet(t, got, want)
241*46c4c49dSIbrahim Kanouche
242*46c4c49dSIbrahim Kanouche	// Also check the sources are unchanged.
243*46c4c49dSIbrahim Kanouche	checkSameStringSet(t, setA, input1)
244*46c4c49dSIbrahim Kanouche	checkSameStringSet(t, setB, input2)
245*46c4c49dSIbrahim Kanouche
246*46c4c49dSIbrahim Kanouche	// Check B - A returns elements in B not in A.
247*46c4c49dSIbrahim Kanouche	got = setB.Difference(setA)
248*46c4c49dSIbrahim Kanouche	want = []string{"b"}
249*46c4c49dSIbrahim Kanouche	checkSameStringSet(t, got, want)
250*46c4c49dSIbrahim Kanouche	// Also check the sources are unchanged.
251*46c4c49dSIbrahim Kanouche	checkSameStringSet(t, setA, input1)
252*46c4c49dSIbrahim Kanouche	checkSameStringSet(t, setB, input2)
253*46c4c49dSIbrahim Kanouche}
254*46c4c49dSIbrahim Kanouche
255*46c4c49dSIbrahim Kanouchefunc TestStringSet_Unique(t *testing.T) {
256*46c4c49dSIbrahim Kanouche	input1 := []string{"a", "c", "d", "e", "f"}
257*46c4c49dSIbrahim Kanouche	input2 := []string{"b", "c", "e"}
258*46c4c49dSIbrahim Kanouche	input3 := []string{"x", "y", "z"}
259*46c4c49dSIbrahim Kanouche
260*46c4c49dSIbrahim Kanouche	// Check Unique(nil) returns a copy of the receiver.
261*46c4c49dSIbrahim Kanouche	setA := NewStringSet(input1...)
262*46c4c49dSIbrahim Kanouche	got := setA.Unique(nil)
263*46c4c49dSIbrahim Kanouche	checkSameStringSet(t, got, input1)
264*46c4c49dSIbrahim Kanouche	// Check that the receiver is unchanged.
265*46c4c49dSIbrahim Kanouche	checkSameStringSet(t, setA, input1)
266*46c4c49dSIbrahim Kanouche
267*46c4c49dSIbrahim Kanouche	// Check Unique returns only elements in A and B not in both A and B.
268*46c4c49dSIbrahim Kanouche	setB := NewStringSet(input2...)
269*46c4c49dSIbrahim Kanouche	got = setA.Unique(setB)
270*46c4c49dSIbrahim Kanouche	want := []string{"a", "b", "d", "f"}
271*46c4c49dSIbrahim Kanouche	checkSameStringSet(t, got, want)
272*46c4c49dSIbrahim Kanouche	// Also check the sources are unchanged.
273*46c4c49dSIbrahim Kanouche	checkSameStringSet(t, setA, input1)
274*46c4c49dSIbrahim Kanouche	checkSameStringSet(t, setB, input2)
275*46c4c49dSIbrahim Kanouche
276*46c4c49dSIbrahim Kanouche	// Check Unique of two disjoint sets is the Union of those sets.
277*46c4c49dSIbrahim Kanouche	setC := NewStringSet(input3...)
278*46c4c49dSIbrahim Kanouche	got = setA.Unique(setC)
279*46c4c49dSIbrahim Kanouche	union := setA.Union(setC)
280*46c4c49dSIbrahim Kanouche
281*46c4c49dSIbrahim Kanouche	if equal := union.Equal(got); !equal {
282*46c4c49dSIbrahim Kanouche		t.Errorf("Union of disjoint Equal(%s, %s) want %v, got %v",
283*46c4c49dSIbrahim Kanouche			union, got, true, equal)
284*46c4c49dSIbrahim Kanouche	}
285*46c4c49dSIbrahim Kanouche
286*46c4c49dSIbrahim Kanouche	// Check Unique is the Union of A - B and B - A.
287*46c4c49dSIbrahim Kanouche	aNotInB := setA.Difference(setB)
288*46c4c49dSIbrahim Kanouche	bNotInA := setB.Difference(setA)
289*46c4c49dSIbrahim Kanouche	union = aNotInB.Union(bNotInA)
290*46c4c49dSIbrahim Kanouche	want = []string{"a", "b", "d", "f"}
291*46c4c49dSIbrahim Kanouche	checkSameStringSet(t, union, want)
292*46c4c49dSIbrahim Kanouche	got = setA.Unique(setB)
293*46c4c49dSIbrahim Kanouche
294*46c4c49dSIbrahim Kanouche	if equal := union.Equal(got); !equal {
295*46c4c49dSIbrahim Kanouche		t.Errorf("Union of differences Equal(%s, %s) want %v, got %v",
296*46c4c49dSIbrahim Kanouche			union, got, true, equal)
297*46c4c49dSIbrahim Kanouche	}
298*46c4c49dSIbrahim Kanouche
299*46c4c49dSIbrahim Kanouche	// Also check the sources are unchanged.
300*46c4c49dSIbrahim Kanouche	checkSameStringSet(t, setA, input1)
301*46c4c49dSIbrahim Kanouche	checkSameStringSet(t, setB, input2)
302*46c4c49dSIbrahim Kanouche}
303*46c4c49dSIbrahim Kanouche
304*46c4c49dSIbrahim Kanouchefunc TestStringSet_Equal(t *testing.T) {
305*46c4c49dSIbrahim Kanouche	input1 := []string{"a", "c", "d", "e", "f"}
306*46c4c49dSIbrahim Kanouche	input2 := []string{"b", "c", "e"}
307*46c4c49dSIbrahim Kanouche	input3 := []string{"a", "c", "d", "e", "g"}
308*46c4c49dSIbrahim Kanouche
309*46c4c49dSIbrahim Kanouche	// Check Equal(nil) returns false.
310*46c4c49dSIbrahim Kanouche	setA := NewStringSet(input1...)
311*46c4c49dSIbrahim Kanouche
312*46c4c49dSIbrahim Kanouche	if equal := setA.Equal(nil); equal {
313*46c4c49dSIbrahim Kanouche		t.Errorf("Equal(%s, %v) want %v, got %v", setA, nil, false, true)
314*46c4c49dSIbrahim Kanouche	}
315*46c4c49dSIbrahim Kanouche
316*46c4c49dSIbrahim Kanouche	// Check that the receiver is unchanged.
317*46c4c49dSIbrahim Kanouche	checkSameStringSet(t, setA, input1)
318*46c4c49dSIbrahim Kanouche
319*46c4c49dSIbrahim Kanouche	// Check Equal returns true for a set and itself.
320*46c4c49dSIbrahim Kanouche	if equal := setA.Equal(setA); !equal {
321*46c4c49dSIbrahim Kanouche		t.Errorf("Equal(%s, %s) want %v, got %v", setA, setA, true, false)
322*46c4c49dSIbrahim Kanouche	}
323*46c4c49dSIbrahim Kanouche
324*46c4c49dSIbrahim Kanouche	// Check that the receiver is unchanged.
325*46c4c49dSIbrahim Kanouche	checkSameStringSet(t, setA, input1)
326*46c4c49dSIbrahim Kanouche
327*46c4c49dSIbrahim Kanouche	// Check Equal returns false for sets of non-equal length.
328*46c4c49dSIbrahim Kanouche	setB := NewStringSet(input2...)
329*46c4c49dSIbrahim Kanouche
330*46c4c49dSIbrahim Kanouche	if equal := setA.Equal(setB); equal {
331*46c4c49dSIbrahim Kanouche		t.Errorf("Equal(%s, %s) want %v, got %v", setA, setB, false, true)
332*46c4c49dSIbrahim Kanouche	}
333*46c4c49dSIbrahim Kanouche
334*46c4c49dSIbrahim Kanouche	// Also check the sources are unchanged.
335*46c4c49dSIbrahim Kanouche	checkSameStringSet(t, setA, input1)
336*46c4c49dSIbrahim Kanouche	checkSameStringSet(t, setB, input2)
337*46c4c49dSIbrahim Kanouche
338*46c4c49dSIbrahim Kanouche	// Check Equal returns false for equal-length sets with different elements.
339*46c4c49dSIbrahim Kanouche	setC := NewStringSet(input3...)
340*46c4c49dSIbrahim Kanouche
341*46c4c49dSIbrahim Kanouche	if equal := setA.Equal(setC); equal {
342*46c4c49dSIbrahim Kanouche		t.Errorf("Equal(%s, %s) want %v, got %v", setA, setC, false, true)
343*46c4c49dSIbrahim Kanouche	}
344*46c4c49dSIbrahim Kanouche
345*46c4c49dSIbrahim Kanouche	if equal := setC.Equal(setA); equal {
346*46c4c49dSIbrahim Kanouche		t.Errorf("Equal(%s, %s) want %v, got %v", setC, setA, false, true)
347*46c4c49dSIbrahim Kanouche	}
348*46c4c49dSIbrahim Kanouche
349*46c4c49dSIbrahim Kanouche	// Also check the sources are unchanged.
350*46c4c49dSIbrahim Kanouche	checkSameStringSet(t, setA, input1)
351*46c4c49dSIbrahim Kanouche	checkSameStringSet(t, setC, input3)
352*46c4c49dSIbrahim Kanouche
353*46c4c49dSIbrahim Kanouche	// Check Equal returns true for a set with itself.
354*46c4c49dSIbrahim Kanouche	if equal := setA.Equal(setA); !equal {
355*46c4c49dSIbrahim Kanouche		t.Errorf("Equal(%s, %s) want %v, got %v", setA, setA, true, false)
356*46c4c49dSIbrahim Kanouche	}
357*46c4c49dSIbrahim Kanouche
358*46c4c49dSIbrahim Kanouche	// Also check the source is unchanged.
359*46c4c49dSIbrahim Kanouche	checkSameStringSet(t, setA, input1)
360*46c4c49dSIbrahim Kanouche
361*46c4c49dSIbrahim Kanouche	// Check Equal returns true for two separate equal sets.
362*46c4c49dSIbrahim Kanouche	anotherA := NewStringSet(input1...)
363*46c4c49dSIbrahim Kanouche
364*46c4c49dSIbrahim Kanouche	if equal := setA.Equal(anotherA); !equal {
365*46c4c49dSIbrahim Kanouche		t.Errorf("Equal(%s, %s) want %v, got %v", setA, anotherA, true, false)
366*46c4c49dSIbrahim Kanouche	}
367*46c4c49dSIbrahim Kanouche
368*46c4c49dSIbrahim Kanouche	// Also check the sources are unchanged.
369*46c4c49dSIbrahim Kanouche	checkSameStringSet(t, setA, input1)
370*46c4c49dSIbrahim Kanouche	checkSameStringSet(t, anotherA, input1)
371*46c4c49dSIbrahim Kanouche
372*46c4c49dSIbrahim Kanouche	// Check for equality comparing to nil struct.
373*46c4c49dSIbrahim Kanouche	var nilSet *StringSet
374*46c4c49dSIbrahim Kanouche	if equal := nilSet.Equal(setA); equal {
375*46c4c49dSIbrahim Kanouche		t.Errorf("Equal(%s, %s) want %v, got %v", nilSet, setA, false, true)
376*46c4c49dSIbrahim Kanouche	}
377*46c4c49dSIbrahim Kanouche	if equal := setA.Equal(nilSet); equal {
378*46c4c49dSIbrahim Kanouche		t.Errorf("Equal(%s, %s) want %v, got %v", setA, nilSet, false, true)
379*46c4c49dSIbrahim Kanouche	}
380*46c4c49dSIbrahim Kanouche	if equal := nilSet.Equal(nilSet); !equal {
381*46c4c49dSIbrahim Kanouche		t.Errorf("Equal(%s, %s) want %v, got %v", nilSet, nilSet, true, false)
382*46c4c49dSIbrahim Kanouche	}
383*46c4c49dSIbrahim Kanouche
384*46c4c49dSIbrahim Kanouche	// Edge case: consider the empty set to be different than the nil set.
385*46c4c49dSIbrahim Kanouche	emptySet := NewStringSet()
386*46c4c49dSIbrahim Kanouche	if equal := nilSet.Equal(emptySet); equal {
387*46c4c49dSIbrahim Kanouche		t.Errorf("Equal(%s, %s) want %v, got %v", nilSet, emptySet, false, true)
388*46c4c49dSIbrahim Kanouche	}
389*46c4c49dSIbrahim Kanouche	if equal := emptySet.Equal(nilSet); equal {
390*46c4c49dSIbrahim Kanouche		t.Errorf("Equal(%s, %s) want %v, got %v", emptySet, nilSet, false, true)
391*46c4c49dSIbrahim Kanouche	}
392*46c4c49dSIbrahim Kanouche	if equal := emptySet.Equal(emptySet); !equal {
393*46c4c49dSIbrahim Kanouche		t.Errorf("Equal(%s, %s) want %v, got %v", emptySet, emptySet, true, false)
394*46c4c49dSIbrahim Kanouche	}
395*46c4c49dSIbrahim Kanouche}
396*46c4c49dSIbrahim Kanouche
397*46c4c49dSIbrahim Kanouchefunc TestStringSet_Union(t *testing.T) {
398*46c4c49dSIbrahim Kanouche	input1 := []string{"a", "c", "d", "e", "f"}
399*46c4c49dSIbrahim Kanouche	input2 := []string{"b", "c", "e"}
400*46c4c49dSIbrahim Kanouche
401*46c4c49dSIbrahim Kanouche	// Check Union(nil) returns a copy of the receiver.
402*46c4c49dSIbrahim Kanouche	setA := NewStringSet(input1...)
403*46c4c49dSIbrahim Kanouche	got := setA.Union(nil)
404*46c4c49dSIbrahim Kanouche	checkSameStringSet(t, got, input1)
405*46c4c49dSIbrahim Kanouche	// Check that the receiver is unchanged.
406*46c4c49dSIbrahim Kanouche	checkSameStringSet(t, setA, input1)
407*46c4c49dSIbrahim Kanouche
408*46c4c49dSIbrahim Kanouche	// Check Union returns the correct result.
409*46c4c49dSIbrahim Kanouche	setB := NewStringSet(input2...)
410*46c4c49dSIbrahim Kanouche	got = setA.Union(setB)
411*46c4c49dSIbrahim Kanouche	want := []string{"a", "b", "c", "d", "e", "f"}
412*46c4c49dSIbrahim Kanouche	checkSameStringSet(t, got, want)
413*46c4c49dSIbrahim Kanouche	// Also check the sources are unchanged.
414*46c4c49dSIbrahim Kanouche	checkSameStringSet(t, setA, input1)
415*46c4c49dSIbrahim Kanouche	checkSameStringSet(t, setB, input2)
416*46c4c49dSIbrahim Kanouche
417*46c4c49dSIbrahim Kanouche	// Reverse the inputs and verify Union produces the same results.
418*46c4c49dSIbrahim Kanouche	setA = NewStringSet(input2...)
419*46c4c49dSIbrahim Kanouche	setB = NewStringSet(input1...)
420*46c4c49dSIbrahim Kanouche	got = setA.Union(setB)
421*46c4c49dSIbrahim Kanouche	checkSameStringSet(t, got, want)
422*46c4c49dSIbrahim Kanouche	// Check the sources are again unchanged.
423*46c4c49dSIbrahim Kanouche	checkSameStringSet(t, setA, input2)
424*46c4c49dSIbrahim Kanouche	checkSameStringSet(t, setB, input1)
425*46c4c49dSIbrahim Kanouche}
426