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 checkSameIntSet(t *testing.T, set *IntSet, unique []int) { 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("NewIntSet(%v) want length %v, got %v", unique, want, got) 28*46c4c49dSIbrahim Kanouche } 29*46c4c49dSIbrahim Kanouche 30*46c4c49dSIbrahim Kanouche // Check that all ints 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 ints. 41*46c4c49dSIbrahim Kanouche sort.Ints(unique) 42*46c4c49dSIbrahim Kanouche 43*46c4c49dSIbrahim Kanouche for i, got := range set.Sorted() { 44*46c4c49dSIbrahim Kanouche want := unique[i] 45*46c4c49dSIbrahim Kanouche if got != want { 46*46c4c49dSIbrahim Kanouche t.Errorf("Sorted(%d) want %v, got %v", i, want, got) 47*46c4c49dSIbrahim Kanouche } 48*46c4c49dSIbrahim Kanouche } 49*46c4c49dSIbrahim Kanouche} 50*46c4c49dSIbrahim Kanouche 51*46c4c49dSIbrahim Kanouchetype enumTest int 52*46c4c49dSIbrahim Kanouche 53*46c4c49dSIbrahim Kanoucheconst ( 54*46c4c49dSIbrahim Kanouche et0 enumTest = iota 55*46c4c49dSIbrahim Kanouche et1 56*46c4c49dSIbrahim Kanouche et2 57*46c4c49dSIbrahim Kanouche et3 58*46c4c49dSIbrahim Kanouche et4 59*46c4c49dSIbrahim Kanouche) 60*46c4c49dSIbrahim Kanouche 61*46c4c49dSIbrahim Kanouchefunc TestNewIntSet(t *testing.T) { 62*46c4c49dSIbrahim Kanouche empty := NewIntSet() 63*46c4c49dSIbrahim Kanouche want := 0 64*46c4c49dSIbrahim Kanouche got := empty.Len() 65*46c4c49dSIbrahim Kanouche 66*46c4c49dSIbrahim Kanouche if got != want { 67*46c4c49dSIbrahim Kanouche t.Errorf("NewIntSet() want length %v, got %v", want, got) 68*46c4c49dSIbrahim Kanouche } 69*46c4c49dSIbrahim Kanouche 70*46c4c49dSIbrahim Kanouche unique := []int{0, 1, 2} 71*46c4c49dSIbrahim Kanouche set := NewIntSet(unique...) 72*46c4c49dSIbrahim Kanouche checkSameIntSet(t, set, unique) 73*46c4c49dSIbrahim Kanouche 74*46c4c49dSIbrahim Kanouche // Append an already-present element. 75*46c4c49dSIbrahim Kanouche nonUnique := append(unique, unique[0]) 76*46c4c49dSIbrahim Kanouche set = NewIntSet(nonUnique...) 77*46c4c49dSIbrahim Kanouche 78*46c4c49dSIbrahim Kanouche // Non-unique unique should collapse to one. 79*46c4c49dSIbrahim Kanouche want = len(unique) 80*46c4c49dSIbrahim Kanouche got = set.Len() 81*46c4c49dSIbrahim Kanouche 82*46c4c49dSIbrahim Kanouche if got != want { 83*46c4c49dSIbrahim Kanouche t.Errorf("NewIntSet(%v) want length %v, got %v", nonUnique, want, got) 84*46c4c49dSIbrahim Kanouche } 85*46c4c49dSIbrahim Kanouche 86*46c4c49dSIbrahim Kanouche // Initialize with enum values cast to int. 87*46c4c49dSIbrahim Kanouche set = NewIntSet(int(et0), int(et1), int(et2)) 88*46c4c49dSIbrahim Kanouche checkSameIntSet(t, set, unique) 89*46c4c49dSIbrahim Kanouche} 90*46c4c49dSIbrahim Kanouche 91*46c4c49dSIbrahim Kanouchefunc TestIntSet_Copy(t *testing.T) { 92*46c4c49dSIbrahim Kanouche // Check both copies represent the same set. 93*46c4c49dSIbrahim Kanouche base := []int{1, 2, 3} 94*46c4c49dSIbrahim Kanouche orig := NewIntSet(base...) 95*46c4c49dSIbrahim Kanouche cpy := orig.Copy() 96*46c4c49dSIbrahim Kanouche checkSameIntSet(t, orig, base) 97*46c4c49dSIbrahim Kanouche checkSameIntSet(t, cpy, base) 98*46c4c49dSIbrahim Kanouche 99*46c4c49dSIbrahim Kanouche // Check the two copies are independent. 100*46c4c49dSIbrahim Kanouche more := []int{4} 101*46c4c49dSIbrahim Kanouche orig.Insert(more...) 102*46c4c49dSIbrahim Kanouche more = append(base, more...) 103*46c4c49dSIbrahim Kanouche checkSameIntSet(t, orig, more) 104*46c4c49dSIbrahim Kanouche checkSameIntSet(t, cpy, base) 105*46c4c49dSIbrahim Kanouche} 106*46c4c49dSIbrahim Kanouche 107*46c4c49dSIbrahim Kanouchefunc TestIntSet_Insert(t *testing.T) { 108*46c4c49dSIbrahim Kanouche unique := []int{0, 1, 2} 109*46c4c49dSIbrahim Kanouche set := NewIntSet(unique...) 110*46c4c49dSIbrahim Kanouche 111*46c4c49dSIbrahim Kanouche // Insert existing element, which should basically be a no-op. 112*46c4c49dSIbrahim Kanouche set.Insert(unique[0]) 113*46c4c49dSIbrahim Kanouche checkSameIntSet(t, set, unique) 114*46c4c49dSIbrahim Kanouche 115*46c4c49dSIbrahim Kanouche // Actually insert new unique elements (cast from enum values this time). 116*46c4c49dSIbrahim Kanouche additional := []int{int(et3), int(et4)} 117*46c4c49dSIbrahim Kanouche longer := append(unique, additional...) 118*46c4c49dSIbrahim Kanouche set.Insert(additional...) 119*46c4c49dSIbrahim Kanouche checkSameIntSet(t, set, longer) 120*46c4c49dSIbrahim Kanouche} 121*46c4c49dSIbrahim Kanouche 122*46c4c49dSIbrahim Kanouchefunc TestIntSet_Delete(t *testing.T) { 123*46c4c49dSIbrahim Kanouche unique := []int{0, 1, 2} 124*46c4c49dSIbrahim Kanouche set := NewIntSet(unique...) 125*46c4c49dSIbrahim Kanouche 126*46c4c49dSIbrahim Kanouche // Delete non-existent element, which should basically be a no-op. 127*46c4c49dSIbrahim Kanouche set.Delete(int(et4)) 128*46c4c49dSIbrahim Kanouche checkSameIntSet(t, set, unique) 129*46c4c49dSIbrahim Kanouche 130*46c4c49dSIbrahim Kanouche // Actually delete existing elements. 131*46c4c49dSIbrahim Kanouche set.Delete(unique[1:]...) 132*46c4c49dSIbrahim Kanouche checkSameIntSet(t, set, unique[:1]) 133*46c4c49dSIbrahim Kanouche} 134*46c4c49dSIbrahim Kanouche 135*46c4c49dSIbrahim Kanouchefunc TestIntSet_Intersect(t *testing.T) { 136*46c4c49dSIbrahim Kanouche input1 := []int{1, 3, 4, 5, 6} 137*46c4c49dSIbrahim Kanouche input2 := []int{2, 3, 5} 138*46c4c49dSIbrahim Kanouche 139*46c4c49dSIbrahim Kanouche // Check Intersect(nil) returns an empty set. 140*46c4c49dSIbrahim Kanouche setA := NewIntSet(input1...) 141*46c4c49dSIbrahim Kanouche got := setA.Intersect(nil) 142*46c4c49dSIbrahim Kanouche checkSameIntSet(t, got, []int{}) 143*46c4c49dSIbrahim Kanouche // Check that the receiver is unchanged. 144*46c4c49dSIbrahim Kanouche checkSameIntSet(t, setA, input1) 145*46c4c49dSIbrahim Kanouche 146*46c4c49dSIbrahim Kanouche // Check Intersect returns the correct result. 147*46c4c49dSIbrahim Kanouche setB := NewIntSet(input2...) 148*46c4c49dSIbrahim Kanouche got = setA.Intersect(setB) 149*46c4c49dSIbrahim Kanouche want := []int{3, 5} 150*46c4c49dSIbrahim Kanouche checkSameIntSet(t, got, want) 151*46c4c49dSIbrahim Kanouche // Also check the sources are unchanged. 152*46c4c49dSIbrahim Kanouche checkSameIntSet(t, setA, input1) 153*46c4c49dSIbrahim Kanouche checkSameIntSet(t, setB, input2) 154*46c4c49dSIbrahim Kanouche 155*46c4c49dSIbrahim Kanouche // Reverse the inputs and verify Intersect produces the same results. 156*46c4c49dSIbrahim Kanouche setA = NewIntSet(input2...) 157*46c4c49dSIbrahim Kanouche setB = NewIntSet(input1...) 158*46c4c49dSIbrahim Kanouche got = setA.Intersect(setB) 159*46c4c49dSIbrahim Kanouche checkSameIntSet(t, got, want) 160*46c4c49dSIbrahim Kanouche // Check the sources are again unchanged. 161*46c4c49dSIbrahim Kanouche checkSameIntSet(t, setA, input2) 162*46c4c49dSIbrahim Kanouche checkSameIntSet(t, setB, input1) 163*46c4c49dSIbrahim Kanouche} 164*46c4c49dSIbrahim Kanouche 165*46c4c49dSIbrahim Kanouchefunc TestIntSet_Disjoint(t *testing.T) { 166*46c4c49dSIbrahim Kanouche input1 := []int{1, 3, 4, 5, 6} 167*46c4c49dSIbrahim Kanouche input2 := []int{2, 3, 5} 168*46c4c49dSIbrahim Kanouche input3 := []int{98, 99, 100} 169*46c4c49dSIbrahim Kanouche 170*46c4c49dSIbrahim Kanouche // Check that sets are always disjoint with the empty set or nil 171*46c4c49dSIbrahim Kanouche setA := NewIntSet(input1...) 172*46c4c49dSIbrahim Kanouche emptySet := NewIntSet() 173*46c4c49dSIbrahim Kanouche 174*46c4c49dSIbrahim Kanouche if disjoint := setA.Disjoint(nil); !disjoint { 175*46c4c49dSIbrahim Kanouche t.Errorf("Disjoint(%s, %v) want %v, got %v", setA, nil, true, disjoint) 176*46c4c49dSIbrahim Kanouche } 177*46c4c49dSIbrahim Kanouche 178*46c4c49dSIbrahim Kanouche if disjoint := setA.Disjoint(emptySet); !disjoint { 179*46c4c49dSIbrahim Kanouche t.Errorf("Disjoint(%s, %s) want %v, got %v", setA, emptySet, true, disjoint) 180*46c4c49dSIbrahim Kanouche } 181*46c4c49dSIbrahim Kanouche 182*46c4c49dSIbrahim Kanouche if disjoint := emptySet.Disjoint(setA); !disjoint { 183*46c4c49dSIbrahim Kanouche t.Errorf("Disjoint(%s, %s) want %v, got %v", emptySet, setA, true, disjoint) 184*46c4c49dSIbrahim Kanouche } 185*46c4c49dSIbrahim Kanouche 186*46c4c49dSIbrahim Kanouche if disjoint := emptySet.Disjoint(emptySet); !disjoint { 187*46c4c49dSIbrahim Kanouche t.Errorf("Disjoint(%s, %s) want %v, got %v", emptySet, emptySet, true, disjoint) 188*46c4c49dSIbrahim Kanouche } 189*46c4c49dSIbrahim Kanouche 190*46c4c49dSIbrahim Kanouche // Also check the sources are unchanged. 191*46c4c49dSIbrahim Kanouche checkSameIntSet(t, setA, input1) 192*46c4c49dSIbrahim Kanouche checkSameIntSet(t, emptySet, []int{}) 193*46c4c49dSIbrahim Kanouche 194*46c4c49dSIbrahim Kanouche // Check two non-empty, non-nil disjoint sets. 195*46c4c49dSIbrahim Kanouche setC := NewIntSet(input3...) 196*46c4c49dSIbrahim Kanouche 197*46c4c49dSIbrahim Kanouche if disjoint := setA.Disjoint(setC); !disjoint { 198*46c4c49dSIbrahim Kanouche t.Errorf("Disjoint(%s, %s) want %v, got %v", setA, setC, true, disjoint) 199*46c4c49dSIbrahim Kanouche } 200*46c4c49dSIbrahim Kanouche 201*46c4c49dSIbrahim Kanouche // Also check the sources are unchanged. 202*46c4c49dSIbrahim Kanouche checkSameIntSet(t, setA, input1) 203*46c4c49dSIbrahim Kanouche checkSameIntSet(t, setC, input3) 204*46c4c49dSIbrahim Kanouche 205*46c4c49dSIbrahim Kanouche // Check that two intersecting sets are not Disjoint. 206*46c4c49dSIbrahim Kanouche setB := NewIntSet(input2...) 207*46c4c49dSIbrahim Kanouche 208*46c4c49dSIbrahim Kanouche if disjoint := setA.Disjoint(setB); disjoint { 209*46c4c49dSIbrahim Kanouche t.Errorf("Disjoint(%s, %s) want %v, got %v", setA, setB, false, disjoint) 210*46c4c49dSIbrahim Kanouche } 211*46c4c49dSIbrahim Kanouche 212*46c4c49dSIbrahim Kanouche // Also check the sources are unchanged. 213*46c4c49dSIbrahim Kanouche checkSameIntSet(t, setA, input1) 214*46c4c49dSIbrahim Kanouche checkSameIntSet(t, setB, input2) 215*46c4c49dSIbrahim Kanouche} 216*46c4c49dSIbrahim Kanouche 217*46c4c49dSIbrahim Kanouchefunc TestIntSet_Difference(t *testing.T) { 218*46c4c49dSIbrahim Kanouche input1 := []int{1, 3, 4, 5, 6} 219*46c4c49dSIbrahim Kanouche input2 := []int{2, 3, 5} 220*46c4c49dSIbrahim Kanouche input3 := []int{98, 99, 100} 221*46c4c49dSIbrahim Kanouche 222*46c4c49dSIbrahim Kanouche // Check Difference(nil) returns a copy of the receiver. 223*46c4c49dSIbrahim Kanouche setA := NewIntSet(input1...) 224*46c4c49dSIbrahim Kanouche got := setA.Difference(nil) 225*46c4c49dSIbrahim Kanouche checkSameIntSet(t, got, input1) 226*46c4c49dSIbrahim Kanouche // Check that the receiver is unchanged. 227*46c4c49dSIbrahim Kanouche checkSameIntSet(t, setA, input1) 228*46c4c49dSIbrahim Kanouche 229*46c4c49dSIbrahim Kanouche // Check A - A returns the empty set. 230*46c4c49dSIbrahim Kanouche got = setA.Difference(setA) 231*46c4c49dSIbrahim Kanouche 232*46c4c49dSIbrahim Kanouche if !got.Empty() { 233*46c4c49dSIbrahim Kanouche t.Errorf("Difference(%s, %s).Empty() want %v, got %v", 234*46c4c49dSIbrahim Kanouche setA, setA, true, false) 235*46c4c49dSIbrahim Kanouche } 236*46c4c49dSIbrahim Kanouche 237*46c4c49dSIbrahim Kanouche checkSameIntSet(t, got, []int{}) 238*46c4c49dSIbrahim Kanouche // Check that the receiver is unchanged. 239*46c4c49dSIbrahim Kanouche checkSameIntSet(t, setA, input1) 240*46c4c49dSIbrahim Kanouche 241*46c4c49dSIbrahim Kanouche // Check A - C simply returns elements in A if A and C are disjoint. 242*46c4c49dSIbrahim Kanouche setC := NewIntSet(input3...) 243*46c4c49dSIbrahim Kanouche got = setA.Difference(setC) 244*46c4c49dSIbrahim Kanouche checkSameIntSet(t, got, input1) 245*46c4c49dSIbrahim Kanouche // Also check the sources are unchanged. 246*46c4c49dSIbrahim Kanouche checkSameIntSet(t, setA, input1) 247*46c4c49dSIbrahim Kanouche checkSameIntSet(t, setC, input3) 248*46c4c49dSIbrahim Kanouche 249*46c4c49dSIbrahim Kanouche // Check A - B returns elements in A not in B. 250*46c4c49dSIbrahim Kanouche setB := NewIntSet(input2...) 251*46c4c49dSIbrahim Kanouche got = setA.Difference(setB) 252*46c4c49dSIbrahim Kanouche want := []int{1, 4, 6} 253*46c4c49dSIbrahim Kanouche checkSameIntSet(t, got, want) 254*46c4c49dSIbrahim Kanouche 255*46c4c49dSIbrahim Kanouche // Also check the sources are unchanged. 256*46c4c49dSIbrahim Kanouche checkSameIntSet(t, setA, input1) 257*46c4c49dSIbrahim Kanouche checkSameIntSet(t, setB, input2) 258*46c4c49dSIbrahim Kanouche 259*46c4c49dSIbrahim Kanouche // Check B - A returns elements in B not in A. 260*46c4c49dSIbrahim Kanouche got = setB.Difference(setA) 261*46c4c49dSIbrahim Kanouche want = []int{2} 262*46c4c49dSIbrahim Kanouche checkSameIntSet(t, got, want) 263*46c4c49dSIbrahim Kanouche // Also check the sources are unchanged. 264*46c4c49dSIbrahim Kanouche checkSameIntSet(t, setA, input1) 265*46c4c49dSIbrahim Kanouche checkSameIntSet(t, setB, input2) 266*46c4c49dSIbrahim Kanouche} 267*46c4c49dSIbrahim Kanouche 268*46c4c49dSIbrahim Kanouchefunc TestIntSet_Unique(t *testing.T) { 269*46c4c49dSIbrahim Kanouche input1 := []int{1, 3, 4, 5, 6} 270*46c4c49dSIbrahim Kanouche input2 := []int{2, 3, 5} 271*46c4c49dSIbrahim Kanouche input3 := []int{98, 99, 100} 272*46c4c49dSIbrahim Kanouche 273*46c4c49dSIbrahim Kanouche // Check Unique(nil) returns a copy of the receiver. 274*46c4c49dSIbrahim Kanouche setA := NewIntSet(input1...) 275*46c4c49dSIbrahim Kanouche got := setA.Unique(nil) 276*46c4c49dSIbrahim Kanouche checkSameIntSet(t, got, input1) 277*46c4c49dSIbrahim Kanouche // Check that the receiver is unchanged. 278*46c4c49dSIbrahim Kanouche checkSameIntSet(t, setA, input1) 279*46c4c49dSIbrahim Kanouche 280*46c4c49dSIbrahim Kanouche // Check Unique returns only elements in A and B not in both A and B. 281*46c4c49dSIbrahim Kanouche setB := NewIntSet(input2...) 282*46c4c49dSIbrahim Kanouche got = setA.Unique(setB) 283*46c4c49dSIbrahim Kanouche want := []int{1, 2, 4, 6} 284*46c4c49dSIbrahim Kanouche checkSameIntSet(t, got, want) 285*46c4c49dSIbrahim Kanouche // Also check the sources are unchanged. 286*46c4c49dSIbrahim Kanouche checkSameIntSet(t, setA, input1) 287*46c4c49dSIbrahim Kanouche checkSameIntSet(t, setB, input2) 288*46c4c49dSIbrahim Kanouche 289*46c4c49dSIbrahim Kanouche // Check Unique of two disjoint sets is the Union of those sets. 290*46c4c49dSIbrahim Kanouche setC := NewIntSet(input3...) 291*46c4c49dSIbrahim Kanouche got = setA.Unique(setC) 292*46c4c49dSIbrahim Kanouche union := setA.Union(setC) 293*46c4c49dSIbrahim Kanouche 294*46c4c49dSIbrahim Kanouche if equal := union.Equal(got); !equal { 295*46c4c49dSIbrahim Kanouche t.Errorf("Union of disjoint Equal(%s, %s) want %v, got %v", 296*46c4c49dSIbrahim Kanouche union, got, true, equal) 297*46c4c49dSIbrahim Kanouche } 298*46c4c49dSIbrahim Kanouche 299*46c4c49dSIbrahim Kanouche // Check Unique is the Union of A - B and B - A. 300*46c4c49dSIbrahim Kanouche aNotInB := setA.Difference(setB) 301*46c4c49dSIbrahim Kanouche bNotInA := setB.Difference(setA) 302*46c4c49dSIbrahim Kanouche union = aNotInB.Union(bNotInA) 303*46c4c49dSIbrahim Kanouche want = []int{1, 2, 4, 6} 304*46c4c49dSIbrahim Kanouche checkSameIntSet(t, union, want) 305*46c4c49dSIbrahim Kanouche got = setA.Unique(setB) 306*46c4c49dSIbrahim Kanouche 307*46c4c49dSIbrahim Kanouche if equal := union.Equal(got); !equal { 308*46c4c49dSIbrahim Kanouche t.Errorf("Union of differences Equal(%s, %s) want %v, got %v", 309*46c4c49dSIbrahim Kanouche union, got, true, equal) 310*46c4c49dSIbrahim Kanouche } 311*46c4c49dSIbrahim Kanouche 312*46c4c49dSIbrahim Kanouche // Also check the sources are unchanged. 313*46c4c49dSIbrahim Kanouche checkSameIntSet(t, setA, input1) 314*46c4c49dSIbrahim Kanouche checkSameIntSet(t, setB, input2) 315*46c4c49dSIbrahim Kanouche} 316*46c4c49dSIbrahim Kanouche 317*46c4c49dSIbrahim Kanouchefunc TestIntSet_Equal(t *testing.T) { 318*46c4c49dSIbrahim Kanouche input1 := []int{1, 3, 4, 5, 6} 319*46c4c49dSIbrahim Kanouche input2 := []int{2, 3, 5} 320*46c4c49dSIbrahim Kanouche input3 := []int{1, 3, 4, 5, 7} 321*46c4c49dSIbrahim Kanouche 322*46c4c49dSIbrahim Kanouche // Check Equal(nil) returns false. 323*46c4c49dSIbrahim Kanouche setA := NewIntSet(input1...) 324*46c4c49dSIbrahim Kanouche 325*46c4c49dSIbrahim Kanouche if equal := setA.Equal(nil); equal { 326*46c4c49dSIbrahim Kanouche t.Errorf("Equal(%s, %v) want %v, got %v", setA, nil, false, true) 327*46c4c49dSIbrahim Kanouche } 328*46c4c49dSIbrahim Kanouche 329*46c4c49dSIbrahim Kanouche // Check that the receiver is unchanged. 330*46c4c49dSIbrahim Kanouche checkSameIntSet(t, setA, input1) 331*46c4c49dSIbrahim Kanouche 332*46c4c49dSIbrahim Kanouche // Check Equal returns true for a set and itself. 333*46c4c49dSIbrahim Kanouche if equal := setA.Equal(setA); !equal { 334*46c4c49dSIbrahim Kanouche t.Errorf("Equal(%s, %s) want %v, got %v", setA, setA, true, false) 335*46c4c49dSIbrahim Kanouche } 336*46c4c49dSIbrahim Kanouche 337*46c4c49dSIbrahim Kanouche // Check that the receiver is unchanged. 338*46c4c49dSIbrahim Kanouche checkSameIntSet(t, setA, input1) 339*46c4c49dSIbrahim Kanouche 340*46c4c49dSIbrahim Kanouche // Check Equal returns false for sets of non-equal length. 341*46c4c49dSIbrahim Kanouche setB := NewIntSet(input2...) 342*46c4c49dSIbrahim Kanouche 343*46c4c49dSIbrahim Kanouche if equal := setA.Equal(setB); equal { 344*46c4c49dSIbrahim Kanouche t.Errorf("Equal(%s, %s) want %v, got %v", setA, setB, false, true) 345*46c4c49dSIbrahim Kanouche } 346*46c4c49dSIbrahim Kanouche 347*46c4c49dSIbrahim Kanouche // Also check the sources are unchanged. 348*46c4c49dSIbrahim Kanouche checkSameIntSet(t, setA, input1) 349*46c4c49dSIbrahim Kanouche checkSameIntSet(t, setB, input2) 350*46c4c49dSIbrahim Kanouche 351*46c4c49dSIbrahim Kanouche // Check Equal returns false for equal-length sets with different elements. 352*46c4c49dSIbrahim Kanouche setC := NewIntSet(input3...) 353*46c4c49dSIbrahim Kanouche 354*46c4c49dSIbrahim Kanouche if equal := setA.Equal(setC); equal { 355*46c4c49dSIbrahim Kanouche t.Errorf("Equal(%s, %s) want %v, got %v", setA, setC, false, true) 356*46c4c49dSIbrahim Kanouche } 357*46c4c49dSIbrahim Kanouche 358*46c4c49dSIbrahim Kanouche if equal := setC.Equal(setA); equal { 359*46c4c49dSIbrahim Kanouche t.Errorf("Equal(%s, %s) want %v, got %v", setC, setA, false, true) 360*46c4c49dSIbrahim Kanouche } 361*46c4c49dSIbrahim Kanouche 362*46c4c49dSIbrahim Kanouche // Also check the sources are unchanged. 363*46c4c49dSIbrahim Kanouche checkSameIntSet(t, setA, input1) 364*46c4c49dSIbrahim Kanouche checkSameIntSet(t, setC, input3) 365*46c4c49dSIbrahim Kanouche 366*46c4c49dSIbrahim Kanouche // Check Equal returns true for a set with itself. 367*46c4c49dSIbrahim Kanouche if equal := setA.Equal(setA); !equal { 368*46c4c49dSIbrahim Kanouche t.Errorf("Equal(%s, %s) want %v, got %v", setA, setA, true, false) 369*46c4c49dSIbrahim Kanouche } 370*46c4c49dSIbrahim Kanouche 371*46c4c49dSIbrahim Kanouche // Also check the source is unchanged. 372*46c4c49dSIbrahim Kanouche checkSameIntSet(t, setA, input1) 373*46c4c49dSIbrahim Kanouche 374*46c4c49dSIbrahim Kanouche // Check Equal returns true for two separate equal sets. 375*46c4c49dSIbrahim Kanouche anotherA := NewIntSet(input1...) 376*46c4c49dSIbrahim Kanouche 377*46c4c49dSIbrahim Kanouche if equal := setA.Equal(anotherA); !equal { 378*46c4c49dSIbrahim Kanouche t.Errorf("Equal(%s, %s) want %v, got %v", setA, anotherA, true, false) 379*46c4c49dSIbrahim Kanouche } 380*46c4c49dSIbrahim Kanouche 381*46c4c49dSIbrahim Kanouche // Also check the sources are unchanged. 382*46c4c49dSIbrahim Kanouche checkSameIntSet(t, setA, input1) 383*46c4c49dSIbrahim Kanouche checkSameIntSet(t, anotherA, input1) 384*46c4c49dSIbrahim Kanouche 385*46c4c49dSIbrahim Kanouche // Check for equality comparing to nil struct. 386*46c4c49dSIbrahim Kanouche var nilSet *IntSet 387*46c4c49dSIbrahim Kanouche if equal := nilSet.Equal(setA); equal { 388*46c4c49dSIbrahim Kanouche t.Errorf("Equal(%s, %s) want %v, got %v", nilSet, setA, false, true) 389*46c4c49dSIbrahim Kanouche } 390*46c4c49dSIbrahim Kanouche if equal := setA.Equal(nilSet); equal { 391*46c4c49dSIbrahim Kanouche t.Errorf("Equal(%s, %s) want %v, got %v", setA, nilSet, false, true) 392*46c4c49dSIbrahim Kanouche } 393*46c4c49dSIbrahim Kanouche if equal := nilSet.Equal(nilSet); !equal { 394*46c4c49dSIbrahim Kanouche t.Errorf("Equal(%s, %s) want %v, got %v", nilSet, nilSet, true, false) 395*46c4c49dSIbrahim Kanouche } 396*46c4c49dSIbrahim Kanouche 397*46c4c49dSIbrahim Kanouche // Edge case: consider the empty set to be different than the nil set. 398*46c4c49dSIbrahim Kanouche emptySet := NewIntSet() 399*46c4c49dSIbrahim Kanouche if equal := nilSet.Equal(emptySet); equal { 400*46c4c49dSIbrahim Kanouche t.Errorf("Equal(%s, %s) want %v, got %v", nilSet, emptySet, false, true) 401*46c4c49dSIbrahim Kanouche } 402*46c4c49dSIbrahim Kanouche if equal := emptySet.Equal(nilSet); equal { 403*46c4c49dSIbrahim Kanouche t.Errorf("Equal(%s, %s) want %v, got %v", emptySet, nilSet, false, true) 404*46c4c49dSIbrahim Kanouche } 405*46c4c49dSIbrahim Kanouche if equal := emptySet.Equal(emptySet); !equal { 406*46c4c49dSIbrahim Kanouche t.Errorf("Equal(%s, %s) want %v, got %v", emptySet, emptySet, true, false) 407*46c4c49dSIbrahim Kanouche } 408*46c4c49dSIbrahim Kanouche} 409*46c4c49dSIbrahim Kanouche 410*46c4c49dSIbrahim Kanouchefunc TestIntSet_Union(t *testing.T) { 411*46c4c49dSIbrahim Kanouche input1 := []int{1, 3, 4, 5, 6} 412*46c4c49dSIbrahim Kanouche input2 := []int{2, 3, 5} 413*46c4c49dSIbrahim Kanouche 414*46c4c49dSIbrahim Kanouche // Check Union(nil) returns a copy of the receiver. 415*46c4c49dSIbrahim Kanouche setA := NewIntSet(input1...) 416*46c4c49dSIbrahim Kanouche got := setA.Union(nil) 417*46c4c49dSIbrahim Kanouche checkSameIntSet(t, got, input1) 418*46c4c49dSIbrahim Kanouche // Check that the receiver is unchanged. 419*46c4c49dSIbrahim Kanouche checkSameIntSet(t, setA, input1) 420*46c4c49dSIbrahim Kanouche 421*46c4c49dSIbrahim Kanouche // Check Union returns the correct result. 422*46c4c49dSIbrahim Kanouche setB := NewIntSet(input2...) 423*46c4c49dSIbrahim Kanouche got = setA.Union(setB) 424*46c4c49dSIbrahim Kanouche want := []int{1, 2, 3, 4, 5, 6} 425*46c4c49dSIbrahim Kanouche checkSameIntSet(t, got, want) 426*46c4c49dSIbrahim Kanouche // Also check the sources are unchanged. 427*46c4c49dSIbrahim Kanouche checkSameIntSet(t, setA, input1) 428*46c4c49dSIbrahim Kanouche checkSameIntSet(t, setB, input2) 429*46c4c49dSIbrahim Kanouche 430*46c4c49dSIbrahim Kanouche // Reverse the inputs and verify Union produces the same results. 431*46c4c49dSIbrahim Kanouche setA = NewIntSet(input2...) 432*46c4c49dSIbrahim Kanouche setB = NewIntSet(input1...) 433*46c4c49dSIbrahim Kanouche got = setA.Union(setB) 434*46c4c49dSIbrahim Kanouche checkSameIntSet(t, got, want) 435*46c4c49dSIbrahim Kanouche // Check the sources are again unchanged. 436*46c4c49dSIbrahim Kanouche checkSameIntSet(t, setA, input2) 437*46c4c49dSIbrahim Kanouche checkSameIntSet(t, setB, input1) 438*46c4c49dSIbrahim Kanouche} 439