stringset/set_test.go
2024-06-10 16:45:24 -07:00

109 lines
2.1 KiB
Go

package stringset
import (
"testing"
)
func TestNewStringSetWith(t *testing.T) {
s := NewStringSet()
if len(s.All()) != 0 {
t.Errorf("expected empty at initiaization")
}
s.Add("foo")
s.Add("bar")
s.Add("bar")
// s = foo,bar
if len(s.All()) != 2 {
t.Errorf("expected 2 entries")
}
if !s.Contains("foo") {
t.Errorf("set missing expected entry")
}
s.Remove("bar")
// s = foo
if len(s.All()) != 1 {
t.Errorf("expected 1 entry")
}
if s.Contains("bar") {
t.Errorf("removed entry still present")
}
if !s.Contains("foo") {
t.Errorf("entry that was not removed is missing")
}
s.Add("qux")
// s = foo, qux
s2 := NewStringSetWith([]string{"foo", "qux"})
if !s.Equals(s2) {
t.Errorf("sets should match")
}
s.Add("lol")
s2.Add("noo")
// s = foo, qux, lol
// s2 = foo, qux, noo
if s.Equals(s2) {
t.Errorf("sets shouldn't match")
}
if !s.Equals(NewStringSetWith([]string{"foo", "qux", "lol"})) {
t.Errorf("unexpected result from All()")
}
if !s.Intersect(s2).Equals(NewStringSetWith([]string{"foo", "qux"})) {
t.Errorf("unexpected result from Intersect()")
}
s.Add("noo")
// s = foo, qux, noo, lol
// s2 = foo, qux, noo
if !s.Diff(s2).Equals(NewStringSetWith([]string{"lol"})) {
t.Errorf("unexpected result from Diff()")
}
s.Remove("foo").Remove("qux")
// s = noo, lol
if !s.Equals(NewStringSetWith([]string{"noo", "lol"})) {
t.Errorf("unexpected result from Remove() chaining")
}
s.Add("foo").Add("qux")
// s = noo, lol, foo, qux
if !s.Equals(NewStringSetWith([]string{"noo", "lol", "foo", "qux"})) {
t.Errorf("unexpected result from Add() chaining")
}
s2 = s.Copy().Remove("noo")
// s = lol, foo, qux
if !s2.Equals(NewStringSetWith([]string{"lol", "foo", "qux"})) {
t.Errorf("unexpected result after cloning and removing")
}
if !s.Equals(NewStringSetWith([]string{"noo", "lol", "foo", "qux"})) {
t.Errorf("unexpected result original was modified via the clone")
}
}
func stringSlicesMatch(s1, s2 []string) bool {
if len(s1) != len(s2) {
return false
}
for i, s := range s1 {
if s != s2[i] {
return false
}
}
return true
}