github.com/weedge/lib@v0.0.0-20230424045628-a36dcc1d90e4/container/set/hashset_test.go (about) 1 package set 2 3 import ( 4 "testing" 5 ) 6 7 func TestSetNew(t *testing.T) { 8 set := NewSet(2, 1) 9 10 if actualValue := set.Size(); actualValue != 2 { 11 t.Errorf("Got %v expected %v", actualValue, 2) 12 } 13 if actualValue := set.Contains(1); actualValue != true { 14 t.Errorf("Got %v expected %v", actualValue, true) 15 } 16 if actualValue := set.Contains(2); actualValue != true { 17 t.Errorf("Got %v expected %v", actualValue, true) 18 } 19 if actualValue := set.Contains(3); actualValue != false { 20 t.Errorf("Got %v expected %v", actualValue, true) 21 } 22 } 23 24 func TestSetAdd(t *testing.T) { 25 set := NewSet() 26 set.Add() 27 set.Add(1) 28 set.Add(2) 29 set.Add(2, 3) 30 set.Add() 31 if actualValue := set.Empty(); actualValue != false { 32 t.Errorf("Got %v expected %v", actualValue, false) 33 } 34 if actualValue := set.Size(); actualValue != 3 { 35 t.Errorf("Got %v expected %v", actualValue, 3) 36 } 37 } 38 39 func TestSetContains(t *testing.T) { 40 set := NewSet() 41 set.Add(3, 1, 2) 42 set.Add(2, 3) 43 set.Add() 44 if actualValue := set.Contains(); actualValue != true { 45 t.Errorf("Got %v expected %v", actualValue, true) 46 } 47 if actualValue := set.Contains(1); actualValue != true { 48 t.Errorf("Got %v expected %v", actualValue, true) 49 } 50 if actualValue := set.Contains(1, 2, 3); actualValue != true { 51 t.Errorf("Got %v expected %v", actualValue, true) 52 } 53 if actualValue := set.Contains(1, 2, 3, 4); actualValue != false { 54 t.Errorf("Got %v expected %v", actualValue, false) 55 } 56 } 57 58 func TestSetRemove(t *testing.T) { 59 set := NewSet() 60 set.Add(3, 1, 2) 61 set.Remove() 62 if actualValue := set.Size(); actualValue != 3 { 63 t.Errorf("Got %v expected %v", actualValue, 3) 64 } 65 set.Remove(1) 66 if actualValue := set.Size(); actualValue != 2 { 67 t.Errorf("Got %v expected %v", actualValue, 2) 68 } 69 set.Remove(3) 70 set.Remove(3) 71 set.Remove() 72 set.Remove(2) 73 if actualValue := set.Size(); actualValue != 0 { 74 t.Errorf("Got %v expected %v", actualValue, 0) 75 } 76 } 77 78 func benchmarkContains(b *testing.B, set *HashSet, size int) { 79 for i := 0; i < b.N; i++ { 80 for n := 0; n < size; n++ { 81 set.Contains(n) 82 } 83 } 84 } 85 86 func benchmarkAdd(b *testing.B, set *HashSet, size int) { 87 for i := 0; i < b.N; i++ { 88 for n := 0; n < size; n++ { 89 set.Add(n) 90 } 91 } 92 } 93 94 func benchmarkRemove(b *testing.B, set *HashSet, size int) { 95 for i := 0; i < b.N; i++ { 96 for n := 0; n < size; n++ { 97 set.Remove(n) 98 } 99 } 100 } 101 102 func BenchmarkHashSetContains100(b *testing.B) { 103 b.StopTimer() 104 size := 100 105 set := NewSet() 106 for n := 0; n < size; n++ { 107 set.Add(n) 108 } 109 b.StartTimer() 110 benchmarkContains(b, set, size) 111 } 112 113 func BenchmarkHashSetContains1000(b *testing.B) { 114 b.StopTimer() 115 size := 1000 116 set := NewSet() 117 for n := 0; n < size; n++ { 118 set.Add(n) 119 } 120 b.StartTimer() 121 benchmarkContains(b, set, size) 122 } 123 124 func BenchmarkHashSetContains10000(b *testing.B) { 125 b.StopTimer() 126 size := 10000 127 set := NewSet() 128 for n := 0; n < size; n++ { 129 set.Add(n) 130 } 131 b.StartTimer() 132 benchmarkContains(b, set, size) 133 } 134 135 func BenchmarkHashSetContains100000(b *testing.B) { 136 b.StopTimer() 137 size := 100000 138 set := NewSet() 139 for n := 0; n < size; n++ { 140 set.Add(n) 141 } 142 b.StartTimer() 143 benchmarkContains(b, set, size) 144 } 145 146 func BenchmarkHashSetAdd100(b *testing.B) { 147 b.StopTimer() 148 size := 100 149 set := NewSet() 150 b.StartTimer() 151 benchmarkAdd(b, set, size) 152 } 153 154 func BenchmarkHashSetAdd1000(b *testing.B) { 155 b.StopTimer() 156 size := 1000 157 set := NewSet() 158 for n := 0; n < size; n++ { 159 set.Add(n) 160 } 161 b.StartTimer() 162 benchmarkAdd(b, set, size) 163 } 164 165 func BenchmarkHashSetAdd10000(b *testing.B) { 166 b.StopTimer() 167 size := 10000 168 set := NewSet() 169 for n := 0; n < size; n++ { 170 set.Add(n) 171 } 172 b.StartTimer() 173 benchmarkAdd(b, set, size) 174 } 175 176 func BenchmarkHashSetAdd100000(b *testing.B) { 177 b.StopTimer() 178 size := 100000 179 set := NewSet() 180 for n := 0; n < size; n++ { 181 set.Add(n) 182 } 183 b.StartTimer() 184 benchmarkAdd(b, set, size) 185 } 186 187 func BenchmarkHashSetRemove100(b *testing.B) { 188 b.StopTimer() 189 size := 100 190 set := NewSet() 191 for n := 0; n < size; n++ { 192 set.Add(n) 193 } 194 b.StartTimer() 195 benchmarkRemove(b, set, size) 196 } 197 198 func BenchmarkHashSetRemove1000(b *testing.B) { 199 b.StopTimer() 200 size := 1000 201 set := NewSet() 202 for n := 0; n < size; n++ { 203 set.Add(n) 204 } 205 b.StartTimer() 206 benchmarkRemove(b, set, size) 207 } 208 209 func BenchmarkHashSetRemove10000(b *testing.B) { 210 b.StopTimer() 211 size := 10000 212 set := NewSet() 213 for n := 0; n < size; n++ { 214 set.Add(n) 215 } 216 b.StartTimer() 217 benchmarkRemove(b, set, size) 218 } 219 220 func BenchmarkHashSetRemove100000(b *testing.B) { 221 b.StopTimer() 222 size := 100000 223 set := NewSet() 224 for n := 0; n < size; n++ { 225 set.Add(n) 226 } 227 b.StartTimer() 228 benchmarkRemove(b, set, size) 229 }