2014-10-06 22:29:52 +00:00
|
|
|
package seq
|
|
|
|
|
|
|
|
import (
|
|
|
|
. "testing"
|
|
|
|
|
|
|
|
"github.com/mediocregopher/ginger/types"
|
|
|
|
)
|
|
|
|
|
2014-10-19 00:03:16 +00:00
|
|
|
// Test that HashSet implements types.Elem (compile-time check)
|
|
|
|
func TestSetElem(t *T) {
|
|
|
|
_ = types.Elem(NewSet())
|
|
|
|
}
|
|
|
|
|
2014-10-06 22:29:52 +00:00
|
|
|
// Test creating a Set and calling the Seq interface methods on it
|
|
|
|
func TestSetSeq(t *T) {
|
2014-10-19 00:03:16 +00:00
|
|
|
ints := elemSliceV(nil, 1, "a", 5.0)
|
2014-10-06 22:29:52 +00:00
|
|
|
|
|
|
|
// Testing creation and Seq interface methods
|
|
|
|
s := NewSet(ints...)
|
|
|
|
ss := testSeqNoOrderGen(t, s, ints)
|
|
|
|
|
|
|
|
// ss should be empty at this point
|
|
|
|
s = ToSet(ss)
|
|
|
|
var nilpointer *Set
|
|
|
|
assertEmpty(s, t)
|
|
|
|
assertValue(s, nilpointer, t)
|
|
|
|
assertValue(len(ToSlice(s)), 0, t)
|
|
|
|
}
|
|
|
|
|
2014-10-19 00:03:16 +00:00
|
|
|
// Test that the Equal method on Sets works
|
|
|
|
func TestSetEqual(t *T) {
|
|
|
|
s, s2 := NewSet(), NewSet()
|
|
|
|
assertValue(s.Equal(s2), true, t)
|
|
|
|
assertValue(s2.Equal(s), true, t)
|
|
|
|
|
|
|
|
s = NewSet(elemSliceV(0, 1, 2)...)
|
|
|
|
assertValue(s.Equal(s2), false, t)
|
|
|
|
assertValue(s2.Equal(s), false, t)
|
|
|
|
|
|
|
|
s2 = NewSet(elemSliceV(0, 1)...)
|
|
|
|
assertValue(s.Equal(s2), false, t)
|
|
|
|
assertValue(s2.Equal(s), false, t)
|
|
|
|
|
|
|
|
s2 = NewSet(elemSliceV(0, 1, 3)...)
|
|
|
|
assertValue(s.Equal(s2), false, t)
|
|
|
|
assertValue(s2.Equal(s), false, t)
|
|
|
|
|
|
|
|
s2 = NewSet(elemSliceV(0, 1, 2)...)
|
|
|
|
assertValue(s.Equal(s2), true, t)
|
|
|
|
assertValue(s2.Equal(s), true, t)
|
|
|
|
}
|
|
|
|
|
2014-10-06 22:29:52 +00:00
|
|
|
// Test setting a value on a Set
|
|
|
|
func TestSetVal(t *T) {
|
2014-10-19 00:03:16 +00:00
|
|
|
ints := elemSliceV(0, 1, 2, 3, 4)
|
|
|
|
ints1 := elemSliceV(0, 1, 2, 3, 4, 5)
|
2014-10-06 22:29:52 +00:00
|
|
|
|
|
|
|
// Degenerate case
|
|
|
|
s := NewSet()
|
|
|
|
assertEmpty(s, t)
|
2014-10-19 00:03:16 +00:00
|
|
|
s, ok := s.SetVal(types.GoType{0})
|
|
|
|
assertSeqContentsSet(s, elemSliceV(0), t)
|
2014-10-06 22:29:52 +00:00
|
|
|
assertValue(ok, true, t)
|
|
|
|
|
|
|
|
s = NewSet(ints...)
|
2014-10-19 00:03:16 +00:00
|
|
|
s1, ok := s.SetVal(types.GoType{5})
|
2014-10-06 22:29:52 +00:00
|
|
|
assertSeqContentsSet(s, ints, t)
|
|
|
|
assertSeqContentsSet(s1, ints1, t)
|
|
|
|
assertValue(ok, true, t)
|
|
|
|
|
2014-10-19 00:03:16 +00:00
|
|
|
s2, ok := s1.SetVal(types.GoType{5})
|
2014-10-06 22:29:52 +00:00
|
|
|
assertSeqContentsSet(s1, ints1, t)
|
|
|
|
assertSeqContentsSet(s2, ints1, t)
|
|
|
|
assertValue(ok, false, t)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test deleting a value from a Set
|
|
|
|
func TestDelVal(t *T) {
|
2014-10-19 00:03:16 +00:00
|
|
|
ints := elemSliceV(0, 1, 2, 3, 4)
|
|
|
|
ints1 := elemSliceV(0, 1, 2, 3)
|
|
|
|
ints2 := elemSliceV(1, 2, 3, 4)
|
|
|
|
ints3 := elemSliceV(1, 2, 3, 4, 5)
|
2014-10-06 22:29:52 +00:00
|
|
|
|
|
|
|
// Degenerate case
|
|
|
|
s := NewSet()
|
|
|
|
assertEmpty(s, t)
|
2014-10-19 00:03:16 +00:00
|
|
|
s, ok := s.DelVal(types.GoType{0})
|
2014-10-06 22:29:52 +00:00
|
|
|
assertEmpty(s, t)
|
|
|
|
assertValue(ok, false, t)
|
|
|
|
|
|
|
|
s = NewSet(ints...)
|
2014-10-19 00:03:16 +00:00
|
|
|
s1, ok := s.DelVal(types.GoType{4})
|
2014-10-06 22:29:52 +00:00
|
|
|
assertSeqContentsSet(s, ints, t)
|
|
|
|
assertSeqContentsSet(s1, ints1, t)
|
|
|
|
assertValue(ok, true, t)
|
|
|
|
|
2014-10-19 00:03:16 +00:00
|
|
|
s1, ok = s1.DelVal(types.GoType{4})
|
2014-10-06 22:29:52 +00:00
|
|
|
assertSeqContentsSet(s1, ints1, t)
|
|
|
|
assertValue(ok, false, t)
|
|
|
|
|
|
|
|
// 0 is the value on the root node of s, which is kind of a special case. We
|
|
|
|
// want to test deleting it and setting a new value (which should get put on
|
|
|
|
// the root node).
|
2014-10-19 00:03:16 +00:00
|
|
|
s2, ok := s.DelVal(types.GoType{0})
|
2014-10-06 22:29:52 +00:00
|
|
|
assertSeqContentsSet(s, ints, t)
|
|
|
|
assertSeqContentsSet(s2, ints2, t)
|
|
|
|
assertValue(ok, true, t)
|
|
|
|
|
2014-10-19 00:03:16 +00:00
|
|
|
s2, ok = s2.DelVal(types.GoType{0})
|
2014-10-06 22:29:52 +00:00
|
|
|
assertSeqContentsSet(s2, ints2, t)
|
|
|
|
assertValue(ok, false, t)
|
|
|
|
|
2014-10-19 00:03:16 +00:00
|
|
|
s3, ok := s2.SetVal(types.GoType{5})
|
2014-10-06 22:29:52 +00:00
|
|
|
assertSeqContentsSet(s2, ints2, t)
|
|
|
|
assertSeqContentsSet(s3, ints3, t)
|
|
|
|
assertValue(ok, true, t)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test getting values from a Set
|
|
|
|
func GetVal(t *T) {
|
|
|
|
//Degenerate case
|
|
|
|
s := NewSet()
|
2014-10-19 00:03:16 +00:00
|
|
|
v, ok := s.GetVal(types.GoType{1})
|
2014-10-06 22:29:52 +00:00
|
|
|
assertValue(v, nil, t)
|
|
|
|
assertValue(ok, false, t)
|
|
|
|
|
2014-10-19 00:03:16 +00:00
|
|
|
s = NewSet(elemSliceV(0, 1, 2, 3, 4)...)
|
|
|
|
v, ok = s.GetVal(types.GoType{1})
|
2014-10-06 22:29:52 +00:00
|
|
|
assertValue(v, 1, t)
|
|
|
|
assertValue(ok, true, t)
|
|
|
|
|
|
|
|
// After delete
|
2014-10-19 00:03:16 +00:00
|
|
|
s, _ = s.DelVal(types.GoType{1})
|
|
|
|
v, ok = s.GetVal(types.GoType{1})
|
2014-10-06 22:29:52 +00:00
|
|
|
assertValue(v, nil, t)
|
|
|
|
assertValue(ok, false, t)
|
|
|
|
|
|
|
|
// After set
|
2014-10-19 00:03:16 +00:00
|
|
|
s, _ = s.SetVal(types.GoType{1})
|
|
|
|
v, ok = s.GetVal(types.GoType{1})
|
2014-10-06 22:29:52 +00:00
|
|
|
assertValue(v, 1, t)
|
|
|
|
assertValue(ok, true, t)
|
|
|
|
|
|
|
|
// After delete root node
|
2014-10-19 00:03:16 +00:00
|
|
|
s, _ = s.DelVal(types.GoType{0})
|
|
|
|
v, ok = s.GetVal(types.GoType{0})
|
2014-10-06 22:29:52 +00:00
|
|
|
assertValue(v, nil, t)
|
|
|
|
assertValue(ok, false, t)
|
|
|
|
|
|
|
|
// After set root node
|
2014-10-19 00:03:16 +00:00
|
|
|
s, _ = s.SetVal(types.GoType{5})
|
|
|
|
v, ok = s.GetVal(types.GoType{5})
|
2014-10-06 22:29:52 +00:00
|
|
|
assertValue(v, 5, t)
|
|
|
|
assertValue(ok, true, t)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test that Size functions properly for all cases
|
|
|
|
func TestSetSize(t *T) {
|
|
|
|
// Degenerate case
|
|
|
|
s := NewSet()
|
|
|
|
assertValue(s.Size(), uint64(0), t)
|
|
|
|
|
|
|
|
// Initialization case
|
2014-10-19 00:03:16 +00:00
|
|
|
s = NewSet(elemSliceV(0, 1, 2)...)
|
2014-10-06 22:29:52 +00:00
|
|
|
assertValue(s.Size(), uint64(3), t)
|
|
|
|
|
|
|
|
// Setting (both value not in and a value already in)
|
2014-10-19 00:03:16 +00:00
|
|
|
s, _ = s.SetVal(types.GoType{3})
|
2014-10-06 22:29:52 +00:00
|
|
|
assertValue(s.Size(), uint64(4), t)
|
2014-10-19 00:03:16 +00:00
|
|
|
s, _ = s.SetVal(types.GoType{3})
|
2014-10-06 22:29:52 +00:00
|
|
|
assertValue(s.Size(), uint64(4), t)
|
|
|
|
|
|
|
|
// Deleting (both value already in and a value not in)
|
2014-10-19 00:03:16 +00:00
|
|
|
s, _ = s.DelVal(types.GoType{3})
|
2014-10-06 22:29:52 +00:00
|
|
|
assertValue(s.Size(), uint64(3), t)
|
2014-10-19 00:03:16 +00:00
|
|
|
s, _ = s.DelVal(types.GoType{3})
|
2014-10-06 22:29:52 +00:00
|
|
|
assertValue(s.Size(), uint64(3), t)
|
|
|
|
|
|
|
|
// Deleting and setting the root node
|
2014-10-19 00:03:16 +00:00
|
|
|
s, _ = s.DelVal(types.GoType{0})
|
2014-10-06 22:29:52 +00:00
|
|
|
assertValue(s.Size(), uint64(2), t)
|
2014-10-19 00:03:16 +00:00
|
|
|
s, _ = s.SetVal(types.GoType{5})
|
2014-10-06 22:29:52 +00:00
|
|
|
assertValue(s.Size(), uint64(3), t)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test that Union functions properly
|
|
|
|
func TestUnion(t *T) {
|
|
|
|
// Degenerate case
|
|
|
|
empty := NewSet()
|
|
|
|
assertEmpty(empty.Union(empty), t)
|
|
|
|
|
2014-10-19 00:03:16 +00:00
|
|
|
ints1 := elemSliceV(0, 1, 2)
|
|
|
|
ints2 := elemSliceV(3, 4, 5)
|
2014-10-06 22:29:52 +00:00
|
|
|
intsu := append(ints1, ints2...)
|
|
|
|
s1 := NewSet(ints1...)
|
|
|
|
s2 := NewSet(ints2...)
|
|
|
|
|
|
|
|
assertSeqContentsSet(s1.Union(empty), ints1, t)
|
|
|
|
assertSeqContentsSet(empty.Union(s1), ints1, t)
|
|
|
|
|
|
|
|
su := s1.Union(s2)
|
|
|
|
assertSeqContentsSet(s1, ints1, t)
|
|
|
|
assertSeqContentsSet(s2, ints2, t)
|
|
|
|
assertSeqContentsSet(su, intsu, t)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test that Intersection functions properly
|
|
|
|
func TestIntersection(t *T) {
|
|
|
|
// Degenerate case
|
|
|
|
empty := NewSet()
|
|
|
|
assertEmpty(empty.Intersection(empty), t)
|
|
|
|
|
2014-10-19 00:03:16 +00:00
|
|
|
ints1 := elemSliceV(0, 1, 2)
|
|
|
|
ints2 := elemSliceV(1, 2, 3)
|
|
|
|
ints3 := elemSliceV(4, 5, 6)
|
|
|
|
intsi := elemSliceV(1, 2)
|
2014-10-06 22:29:52 +00:00
|
|
|
s1 := NewSet(ints1...)
|
|
|
|
s2 := NewSet(ints2...)
|
|
|
|
s3 := NewSet(ints3...)
|
|
|
|
|
|
|
|
assertEmpty(s1.Intersection(empty), t)
|
|
|
|
assertEmpty(empty.Intersection(s1), t)
|
|
|
|
|
|
|
|
si := s1.Intersection(s2)
|
|
|
|
assertEmpty(s1.Intersection(s3), t)
|
|
|
|
assertSeqContentsSet(s1, ints1, t)
|
|
|
|
assertSeqContentsSet(s2, ints2, t)
|
|
|
|
assertSeqContentsSet(s3, ints3, t)
|
|
|
|
assertSeqContentsSet(si, intsi, t)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test that Difference functions properly
|
|
|
|
func TestDifference(t *T) {
|
|
|
|
// Degenerate case
|
|
|
|
empty := NewSet()
|
|
|
|
assertEmpty(empty.Difference(empty), t)
|
|
|
|
|
2014-10-19 00:03:16 +00:00
|
|
|
ints1 := elemSliceV(0, 1, 2, 3)
|
|
|
|
ints2 := elemSliceV(2, 3, 4)
|
|
|
|
intsd := elemSliceV(0, 1)
|
2014-10-06 22:29:52 +00:00
|
|
|
s1 := NewSet(ints1...)
|
|
|
|
s2 := NewSet(ints2...)
|
|
|
|
|
|
|
|
assertSeqContentsSet(s1.Difference(empty), ints1, t)
|
|
|
|
assertEmpty(empty.Difference(s1), t)
|
|
|
|
|
|
|
|
sd := s1.Difference(s2)
|
|
|
|
assertSeqContentsSet(s1, ints1, t)
|
|
|
|
assertSeqContentsSet(s2, ints2, t)
|
|
|
|
assertSeqContentsSet(sd, intsd, t)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test that SymDifference functions properly
|
|
|
|
func TestSymDifference(t *T) {
|
|
|
|
// Degenerate case
|
|
|
|
empty := NewSet()
|
|
|
|
assertEmpty(empty.SymDifference(empty), t)
|
|
|
|
|
2014-10-19 00:03:16 +00:00
|
|
|
ints1 := elemSliceV(0, 1, 2, 3)
|
|
|
|
ints2 := elemSliceV(2, 3, 4)
|
|
|
|
intsd := elemSliceV(0, 1, 4)
|
2014-10-06 22:29:52 +00:00
|
|
|
s1 := NewSet(ints1...)
|
|
|
|
s2 := NewSet(ints2...)
|
|
|
|
|
|
|
|
assertSeqContentsSet(s1.SymDifference(empty), ints1, t)
|
|
|
|
assertSeqContentsSet(empty.SymDifference(s1), ints1, t)
|
|
|
|
|
|
|
|
sd := s1.SymDifference(s2)
|
|
|
|
assertSeqContentsSet(s1, ints1, t)
|
|
|
|
assertSeqContentsSet(s2, ints2, t)
|
|
|
|
assertSeqContentsSet(sd, intsd, t)
|
|
|
|
}
|