util/set: add some more Set operations (#10022)

Updates #cleanup

Signed-off-by: Chris Palmer <cpalmer@tailscale.com>
This commit is contained in:
Chris Palmer
2023-10-31 17:15:40 -07:00
committed by GitHub
parent 7f3208592f
commit 00375f56ea
2 changed files with 74 additions and 3 deletions
+51 -1
View File
@@ -54,7 +54,7 @@ func TestSet(t *testing.T) {
func TestSetOf(t *testing.T) {
s := SetOf[int]([]int{1, 2, 3, 4, 4, 1})
if s.Len() != 4 {
t.Errorf("wrong len %d; want 2", s.Len())
t.Errorf("wrong len %d; want 4", s.Len())
}
for _, n := range []int{1, 2, 3, 4} {
if !s.Contains(n) {
@@ -62,3 +62,53 @@ func TestSetOf(t *testing.T) {
}
}
}
func TestEqual(t *testing.T) {
type test struct {
name string
a Set[int]
b Set[int]
expected bool
}
tests := []test{
{
"equal",
SetOf([]int{1, 2, 3, 4}),
SetOf([]int{1, 2, 3, 4}),
true,
},
{
"not equal",
SetOf([]int{1, 2, 3, 4}),
SetOf([]int{1, 2, 3, 5}),
false,
},
{
"different lengths",
SetOf([]int{1, 2, 3, 4, 5}),
SetOf([]int{1, 2, 3, 5}),
false,
},
}
for _, tt := range tests {
if tt.a.Equal(tt.b) != tt.expected {
t.Errorf("%s: failed", tt.name)
}
}
}
func TestClone(t *testing.T) {
s := SetOf[int]([]int{1, 2, 3, 4, 4, 1})
if s.Len() != 4 {
t.Errorf("wrong len %d; want 4", s.Len())
}
s2 := Clone(s)
if !s.Equal(s2) {
t.Error("clone not equal to original")
}
s.Add(100)
if s.Equal(s2) {
t.Error("clone is not distinct from original")
}
}