github.com/enetx/g@v1.0.80/tests/map_test.go (about) 1 package g_test 2 3 import ( 4 "reflect" 5 "strings" 6 "testing" 7 8 "github.com/enetx/g" 9 "github.com/enetx/g/cmp" 10 "github.com/enetx/g/pkg/ref" 11 ) 12 13 func TestMapFromStd(t *testing.T) { 14 // Test case 1: Test conversion of an empty standard map 15 emptyStdMap := map[string]int{} 16 emptyGenericMap := g.MapFromStd(emptyStdMap) 17 if len(emptyGenericMap) != 0 { 18 t.Errorf("Test case 1 failed: Expected empty generic map, got %v", emptyGenericMap) 19 } 20 21 // Test case 2: Test conversion of a standard map with elements 22 stdMap := map[string]int{"a": 1, "b": 2, "c": 3} 23 genericMap := g.MapFromStd(stdMap) 24 for k, v := range stdMap { 25 if genericMap[k] != v { 26 t.Errorf("Test case 2 failed: Value mismatch for key %s. Expected %d, got %d", k, v, genericMap[k]) 27 } 28 } 29 } 30 31 func TestMapClear(t *testing.T) { 32 // Test case 1: Clearing an empty map 33 emptyMap := g.Map[string, int]{} 34 clearedEmptyMap := emptyMap.Clear() 35 if !clearedEmptyMap.Empty() { 36 t.Errorf("Test case 1 failed: Cleared empty map should be empty") 37 } 38 39 // Test case 2: Clearing a non-empty map 40 testMap := g.Map[string, int]{"a": 1, "b": 2, "c": 3} 41 clearedTestMap := testMap.Clear() 42 if !clearedTestMap.Empty() { 43 t.Errorf("Test case 2 failed: Cleared test map should be empty") 44 } 45 } 46 47 func TestMapEmpty(t *testing.T) { 48 // Test case 1: Empty map 49 emptyMap := g.Map[string, int]{} 50 if !emptyMap.Empty() { 51 t.Errorf("Test case 1 failed: Empty map should be empty") 52 } 53 54 // Test case 2: Non-empty map 55 testMap := g.Map[string, int]{"a": 1, "b": 2, "c": 3} 56 if testMap.Empty() { 57 t.Errorf("Test case 2 failed: Non-empty map should not be empty") 58 } 59 } 60 61 func TestMapString(t *testing.T) { 62 // Test case 1: Empty map 63 emptyMap := g.Map[string, int]{} 64 expectedEmptyMapString := "Map{}" 65 emptyMapString := emptyMap.String() 66 if emptyMapString != expectedEmptyMapString { 67 t.Errorf("Test case 1 failed: Expected %q, got %q", expectedEmptyMapString, emptyMapString) 68 } 69 // Test case 2: Map with elements 70 testMap := g.Map[string, int]{"a": 1} 71 expectedTestMapString := "Map{a:1}" 72 testMapString := testMap.String() 73 if testMapString != expectedTestMapString { 74 t.Errorf("Test case 2 failed: Expected %q, got %q", expectedTestMapString, testMapString) 75 } 76 } 77 78 func TestMapKeys(t *testing.T) { 79 m := g.NewMap[string, int]() 80 m.Set("a", 1) 81 m.Set("b", 2) 82 m.Set("c", 3) 83 84 keys := m.Keys() 85 if keys.Len() != 3 { 86 t.Errorf("Expected 3 keys, got %d", keys.Len()) 87 } 88 89 if !keys.Contains("a") { 90 t.Errorf("Expected key 'a'") 91 } 92 93 if !keys.Contains("b") { 94 t.Errorf("Expected key 'b'") 95 } 96 97 if !keys.Contains("c") { 98 t.Errorf("Expected key 'c'") 99 } 100 } 101 102 func TestMapValues(t *testing.T) { 103 m := g.NewMap[string, int]() 104 105 m.Set("a", 1) 106 m.Set("b", 2) 107 m.Set("c", 3) 108 109 values := m.Values() 110 111 if values.Len() != 3 { 112 t.Errorf("Expected 3 values, got %d", values.Len()) 113 } 114 115 if !values.Contains(1) { 116 t.Errorf("Expected value '1'") 117 } 118 119 if !values.Contains(2) { 120 t.Errorf("Expected value '2'") 121 } 122 123 if !values.Contains(3) { 124 t.Errorf("Expected value '3'") 125 } 126 } 127 128 func TestMapClone(t *testing.T) { 129 m := g.NewMap[string, int]() 130 m["a"] = 1 131 m["b"] = 2 132 m["c"] = 3 133 134 nm := m.Clone() 135 136 if m.Len() != nm.Len() { 137 t.Errorf("Clone failed: expected %d, got %d", m.Len(), nm.Len()) 138 } 139 140 for k, v := range m { 141 if nm[k] != v { 142 t.Errorf("Clone failed: expected %d, got %d", v, nm[k]) 143 } 144 } 145 } 146 147 func TestMapCopy(t *testing.T) { 148 src := g.Map[string, int]{ 149 "a": 1, 150 "b": 2, 151 "c": 3, 152 } 153 154 dst := g.Map[string, int]{ 155 "d": 4, 156 "e": 5, 157 "a": 6, 158 } 159 160 dst.Copy(src) 161 162 if dst.Len() != 5 { 163 t.Errorf("Expected len(dst) to be 5, got %d", len(dst)) 164 } 165 166 if dst["a"] != 1 { 167 t.Errorf("Expected dst[\"a\"] to be 1, got %d", dst["a"]) 168 } 169 170 if dst["b"] != 2 { 171 t.Errorf("Expected dst[\"b\"] to be 2, got %d", dst["b"]) 172 } 173 174 if dst["c"] != 3 { 175 t.Errorf("Expected dst[\"c\"] to be 3, got %d", dst["c"]) 176 } 177 } 178 179 func TestMapAdd(t *testing.T) { 180 m := g.Map[string, string]{} 181 m = m.Set("key", "value") 182 183 if m["key"] != "value" { 184 t.Error("Expected value to be 'value'") 185 } 186 } 187 188 func TestMapDelete(t *testing.T) { 189 m := g.Map[string, int]{"a": 1, "b": 2, "c": 3} 190 191 m = m.Delete("a", "b") 192 193 if m.Len() != 1 { 194 t.Errorf("Expected length of 1, got %d", m.Len()) 195 } 196 197 if _, ok := m["a"]; ok { 198 t.Errorf("Expected key 'a' to be deleted") 199 } 200 201 if _, ok := m["b"]; ok { 202 t.Errorf("Expected key 'b' to be deleted") 203 } 204 205 if _, ok := m["c"]; !ok { 206 t.Errorf("Expected key 'c' to be present") 207 } 208 } 209 210 func TestMapEq(t *testing.T) { 211 // Test case 1: Equal maps 212 map1 := g.Map[string, int]{"a": 1, "b": 2, "c": 3} 213 map2 := g.Map[string, int]{"a": 1, "b": 2, "c": 3} 214 if !map1.Eq(map2) { 215 t.Errorf("Test case 1 failed: Equal maps should be considered equal") 216 } 217 218 // Test case 2: Maps with different lengths 219 map3 := g.Map[string, int]{"a": 1, "b": 2} 220 if map1.Eq(map3) { 221 t.Errorf("Test case 2 failed: Maps with different lengths should not be considered equal") 222 } 223 224 // Test case 3: Maps with different values 225 map4 := g.Map[string, int]{"a": 1, "b": 2, "c": 4} 226 if map1.Eq(map4) { 227 t.Errorf("Test case 3 failed: Maps with different values should not be considered equal") 228 } 229 230 // Test case 4 231 map5 := g.Map[string, []int]{"a": []int{1}, "b": []int{2}, "c": []int{4}} 232 map6 := g.Map[string, []int]{"a": []int{1}, "b": []int{2}, "c": []int{4}} 233 if map5.Ne(map6) { 234 t.Errorf("Test case 4 failed: Equal maps should be considered equal") 235 } 236 237 // Test case 5 238 map7 := g.Map[string, []int]{"a": []int{2}, "b": []int{5}, "c": []int{4}} 239 if map5.Eq(map7) { 240 t.Errorf("Test case 5 failed: Maps with different values should not be considered equal") 241 } 242 } 243 244 func TestMapToMap(t *testing.T) { 245 m := g.NewMap[string, int]() 246 m.Set("a", 1) 247 m.Set("b", 2) 248 m.Set("c", 3) 249 250 nmap := m.Std() 251 252 if len(nmap) != 3 { 253 t.Errorf("Expected 3, got %d", len(nmap)) 254 } 255 256 if nmap["a"] != 1 { 257 t.Errorf("Expected 1, got %d", nmap["a"]) 258 } 259 260 if nmap["b"] != 2 { 261 t.Errorf("Expected 2, got %d", nmap["b"]) 262 } 263 264 if nmap["c"] != 3 { 265 t.Errorf("Expected 3, got %d", nmap["c"]) 266 } 267 } 268 269 func TestMapLen(t *testing.T) { 270 m := g.Map[int, int]{} 271 if m.Len() != 0 { 272 t.Errorf("Expected 0, got %d", m.Len()) 273 } 274 275 m[1] = 1 276 if m.Len() != 1 { 277 t.Errorf("Expected 1, got %d", m.Len()) 278 } 279 280 m[2] = 2 281 if m.Len() != 2 { 282 t.Errorf("Expected 2, got %d", m.Len()) 283 } 284 } 285 286 func TestMapMap(t *testing.T) { 287 m := g.NewMap[int, string](3) 288 m.Set(1, "one") 289 m.Set(2, "two") 290 m.Set(3, "three") 291 292 expected := g.NewMap[int, string](3) 293 expected.Set(2, "one") 294 expected.Set(4, "two") 295 expected.Set(6, "three") 296 297 mapped := m.Iter().Map(func(k int, v string) (int, string) { return k * 2, v }).Collect() 298 299 if !reflect.DeepEqual(mapped, expected) { 300 t.Errorf("Map failed: expected %v, but got %v", expected, mapped) 301 } 302 303 expected = g.NewMap[int, string](3) 304 expected.Set(1, "one_suffix") 305 expected.Set(2, "two_suffix") 306 expected.Set(3, "three_suffix") 307 308 mapped = m.Iter().Map(func(k int, v string) (int, string) { return k, v + "_suffix" }).Collect() 309 310 if !reflect.DeepEqual(mapped, expected) { 311 t.Errorf("Map failed: expected %v, but got %v", expected, mapped) 312 } 313 314 expected = g.NewMap[int, string](3) 315 expected.Set(0, "") 316 expected.Set(1, "one") 317 expected.Set(3, "three") 318 319 mapped = m.Iter().Map(func(k int, v string) (int, string) { 320 if k == 2 { 321 return 0, "" 322 } 323 return k, v 324 }).Collect() 325 326 if !reflect.DeepEqual(mapped, expected) { 327 t.Errorf("Map failed: expected %v, but got %v", expected, mapped) 328 } 329 } 330 331 func TestMapFilter(t *testing.T) { 332 m := g.NewMap[string, int](3) 333 m.Set("one", 1) 334 m.Set("two", 2) 335 m.Set("three", 3) 336 337 expected := g.NewMap[string, int](1) 338 expected.Set("two", 2) 339 340 filtered := m.Iter().Filter(func(k string, v int) bool { return v%2 == 0 }).Collect() 341 342 if !reflect.DeepEqual(filtered, expected) { 343 t.Errorf("Filter failed: expected %v, but got %v", expected, filtered) 344 } 345 346 expected = g.NewMap[string, int](2) 347 expected.Set("one", 1) 348 expected.Set("three", 3) 349 350 filtered = m.Iter().Filter(func(k string, v int) bool { return strings.Contains(k, "e") }).Collect() 351 352 if !reflect.DeepEqual(filtered, expected) { 353 t.Errorf("Filter failed: expected %v, but got %v", expected, filtered) 354 } 355 356 expected = g.NewMap[string, int](3) 357 expected.Set("one", 1) 358 expected.Set("two", 2) 359 expected.Set("three", 3) 360 361 filtered = m.Iter().Filter(func(k string, v int) bool { return true }).Collect() 362 363 if !reflect.DeepEqual(filtered, expected) { 364 t.Errorf("Filter failed: expected %v, but got %v", expected, filtered) 365 } 366 367 expected = g.NewMap[string, int](0) 368 369 filtered = m.Iter().Filter(func(k string, v int) bool { return false }).Collect() 370 371 if !reflect.DeepEqual(filtered, expected) { 372 t.Errorf("Filter failed: expected %v, but got %v", expected, filtered) 373 } 374 } 375 376 func TestMapInvertValues(t *testing.T) { 377 m := g.NewMap[int, string](0) 378 inv := m.Invert() 379 380 if inv.Len() != 0 { 381 t.Errorf("Expected inverted map to have length 0, but got length %d", inv.Len()) 382 } 383 384 m2 := g.NewMap[string, int](3) 385 m2.Set("one", 1) 386 m2.Set("two", 2) 387 m2.Set("three", 3) 388 389 inv2 := m2.Invert() 390 391 if inv2.Len() != 3 { 392 t.Errorf("Expected inverted map to have length 3, but got length %d", inv2.Len()) 393 } 394 395 if inv2.Get(1).Some() != "one" { 396 t.Errorf("Expected inverted map to map 1 to 'one', but got %s", inv2.Get(1).Some()) 397 } 398 399 if inv2.Get(2).Some() != "two" { 400 t.Errorf("Expected inverted map to map 2 to 'two', but got %s", inv2.Get(2).Some()) 401 } 402 403 if inv2.Get(3).Some() != "three" { 404 t.Errorf("Expected inverted map to map 3 to 'three', but got %s", inv2.Get(3).Some()) 405 } 406 } 407 408 func TestMapGet(t *testing.T) { 409 // Test case 1: Get existing key 410 map1 := g.Map[string, int]{"a": 1, "b": 2, "c": 3} 411 key1 := "b" 412 expectedValue1 := 2 413 value1 := map1.Get(key1) 414 if value1.Some() != expectedValue1 { 415 t.Errorf("Test case 1 failed: Expected value for key '%s' is %v, got %v", key1, expectedValue1, value1) 416 } 417 418 // Test case 2: Get non-existing key 419 key2 := "d" 420 value2 := map1.Get(key2) 421 if value2.IsSome() { 422 t.Errorf("Test case 2 failed, got %v", value2.Some()) 423 } 424 } 425 426 func TestMapGetOrSet(t *testing.T) { 427 // Create a new ordered Map called "m" with string keys and integer pointers as values 428 m := g.NewMap[string, *int]() 429 430 // Use GetOrSet to set the value for the key "root" to 3 if it doesn't exist 431 m.GetOrSet("root", ref.Of(3)) 432 433 // Check if the value for the key "root" is equal to 3 434 value := m.Get("root").Some() 435 if *value != 3 { 436 t.Errorf("Expected value 3 for key 'root', but got %v", *value) 437 } 438 439 // Use GetOrSet to retrieve the value for the key "root" (which is 3), multiply it by 2 440 *m.GetOrSet("root", ref.Of(10)) *= 2 441 442 // Check if the value for the key "root" is equal to 6 443 value = m.Get("root").Some() 444 if *value != 6 { 445 t.Errorf("Expected value 6 for key 'root', but got %v", *value) 446 } 447 } 448 449 func TestRandomMap(t *testing.T) { 450 // Create a map for testing 451 testMap := g.NewMap[string, int]() 452 testMap.Set("one", 1) 453 testMap.Set("two", 2) 454 testMap.Set("three", 3) 455 456 // Randomly select a key-value pair 457 randomResult := testMap.Iter().Take(1).Collect() 458 459 // Check if the result is a map with a single key-value pair 460 if randomResult.Len() != 1 { 461 t.Errorf("Expected a map with a single key-value pair, but got length %d", randomResult.Len()) 462 } 463 464 // Check if the selected key exists in the original map 465 key := randomResult.Keys()[0] 466 if !testMap.Contains(key) { 467 t.Errorf("Randomly selected key not found in the original map") 468 } 469 470 // Check if the selected value matches the original map 471 value := randomResult.Get(key).Some() 472 originalValue := testMap.Get(key).Some() 473 if value != originalValue { 474 t.Errorf("Randomly selected value does not match the original value") 475 } 476 } 477 478 func TestRandomEmptyMap(t *testing.T) { 479 // Create an empty map for testing 480 testMap := g.NewMap[string, int]() 481 482 // Attempt to randomly select a key-value pair 483 randomResult := testMap.Iter().Take(1).Collect() 484 485 // Check if the result is an empty map 486 if randomResult.Len() != 0 { 487 t.Errorf("Expected an empty map, but got length %d", randomResult.Len()) 488 } 489 } 490 491 func TestRandomSampleMap(t *testing.T) { 492 // Create a map for testing 493 testMap := g.NewMap[string, int]() 494 testMap.Set("one", 1) 495 testMap.Set("two", 2) 496 testMap.Set("three", 3) 497 498 // Randomly select a sample of key-value pairs 499 randomResult := testMap.Iter().Take(2).Collect() 500 501 // Check if the result is a map with the specified number of key-value pairs 502 if randomResult.Len() != 2 { 503 t.Errorf("Expected a map with 2 key-value pairs, but got length %d", randomResult.Len()) 504 } 505 506 // Check if all selected keys exist in the original map 507 keys := randomResult.Keys() 508 for _, key := range keys { 509 if !testMap.Contains(key) { 510 t.Errorf("Randomly selected key '%s' not found in the original map", key) 511 } 512 } 513 514 // Check if the selected values match the original map 515 for _, key := range keys { 516 value := randomResult.Get(key).Some() 517 originalValue := testMap.Get(key).Some() 518 if value != originalValue { 519 t.Errorf("Randomly selected value for key '%s' does not match the original value", key) 520 } 521 } 522 } 523 524 func TestRandomSampleEmptyMap(t *testing.T) { 525 // Create an empty map for testing 526 testMap := g.NewMap[string, int]() 527 528 // Attempt to randomly select a sample of key-value pairs 529 randomResult := testMap.Iter().Take(3).Collect() 530 531 // Check if the result is an empty map 532 if randomResult.Len() != 0 { 533 t.Errorf("Expected an empty map, but got length %d", randomResult.Len()) 534 } 535 } 536 537 func TestRandomSampleFullMap(t *testing.T) { 538 // Create a map for testing 539 testMap := g.NewMap[string, int]() 540 testMap.Set("one", 1) 541 testMap.Set("two", 2) 542 543 // Randomly select a sample of key-value pairs 544 randomResult := testMap.Iter().Take(2).Collect() 545 546 // Check if the result is the same as the original map 547 if randomResult.Len() != 2 { 548 t.Errorf("Expected a map with 2 key-value pairs, but got length %d", randomResult.Len()) 549 } 550 551 keys := randomResult.Keys() 552 for _, key := range keys { 553 if !testMap.Contains(key) { 554 t.Errorf("Randomly selected key '%s' not found in the original map", key) 555 } 556 } 557 } 558 559 func TestRandomRangeMapEmpty(t *testing.T) { 560 // Create an empty map for testing 561 testMap := g.NewMap[string, int]() 562 563 // Attempt to randomize a range 564 subrangeMap := testMap.Iter().Take(g.Int(3).RandomRange(5).AsUInt()).Collect() 565 566 // Check if the result is an empty map 567 if subrangeMap.Len() != 0 { 568 t.Errorf("Expected an empty map, but got length %d", subrangeMap.Len()) 569 } 570 } 571 572 func TestRandomRangeMapInvalidRange(t *testing.T) { 573 // Create a map for testing 574 testMap := g.NewMap[string, int]() 575 testMap.Set("one", 1) 576 testMap.Set("two", 2) 577 578 // Test an invalid range 579 580 subrangeMap := testMap.Iter().Take(g.Int(3).RandomRange(5).AsUInt()).Collect() 581 582 // Check if the result is the same as the original map 583 if subrangeMap.Len() != 2 { 584 t.Errorf("Expected a map with 2 key-value pairs, but got length %d", subrangeMap.Len()) 585 } 586 } 587 588 func TestMapNe(t *testing.T) { 589 // Test case 1: Maps are equal 590 map1 := g.Map[string, int]{"a": 1, "b": 2} 591 map2 := g.Map[string, int]{"a": 1, "b": 2} 592 expectedResult1 := false 593 result1 := map1.Ne(map2) 594 if result1 != expectedResult1 { 595 t.Errorf("Test case 1 failed: Expected result is %t, got %t", expectedResult1, result1) 596 } 597 598 // Test case 2: Maps are not equal 599 map3 := g.Map[string, int]{"a": 1, "b": 2} 600 map4 := g.Map[string, int]{"a": 1, "b": 3} 601 expectedResult2 := true 602 result2 := map3.Ne(map4) 603 if result2 != expectedResult2 { 604 t.Errorf("Test case 2 failed: Expected result is %t, got %t", expectedResult2, result2) 605 } 606 } 607 608 func TestMapNotEmpty(t *testing.T) { 609 // Test case 1: Map is not empty 610 map1 := g.Map[string, int]{"a": 1, "b": 2} 611 expectedResult1 := true 612 result1 := map1.NotEmpty() 613 if result1 != expectedResult1 { 614 t.Errorf("Test case 1 failed: Expected result is %t, got %t", expectedResult1, result1) 615 } 616 617 // Test case 2: Map is empty 618 map2 := g.Map[string, int]{} 619 expectedResult2 := false 620 result2 := map2.NotEmpty() 621 if result2 != expectedResult2 { 622 t.Errorf("Test case 2 failed: Expected result is %t, got %t", expectedResult2, result2) 623 } 624 } 625 626 func TestMapIterChain(t *testing.T) { 627 // Test case 1: Concatenate two iterators 628 iter1 := g.NewMap[int, string]().Set(1, "a").Iter() 629 iter2 := g.NewMap[int, string]().Set(2, "b").Iter() 630 631 concatenated := iter1.Chain(iter2).Collect() 632 633 expected := g.NewMap[int, string]().Set(1, "a").Set(2, "b") 634 if !reflect.DeepEqual(concatenated, expected) { 635 t.Errorf("Expected concatenated map to be %v, got %v", expected, concatenated) 636 } 637 638 // Test case 2: Concatenate three iterators 639 iter3 := g.NewMap[int, string]().Set(3, "c").Iter() 640 641 concatenated2 := iter1.Chain(iter2, iter3).Collect() 642 643 expected2 := g.NewMap[int, string]().Set(1, "a").Set(2, "b").Set(3, "c") 644 if !reflect.DeepEqual(concatenated2, expected2) { 645 t.Errorf("Expected concatenated map to be %v, got %v", expected2, concatenated2) 646 } 647 } 648 649 func TestMapIterCount(t *testing.T) { 650 // Test case 1: Count elements in a non-empty map 651 iter := g.NewMap[int, string]().Set(1, "a").Set(2, "b").Iter() 652 653 count := iter.Count() 654 655 expected := g.Int(2) 656 if count != expected { 657 t.Errorf("Expected count to be %d, got %d", expected, count) 658 } 659 660 // Test case 2: Count elements in an empty map 661 emptyIter := g.NewMap[int, string]().Iter() 662 663 emptyCount := emptyIter.Count() 664 665 emptyExpected := g.Int(0) 666 if emptyCount != emptyExpected { 667 t.Errorf("Expected count to be %d, got %d", emptyExpected, emptyCount) 668 } 669 } 670 671 func TestMapIterExclude(t *testing.T) { 672 // Test case 1: Exclude even values 673 m := g.NewMap[int, string](). 674 Set(1, "a"). 675 Set(2, "b"). 676 Set(3, "c"). 677 Set(4, "d"). 678 Set(5, "e") 679 680 notEven := m.Iter(). 681 Exclude(func(k int, v string) bool { 682 return k%2 == 0 683 }). 684 Collect() 685 686 expected := g.NewMap[int, string](). 687 Set(1, "a"). 688 Set(3, "c"). 689 Set(5, "e") 690 691 if !notEven.Eq(expected) { 692 t.Errorf("Excluded result incorrect, expected: %v, got: %v", expected, notEven) 693 } 694 695 // Test case 2: Exclude all elements 696 empty := m.Iter(). 697 Exclude(func(k int, v string) bool { 698 return true 699 }). 700 Collect() 701 702 if !empty.Empty() { 703 t.Errorf("Expected empty map after exclusion, got: %v", empty) 704 } 705 } 706 707 func TestMapIterFind(t *testing.T) { 708 // Test case 1: Find an existing element 709 m := g.NewMap[int, string](). 710 Set(1, "a"). 711 Set(2, "b"). 712 Set(3, "c"). 713 Set(4, "d"). 714 Set(5, "e") 715 716 found := m.Iter(). 717 Find(func(k int, v string) bool { 718 return k == 3 719 }) 720 721 if found.IsNone() { 722 t.Errorf("Expected to find key-value pair, got None") 723 } else { 724 expected := g.Pair[int, string]{Key: 3, Value: "c"} 725 if found.Some() != expected { 726 t.Errorf("Found key-value pair incorrect, expected: %v, got: %v", expected, found.Some()) 727 } 728 } 729 730 // Test case 2: Find a non-existing element 731 notFound := m.Iter(). 732 Find(func(k int, v string) bool { 733 return k == 6 734 }) 735 736 if notFound.IsSome() { 737 t.Errorf("Expected not to find key-value pair, got: %v", notFound.Some()) 738 } 739 } 740 741 func TestMapIterRange(t *testing.T) { 742 // Define a map to iterate over 743 m := g.NewMap[int, string]() 744 m.Set(1, "apple") 745 m.Set(2, "banana") 746 m.Set(3, "cherry") 747 748 // Define a slice to collect the keys visited during iteration 749 var keysVisited g.Slice[int] 750 751 // Iterate over the map using Range 752 m.Iter().Range(func(k int, v string) bool { 753 keysVisited = append(keysVisited, k) 754 // Continue iterating until all elements are visited 755 return true 756 }) 757 758 keysVisited.SortBy(func(a, b int) cmp.Ordering { return cmp.Cmp(a, b) }) 759 760 // Check if all keys were visited 761 expectedKeys := g.Slice[int]{1, 2, 3} 762 763 if !reflect.DeepEqual(keysVisited, expectedKeys) { 764 t.Errorf("Expected keys visited to be %v, got %v", expectedKeys, keysVisited) 765 } 766 } 767 768 func TestMapIterInspect(t *testing.T) { 769 // Define a map to iterate over 770 m := g.NewMap[int, string]() 771 m.Set(1, "apple") 772 m.Set(2, "banana") 773 m.Set(3, "cherry") 774 775 // Define a slice to store the inspected key-value pairs 776 inspected := g.NewMap[int, string]() 777 778 // Create a new iterator with Inspect and collect the pairs 779 m.Iter().Inspect(func(k int, v string) { 780 inspected.Set(k, v) 781 }).Collect() 782 783 if !inspected.Eq(m) { 784 t.Errorf("Expected inspected map to be %v, got %v", m, inspected) 785 } 786 }