Something went wrong. Try again.
Monorepo for Tangled
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412package sets
import ( "slices" "testing" "testing/quick")
func TestNew(t *testing.T) { s := New[int]() if s.Len() != 0 { t.Errorf("New set should be empty, got length %d", s.Len()) } if !s.IsEmpty() { t.Error("New set should be empty") }}
func TestFromSlice(t *testing.T) { s := Collect(slices.Values([]int{1, 2, 3, 2, 1})) if s.Len() != 3 { t.Errorf("Expected length 3, got %d", s.Len()) } if !s.Contains(1) || !s.Contains(2) || !s.Contains(3) { t.Error("Set should contain all unique elements from slice") }}
func TestInsert(t *testing.T) { s := New[string]()
if !s.Insert("hello") { t.Error("First insert should return true") } if s.Insert("hello") { t.Error("Duplicate insert should return false") } if s.Len() != 1 { t.Errorf("Expected length 1, got %d", s.Len()) }}
func TestRemove(t *testing.T) { s := Collect(slices.Values([]int{1, 2, 3}))
if !s.Remove(2) { t.Error("Remove existing element should return true") } if s.Remove(2) { t.Error("Remove non-existing element should return false") } if s.Contains(2) { t.Error("Element should be removed") } if s.Len() != 2 { t.Errorf("Expected length 2, got %d", s.Len()) }}
func TestContains(t *testing.T) { s := Collect(slices.Values([]int{1, 2, 3}))
if !s.Contains(1) { t.Error("Should contain 1") } if s.Contains(4) { t.Error("Should not contain 4") }}
func TestClear(t *testing.T) { s := Collect(slices.Values([]int{1, 2, 3})) s.Clear()
if !s.IsEmpty() { t.Error("Set should be empty after clear") } if s.Len() != 0 { t.Errorf("Expected length 0, got %d", s.Len()) }}
func TestIterator(t *testing.T) { s := Collect(slices.Values([]int{1, 2, 3})) var items []int
for item := range s.All() { items = append(items, item) }
slices.Sort(items) expected := []int{1, 2, 3} if !slices.Equal(items, expected) { t.Errorf("Expected %v, got %v", expected, items) }}
func TestClone(t *testing.T) { s1 := Collect(slices.Values([]int{1, 2, 3})) s2 := s1.Clone()
if !s1.Equal(s2) { t.Error("Cloned set should be equal to original") }
s2.Insert(4) if s1.Contains(4) { t.Error("Modifying clone should not affect original") }}
func TestUnion(t *testing.T) { s1 := Collect(slices.Values([]int{1, 2})) s2 := Collect(slices.Values([]int{2, 3}))
result := Collect(s1.Union(s2)) expected := Collect(slices.Values([]int{1, 2, 3}))
if !result.Equal(expected) { t.Errorf("Expected %v, got %v", expected, result) }}
func TestIntersection(t *testing.T) { s1 := Collect(slices.Values([]int{1, 2, 3})) s2 := Collect(slices.Values([]int{2, 3, 4}))
expected := Collect(slices.Values([]int{2, 3})) result := Collect(s1.Intersection(s2))
if !result.Equal(expected) { t.Errorf("Expected %v, got %v", expected, result) }}
func TestDifference(t *testing.T) { s1 := Collect(slices.Values([]int{1, 2, 3})) s2 := Collect(slices.Values([]int{2, 3, 4}))
expected := Collect(slices.Values([]int{1})) result := Collect(s1.Difference(s2))
if !result.Equal(expected) { t.Errorf("Expected %v, got %v", expected, result) }}
func TestSymmetricDifference(t *testing.T) { s1 := Collect(slices.Values([]int{1, 2, 3})) s2 := Collect(slices.Values([]int{2, 3, 4}))
expected := Collect(slices.Values([]int{1, 4})) result := Collect(s1.SymmetricDifference(s2))
if !result.Equal(expected) { t.Errorf("Expected %v, got %v", expected, result) }}
func TestSymmetricDifferenceCommutativeProperty(t *testing.T) { s1 := Collect(slices.Values([]int{1, 2, 3})) s2 := Collect(slices.Values([]int{2, 3, 4}))
result1 := Collect(s1.SymmetricDifference(s2)) result2 := Collect(s2.SymmetricDifference(s1))
if !result1.Equal(result2) { t.Errorf("Expected %v, got %v", result1, result2) }}
func TestIsSubset(t *testing.T) { s1 := Collect(slices.Values([]int{1, 2})) s2 := Collect(slices.Values([]int{1, 2, 3}))
if !s1.IsSubset(s2) { t.Error("s1 should be subset of s2") } if s2.IsSubset(s1) { t.Error("s2 should not be subset of s1") }}
func TestIsSuperset(t *testing.T) { s1 := Collect(slices.Values([]int{1, 2, 3})) s2 := Collect(slices.Values([]int{1, 2}))
if !s1.IsSuperset(s2) { t.Error("s1 should be superset of s2") } if s2.IsSuperset(s1) { t.Error("s2 should not be superset of s1") }}
func TestIsDisjoint(t *testing.T) { s1 := Collect(slices.Values([]int{1, 2})) s2 := Collect(slices.Values([]int{3, 4})) s3 := Collect(slices.Values([]int{2, 3}))
if !s1.IsDisjoint(s2) { t.Error("s1 and s2 should be disjoint") } if s1.IsDisjoint(s3) { t.Error("s1 and s3 should not be disjoint") }}
func TestEqual(t *testing.T) { s1 := Collect(slices.Values([]int{1, 2, 3})) s2 := Collect(slices.Values([]int{3, 2, 1})) s3 := Collect(slices.Values([]int{1, 2}))
if !s1.Equal(s2) { t.Error("s1 and s2 should be equal") } if s1.Equal(s3) { t.Error("s1 and s3 should not be equal") }}
func TestCollect(t *testing.T) { s1 := Collect(slices.Values([]int{1, 2})) s2 := Collect(slices.Values([]int{2, 3}))
unionSet := Collect(s1.Union(s2)) if unionSet.Len() != 3 { t.Errorf("Expected union set length 3, got %d", unionSet.Len()) } if !unionSet.Contains(1) || !unionSet.Contains(2) || !unionSet.Contains(3) { t.Error("Union set should contain 1, 2, and 3") }
diffSet := Collect(s1.Difference(s2)) if diffSet.Len() != 1 { t.Errorf("Expected difference set length 1, got %d", diffSet.Len()) } if !diffSet.Contains(1) { t.Error("Difference set should contain 1") }}
func TestPropertySingletonLen(t *testing.T) { f := func(item int) bool { single := Singleton(item) return single.Len() == 1 }
if err := quick.Check(f, nil); err != nil { t.Error(err) }}
func TestPropertyInsertIdempotent(t *testing.T) { f := func(s Set[int], item int) bool { clone := s.Clone()
clone.Insert(item) firstLen := clone.Len()
clone.Insert(item) secondLen := clone.Len()
return firstLen == secondLen }
if err := quick.Check(f, nil); err != nil { t.Error(err) }}
func TestPropertyUnionCommutative(t *testing.T) { f := func(s1 Set[int], s2 Set[int]) bool { union1 := Collect(s1.Union(s2)) union2 := Collect(s2.Union(s1)) return union1.Equal(union2) }
if err := quick.Check(f, nil); err != nil { t.Error(err) }}
func TestPropertyIntersectionCommutative(t *testing.T) { f := func(s1 Set[int], s2 Set[int]) bool { inter1 := Collect(s1.Intersection(s2)) inter2 := Collect(s2.Intersection(s1)) return inter1.Equal(inter2) }
if err := quick.Check(f, nil); err != nil { t.Error(err) }}
func TestPropertyCloneEquals(t *testing.T) { f := func(s Set[int]) bool { clone := s.Clone() return s.Equal(clone) }
if err := quick.Check(f, nil); err != nil { t.Error(err) }}
func TestPropertyIntersectionIsSubset(t *testing.T) { f := func(s1 Set[int], s2 Set[int]) bool { inter := Collect(s1.Intersection(s2)) return inter.IsSubset(s1) && inter.IsSubset(s2) }
if err := quick.Check(f, nil); err != nil { t.Error(err) }}
func TestPropertyUnionIsSuperset(t *testing.T) { f := func(s1 Set[int], s2 Set[int]) bool { union := Collect(s1.Union(s2)) return union.IsSuperset(s1) && union.IsSuperset(s2) }
if err := quick.Check(f, nil); err != nil { t.Error(err) }}
func TestPropertyDifferenceDisjoint(t *testing.T) { f := func(s1 Set[int], s2 Set[int]) bool { diff := Collect(s1.Difference(s2)) return diff.IsDisjoint(s2) }
if err := quick.Check(f, nil); err != nil { t.Error(err) }}
func TestPropertySymmetricDifferenceCommutative(t *testing.T) { f := func(s1 Set[int], s2 Set[int]) bool { symDiff1 := Collect(s1.SymmetricDifference(s2)) symDiff2 := Collect(s2.SymmetricDifference(s1)) return symDiff1.Equal(symDiff2) }
if err := quick.Check(f, nil); err != nil { t.Error(err) }}
func TestPropertyRemoveWorks(t *testing.T) { f := func(s Set[int], item int) bool { clone := s.Clone() clone.Insert(item) clone.Remove(item) return !clone.Contains(item) }
if err := quick.Check(f, nil); err != nil { t.Error(err) }}
func TestPropertyClearEmpty(t *testing.T) { f := func(s Set[int]) bool { s.Clear() return s.IsEmpty() && s.Len() == 0 }
if err := quick.Check(f, nil); err != nil { t.Error(err) }}
func TestPropertyIsSubsetReflexive(t *testing.T) { f := func(s Set[int]) bool { return s.IsSubset(s) }
if err := quick.Check(f, nil); err != nil { t.Error(err) }}
func TestPropertyDeMorganUnion(t *testing.T) { f := func(s1 Set[int], s2 Set[int], universe Set[int]) bool { // create a universe that contains both sets u := universe.Clone() for item := range s1.All() { u.Insert(item) } for item := range s2.All() { u.Insert(item) }
// (A u B)' = A' n B' union := Collect(s1.Union(s2)) complementUnion := Collect(u.Difference(union))
complementS1 := Collect(u.Difference(s1)) complementS2 := Collect(u.Difference(s2)) intersectionComplements := Collect(complementS1.Intersection(complementS2))
return complementUnion.Equal(intersectionComplements) }
if err := quick.Check(f, nil); err != nil { t.Error(err) }}