github.com/spotmaxtech/k8s-apimachinery-v0260@v0.0.1/pkg/util/sets/set_generic_test.go (about)

     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  	"github.com/spotmaxtech/k8s-apimachinery-v0260/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 TestNewSet(t *testing.T) {
    88  	s := sets.New("a", "b", "c")
    89  	if len(s) != 3 {
    90  		t.Errorf("Expected len=3: %d", len(s))
    91  	}
    92  	if !s.Has("a") || !s.Has("b") || !s.Has("c") {
    93  		t.Errorf("Unexpected contents: %#v", s)
    94  	}
    95  }
    96  
    97  func TestKeySet(t *testing.T) {
    98  	m := map[string]int{"a": 1, "b": 2, "c": 3}
    99  	ss := sets.KeySet[string](m)
   100  	if !ss.Equal(sets.New("a", "b", "c")) {
   101  		t.Errorf("Unexpected contents: %#v", sets.List(ss))
   102  	}
   103  }
   104  
   105  func TestNewEmptySet(t *testing.T) {
   106  	s := sets.New[string]()
   107  	if len(s) != 0 {
   108  		t.Errorf("Expected len=0: %d", len(s))
   109  	}
   110  	s.Insert("a", "b", "c")
   111  	if len(s) != 3 {
   112  		t.Errorf("Expected len=3: %d", len(s))
   113  	}
   114  	if !s.Has("a") || !s.Has("b") || !s.Has("c") {
   115  		t.Errorf("Unexpected contents: %#v", s)
   116  	}
   117  }
   118  
   119  func TestSortedList(t *testing.T) {
   120  	s := sets.New("z", "y", "x", "a")
   121  	if !reflect.DeepEqual(sets.List(s), []string{"a", "x", "y", "z"}) {
   122  		t.Errorf("List gave unexpected result: %#v", sets.List(s))
   123  	}
   124  }
   125  
   126  func TestSetDifference(t *testing.T) {
   127  	a := sets.New("1", "2", "3")
   128  	b := sets.New("1", "2", "4", "5")
   129  	c := a.Difference(b)
   130  	d := b.Difference(a)
   131  	if len(c) != 1 {
   132  		t.Errorf("Expected len=1: %d", len(c))
   133  	}
   134  	if !c.Has("3") {
   135  		t.Errorf("Unexpected contents: %#v", sets.List(c))
   136  	}
   137  	if len(d) != 2 {
   138  		t.Errorf("Expected len=2: %d", len(d))
   139  	}
   140  	if !d.Has("4") || !d.Has("5") {
   141  		t.Errorf("Unexpected contents: %#v", sets.List(d))
   142  	}
   143  }
   144  
   145  func TestSetSymmetricDifference(t *testing.T) {
   146  	a := sets.New("1", "2", "3")
   147  	b := sets.New("1", "2", "4", "5")
   148  	c := a.SymmetricDifference(b)
   149  	d := b.SymmetricDifference(a)
   150  	if !c.Equal(sets.New("3", "4", "5")) {
   151  		t.Errorf("Unexpected contents: %#v", sets.List(c))
   152  	}
   153  	if !d.Equal(sets.New("3", "4", "5")) {
   154  		t.Errorf("Unexpected contents: %#v", sets.List(d))
   155  	}
   156  }
   157  
   158  func TestSetHasAny(t *testing.T) {
   159  	a := sets.New("1", "2", "3")
   160  
   161  	if !a.HasAny("1", "4") {
   162  		t.Errorf("expected true, got false")
   163  	}
   164  
   165  	if a.HasAny("0", "4") {
   166  		t.Errorf("expected false, got true")
   167  	}
   168  }
   169  
   170  func TestSetEquals(t *testing.T) {
   171  	// Simple case (order doesn't matter)
   172  	a := sets.New("1", "2")
   173  	b := sets.New("2", "1")
   174  	if !a.Equal(b) {
   175  		t.Errorf("Expected to be equal: %v vs %v", a, b)
   176  	}
   177  
   178  	// It is a set; duplicates are ignored
   179  	b = sets.New("2", "2", "1")
   180  	if !a.Equal(b) {
   181  		t.Errorf("Expected to be equal: %v vs %v", a, b)
   182  	}
   183  
   184  	// Edge cases around empty sets / empty strings
   185  	a = sets.New[string]()
   186  	b = sets.New[string]()
   187  	if !a.Equal(b) {
   188  		t.Errorf("Expected to be equal: %v vs %v", a, b)
   189  	}
   190  
   191  	b = sets.New("1", "2", "3")
   192  	if a.Equal(b) {
   193  		t.Errorf("Expected to be not-equal: %v vs %v", a, b)
   194  	}
   195  
   196  	b = sets.New("1", "2", "")
   197  	if a.Equal(b) {
   198  		t.Errorf("Expected to be not-equal: %v vs %v", a, b)
   199  	}
   200  
   201  	// Check for equality after mutation
   202  	a = sets.New[string]()
   203  	a.Insert("1")
   204  	if a.Equal(b) {
   205  		t.Errorf("Expected to be not-equal: %v vs %v", a, b)
   206  	}
   207  
   208  	a.Insert("2")
   209  	if a.Equal(b) {
   210  		t.Errorf("Expected to be not-equal: %v vs %v", a, b)
   211  	}
   212  
   213  	a.Insert("")
   214  	if !a.Equal(b) {
   215  		t.Errorf("Expected to be equal: %v vs %v", a, b)
   216  	}
   217  
   218  	a.Delete("")
   219  	if a.Equal(b) {
   220  		t.Errorf("Expected to be not-equal: %v vs %v", a, b)
   221  	}
   222  }
   223  
   224  func TestUnion(t *testing.T) {
   225  	tests := []struct {
   226  		s1       sets.Set[string]
   227  		s2       sets.Set[string]
   228  		expected sets.Set[string]
   229  	}{
   230  		{
   231  			sets.New("1", "2", "3", "4"),
   232  			sets.New("3", "4", "5", "6"),
   233  			sets.New("1", "2", "3", "4", "5", "6"),
   234  		},
   235  		{
   236  			sets.New("1", "2", "3", "4"),
   237  			sets.New[string](),
   238  			sets.New("1", "2", "3", "4"),
   239  		},
   240  		{
   241  			sets.New[string](),
   242  			sets.New("1", "2", "3", "4"),
   243  			sets.New("1", "2", "3", "4"),
   244  		},
   245  		{
   246  			sets.New[string](),
   247  			sets.New[string](),
   248  			sets.New[string](),
   249  		},
   250  	}
   251  
   252  	for _, test := range tests {
   253  		union := test.s1.Union(test.s2)
   254  		if union.Len() != test.expected.Len() {
   255  			t.Errorf("Expected union.Len()=%d but got %d", test.expected.Len(), union.Len())
   256  		}
   257  
   258  		if !union.Equal(test.expected) {
   259  			t.Errorf("Expected union.Equal(expected) but not true.  union:%v expected:%v", sets.List(union), sets.List(test.expected))
   260  		}
   261  	}
   262  }
   263  
   264  func TestIntersection(t *testing.T) {
   265  	tests := []struct {
   266  		s1       sets.Set[string]
   267  		s2       sets.Set[string]
   268  		expected sets.Set[string]
   269  	}{
   270  		{
   271  			sets.New("1", "2", "3", "4"),
   272  			sets.New("3", "4", "5", "6"),
   273  			sets.New("3", "4"),
   274  		},
   275  		{
   276  			sets.New("1", "2", "3", "4"),
   277  			sets.New("1", "2", "3", "4"),
   278  			sets.New("1", "2", "3", "4"),
   279  		},
   280  		{
   281  			sets.New("1", "2", "3", "4"),
   282  			sets.New[string](),
   283  			sets.New[string](),
   284  		},
   285  		{
   286  			sets.New[string](),
   287  			sets.New("1", "2", "3", "4"),
   288  			sets.New[string](),
   289  		},
   290  		{
   291  			sets.New[string](),
   292  			sets.New[string](),
   293  			sets.New[string](),
   294  		},
   295  	}
   296  
   297  	for _, test := range tests {
   298  		intersection := test.s1.Intersection(test.s2)
   299  		if intersection.Len() != test.expected.Len() {
   300  			t.Errorf("Expected intersection.Len()=%d but got %d", test.expected.Len(), intersection.Len())
   301  		}
   302  
   303  		if !intersection.Equal(test.expected) {
   304  			t.Errorf("Expected intersection.Equal(expected) but not true.  intersection:%v expected:%v", sets.List(intersection), sets.List(intersection))
   305  		}
   306  	}
   307  }