github.com/songzhibin97/go-baseutils@v0.0.2-0.20240302024150-487d8ce9c082/structure/sets/hashset/hashset_test.go (about) 1 package hashset 2 3 import ( 4 "encoding/json" 5 "strings" 6 "testing" 7 ) 8 9 func TestSetNew(t *testing.T) { 10 set := New[int](2, 1) 11 12 if actualValue := set.Size(); actualValue != 2 { 13 t.Errorf("Got %v expected %v", actualValue, 2) 14 } 15 if actualValue := set.Contains(1); actualValue != true { 16 t.Errorf("Got %v expected %v", actualValue, true) 17 } 18 if actualValue := set.Contains(2); actualValue != true { 19 t.Errorf("Got %v expected %v", actualValue, true) 20 } 21 if actualValue := set.Contains(3); actualValue != false { 22 t.Errorf("Got %v expected %v", actualValue, true) 23 } 24 } 25 26 func TestSetAdd(t *testing.T) { 27 set := New[int]() 28 set.Add() 29 set.Add(1) 30 set.Add(2) 31 set.Add(2, 3) 32 set.Add() 33 if actualValue := set.Empty(); actualValue != false { 34 t.Errorf("Got %v expected %v", actualValue, false) 35 } 36 if actualValue := set.Size(); actualValue != 3 { 37 t.Errorf("Got %v expected %v", actualValue, 3) 38 } 39 } 40 41 func TestSetContains(t *testing.T) { 42 set := New[int]() 43 set.Add(3, 1, 2) 44 set.Add(2, 3) 45 set.Add() 46 if actualValue := set.Contains(); actualValue != true { 47 t.Errorf("Got %v expected %v", actualValue, true) 48 } 49 if actualValue := set.Contains(1); actualValue != true { 50 t.Errorf("Got %v expected %v", actualValue, true) 51 } 52 if actualValue := set.Contains(1, 2, 3); actualValue != true { 53 t.Errorf("Got %v expected %v", actualValue, true) 54 } 55 if actualValue := set.Contains(1, 2, 3, 4); actualValue != false { 56 t.Errorf("Got %v expected %v", actualValue, false) 57 } 58 } 59 60 func TestSetRemove(t *testing.T) { 61 set := New[int]() 62 set.Add(3, 1, 2) 63 set.Remove() 64 if actualValue := set.Size(); actualValue != 3 { 65 t.Errorf("Got %v expected %v", actualValue, 3) 66 } 67 set.Remove(1) 68 if actualValue := set.Size(); actualValue != 2 { 69 t.Errorf("Got %v expected %v", actualValue, 2) 70 } 71 set.Remove(3) 72 set.Remove(3) 73 set.Remove() 74 set.Remove(2) 75 if actualValue := set.Size(); actualValue != 0 { 76 t.Errorf("Got %v expected %v", actualValue, 0) 77 } 78 } 79 80 func TestSetSerialization(t *testing.T) { 81 set := New[string]() 82 set.Add("a", "b", "c") 83 84 var err error 85 assert := func() { 86 if actualValue, expectedValue := set.Size(), 3; actualValue != expectedValue { 87 t.Errorf("Got %v expected %v", actualValue, expectedValue) 88 } 89 if actualValue := set.Contains("a", "b", "c"); actualValue != true { 90 t.Errorf("Got %v expected %v", actualValue, true) 91 } 92 if err != nil { 93 t.Errorf("Got error %v", err) 94 } 95 } 96 97 assert() 98 99 bytes, err := set.MarshalJSON() 100 assert() 101 102 err = set.UnmarshalJSON(bytes) 103 assert() 104 105 bytes, err = json.Marshal([]interface{}{"a", "b", "c", set}) 106 if err != nil { 107 t.Errorf("Got error %v", err) 108 } 109 110 err = json.Unmarshal([]byte(`["1","2","3"]`), &set) 111 if err != nil { 112 t.Errorf("Got error %v", err) 113 } 114 } 115 116 func TestSetString(t *testing.T) { 117 c := New[int]() 118 c.Add(1) 119 if !strings.HasPrefix(c.String(), "HashSet") { 120 t.Errorf("String should start with container name") 121 } 122 } 123 124 func TestSetIntersection(t *testing.T) { 125 set := New[string]() 126 another := New[string]() 127 128 intersection := set.Intersection(another) 129 if actualValue, expectedValue := intersection.Size(), 0; actualValue != expectedValue { 130 t.Errorf("Got %v expected %v", actualValue, expectedValue) 131 } 132 133 set.Add("a", "b", "c", "d") 134 another.Add("c", "d", "e", "f") 135 136 intersection = set.Intersection(another) 137 138 if actualValue, expectedValue := intersection.Size(), 2; actualValue != expectedValue { 139 t.Errorf("Got %v expected %v", actualValue, expectedValue) 140 } 141 if actualValue := intersection.Contains("c", "d"); actualValue != true { 142 t.Errorf("Got %v expected %v", actualValue, true) 143 } 144 } 145 146 func TestSetUnion(t *testing.T) { 147 set := New[string]() 148 another := New[string]() 149 150 union := set.Union(another) 151 if actualValue, expectedValue := union.Size(), 0; actualValue != expectedValue { 152 t.Errorf("Got %v expected %v", actualValue, expectedValue) 153 } 154 155 set.Add("a", "b", "c", "d") 156 another.Add("c", "d", "e", "f") 157 158 union = set.Union(another) 159 160 if actualValue, expectedValue := union.Size(), 6; actualValue != expectedValue { 161 t.Errorf("Got %v expected %v", actualValue, expectedValue) 162 } 163 if actualValue := union.Contains("a", "b", "c", "d", "e", "f"); actualValue != true { 164 t.Errorf("Got %v expected %v", actualValue, true) 165 } 166 } 167 168 func TestSetDifference(t *testing.T) { 169 set := New[string]() 170 another := New[string]() 171 172 difference := set.Difference(another) 173 if actualValue, expectedValue := difference.Size(), 0; actualValue != expectedValue { 174 t.Errorf("Got %v expected %v", actualValue, expectedValue) 175 } 176 177 set.Add("a", "b", "c", "d") 178 another.Add("c", "d", "e", "f") 179 180 difference = set.Difference(another) 181 182 if actualValue, expectedValue := difference.Size(), 2; actualValue != expectedValue { 183 t.Errorf("Got %v expected %v", actualValue, expectedValue) 184 } 185 if actualValue := difference.Contains("a", "b"); actualValue != true { 186 t.Errorf("Got %v expected %v", actualValue, true) 187 } 188 } 189 190 func benchmarkContains[E int](b *testing.B, set *Set[E], size int) { 191 for i := 0; i < b.N; i++ { 192 for n := 0; n < size; n++ { 193 set.Contains(E(n)) 194 } 195 } 196 } 197 198 func benchmarkAdd[E int](b *testing.B, set *Set[E], size int) { 199 for i := 0; i < b.N; i++ { 200 for n := 0; n < size; n++ { 201 set.Add(E(n)) 202 } 203 } 204 } 205 206 func benchmarkRemove[E int](b *testing.B, set *Set[E], size int) { 207 for i := 0; i < b.N; i++ { 208 for n := 0; n < size; n++ { 209 set.Remove(E(n)) 210 } 211 } 212 } 213 214 func BenchmarkHashSetContains100(b *testing.B) { 215 b.StopTimer() 216 size := 100 217 set := New[int]() 218 for n := 0; n < size; n++ { 219 set.Add(n) 220 } 221 b.StartTimer() 222 benchmarkContains(b, set, size) 223 } 224 225 func BenchmarkHashSetContains1000(b *testing.B) { 226 b.StopTimer() 227 size := 1000 228 set := New[int]() 229 for n := 0; n < size; n++ { 230 set.Add(n) 231 } 232 b.StartTimer() 233 benchmarkContains(b, set, size) 234 } 235 236 func BenchmarkHashSetContains10000(b *testing.B) { 237 b.StopTimer() 238 size := 10000 239 set := New[int]() 240 for n := 0; n < size; n++ { 241 set.Add(n) 242 } 243 b.StartTimer() 244 benchmarkContains(b, set, size) 245 } 246 247 func BenchmarkHashSetContains100000(b *testing.B) { 248 b.StopTimer() 249 size := 100000 250 set := New[int]() 251 for n := 0; n < size; n++ { 252 set.Add(n) 253 } 254 b.StartTimer() 255 benchmarkContains(b, set, size) 256 } 257 258 func BenchmarkHashSetAdd100(b *testing.B) { 259 b.StopTimer() 260 size := 100 261 set := New[int]() 262 b.StartTimer() 263 benchmarkAdd(b, set, size) 264 } 265 266 func BenchmarkHashSetAdd1000(b *testing.B) { 267 b.StopTimer() 268 size := 1000 269 set := New[int]() 270 for n := 0; n < size; n++ { 271 set.Add(n) 272 } 273 b.StartTimer() 274 benchmarkAdd(b, set, size) 275 } 276 277 func BenchmarkHashSetAdd10000(b *testing.B) { 278 b.StopTimer() 279 size := 10000 280 set := New[int]() 281 for n := 0; n < size; n++ { 282 set.Add(n) 283 } 284 b.StartTimer() 285 benchmarkAdd(b, set, size) 286 } 287 288 func BenchmarkHashSetAdd100000(b *testing.B) { 289 b.StopTimer() 290 size := 100000 291 set := New[int]() 292 for n := 0; n < size; n++ { 293 set.Add(n) 294 } 295 b.StartTimer() 296 benchmarkAdd(b, set, size) 297 } 298 299 func BenchmarkHashSetRemove100(b *testing.B) { 300 b.StopTimer() 301 size := 100 302 set := New[int]() 303 for n := 0; n < size; n++ { 304 set.Add(n) 305 } 306 b.StartTimer() 307 benchmarkRemove(b, set, size) 308 } 309 310 func BenchmarkHashSetRemove1000(b *testing.B) { 311 b.StopTimer() 312 size := 1000 313 set := New[int]() 314 for n := 0; n < size; n++ { 315 set.Add(n) 316 } 317 b.StartTimer() 318 benchmarkRemove(b, set, size) 319 } 320 321 func BenchmarkHashSetRemove10000(b *testing.B) { 322 b.StopTimer() 323 size := 10000 324 set := New[int]() 325 for n := 0; n < size; n++ { 326 set.Add(n) 327 } 328 b.StartTimer() 329 benchmarkRemove(b, set, size) 330 } 331 332 func BenchmarkHashSetRemove100000(b *testing.B) { 333 b.StopTimer() 334 size := 100000 335 set := New[int]() 336 for n := 0; n < size; n++ { 337 set.Add(n) 338 } 339 b.StartTimer() 340 benchmarkRemove(b, set, size) 341 }