| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package treebidimap |
| 6 |
|
| 7 |
import ( |
| 8 |
"encoding/json" |
| 9 |
"strings" |
| 10 |
"testing" |
| 11 |
|
| 12 |
"github.com/emirpasic/gods/v2/testutils" |
| 13 |
) |
| 14 |
|
| 15 |
func TestMapPut(t *testing.T) { |
| 16 |
m := New[int, string]() |
| 17 |
m.Put(5, "e") |
| 18 |
m.Put(6, "f") |
| 19 |
m.Put(7, "g") |
| 20 |
m.Put(3, "c") |
| 21 |
m.Put(4, "d") |
| 22 |
m.Put(1, "x") |
| 23 |
m.Put(2, "b") |
| 24 |
m.Put(1, "a") |
| 25 |
|
| 26 |
if actualValue := m.Size(); actualValue != 7 { |
| 27 |
t.Errorf("Got %v expected %v", actualValue, 7) |
| 28 |
} |
| 29 |
testutils.SameElements(t, m.Keys(), []int{1, 2, 3, 4, 5, 6, 7}) |
| 30 |
testutils.SameElements(t, m.Values(), []string{"a", "b", "c", "d", "e", "f", "g"}) |
| 31 |
|
| 32 |
|
| 33 |
tests1 := [][]interface{}{ |
| 34 |
{1, "a", true}, |
| 35 |
{2, "b", true}, |
| 36 |
{3, "c", true}, |
| 37 |
{4, "d", true}, |
| 38 |
{5, "e", true}, |
| 39 |
{6, "f", true}, |
| 40 |
{7, "g", true}, |
| 41 |
{8, "", false}, |
| 42 |
} |
| 43 |
|
| 44 |
for _, test := range tests1 { |
| 45 |
|
| 46 |
actualValue, actualFound := m.Get(test[0].(int)) |
| 47 |
if actualValue != test[1] || actualFound != test[2] { |
| 48 |
t.Errorf("Got %v expected %v", actualValue, test[1]) |
| 49 |
} |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
func TestMapRemove(t *testing.T) { |
| 54 |
m := New[int, string]() |
| 55 |
m.Put(5, "e") |
| 56 |
m.Put(6, "f") |
| 57 |
m.Put(7, "g") |
| 58 |
m.Put(3, "c") |
| 59 |
m.Put(4, "d") |
| 60 |
m.Put(1, "x") |
| 61 |
m.Put(2, "b") |
| 62 |
m.Put(1, "a") |
| 63 |
|
| 64 |
m.Remove(5) |
| 65 |
m.Remove(6) |
| 66 |
m.Remove(7) |
| 67 |
m.Remove(8) |
| 68 |
m.Remove(5) |
| 69 |
|
| 70 |
testutils.SameElements(t, m.Keys(), []int{1, 2, 3, 4}) |
| 71 |
testutils.SameElements(t, m.Values(), []string{"a", "b", "c", "d"}) |
| 72 |
|
| 73 |
if actualValue := m.Size(); actualValue != 4 { |
| 74 |
t.Errorf("Got %v expected %v", actualValue, 4) |
| 75 |
} |
| 76 |
|
| 77 |
tests2 := [][]interface{}{ |
| 78 |
{1, "a", true}, |
| 79 |
{2, "b", true}, |
| 80 |
{3, "c", true}, |
| 81 |
{4, "d", true}, |
| 82 |
{5, "", false}, |
| 83 |
{6, "", false}, |
| 84 |
{7, "", false}, |
| 85 |
{8, "", false}, |
| 86 |
} |
| 87 |
|
| 88 |
for _, test := range tests2 { |
| 89 |
actualValue, actualFound := m.Get(test[0].(int)) |
| 90 |
if actualValue != test[1] || actualFound != test[2] { |
| 91 |
t.Errorf("Got %v expected %v", actualValue, test[1]) |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
m.Remove(1) |
| 96 |
m.Remove(4) |
| 97 |
m.Remove(2) |
| 98 |
m.Remove(3) |
| 99 |
m.Remove(2) |
| 100 |
m.Remove(2) |
| 101 |
|
| 102 |
testutils.SameElements(t, m.Keys(), nil) |
| 103 |
testutils.SameElements(t, m.Values(), nil) |
| 104 |
if actualValue := m.Size(); actualValue != 0 { |
| 105 |
t.Errorf("Got %v expected %v", actualValue, 0) |
| 106 |
} |
| 107 |
if actualValue := m.Empty(); actualValue != true { |
| 108 |
t.Errorf("Got %v expected %v", actualValue, true) |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
func TestMapGetKey(t *testing.T) { |
| 113 |
m := New[int, string]() |
| 114 |
m.Put(5, "e") |
| 115 |
m.Put(6, "f") |
| 116 |
m.Put(7, "g") |
| 117 |
m.Put(3, "c") |
| 118 |
m.Put(4, "d") |
| 119 |
m.Put(1, "x") |
| 120 |
m.Put(2, "b") |
| 121 |
m.Put(1, "a") |
| 122 |
|
| 123 |
|
| 124 |
tests1 := [][]interface{}{ |
| 125 |
{1, "a", true}, |
| 126 |
{2, "b", true}, |
| 127 |
{3, "c", true}, |
| 128 |
{4, "d", true}, |
| 129 |
{5, "e", true}, |
| 130 |
{6, "f", true}, |
| 131 |
{7, "g", true}, |
| 132 |
{0, "x", false}, |
| 133 |
} |
| 134 |
|
| 135 |
for _, test := range tests1 { |
| 136 |
|
| 137 |
actualValue, actualFound := m.GetKey(test[1].(string)) |
| 138 |
if actualValue != test[0] || actualFound != test[2] { |
| 139 |
t.Errorf("Got %v expected %v", actualValue, test[0]) |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
func sameElements(a []interface{}, b []interface{}) bool { |
| 145 |
if len(a) != len(b) { |
| 146 |
return false |
| 147 |
} |
| 148 |
for _, av := range a { |
| 149 |
found := false |
| 150 |
for _, bv := range b { |
| 151 |
if av == bv { |
| 152 |
found = true |
| 153 |
break |
| 154 |
} |
| 155 |
} |
| 156 |
if !found { |
| 157 |
return false |
| 158 |
} |
| 159 |
} |
| 160 |
return true |
| 161 |
} |
| 162 |
|
| 163 |
func TestMapEach(t *testing.T) { |
| 164 |
m := New[string, int]() |
| 165 |
m.Put("c", 3) |
| 166 |
m.Put("a", 1) |
| 167 |
m.Put("b", 2) |
| 168 |
count := 0 |
| 169 |
m.Each(func(key string, value int) { |
| 170 |
count++ |
| 171 |
if actualValue, expectedValue := count, value; actualValue != expectedValue { |
| 172 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 173 |
} |
| 174 |
switch value { |
| 175 |
case 1: |
| 176 |
if actualValue, expectedValue := key, "a"; actualValue != expectedValue { |
| 177 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 178 |
} |
| 179 |
case 2: |
| 180 |
if actualValue, expectedValue := key, "b"; actualValue != expectedValue { |
| 181 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 182 |
} |
| 183 |
case 3: |
| 184 |
if actualValue, expectedValue := key, "c"; actualValue != expectedValue { |
| 185 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 186 |
} |
| 187 |
default: |
| 188 |
t.Errorf("Too many") |
| 189 |
} |
| 190 |
}) |
| 191 |
} |
| 192 |
|
| 193 |
func TestMapMap(t *testing.T) { |
| 194 |
m := New[string, int]() |
| 195 |
m.Put("c", 3) |
| 196 |
m.Put("a", 1) |
| 197 |
m.Put("b", 2) |
| 198 |
mappedMap := m.Map(func(key1 string, value1 int) (key2 string, value2 int) { |
| 199 |
return key1, value1 * value1 |
| 200 |
}) |
| 201 |
if actualValue, _ := mappedMap.Get("a"); actualValue != 1 { |
| 202 |
t.Errorf("Got %v expected %v", actualValue, "mapped: a") |
| 203 |
} |
| 204 |
if actualValue, _ := mappedMap.Get("b"); actualValue != 4 { |
| 205 |
t.Errorf("Got %v expected %v", actualValue, "mapped: b") |
| 206 |
} |
| 207 |
if actualValue, _ := mappedMap.Get("c"); actualValue != 9 { |
| 208 |
t.Errorf("Got %v expected %v", actualValue, "mapped: c") |
| 209 |
} |
| 210 |
if mappedMap.Size() != 3 { |
| 211 |
t.Errorf("Got %v expected %v", mappedMap.Size(), 3) |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
func TestMapSelect(t *testing.T) { |
| 216 |
m := New[string, int]() |
| 217 |
m.Put("c", 3) |
| 218 |
m.Put("a", 1) |
| 219 |
m.Put("b", 2) |
| 220 |
selectedMap := m.Select(func(key string, value int) bool { |
| 221 |
return key >= "a" && key <= "b" |
| 222 |
}) |
| 223 |
if actualValue, _ := selectedMap.Get("a"); actualValue != 1 { |
| 224 |
t.Errorf("Got %v expected %v", actualValue, "value: a") |
| 225 |
} |
| 226 |
if actualValue, _ := selectedMap.Get("b"); actualValue != 2 { |
| 227 |
t.Errorf("Got %v expected %v", actualValue, "value: b") |
| 228 |
} |
| 229 |
if selectedMap.Size() != 2 { |
| 230 |
t.Errorf("Got %v expected %v", selectedMap.Size(), 2) |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
func TestMapAny(t *testing.T) { |
| 235 |
m := New[string, int]() |
| 236 |
m.Put("c", 3) |
| 237 |
m.Put("a", 1) |
| 238 |
m.Put("b", 2) |
| 239 |
any := m.Any(func(key string, value int) bool { |
| 240 |
return value == 3 |
| 241 |
}) |
| 242 |
if any != true { |
| 243 |
t.Errorf("Got %v expected %v", any, true) |
| 244 |
} |
| 245 |
any = m.Any(func(key string, value int) bool { |
| 246 |
return value == 4 |
| 247 |
}) |
| 248 |
if any != false { |
| 249 |
t.Errorf("Got %v expected %v", any, false) |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
func TestMapAll(t *testing.T) { |
| 254 |
m := New[string, int]() |
| 255 |
m.Put("c", 3) |
| 256 |
m.Put("a", 1) |
| 257 |
m.Put("b", 2) |
| 258 |
all := m.All(func(key string, value int) bool { |
| 259 |
return key >= "a" && key <= "c" |
| 260 |
}) |
| 261 |
if all != true { |
| 262 |
t.Errorf("Got %v expected %v", all, true) |
| 263 |
} |
| 264 |
all = m.All(func(key string, value int) bool { |
| 265 |
return key >= "a" && key <= "b" |
| 266 |
}) |
| 267 |
if all != false { |
| 268 |
t.Errorf("Got %v expected %v", all, false) |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
func TestMapFind(t *testing.T) { |
| 273 |
m := New[string, int]() |
| 274 |
m.Put("c", 3) |
| 275 |
m.Put("a", 1) |
| 276 |
m.Put("b", 2) |
| 277 |
foundKey, foundValue := m.Find(func(key string, value int) bool { |
| 278 |
return key == "c" |
| 279 |
}) |
| 280 |
if foundKey != "c" || foundValue != 3 { |
| 281 |
t.Errorf("Got %v -> %v expected %v -> %v", foundKey, foundValue, "c", 3) |
| 282 |
} |
| 283 |
foundKey, foundValue = m.Find(func(key string, value int) bool { |
| 284 |
return key == "x" |
| 285 |
}) |
| 286 |
if foundKey != "" || foundValue != 0 { |
| 287 |
t.Errorf("Got %v at %v expected %v at %v", foundValue, foundKey, nil, nil) |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
func TestMapChaining(t *testing.T) { |
| 292 |
m := New[string, int]() |
| 293 |
m.Put("c", 3) |
| 294 |
m.Put("a", 1) |
| 295 |
m.Put("b", 2) |
| 296 |
chainedMap := m.Select(func(key string, value int) bool { |
| 297 |
return value > 1 |
| 298 |
}).Map(func(key string, value int) (string, int) { |
| 299 |
return key + key, value * value |
| 300 |
}) |
| 301 |
if actualValue := chainedMap.Size(); actualValue != 2 { |
| 302 |
t.Errorf("Got %v expected %v", actualValue, 2) |
| 303 |
} |
| 304 |
if actualValue, found := chainedMap.Get("aa"); actualValue != 0 || found { |
| 305 |
t.Errorf("Got %v expected %v", actualValue, nil) |
| 306 |
} |
| 307 |
if actualValue, found := chainedMap.Get("bb"); actualValue != 4 || !found { |
| 308 |
t.Errorf("Got %v expected %v", actualValue, 4) |
| 309 |
} |
| 310 |
if actualValue, found := chainedMap.Get("cc"); actualValue != 9 || !found { |
| 311 |
t.Errorf("Got %v expected %v", actualValue, 9) |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
func TestMapIteratorNextOnEmpty(t *testing.T) { |
| 316 |
m := New[string, string]() |
| 317 |
it := m.Iterator() |
| 318 |
it = m.Iterator() |
| 319 |
for it.Next() { |
| 320 |
t.Errorf("Shouldn't iterate on empty map") |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
func TestMapIteratorPrevOnEmpty(t *testing.T) { |
| 325 |
m := New[string, string]() |
| 326 |
it := m.Iterator() |
| 327 |
it = m.Iterator() |
| 328 |
for it.Prev() { |
| 329 |
t.Errorf("Shouldn't iterate on empty map") |
| 330 |
} |
| 331 |
} |
| 332 |
|
| 333 |
func TestMapIteratorNext(t *testing.T) { |
| 334 |
m := New[string, int]() |
| 335 |
m.Put("c", 3) |
| 336 |
m.Put("a", 1) |
| 337 |
m.Put("b", 2) |
| 338 |
|
| 339 |
it := m.Iterator() |
| 340 |
count := 0 |
| 341 |
for it.Next() { |
| 342 |
count++ |
| 343 |
key := it.Key() |
| 344 |
value := it.Value() |
| 345 |
switch key { |
| 346 |
case "a": |
| 347 |
if actualValue, expectedValue := value, 1; actualValue != expectedValue { |
| 348 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 349 |
} |
| 350 |
case "b": |
| 351 |
if actualValue, expectedValue := value, 2; actualValue != expectedValue { |
| 352 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 353 |
} |
| 354 |
case "c": |
| 355 |
if actualValue, expectedValue := value, 3; actualValue != expectedValue { |
| 356 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 357 |
} |
| 358 |
default: |
| 359 |
t.Errorf("Too many") |
| 360 |
} |
| 361 |
if actualValue, expectedValue := value, count; actualValue != expectedValue { |
| 362 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 363 |
} |
| 364 |
} |
| 365 |
if actualValue, expectedValue := count, 3; actualValue != expectedValue { |
| 366 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 367 |
} |
| 368 |
} |
| 369 |
|
| 370 |
func TestMapIteratorPrev(t *testing.T) { |
| 371 |
m := New[string, int]() |
| 372 |
m.Put("c", 3) |
| 373 |
m.Put("a", 1) |
| 374 |
m.Put("b", 2) |
| 375 |
|
| 376 |
it := m.Iterator() |
| 377 |
for it.Next() { |
| 378 |
} |
| 379 |
countDown := m.Size() |
| 380 |
for it.Prev() { |
| 381 |
key := it.Key() |
| 382 |
value := it.Value() |
| 383 |
switch key { |
| 384 |
case "a": |
| 385 |
if actualValue, expectedValue := value, 1; actualValue != expectedValue { |
| 386 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 387 |
} |
| 388 |
case "b": |
| 389 |
if actualValue, expectedValue := value, 2; actualValue != expectedValue { |
| 390 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 391 |
} |
| 392 |
case "c": |
| 393 |
if actualValue, expectedValue := value, 3; actualValue != expectedValue { |
| 394 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 395 |
} |
| 396 |
default: |
| 397 |
t.Errorf("Too many") |
| 398 |
} |
| 399 |
if actualValue, expectedValue := value, countDown; actualValue != expectedValue { |
| 400 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 401 |
} |
| 402 |
countDown-- |
| 403 |
} |
| 404 |
if actualValue, expectedValue := countDown, 0; actualValue != expectedValue { |
| 405 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 406 |
} |
| 407 |
} |
| 408 |
|
| 409 |
func TestMapIteratorBegin(t *testing.T) { |
| 410 |
m := New[int, string]() |
| 411 |
it := m.Iterator() |
| 412 |
it.Begin() |
| 413 |
m.Put(3, "c") |
| 414 |
m.Put(1, "a") |
| 415 |
m.Put(2, "b") |
| 416 |
for it.Next() { |
| 417 |
} |
| 418 |
it.Begin() |
| 419 |
it.Next() |
| 420 |
if key, value := it.Key(), it.Value(); key != 1 || value != "a" { |
| 421 |
t.Errorf("Got %v,%v expected %v,%v", key, value, 1, "a") |
| 422 |
} |
| 423 |
} |
| 424 |
|
| 425 |
func TestMapIteratorEnd(t *testing.T) { |
| 426 |
m := New[int, string]() |
| 427 |
it := m.Iterator() |
| 428 |
m.Put(3, "c") |
| 429 |
m.Put(1, "a") |
| 430 |
m.Put(2, "b") |
| 431 |
it.End() |
| 432 |
it.Prev() |
| 433 |
if key, value := it.Key(), it.Value(); key != 3 || value != "c" { |
| 434 |
t.Errorf("Got %v,%v expected %v,%v", key, value, 3, "c") |
| 435 |
} |
| 436 |
} |
| 437 |
|
| 438 |
func TestMapIteratorFirst(t *testing.T) { |
| 439 |
m := New[int, string]() |
| 440 |
m.Put(3, "c") |
| 441 |
m.Put(1, "a") |
| 442 |
m.Put(2, "b") |
| 443 |
it := m.Iterator() |
| 444 |
if actualValue, expectedValue := it.First(), true; actualValue != expectedValue { |
| 445 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 446 |
} |
| 447 |
if key, value := it.Key(), it.Value(); key != 1 || value != "a" { |
| 448 |
t.Errorf("Got %v,%v expected %v,%v", key, value, 1, "a") |
| 449 |
} |
| 450 |
} |
| 451 |
|
| 452 |
func TestMapIteratorLast(t *testing.T) { |
| 453 |
m := New[int, string]() |
| 454 |
m.Put(3, "c") |
| 455 |
m.Put(1, "a") |
| 456 |
m.Put(2, "b") |
| 457 |
it := m.Iterator() |
| 458 |
if actualValue, expectedValue := it.Last(), true; actualValue != expectedValue { |
| 459 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 460 |
} |
| 461 |
if key, value := it.Key(), it.Value(); key != 3 || value != "c" { |
| 462 |
t.Errorf("Got %v,%v expected %v,%v", key, value, 3, "c") |
| 463 |
} |
| 464 |
} |
| 465 |
|
| 466 |
func TestMapIteratorNextTo(t *testing.T) { |
| 467 |
|
| 468 |
seek := func(index int, value string) bool { |
| 469 |
return strings.HasSuffix(value, "b") |
| 470 |
} |
| 471 |
|
| 472 |
|
| 473 |
{ |
| 474 |
m := New[int, string]() |
| 475 |
it := m.Iterator() |
| 476 |
for it.NextTo(seek) { |
| 477 |
t.Errorf("Shouldn't iterate on empty map") |
| 478 |
} |
| 479 |
} |
| 480 |
|
| 481 |
|
| 482 |
{ |
| 483 |
m := New[int, string]() |
| 484 |
m.Put(0, "xx") |
| 485 |
m.Put(1, "yy") |
| 486 |
it := m.Iterator() |
| 487 |
for it.NextTo(seek) { |
| 488 |
t.Errorf("Shouldn't iterate on empty map") |
| 489 |
} |
| 490 |
} |
| 491 |
|
| 492 |
|
| 493 |
{ |
| 494 |
m := New[int, string]() |
| 495 |
m.Put(0, "aa") |
| 496 |
m.Put(1, "bb") |
| 497 |
m.Put(2, "cc") |
| 498 |
it := m.Iterator() |
| 499 |
it.Begin() |
| 500 |
if !it.NextTo(seek) { |
| 501 |
t.Errorf("Shouldn't iterate on empty map") |
| 502 |
} |
| 503 |
if index, value := it.Key(), it.Value(); index != 1 || value != "bb" { |
| 504 |
t.Errorf("Got %v,%v expected %v,%v", index, value, 1, "bb") |
| 505 |
} |
| 506 |
if !it.Next() { |
| 507 |
t.Errorf("Should go to first element") |
| 508 |
} |
| 509 |
if index, value := it.Key(), it.Value(); index != 2 || value != "cc" { |
| 510 |
t.Errorf("Got %v,%v expected %v,%v", index, value, 2, "cc") |
| 511 |
} |
| 512 |
if it.Next() { |
| 513 |
t.Errorf("Should not go past last element") |
| 514 |
} |
| 515 |
} |
| 516 |
} |
| 517 |
|
| 518 |
func TestMapIteratorPrevTo(t *testing.T) { |
| 519 |
|
| 520 |
seek := func(index int, value string) bool { |
| 521 |
return strings.HasSuffix(value, "b") |
| 522 |
} |
| 523 |
|
| 524 |
|
| 525 |
{ |
| 526 |
m := New[int, string]() |
| 527 |
it := m.Iterator() |
| 528 |
it.End() |
| 529 |
for it.PrevTo(seek) { |
| 530 |
t.Errorf("Shouldn't iterate on empty map") |
| 531 |
} |
| 532 |
} |
| 533 |
|
| 534 |
|
| 535 |
{ |
| 536 |
m := New[int, string]() |
| 537 |
m.Put(0, "xx") |
| 538 |
m.Put(1, "yy") |
| 539 |
it := m.Iterator() |
| 540 |
it.End() |
| 541 |
for it.PrevTo(seek) { |
| 542 |
t.Errorf("Shouldn't iterate on empty map") |
| 543 |
} |
| 544 |
} |
| 545 |
|
| 546 |
|
| 547 |
{ |
| 548 |
m := New[int, string]() |
| 549 |
m.Put(0, "aa") |
| 550 |
m.Put(1, "bb") |
| 551 |
m.Put(2, "cc") |
| 552 |
it := m.Iterator() |
| 553 |
it.End() |
| 554 |
if !it.PrevTo(seek) { |
| 555 |
t.Errorf("Shouldn't iterate on empty map") |
| 556 |
} |
| 557 |
if index, value := it.Key(), it.Value(); index != 1 || value != "bb" { |
| 558 |
t.Errorf("Got %v,%v expected %v,%v", index, value, 1, "bb") |
| 559 |
} |
| 560 |
if !it.Prev() { |
| 561 |
t.Errorf("Should go to first element") |
| 562 |
} |
| 563 |
if index, value := it.Key(), it.Value(); index != 0 || value != "aa" { |
| 564 |
t.Errorf("Got %v,%v expected %v,%v", index, value, 0, "aa") |
| 565 |
} |
| 566 |
if it.Prev() { |
| 567 |
t.Errorf("Should not go before first element") |
| 568 |
} |
| 569 |
} |
| 570 |
} |
| 571 |
|
| 572 |
func TestMapSerialization(t *testing.T) { |
| 573 |
for i := 0; i < 10; i++ { |
| 574 |
original := New[string, string]() |
| 575 |
original.Put("d", "4") |
| 576 |
original.Put("e", "5") |
| 577 |
original.Put("c", "3") |
| 578 |
original.Put("b", "2") |
| 579 |
original.Put("a", "1") |
| 580 |
|
| 581 |
serialized, err := original.ToJSON() |
| 582 |
if err != nil { |
| 583 |
t.Errorf("Got error %v", err) |
| 584 |
} |
| 585 |
|
| 586 |
deserialized := New[string, string]() |
| 587 |
err = deserialized.FromJSON(serialized) |
| 588 |
if err != nil { |
| 589 |
t.Errorf("Got error %v", err) |
| 590 |
} |
| 591 |
|
| 592 |
if original.Size() != deserialized.Size() { |
| 593 |
t.Errorf("Got map of size %d, expected %d", original.Size(), deserialized.Size()) |
| 594 |
} |
| 595 |
original.Each(func(key string, expected string) { |
| 596 |
actual, ok := deserialized.Get(key) |
| 597 |
if !ok || actual != expected { |
| 598 |
t.Errorf("Did not find expected value %v for key %v in deserialied map (got %q)", expected, key, actual) |
| 599 |
} |
| 600 |
}) |
| 601 |
} |
| 602 |
|
| 603 |
m := New[string, float64]() |
| 604 |
m.Put("a", 1.0) |
| 605 |
m.Put("b", 2.0) |
| 606 |
m.Put("c", 3.0) |
| 607 |
|
| 608 |
_, err := json.Marshal([]interface{}{"a", "b", "c", m}) |
| 609 |
if err != nil { |
| 610 |
t.Errorf("Got error %v", err) |
| 611 |
} |
| 612 |
|
| 613 |
err = json.Unmarshal([]byte(`{"a":1,"b":2}`), &m) |
| 614 |
if err != nil { |
| 615 |
t.Errorf("Got error %v", err) |
| 616 |
} |
| 617 |
} |
| 618 |
|
| 619 |
func TestMapString(t *testing.T) { |
| 620 |
c := New[string, string]() |
| 621 |
c.Put("a", "a") |
| 622 |
if !strings.HasPrefix(c.String(), "TreeBidiMap") { |
| 623 |
t.Errorf("String should start with container name") |
| 624 |
} |
| 625 |
} |
| 626 |
|
| 627 |
func benchmarkGet(b *testing.B, m *Map[int, int], size int) { |
| 628 |
for i := 0; i < b.N; i++ { |
| 629 |
for n := 0; n < size; n++ { |
| 630 |
m.Get(n) |
| 631 |
} |
| 632 |
} |
| 633 |
} |
| 634 |
|
| 635 |
func benchmarkPut(b *testing.B, m *Map[int, int], size int) { |
| 636 |
for i := 0; i < b.N; i++ { |
| 637 |
for n := 0; n < size; n++ { |
| 638 |
m.Put(n, n) |
| 639 |
} |
| 640 |
} |
| 641 |
} |
| 642 |
|
| 643 |
func benchmarkRemove(b *testing.B, m *Map[int, int], size int) { |
| 644 |
for i := 0; i < b.N; i++ { |
| 645 |
for n := 0; n < size; n++ { |
| 646 |
m.Remove(n) |
| 647 |
} |
| 648 |
} |
| 649 |
} |
| 650 |
|
| 651 |
func BenchmarkTreeBidiMapGet100(b *testing.B) { |
| 652 |
b.StopTimer() |
| 653 |
size := 100 |
| 654 |
m := New[int, int]() |
| 655 |
for n := 0; n < size; n++ { |
| 656 |
m.Put(n, n) |
| 657 |
} |
| 658 |
b.StartTimer() |
| 659 |
benchmarkGet(b, m, size) |
| 660 |
} |
| 661 |
|
| 662 |
func BenchmarkTreeBidiMapGet1000(b *testing.B) { |
| 663 |
b.StopTimer() |
| 664 |
size := 1000 |
| 665 |
m := New[int, int]() |
| 666 |
for n := 0; n < size; n++ { |
| 667 |
m.Put(n, n) |
| 668 |
} |
| 669 |
b.StartTimer() |
| 670 |
benchmarkGet(b, m, size) |
| 671 |
} |
| 672 |
|
| 673 |
func BenchmarkTreeBidiMapGet10000(b *testing.B) { |
| 674 |
b.StopTimer() |
| 675 |
size := 10000 |
| 676 |
m := New[int, int]() |
| 677 |
for n := 0; n < size; n++ { |
| 678 |
m.Put(n, n) |
| 679 |
} |
| 680 |
b.StartTimer() |
| 681 |
benchmarkGet(b, m, size) |
| 682 |
} |
| 683 |
|
| 684 |
func BenchmarkTreeBidiMapGet100000(b *testing.B) { |
| 685 |
b.StopTimer() |
| 686 |
size := 100000 |
| 687 |
m := New[int, int]() |
| 688 |
for n := 0; n < size; n++ { |
| 689 |
m.Put(n, n) |
| 690 |
} |
| 691 |
b.StartTimer() |
| 692 |
benchmarkGet(b, m, size) |
| 693 |
} |
| 694 |
|
| 695 |
func BenchmarkTreeBidiMapPut100(b *testing.B) { |
| 696 |
b.StopTimer() |
| 697 |
size := 100 |
| 698 |
m := New[int, int]() |
| 699 |
b.StartTimer() |
| 700 |
benchmarkPut(b, m, size) |
| 701 |
} |
| 702 |
|
| 703 |
func BenchmarkTreeBidiMapPut1000(b *testing.B) { |
| 704 |
b.StopTimer() |
| 705 |
size := 1000 |
| 706 |
m := New[int, int]() |
| 707 |
for n := 0; n < size; n++ { |
| 708 |
m.Put(n, n) |
| 709 |
} |
| 710 |
b.StartTimer() |
| 711 |
benchmarkPut(b, m, size) |
| 712 |
} |
| 713 |
|
| 714 |
func BenchmarkTreeBidiMapPut10000(b *testing.B) { |
| 715 |
b.StopTimer() |
| 716 |
size := 10000 |
| 717 |
m := New[int, int]() |
| 718 |
for n := 0; n < size; n++ { |
| 719 |
m.Put(n, n) |
| 720 |
} |
| 721 |
b.StartTimer() |
| 722 |
benchmarkPut(b, m, size) |
| 723 |
} |
| 724 |
|
| 725 |
func BenchmarkTreeBidiMapPut100000(b *testing.B) { |
| 726 |
b.StopTimer() |
| 727 |
size := 100000 |
| 728 |
m := New[int, int]() |
| 729 |
for n := 0; n < size; n++ { |
| 730 |
m.Put(n, n) |
| 731 |
} |
| 732 |
b.StartTimer() |
| 733 |
benchmarkPut(b, m, size) |
| 734 |
} |
| 735 |
|
| 736 |
func BenchmarkTreeBidiMapRemove100(b *testing.B) { |
| 737 |
b.StopTimer() |
| 738 |
size := 100 |
| 739 |
m := New[int, int]() |
| 740 |
for n := 0; n < size; n++ { |
| 741 |
m.Put(n, n) |
| 742 |
} |
| 743 |
b.StartTimer() |
| 744 |
benchmarkRemove(b, m, size) |
| 745 |
} |
| 746 |
|
| 747 |
func BenchmarkTreeBidiMapRemove1000(b *testing.B) { |
| 748 |
b.StopTimer() |
| 749 |
size := 1000 |
| 750 |
m := New[int, int]() |
| 751 |
for n := 0; n < size; n++ { |
| 752 |
m.Put(n, n) |
| 753 |
} |
| 754 |
b.StartTimer() |
| 755 |
benchmarkRemove(b, m, size) |
| 756 |
} |
| 757 |
|
| 758 |
func BenchmarkTreeBidiMapRemove10000(b *testing.B) { |
| 759 |
b.StopTimer() |
| 760 |
size := 10000 |
| 761 |
m := New[int, int]() |
| 762 |
for n := 0; n < size; n++ { |
| 763 |
m.Put(n, n) |
| 764 |
} |
| 765 |
b.StartTimer() |
| 766 |
benchmarkRemove(b, m, size) |
| 767 |
} |
| 768 |
|
| 769 |
func BenchmarkTreeBidiMapRemove100000(b *testing.B) { |
| 770 |
b.StopTimer() |
| 771 |
size := 100000 |
| 772 |
m := New[int, int]() |
| 773 |
for n := 0; n < size; n++ { |
| 774 |
m.Put(n, n) |
| 775 |
} |
| 776 |
b.StartTimer() |
| 777 |
benchmarkRemove(b, m, size) |
| 778 |
} |