Skip to content

Commit

Permalink
better factory functions for Vector and Set (#89)
Browse files Browse the repository at this point in the history
  • Loading branch information
chen3feng authored Sep 1, 2022
1 parent f431e70 commit ad4c1af
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 91 deletions.
4 changes: 2 additions & 2 deletions builtin_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
// BuiltinSet is an associative container that contains an unordered set of unique objects of type K.
type BuiltinSet[K comparable] map[K]struct{}

// MakeBuiltinSetOf creates a new BuiltinSet object with the initial content from ks.
func MakeBuiltinSetOf[K comparable](ks ...K) BuiltinSet[K] {
// SetOf creates a new BuiltinSet object with the initial content from ks.
func SetOf[K comparable](ks ...K) BuiltinSet[K] {
s := make(BuiltinSet[K])
s.InsertN(ks...)
return s
Expand Down
72 changes: 34 additions & 38 deletions builtin_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ import (
"testing"
)

func setOf[T comparable](a ...T) BuiltinSet[T] {
return MakeBuiltinSetOf(a...)
}

func Test_BuiltinSet_Interface(t *testing.T) {
s := make(BuiltinSet[int])
_ = Set[int](&s)
Expand All @@ -28,7 +24,7 @@ func Test_MakeBuiltinSet2(t *testing.T) {
}

func Test_MakeBuiltinSetOf(t *testing.T) {
s := setOf("hello", "world")
s := SetOf("hello", "world")
expectEq(t, s.Len(), 2)
}

Expand All @@ -40,25 +36,25 @@ func Test_BuiltinSet_IsEmpty(t *testing.T) {
}

func Test_BuiltinSet_Clear(t *testing.T) {
s := setOf("hello", "world")
s := SetOf("hello", "world")
s.Clear()
expectTrue(t, s.IsEmpty())
}

func Test_BuiltinSet_String(t *testing.T) {
s := setOf("hello", "world")
s := SetOf("hello", "world")
expectTrue(t, strings.HasPrefix(fmt.Sprintf("%v", s), "BuiltinSet[string]"))
}

func Test_BuiltinSet_Has(t *testing.T) {
s := setOf("hello", "world")
s := SetOf("hello", "world")
expectTrue(t, s.Has("hello"))
expectTrue(t, s.Has("world"))
expectFalse(t, s.Has("!"))
}

func Test_BuiltinSet_Get(t *testing.T) {
s := setOf("hello", "world")
s := SetOf("hello", "world")
expectTrue(t, s.Has("hello"))
expectTrue(t, s.Has("world"))
expectFalse(t, s.Has("!"))
Expand All @@ -81,7 +77,7 @@ func Test_BuiltinSet_InsertN(t *testing.T) {
}

func Test_BuiltinSet_Remove(t *testing.T) {
s := setOf("hello", "world")
s := SetOf("hello", "world")
s.Remove("hello")
expectEq(t, s.Len(), 1)
s.Remove("hello")
Expand All @@ -91,27 +87,27 @@ func Test_BuiltinSet_Remove(t *testing.T) {
}

func Test_BuiltinSet_RemoveN(t *testing.T) {
s := setOf("hello", "world")
s := SetOf("hello", "world")
s.RemoveN("hello", "world")
s.Remove("world")
expectTrue(t, s.IsEmpty())
}

func Test_BuiltinSet_Keys(t *testing.T) {
s := setOf("hello", "world")
s := SetOf("hello", "world")
ks := s.Keys()
expectEq(t, 2, len(ks))
}

func Test_BuiltinSet_For(t *testing.T) {
s := setOf("hello", "world")
s := SetOf("hello", "world")
for v := range s {
expectTrue(t, v == "hello" || v == "world")
}
}

func Test_BuiltinSet_ForEach(t *testing.T) {
s := setOf("hello", "world")
s := SetOf("hello", "world")
c := 0
s.ForEach(func(string) {
c++
Expand All @@ -120,7 +116,7 @@ func Test_BuiltinSet_ForEach(t *testing.T) {
}

func Test_BuiltinSet_ForEachIf(t *testing.T) {
s := setOf("hello", "world")
s := SetOf("hello", "world")
c := 0
s.ForEachIf(func(string) bool {
c++
Expand All @@ -130,60 +126,60 @@ func Test_BuiltinSet_ForEachIf(t *testing.T) {
}

func Test_BuiltinSet_Update(t *testing.T) {
s := setOf(1, 2, 3)
s.Update(setOf(3, 4))
s := SetOf(1, 2, 3)
s.Update(SetOf(3, 4))
expectEq(t, s.Len(), 4)
expectTrue(t, s.Has(4))
}

func Test_BuiltinSet_Union(t *testing.T) {
s := setOf(1, 2, 3)
s2 := s.Union(setOf(3, 4))
s := SetOf(1, 2, 3)
s2 := s.Union(SetOf(3, 4))
expectEq(t, s2.Len(), 4)
expectTrue(t, s2.Has(4))
}

func Test_BuiltinSet_Intersection(t *testing.T) {
s := setOf(1, 2, 3).Intersection(setOf(3, 4))
s := SetOf(1, 2, 3).Intersection(SetOf(3, 4))
expectEq(t, s.Len(), 1)
expectTrue(t, s.Has(3))
s = setOf(3, 4).Intersection(setOf(1, 2, 3))
s = SetOf(3, 4).Intersection(SetOf(1, 2, 3))
expectEq(t, s.Len(), 1)
expectTrue(t, s.Has(3))
}

func Test_BuiltinSet_Difference(t *testing.T) {
s := setOf(1, 2, 3).Difference(setOf(3, 4))
s := SetOf(1, 2, 3).Difference(SetOf(3, 4))
expectEq(t, s.Len(), 2)
expectTrue(t, s.Has(1))
expectTrue(t, s.Has(2))
s = setOf(1, 2).Difference(setOf(3, 4))
s = SetOf(1, 2).Difference(SetOf(3, 4))
expectEq(t, s.Len(), 2)
expectTrue(t, s.Has(1))
expectTrue(t, s.Has(2))
}

func Test_BuiltinSet_IsDisjointOf(t *testing.T) {
s1 := setOf(1, 2, 3)
s2 := setOf(3, 4)
s1 := SetOf(1, 2, 3)
s2 := SetOf(3, 4)
expectFalse(t, s1.IsDisjointOf(s2))
expectTrue(t, s1.IsDisjointOf(setOf(4, 5)))
expectTrue(t, s1.IsDisjointOf(SetOf(4, 5)))
}

func Test_BuiltinSet_IsSubsetOf(t *testing.T) {
expectTrue(t, setOf[int]().IsSubsetOf(setOf[int]()))
expectTrue(t, setOf[int]().IsSubsetOf(setOf(1)))
expectTrue(t, setOf(1, 2, 3).IsSubsetOf(setOf(1, 2, 3)))
expectTrue(t, setOf(1, 2).IsSubsetOf(setOf(1, 2, 3)))
expectFalse(t, setOf(1, 2, 3).IsSubsetOf(setOf(1, 2)))
expectFalse(t, setOf(1, 2).IsSubsetOf(setOf(2, 3)))
expectTrue(t, SetOf[int]().IsSubsetOf(SetOf[int]()))
expectTrue(t, SetOf[int]().IsSubsetOf(SetOf(1)))
expectTrue(t, SetOf(1, 2, 3).IsSubsetOf(SetOf(1, 2, 3)))
expectTrue(t, SetOf(1, 2).IsSubsetOf(SetOf(1, 2, 3)))
expectFalse(t, SetOf(1, 2, 3).IsSubsetOf(SetOf(1, 2)))
expectFalse(t, SetOf(1, 2).IsSubsetOf(SetOf(2, 3)))
}

func Test_BuiltinSet_IsSupersetOf(t *testing.T) {
expectTrue(t, setOf[int]().IsSupersetOf(setOf[int]()))
expectTrue(t, setOf(1).IsSupersetOf(setOf[int]()))
expectTrue(t, setOf(1, 2, 3).IsSupersetOf(setOf(1, 2, 3)))
expectTrue(t, setOf(1, 2, 3).IsSupersetOf(setOf(1, 2)))
expectFalse(t, setOf(1, 2).IsSupersetOf(setOf(1, 2, 3)))
expectFalse(t, setOf(1, 2).IsSupersetOf(setOf(2, 3)))
expectTrue(t, SetOf[int]().IsSupersetOf(SetOf[int]()))
expectTrue(t, SetOf(1).IsSupersetOf(SetOf[int]()))
expectTrue(t, SetOf(1, 2, 3).IsSupersetOf(SetOf(1, 2, 3)))
expectTrue(t, SetOf(1, 2, 3).IsSupersetOf(SetOf(1, 2)))
expectFalse(t, SetOf(1, 2).IsSupersetOf(SetOf(1, 2, 3)))
expectFalse(t, SetOf(1, 2).IsSupersetOf(SetOf(2, 3)))
}
Loading

0 comments on commit ad4c1af

Please sign in to comment.