2017-02-02 19:03:20 +01:00
|
|
|
package dag
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestSetDifference(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
Name string
|
|
|
|
A, B []interface{}
|
|
|
|
Expected []interface{}
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"same",
|
|
|
|
[]interface{}{1, 2, 3},
|
|
|
|
[]interface{}{3, 1, 2},
|
|
|
|
[]interface{}{},
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"A has extra elements",
|
|
|
|
[]interface{}{1, 2, 3},
|
|
|
|
[]interface{}{3, 2},
|
|
|
|
[]interface{}{1},
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"B has extra elements",
|
|
|
|
[]interface{}{1, 2, 3},
|
|
|
|
[]interface{}{3, 2, 1, 4},
|
|
|
|
[]interface{}{},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tc := range cases {
|
|
|
|
t.Run(fmt.Sprintf("%d-%s", i, tc.Name), func(t *testing.T) {
|
2020-01-07 21:38:41 +01:00
|
|
|
one := make(Set)
|
|
|
|
two := make(Set)
|
|
|
|
expected := make(Set)
|
2017-02-02 19:03:20 +01:00
|
|
|
for _, v := range tc.A {
|
|
|
|
one.Add(v)
|
|
|
|
}
|
|
|
|
for _, v := range tc.B {
|
|
|
|
two.Add(v)
|
|
|
|
}
|
|
|
|
for _, v := range tc.Expected {
|
|
|
|
expected.Add(v)
|
|
|
|
}
|
|
|
|
|
2020-01-07 21:38:41 +01:00
|
|
|
actual := one.Difference(two)
|
|
|
|
match := actual.Intersection(expected)
|
2017-02-02 19:03:20 +01:00
|
|
|
if match.Len() != expected.Len() {
|
|
|
|
t.Fatalf("bad: %#v", actual.List())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2017-05-11 02:55:11 +02:00
|
|
|
|
|
|
|
func TestSetFilter(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
Input []interface{}
|
|
|
|
Expected []interface{}
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
[]interface{}{1, 2, 3},
|
|
|
|
[]interface{}{1, 2, 3},
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
[]interface{}{4, 5, 6},
|
|
|
|
[]interface{}{4},
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
[]interface{}{7, 8, 9},
|
|
|
|
[]interface{}{},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tc := range cases {
|
|
|
|
t.Run(fmt.Sprintf("%d-%#v", i, tc.Input), func(t *testing.T) {
|
2020-01-07 21:38:41 +01:00
|
|
|
input := make(Set)
|
|
|
|
expected := make(Set)
|
2017-05-11 02:55:11 +02:00
|
|
|
for _, v := range tc.Input {
|
|
|
|
input.Add(v)
|
|
|
|
}
|
|
|
|
for _, v := range tc.Expected {
|
|
|
|
expected.Add(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := input.Filter(func(v interface{}) bool {
|
|
|
|
return v.(int) < 5
|
|
|
|
})
|
2020-01-07 21:38:41 +01:00
|
|
|
match := actual.Intersection(expected)
|
2017-05-11 02:55:11 +02:00
|
|
|
if match.Len() != expected.Len() {
|
|
|
|
t.Fatalf("bad: %#v", actual.List())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2020-06-11 05:01:00 +02:00
|
|
|
|
|
|
|
func TestSetCopy(t *testing.T) {
|
|
|
|
a := make(Set)
|
|
|
|
a.Add(1)
|
|
|
|
a.Add(2)
|
|
|
|
|
|
|
|
b := a.Copy()
|
|
|
|
b.Add(3)
|
|
|
|
|
|
|
|
diff := b.Difference(a)
|
|
|
|
|
|
|
|
if diff.Len() != 1 {
|
|
|
|
t.Fatalf("expected single diff value, got %#v", diff)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !diff.Include(3) {
|
|
|
|
t.Fatalf("diff does not contain 3, got %#v", diff)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|