istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pkg/util/smallset/smallset_test.go (about) 1 // Copyright Istio Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package smallset_test 16 17 import ( 18 "fmt" 19 "testing" 20 21 "istio.io/istio/pkg/test/util/assert" 22 "istio.io/istio/pkg/util/sets" 23 "istio.io/istio/pkg/util/smallset" 24 ) 25 26 func TestSet(t *testing.T) { 27 elements := []string{"d", "b", "a", "d"} 28 set := smallset.New(elements...) 29 30 assert.Equal(t, set.Len(), 3) 31 assert.Equal(t, set.List(), []string{"a", "b", "d"}) 32 33 assert.Equal(t, set.Contains("a"), true) 34 assert.Equal(t, set.Contains("b"), true) 35 assert.Equal(t, set.Contains("c"), false) 36 assert.Equal(t, set.Contains("d"), true) 37 assert.Equal(t, set.Contains("e"), false) 38 39 nset := set.CopyAndInsert("z", "c", "a") 40 // Should not mutate original set 41 assert.Equal(t, set.List(), []string{"a", "b", "d"}) 42 assert.Equal(t, nset.List(), []string{"a", "b", "c", "d", "z"}) 43 44 assert.Equal(t, nset.Contains("a"), true) 45 assert.Equal(t, nset.Contains("b"), true) 46 assert.Equal(t, nset.Contains("c"), true) 47 assert.Equal(t, nset.Contains("d"), true) 48 assert.Equal(t, nset.Contains("e"), false) 49 assert.Equal(t, nset.Contains("z"), true) 50 } 51 52 func TestNew(t *testing.T) { 53 var uninit smallset.Set[string] 54 assert.Equal(t, uninit.IsNil(), true) 55 assert.Equal(t, uninit.IsEmpty(), true) 56 empty := smallset.New[string]() 57 assert.Equal(t, empty.IsNil(), true) 58 assert.Equal(t, empty.IsEmpty(), true) 59 empty2 := smallset.New[string]([]string{}...) 60 assert.Equal(t, empty2.IsNil(), false) 61 assert.Equal(t, empty2.IsEmpty(), true) 62 } 63 64 func BenchmarkSet(b *testing.B) { 65 items1000 := []string{} 66 for i := 0; i < 1000; i++ { 67 items1000 = append(items1000, fmt.Sprint(i)) 68 } 69 items100 := []string{} 70 for i := 0; i < 100; i++ { 71 items100 = append(items100, fmt.Sprint(i)) 72 } 73 items2 := []string{} 74 for i := 0; i < 2; i++ { 75 items2 = append(items2, fmt.Sprint(i)) 76 } 77 b.Run("Set", func(b *testing.B) { 78 set1000 := sets.New(items1000...) 79 set100 := sets.New(items100...) 80 set2 := sets.New(items2...) 81 b.Run("New/1", func(b *testing.B) { 82 for range b.N { 83 _ = sets.New("a") 84 } 85 }) 86 b.Run("New/1000", func(b *testing.B) { 87 for range b.N { 88 _ = sets.New(items1000...) 89 } 90 }) 91 b.Run("Contains/1000", func(b *testing.B) { 92 for range b.N { 93 // Check an item in and out of the set 94 _ = set1000.Contains("456") 95 _ = set1000.Contains("abc") 96 } 97 }) 98 b.Run("Contains/100", func(b *testing.B) { 99 for range b.N { 100 _ = set100.Contains("45") 101 _ = set100.Contains("abc") 102 } 103 }) 104 b.Run("Contains/2", func(b *testing.B) { 105 for range b.N { 106 _ = set2.Contains("1") 107 _ = set2.Contains("abc") 108 } 109 }) 110 }) 111 b.Run("SmallSet", func(b *testing.B) { 112 set1000 := smallset.New(items1000...) 113 set100 := smallset.New(items100...) 114 set2 := smallset.New(items2...) 115 b.Run("New/1", func(b *testing.B) { 116 for range b.N { 117 _ = smallset.New("a") 118 } 119 }) 120 b.Run("New/1000", func(b *testing.B) { 121 for range b.N { 122 _ = smallset.New(items1000...) 123 } 124 }) 125 b.Run("NewPresorted/1000", func(b *testing.B) { 126 for range b.N { 127 _ = smallset.NewPresorted(items1000...) 128 } 129 }) 130 b.Run("Contains/1000", func(b *testing.B) { 131 for range b.N { 132 // Check an item in and out of the set 133 _ = set1000.Contains("456") 134 _ = set1000.Contains("abc") 135 } 136 }) 137 b.Run("Contains/100", func(b *testing.B) { 138 for range b.N { 139 // Check an item in and out of the set 140 _ = set100.Contains("45") 141 _ = set100.Contains("abc") 142 } 143 }) 144 b.Run("Contains/2", func(b *testing.B) { 145 for range b.N { 146 // Check an item in and out of the set 147 _ = set2.Contains("1") 148 _ = set2.Contains("abc") 149 } 150 }) 151 }) 152 }