mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-21 10:51:29 +00:00
Merge pull request #289 from thockin/cleanups
Add a NewStringSet() function
This commit is contained in:
commit
fe4ce67aeb
@ -21,6 +21,13 @@ type empty struct{}
|
||||
// A set of strings, implemented via map[string]struct{} for minimal memory consumption.
|
||||
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.
|
||||
func (s StringSet) Insert(items ...string) {
|
||||
for _, item := range items {
|
||||
|
@ -22,7 +22,13 @@ import (
|
||||
|
||||
func TestStringSet(t *testing.T) {
|
||||
s := StringSet{}
|
||||
if len(s) != 0 {
|
||||
t.Errorf("Expected len=0: %d", len(s))
|
||||
}
|
||||
s.Insert("a", "b")
|
||||
if len(s) != 2 {
|
||||
t.Errorf("Expected len=2: %d", len(s))
|
||||
}
|
||||
s.Insert("c")
|
||||
if s.Has("d") {
|
||||
t.Errorf("Unexpected contents: %#v", s)
|
||||
@ -35,3 +41,13 @@ func TestStringSet(t *testing.T) {
|
||||
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