sets/functions_test.go

62 lines
1.5 KiB
Go
Raw Normal View History

2023-07-21 23:09:38 +02:00
package sets
import (
"testing"
"github.com/stretchr/testify/require"
)
func getSetForElements(t *testing.T, elements ...int) *Set[int] {
t.Helper()
target := map[int]struct{}{}
for _, el := range elements {
target[el] = struct{}{}
}
return &Set[int]{
store: target,
}
}
func TestUnion(t *testing.T) {
base := getSetForElements(t, 0, 1, 2, 3, 4, 5)
other := getSetForElements(t, 3, 4, 5, 6, 7, 8)
expected := []int{0, 1, 2, 3, 4, 5, 6, 7, 8}
require.ElementsMatch(t, expected, Union(base, other).Slice())
}
func TestIntersection(t *testing.T) {
base := getSetForElements(t, 0, 1, 2, 3, 4, 5)
other := getSetForElements(t, 3, 4, 5, 6, 7, 8)
expected := []int{3, 4, 5}
require.ElementsMatch(t, expected, Intersection(base, other).Slice())
}
func TestDiff(t *testing.T) {
base := getSetForElements(t, 0, 1, 2, 3, 4, 5)
other := getSetForElements(t, 3, 4, 5, 6, 7, 8)
expected := []int{0, 1, 2}
require.ElementsMatch(t, expected, Diff(base, other).Slice())
}
func TestSymmetricDiff(t *testing.T) {
base := getSetForElements(t, 0, 1, 2, 3, 4, 5)
other := getSetForElements(t, 3, 4, 5, 6, 7, 8)
expected := []int{0, 1, 2, 6, 7, 8}
require.ElementsMatch(t, expected, SymmetricDiff(base, other).Slice())
}
func TestEqual(t *testing.T) {
base := getSetForElements(t, 0, 1, 2, 3, 4, 5)
otherDifferent := getSetForElements(t, 3, 4, 5, 6, 7, 8)
otherEqual := getSetForElements(t, 0, 1, 2, 3, 4, 5)
require.False(t, Equal(base, otherDifferent))
require.True(t, Equal(base, otherEqual))
}