...

Source file src/k8s.io/apimachinery/pkg/util/sets/set_generic_test.go

Documentation: k8s.io/apimachinery/pkg/util/sets

     1  /*
     2  Copyright 2022 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package sets_test
    18  
    19  import (
    20  	"reflect"
    21  	"testing"
    22  
    23  	"k8s.io/apimachinery/pkg/util/sets"
    24  )
    25  
    26  func TestSet(t *testing.T) {
    27  	s := sets.Set[string]{}
    28  	s2 := sets.Set[string]{}
    29  	if len(s) != 0 {
    30  		t.Errorf("Expected len=0: %d", len(s))
    31  	}
    32  	s.Insert("a", "b")
    33  	if len(s) != 2 {
    34  		t.Errorf("Expected len=2: %d", len(s))
    35  	}
    36  	s.Insert("c")
    37  	if s.Has("d") {
    38  		t.Errorf("Unexpected contents: %#v", s)
    39  	}
    40  	if !s.Has("a") {
    41  		t.Errorf("Missing contents: %#v", s)
    42  	}
    43  	s.Delete("a")
    44  	if s.Has("a") {
    45  		t.Errorf("Unexpected contents: %#v", s)
    46  	}
    47  	s.Insert("a")
    48  	if s.HasAll("a", "b", "d") {
    49  		t.Errorf("Unexpected contents: %#v", s)
    50  	}
    51  	if !s.HasAll("a", "b") {
    52  		t.Errorf("Missing contents: %#v", s)
    53  	}
    54  	s2.Insert("a", "b", "d")
    55  	if s.IsSuperset(s2) {
    56  		t.Errorf("Unexpected contents: %#v", s)
    57  	}
    58  	s2.Delete("d")
    59  	if !s.IsSuperset(s2) {
    60  		t.Errorf("Missing contents: %#v", s)
    61  	}
    62  }
    63  
    64  func TestSetDeleteMultiples(t *testing.T) {
    65  	s := sets.Set[string]{}
    66  	s.Insert("a", "b", "c")
    67  	if len(s) != 3 {
    68  		t.Errorf("Expected len=3: %d", len(s))
    69  	}
    70  
    71  	s.Delete("a", "c")
    72  	if len(s) != 1 {
    73  		t.Errorf("Expected len=1: %d", len(s))
    74  	}
    75  	if s.Has("a") {
    76  		t.Errorf("Unexpected contents: %#v", s)
    77  	}
    78  	if s.Has("c") {
    79  		t.Errorf("Unexpected contents: %#v", s)
    80  	}
    81  	if !s.Has("b") {
    82  		t.Errorf("Missing contents: %#v", s)
    83  	}
    84  
    85  }
    86  
    87  func TestSetClear(t *testing.T) {
    88  	s := sets.Set[string]{}
    89  	s.Insert("a", "b", "c")
    90  	if s.Len() != 3 {
    91  		t.Errorf("Expected len=3: %d", s.Len())
    92  	}
    93  
    94  	s.Clear()
    95  	if s.Len() != 0 {
    96  		t.Errorf("Expected len=0: %d", s.Len())
    97  	}
    98  }
    99  
   100  func TestSetClearWithSharedReference(t *testing.T) {
   101  	s := sets.Set[string]{}
   102  	s.Insert("a", "b", "c")
   103  	if s.Len() != 3 {
   104  		t.Errorf("Expected len=3: %d", s.Len())
   105  	}
   106  
   107  	m := s
   108  	s.Clear()
   109  	if s.Len() != 0 {
   110  		t.Errorf("Expected len=0 on the cleared set: %d", s.Len())
   111  	}
   112  	if m.Len() != 0 {
   113  		t.Errorf("Expected len=0 on the shared reference: %d", m.Len())
   114  	}
   115  }
   116  
   117  func TestSetClearInSeparateFunction(t *testing.T) {
   118  	s := sets.Set[string]{}
   119  	s.Insert("a", "b", "c")
   120  	if s.Len() != 3 {
   121  		t.Errorf("Expected len=3: %d", s.Len())
   122  	}
   123  
   124  	clearSetAndAdd(s, "d")
   125  	if s.Len() != 1 {
   126  		t.Errorf("Expected len=1: %d", s.Len())
   127  	}
   128  	if !s.Has("d") {
   129  		t.Errorf("Unexpected contents: %#v", s)
   130  	}
   131  }
   132  
   133  func clearSetAndAdd[T comparable](s sets.Set[T], a T) {
   134  	s.Clear()
   135  	s.Insert(a)
   136  }
   137  
   138  func TestNewSet(t *testing.T) {
   139  	s := sets.New("a", "b", "c")
   140  	if len(s) != 3 {
   141  		t.Errorf("Expected len=3: %d", len(s))
   142  	}
   143  	if !s.Has("a") || !s.Has("b") || !s.Has("c") {
   144  		t.Errorf("Unexpected contents: %#v", s)
   145  	}
   146  }
   147  
   148  func TestKeySet(t *testing.T) {
   149  	m := map[string]int{"a": 1, "b": 2, "c": 3}
   150  	ss := sets.KeySet[string](m)
   151  	if !ss.Equal(sets.New("a", "b", "c")) {
   152  		t.Errorf("Unexpected contents: %#v", sets.List(ss))
   153  	}
   154  }
   155  
   156  func TestNewEmptySet(t *testing.T) {
   157  	s := sets.New[string]()
   158  	if len(s) != 0 {
   159  		t.Errorf("Expected len=0: %d", len(s))
   160  	}
   161  	s.Insert("a", "b", "c")
   162  	if len(s) != 3 {
   163  		t.Errorf("Expected len=3: %d", len(s))
   164  	}
   165  	if !s.Has("a") || !s.Has("b") || !s.Has("c") {
   166  		t.Errorf("Unexpected contents: %#v", s)
   167  	}
   168  }
   169  
   170  func TestSortedList(t *testing.T) {
   171  	s := sets.New("z", "y", "x", "a")
   172  	if !reflect.DeepEqual(sets.List(s), []string{"a", "x", "y", "z"}) {
   173  		t.Errorf("List gave unexpected result: %#v", sets.List(s))
   174  	}
   175  }
   176  
   177  func TestSetDifference(t *testing.T) {
   178  	a := sets.New("1", "2", "3")
   179  	b := sets.New("1", "2", "4", "5")
   180  	c := a.Difference(b)
   181  	d := b.Difference(a)
   182  	if len(c) != 1 {
   183  		t.Errorf("Expected len=1: %d", len(c))
   184  	}
   185  	if !c.Has("3") {
   186  		t.Errorf("Unexpected contents: %#v", sets.List(c))
   187  	}
   188  	if len(d) != 2 {
   189  		t.Errorf("Expected len=2: %d", len(d))
   190  	}
   191  	if !d.Has("4") || !d.Has("5") {
   192  		t.Errorf("Unexpected contents: %#v", sets.List(d))
   193  	}
   194  }
   195  
   196  func TestSetSymmetricDifference(t *testing.T) {
   197  	a := sets.New("1", "2", "3")
   198  	b := sets.New("1", "2", "4", "5")
   199  	c := a.SymmetricDifference(b)
   200  	d := b.SymmetricDifference(a)
   201  	if !c.Equal(sets.New("3", "4", "5")) {
   202  		t.Errorf("Unexpected contents: %#v", sets.List(c))
   203  	}
   204  	if !d.Equal(sets.New("3", "4", "5")) {
   205  		t.Errorf("Unexpected contents: %#v", sets.List(d))
   206  	}
   207  }
   208  
   209  func TestSetHasAny(t *testing.T) {
   210  	a := sets.New("1", "2", "3")
   211  
   212  	if !a.HasAny("1", "4") {
   213  		t.Errorf("expected true, got false")
   214  	}
   215  
   216  	if a.HasAny("0", "4") {
   217  		t.Errorf("expected false, got true")
   218  	}
   219  }
   220  
   221  func TestSetEquals(t *testing.T) {
   222  	// Simple case (order doesn't matter)
   223  	a := sets.New("1", "2")
   224  	b := sets.New("2", "1")
   225  	if !a.Equal(b) {
   226  		t.Errorf("Expected to be equal: %v vs %v", a, b)
   227  	}
   228  
   229  	// It is a set; duplicates are ignored
   230  	b = sets.New("2", "2", "1")
   231  	if !a.Equal(b) {
   232  		t.Errorf("Expected to be equal: %v vs %v", a, b)
   233  	}
   234  
   235  	// Edge cases around empty sets / empty strings
   236  	a = sets.New[string]()
   237  	b = sets.New[string]()
   238  	if !a.Equal(b) {
   239  		t.Errorf("Expected to be equal: %v vs %v", a, b)
   240  	}
   241  
   242  	b = sets.New("1", "2", "3")
   243  	if a.Equal(b) {
   244  		t.Errorf("Expected to be not-equal: %v vs %v", a, b)
   245  	}
   246  
   247  	b = sets.New("1", "2", "")
   248  	if a.Equal(b) {
   249  		t.Errorf("Expected to be not-equal: %v vs %v", a, b)
   250  	}
   251  
   252  	// Check for equality after mutation
   253  	a = sets.New[string]()
   254  	a.Insert("1")
   255  	if a.Equal(b) {
   256  		t.Errorf("Expected to be not-equal: %v vs %v", a, b)
   257  	}
   258  
   259  	a.Insert("2")
   260  	if a.Equal(b) {
   261  		t.Errorf("Expected to be not-equal: %v vs %v", a, b)
   262  	}
   263  
   264  	a.Insert("")
   265  	if !a.Equal(b) {
   266  		t.Errorf("Expected to be equal: %v vs %v", a, b)
   267  	}
   268  
   269  	a.Delete("")
   270  	if a.Equal(b) {
   271  		t.Errorf("Expected to be not-equal: %v vs %v", a, b)
   272  	}
   273  }
   274  
   275  func TestUnion(t *testing.T) {
   276  	tests := []struct {
   277  		s1       sets.Set[string]
   278  		s2       sets.Set[string]
   279  		expected sets.Set[string]
   280  	}{
   281  		{
   282  			sets.New("1", "2", "3", "4"),
   283  			sets.New("3", "4", "5", "6"),
   284  			sets.New("1", "2", "3", "4", "5", "6"),
   285  		},
   286  		{
   287  			sets.New("1", "2", "3", "4"),
   288  			sets.New[string](),
   289  			sets.New("1", "2", "3", "4"),
   290  		},
   291  		{
   292  			sets.New[string](),
   293  			sets.New("1", "2", "3", "4"),
   294  			sets.New("1", "2", "3", "4"),
   295  		},
   296  		{
   297  			sets.New[string](),
   298  			sets.New[string](),
   299  			sets.New[string](),
   300  		},
   301  	}
   302  
   303  	for _, test := range tests {
   304  		union := test.s1.Union(test.s2)
   305  		if union.Len() != test.expected.Len() {
   306  			t.Errorf("Expected union.Len()=%d but got %d", test.expected.Len(), union.Len())
   307  		}
   308  
   309  		if !union.Equal(test.expected) {
   310  			t.Errorf("Expected union.Equal(expected) but not true.  union:%v expected:%v", sets.List(union), sets.List(test.expected))
   311  		}
   312  	}
   313  }
   314  
   315  func TestIntersection(t *testing.T) {
   316  	tests := []struct {
   317  		s1       sets.Set[string]
   318  		s2       sets.Set[string]
   319  		expected sets.Set[string]
   320  	}{
   321  		{
   322  			sets.New("1", "2", "3", "4"),
   323  			sets.New("3", "4", "5", "6"),
   324  			sets.New("3", "4"),
   325  		},
   326  		{
   327  			sets.New("1", "2", "3", "4"),
   328  			sets.New("1", "2", "3", "4"),
   329  			sets.New("1", "2", "3", "4"),
   330  		},
   331  		{
   332  			sets.New("1", "2", "3", "4"),
   333  			sets.New[string](),
   334  			sets.New[string](),
   335  		},
   336  		{
   337  			sets.New[string](),
   338  			sets.New("1", "2", "3", "4"),
   339  			sets.New[string](),
   340  		},
   341  		{
   342  			sets.New[string](),
   343  			sets.New[string](),
   344  			sets.New[string](),
   345  		},
   346  	}
   347  
   348  	for _, test := range tests {
   349  		intersection := test.s1.Intersection(test.s2)
   350  		if intersection.Len() != test.expected.Len() {
   351  			t.Errorf("Expected intersection.Len()=%d but got %d", test.expected.Len(), intersection.Len())
   352  		}
   353  
   354  		if !intersection.Equal(test.expected) {
   355  			t.Errorf("Expected intersection.Equal(expected) but not true.  intersection:%v expected:%v", sets.List(intersection), sets.List(intersection))
   356  		}
   357  	}
   358  }
   359  

View as plain text