From 2d9bad95f83456c56ac36fb7604b156ca783566a Mon Sep 17 00:00:00 2001 From: Danny Jones Date: Fri, 18 Jul 2014 13:08:11 -0700 Subject: [PATCH] Added HasAll utility method for string set. Added HasAll method which detects if one set contains all of the memebers of another set. A.HasAll(B) returns true if A is a superset of B. --- pkg/api/validation_test.go | 3 ++- pkg/util/set.go | 10 ++++++++++ pkg/util/set_test.go | 10 ++++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/pkg/api/validation_test.go b/pkg/api/validation_test.go index e7a3d271e6c..38d689b7773 100644 --- a/pkg/api/validation_test.go +++ b/pkg/api/validation_test.go @@ -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) } diff --git a/pkg/util/set.go b/pkg/util/set.go index 850b7f4eb20..de49cd59bbb 100644 --- a/pkg/util/set.go +++ b/pkg/util/set.go @@ -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)) diff --git a/pkg/util/set_test.go b/pkg/util/set_test.go index fc7f49733a0..9ceb8b8326d 100644 --- a/pkg/util/set_test.go +++ b/pkg/util/set_test.go @@ -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)) } @@ -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) {