Skip to content

Commit

Permalink
Merge pull request kubernetes#522 from Sarsate/has-all
Browse files Browse the repository at this point in the history
Added HasAll utility method for string set.
  • Loading branch information
lavalamp committed Jul 18, 2014
2 parents d8faca9 + 2d9bad9 commit dd36c45
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
3 changes: 2 additions & 1 deletion pkg/api/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ func TestValidateVolumes(t *testing.T) {
if len(errs) != 0 {
t.Errorf("expected success: %v", errs)
}
if len(names) != 4 || !names.Has("abc") || !names.Has("123") || !names.Has("abc-123") || !names.Has("empty") {
expectedSet := util.NewStringSet("abc", "123", "abc-123", "empty")
if len(names) != 4 || !names.HasAll(expectedSet) {
t.Errorf("wrong names result: %v", names)
}

Expand Down
10 changes: 10 additions & 0 deletions pkg/util/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@ func (s StringSet) Has(item string) bool {
return contained
}

// HasAll returns true iff all items are contained in the set.
func (s1 StringSet) HasAll(s2 StringSet) bool {
for item, _ := range s2 {
if !s1.Has(item) {
return false
}
}
return true
}

// List returns the contents as a sorted string slice.
func (s StringSet) List() []string {
res := make([]string, 0, len(s))
Expand Down
10 changes: 10 additions & 0 deletions pkg/util/set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

func TestStringSet(t *testing.T) {
s := StringSet{}
s2 := StringSet{}
if len(s) != 0 {
t.Errorf("Expected len=0: %d", len(s))
}
Expand All @@ -41,6 +42,15 @@ func TestStringSet(t *testing.T) {
if s.Has("a") {
t.Errorf("Unexpected contents: %#v", s)
}
s.Insert("a")
s2.Insert("a","b","d")
if s.HasAll(s2) {
t.Errorf("Unexpected contents: %#v", s)
}
s2.Delete("d")
if !s.HasAll(s2) {
t.Errorf("Missing contents: %#v", s)
}
}

func TestNewStringSet(t *testing.T) {
Expand Down

0 comments on commit dd36c45

Please sign in to comment.