mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-23 03:41:45 +00:00
Add a NewStringSet() function
Also beef up tests to cover len(ss).
This commit is contained in:
parent
73a494c928
commit
b65d685a39
@ -21,6 +21,13 @@ type empty struct{}
|
|||||||
// A set of strings, implemented via map[string]struct{} for minimal memory consumption.
|
// A set of strings, implemented via map[string]struct{} for minimal memory consumption.
|
||||||
type StringSet map[string]empty
|
type StringSet map[string]empty
|
||||||
|
|
||||||
|
// NewStringSet creates a StringSet from a list of values.
|
||||||
|
func NewStringSet(items ...string) StringSet {
|
||||||
|
ss := StringSet{}
|
||||||
|
ss.Insert(items...)
|
||||||
|
return ss
|
||||||
|
}
|
||||||
|
|
||||||
// Insert adds items to the set.
|
// Insert adds items to the set.
|
||||||
func (s StringSet) Insert(items ...string) {
|
func (s StringSet) Insert(items ...string) {
|
||||||
for _, item := range items {
|
for _, item := range items {
|
||||||
|
@ -22,7 +22,13 @@ import (
|
|||||||
|
|
||||||
func TestStringSet(t *testing.T) {
|
func TestStringSet(t *testing.T) {
|
||||||
s := StringSet{}
|
s := StringSet{}
|
||||||
|
if len(s) != 0 {
|
||||||
|
t.Errorf("Expected len=0: %d", len(s))
|
||||||
|
}
|
||||||
s.Insert("a", "b")
|
s.Insert("a", "b")
|
||||||
|
if len(s) != 2 {
|
||||||
|
t.Errorf("Expected len=2: %d", len(s))
|
||||||
|
}
|
||||||
s.Insert("c")
|
s.Insert("c")
|
||||||
if s.Has("d") {
|
if s.Has("d") {
|
||||||
t.Errorf("Unexpected contents: %#v", s)
|
t.Errorf("Unexpected contents: %#v", s)
|
||||||
@ -35,3 +41,13 @@ func TestStringSet(t *testing.T) {
|
|||||||
t.Errorf("Unexpected contents: %#v", s)
|
t.Errorf("Unexpected contents: %#v", s)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestNewStringSet(t *testing.T) {
|
||||||
|
s := NewStringSet("a", "b", "c")
|
||||||
|
if len(s) != 3 {
|
||||||
|
t.Errorf("Expected len=3: %d", len(s))
|
||||||
|
}
|
||||||
|
if !s.Has("a") || !s.Has("b") || !s.Has("c") {
|
||||||
|
t.Errorf("Unexpected contents: %#v", s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user