mirror of
https://blitiri.com.ar/repos/chasquid
synced 2025-12-19 14:57:04 +00:00
Implement a string set data type
We will need a few string sets in different places, create it as a separate package for convenience. For now, it only implements strings, but it may be extended to other data types in the future.
This commit is contained in:
40
internal/set/set_test.go
Normal file
40
internal/set/set_test.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package set
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestString(t *testing.T) {
|
||||
s1 := &String{}
|
||||
|
||||
// Test that Has works on a new set.
|
||||
if s1.Has("x") {
|
||||
t.Error("'x' is in the empty set")
|
||||
}
|
||||
|
||||
s1.Add("a")
|
||||
s1.Add("b", "ccc")
|
||||
|
||||
expectStrings(s1, []string{"a", "b", "ccc"}, []string{"notin"}, t)
|
||||
|
||||
s2 := NewString("a", "b", "c")
|
||||
expectStrings(s2, []string{"a", "b", "c"}, []string{"notin"}, t)
|
||||
|
||||
// Test that Has works (and not panics) on a nil set.
|
||||
var s3 *String
|
||||
if s3.Has("x") {
|
||||
t.Error("'x' is in the nil set")
|
||||
}
|
||||
}
|
||||
|
||||
func expectStrings(s *String, in []string, notIn []string, t *testing.T) {
|
||||
for _, str := range in {
|
||||
if !s.Has(str) {
|
||||
t.Errorf("String %q not in set, it should be", str)
|
||||
}
|
||||
}
|
||||
|
||||
for _, str := range notIn {
|
||||
if s.Has(str) {
|
||||
t.Errorf("String %q is in the set, should not be", str)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user