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