github.com/enetx/g@v1.0.80/tests/mapord_test.go (about) 1 package g_test 2 3 import ( 4 "context" 5 "reflect" 6 "slices" 7 "testing" 8 9 "github.com/enetx/g" 10 "github.com/enetx/g/cmp" 11 ) 12 13 func TestMapOrdIterSortBy(t *testing.T) { 14 // Sample data 15 data := g.NewMapOrd[int, string]() 16 data. 17 Set(1, "d"). 18 Set(3, "b"). 19 Set(2, "c"). 20 Set(5, "e"). 21 Set(4, "a") 22 23 // Expected result 24 expected := g.NewMapOrd[int, string]() 25 expected. 26 Set(1, "d"). 27 Set(2, "c"). 28 Set(3, "b"). 29 Set(4, "a"). 30 Set(5, "e") 31 32 sortedItems := data.Iter(). 33 SortBy(func(a, b g.Pair[int, string]) cmp.Ordering { return cmp.Cmp(a.Key, b.Key) }). 34 Collect() 35 36 // Check if the result matches the expected output 37 if !reflect.DeepEqual(sortedItems, expected) { 38 t.Errorf("Expected %v, got %v", expected, sortedItems) 39 } 40 41 expected = g.NewMapOrd[int, string]() 42 expected. 43 Set(4, "a"). 44 Set(3, "b"). 45 Set(2, "c"). 46 Set(1, "d"). 47 Set(5, "e") 48 49 sortedItems = data.Iter(). 50 SortBy(func(a, b g.Pair[int, string]) cmp.Ordering { return cmp.Cmp(a.Value, b.Value) }). 51 Collect() 52 53 // Check if the result matches the expected output 54 if !reflect.DeepEqual(sortedItems, expected) { 55 t.Errorf("Expected %v, got %v", expected, sortedItems) 56 } 57 } 58 59 func TestMapOrdIterStepBy(t *testing.T) { 60 // Test case 1: StepBy with a step size of 2 61 mapData := g.NewMapOrd[string, int]() 62 mapData. 63 Set("one", 1). 64 Set("two", 2). 65 Set("three", 3). 66 Set("four", 4). 67 Set("five", 5) 68 69 expectedResult := g.NewMapOrd[string, int]() 70 expectedResult. 71 Set("one", 1). 72 Set("three", 3). 73 Set("five", 5) 74 75 iter := mapData.Iter().StepBy(2) 76 result := iter.Collect() 77 78 if !reflect.DeepEqual(result, expectedResult) { 79 t.Errorf("StepBy failed. Expected %v, got %v", expectedResult, result) 80 } 81 82 // Test case 2: StepBy with a step size of 3 83 mapData = g.NewMapOrd[string, int]() 84 mapData. 85 Set("one", 1). 86 Set("two", 2). 87 Set("three", 3). 88 Set("four", 4). 89 Set("five", 5) 90 91 expectedResult = g.NewMapOrd[string, int]() 92 expectedResult.Set("one", 1).Set("four", 4) 93 94 iter = mapData.Iter().StepBy(3) 95 result = iter.Collect() 96 97 if !reflect.DeepEqual(result, expectedResult) { 98 t.Errorf("StepBy failed. Expected %v, got %v", expectedResult, result) 99 } 100 101 // Test case 3: StepBy with a step size larger than the map length 102 103 mapData = g.NewMapOrd[string, int]() 104 mapData.Set("one", 1). 105 Set("two", 2). 106 Set("three", 3) 107 108 expectedResult = g.NewMapOrd[string, int]() 109 expectedResult.Set("one", 1) 110 111 iter = mapData.Iter().StepBy(5) 112 result = iter.Collect() 113 114 if !reflect.DeepEqual(result, expectedResult) { 115 t.Errorf("StepBy failed. Expected %v, got %v", expectedResult, result) 116 } 117 118 // Test case 4: StepBy with a step size of 1 119 mapData = g.NewMapOrd[string, int]() 120 mapData.Set("one", 1). 121 Set("two", 2). 122 Set("three", 3) 123 124 expectedResult = g.NewMapOrd[string, int]() 125 expectedResult. 126 Set("one", 1). 127 Set("two", 2). 128 Set("three", 3) 129 130 iter = mapData.Iter().StepBy(1) 131 result = iter.Collect() 132 133 if !reflect.DeepEqual(result, expectedResult) { 134 t.Errorf("StepBy failed. Expected %v, got %v", expectedResult, result) 135 } 136 } 137 138 func TestMapOrdIterRange(t *testing.T) { 139 // Test scenario: Function stops at a specific key-value pair 140 t.Run("FunctionStopsAtSpecificPair", func(t *testing.T) { 141 orderedMap := g.MapOrd[string, int]{ 142 {Key: "a", Value: 1}, 143 {Key: "b", Value: 2}, 144 {Key: "c", Value: 3}, 145 } 146 expected := map[string]int{"a": 1, "b": 2} 147 148 result := make(map[string]int) 149 stopAtB := func(key string, val int) bool { 150 result[key] = val 151 return key != "b" 152 } 153 154 orderedMap.Iter().Range(stopAtB) 155 156 if !reflect.DeepEqual(result, expected) { 157 t.Errorf("Expected: %v, Got: %v", expected, result) 158 } 159 }) 160 161 // Test scenario: Function always returns true 162 t.Run("FunctionAlwaysTrue", func(t *testing.T) { 163 orderedMap := g.MapOrd[string, int]{ 164 {"a", 1}, 165 {"b", 2}, 166 {"c", 3}, 167 } 168 169 expected := map[string]int{"a": 1, "b": 2, "c": 3} 170 171 result := make(map[string]int) 172 alwaysTrue := func(key string, val int) bool { 173 result[key] = val 174 return true 175 } 176 177 orderedMap.Iter().Range(alwaysTrue) 178 179 if !reflect.DeepEqual(result, expected) { 180 t.Errorf("Expected: %v, Got: %v", expected, result) 181 } 182 }) 183 184 // Test scenario: Empty ordered map 185 t.Run("EmptyMap", func(t *testing.T) { 186 emptyMap := g.MapOrd[string, int]{} 187 expected := make(map[string]int) 188 189 result := make(map[string]int) 190 anyFunc := func(key string, val int) bool { 191 result[key] = val 192 return true 193 } 194 195 emptyMap.Iter().Range(anyFunc) 196 197 if !reflect.DeepEqual(result, expected) { 198 t.Errorf("Expected: %v, Got: %v", expected, result) 199 } 200 }) 201 } 202 203 func TestMapOrdNe(t *testing.T) { 204 // Test case 1: Maps are equal 205 m1 := g.NewMapOrd[int, string]() 206 m2 := g.NewMapOrd[int, string]() 207 m1.Set(1, "a") 208 m2.Set(1, "a") 209 if m1.Ne(m2) { 210 t.Errorf("Expected maps to be equal") 211 } 212 213 // Test case 2: Maps are not equal 214 m2.Set(2, "b") 215 if !m1.Ne(m2) { 216 t.Errorf("Expected maps to be not equal") 217 } 218 } 219 220 func TestMapOrdNotEmpty(t *testing.T) { 221 // Test case 1: Map is empty 222 m := g.NewMapOrd[int, string]() 223 if m.NotEmpty() { 224 t.Errorf("Expected map to be empty") 225 } 226 227 // Test case 2: Map is not empty 228 m.Set(1, "a") 229 if !m.NotEmpty() { 230 t.Errorf("Expected map to be not empty") 231 } 232 } 233 234 func TestMapOrdString(t *testing.T) { 235 // Test case 1: Map with elements 236 m := g.NewMapOrd[int, string]() 237 m.Set(1, "a") 238 m.Set(2, "b") 239 m.Set(3, "c") 240 expected := "MapOrd{1:a, 2:b, 3:c}" 241 if str := m.String(); str != expected { 242 t.Errorf("Expected string representation to be %s, got %s", expected, str) 243 } 244 245 // Test case 2: Empty Map 246 m2 := g.NewMapOrd[string, int]() 247 expected2 := "MapOrd{}" 248 if str := m2.String(); str != expected2 { 249 t.Errorf("Expected string representation to be %s, got %s", expected2, str) 250 } 251 } 252 253 func TestMapOrdClear(t *testing.T) { 254 // Test case 1: Map with elements 255 m := g.NewMapOrd[int, string]() 256 m.Set(1, "a") 257 m.Set(2, "b") 258 m.Clear() 259 if !m.Empty() { 260 t.Errorf("Expected map to be empty after clearing") 261 } 262 263 // Test case 2: Empty Map 264 m2 := g.NewMapOrd[string, int]() 265 m2.Clear() 266 if !m2.Empty() { 267 t.Errorf("Expected empty map to remain empty after clearing") 268 } 269 } 270 271 func TestMapOrdContains(t *testing.T) { 272 // Test case 1: Map contains the key 273 m := g.NewMapOrd[int, string]() 274 m.Set(1, "a") 275 if !m.Contains(1) { 276 t.Errorf("Expected map to contain the key") 277 } 278 279 // Test case 2: Map doesn't contain the key 280 if m.Contains(2) { 281 t.Errorf("Expected map not to contain the key") 282 } 283 284 // Test case 3: Map contains the key 285 m2 := g.NewMapOrd[[]int, []int]() 286 m2.Set([]int{0}, []int{1}) 287 288 if !m2.Contains([]int{0}) { 289 t.Errorf("Expected map to contain the key") 290 } 291 } 292 293 func TestMapOrdValues(t *testing.T) { 294 // Test case 1: Map with elements 295 m := g.NewMapOrd[int, string]() 296 m.Set(1, "a") 297 m.Set(2, "b") 298 m.Set(3, "c") 299 expected := g.Slice[string]{"a", "b", "c"} 300 values := m.Values() 301 if len(values) != len(expected) { 302 t.Errorf("Expected values to have length %d, got %d", len(expected), len(values)) 303 } 304 for i, v := range values { 305 if v != expected[i] { 306 t.Errorf("Expected value at index %d to be %s, got %s", i, expected[i], v) 307 } 308 } 309 310 // Test case 2: Empty Map 311 m2 := g.NewMapOrd[string, int]() 312 values2 := m2.Values() 313 if len(values2) != 0 { 314 t.Errorf("Expected values to be empty for an empty map") 315 } 316 } 317 318 func TestMapOrdInvert(t *testing.T) { 319 // Test case 1: Map with elements 320 m := g.NewMapOrd[int, string]() 321 m.Set(1, "a") 322 m.Set(2, "b") 323 m.Set(3, "c") 324 inverted := m.Invert() 325 expected := g.NewMapOrd[string, int]() 326 expected.Set("a", 1) 327 expected.Set("b", 2) 328 expected.Set("c", 3) 329 if inverted.Len() != expected.Len() { 330 t.Errorf("Expected inverted map to have length %d, got %d", expected.Len(), inverted.Len()) 331 } 332 333 inverted.Iter().ForEach(func(k string, v int) { 334 if !expected.Contains(k) { 335 t.Errorf("Expected inverted map to contain key-value pair %s:%d", k, v) 336 } 337 }) 338 339 // Test case 2: Empty Map 340 m2 := g.NewMapOrd[string, int]() 341 inverted2 := m2.Invert() 342 if inverted2.Len() != 0 { 343 t.Errorf("Expected inverted map of an empty map to be empty") 344 } 345 } 346 347 func TestMapOrdGetOrSet(t *testing.T) { 348 // Test case 1: Key exists 349 m := g.NewMapOrd[string, int]() 350 m.Set("key1", 10) 351 defaultValue := 20 352 result := m.GetOrSet("key1", defaultValue) 353 if result != 10 { 354 t.Errorf("Expected value to be 10, got %d", result) 355 } 356 357 // Test case 2: Key doesn't exist 358 result = m.GetOrSet("key2", defaultValue) 359 if result != defaultValue { 360 t.Errorf("Expected value to be %d, got %d", defaultValue, result) 361 } 362 if value := m.Get("key2"); value.Some() != defaultValue { 363 t.Errorf("Expected key2 to be set with default value") 364 } 365 } 366 367 func TestMapOrdClone(t *testing.T) { 368 // Test case 1: Map with elements 369 m := g.NewMapOrd[int, string]() 370 m.Set(1, "a") 371 m.Set(2, "b") 372 m.Set(3, "c") 373 cloned := m.Clone() 374 if cloned.Len() != m.Len() { 375 t.Errorf("Expected cloned map to have length %d, got %d", m.Len(), cloned.Len()) 376 } 377 cloned.Iter().ForEach(func(k int, v string) { 378 if m.Get(k).Some() != v { 379 t.Errorf("Expected cloned map to have key-value pair %d:%s", k, v) 380 } 381 }) 382 383 // Test case 2: Empty Map 384 m2 := g.NewMapOrd[string, int]() 385 cloned2 := m2.Clone() 386 if cloned2.Len() != 0 { 387 t.Errorf("Expected cloned map of an empty map to be empty") 388 } 389 } 390 391 func TestMapOrdCopy(t *testing.T) { 392 // Test case 1: Map with elements 393 m := g.NewMapOrd[int, string]() 394 m.Set(1, "a") 395 m.Set(2, "b") 396 m.Set(3, "c") 397 398 src := g.NewMapOrd[int, string]() 399 src.Set(4, "d") 400 src.Set(5, "e") 401 402 m.Copy(src) 403 if m.Len() != 5 { 404 t.Errorf("Expected copied map to have length %d, got %d", 5, m.Len()) 405 } 406 407 src.Iter().ForEach(func(k int, v string) { 408 if m.Get(k).Some() != v { 409 t.Errorf("Expected copied map to have key-value pair %d:%s", k, v) 410 } 411 }) 412 413 // Test case 2: Empty Source Map 414 m2 := g.NewMapOrd[string, int]() 415 src2 := g.NewMapOrd[string, int]() 416 m2.Copy(src2) 417 if m2.Len() != 0 { 418 t.Errorf("Expected copied map of an empty source map to be empty") 419 } 420 } 421 422 func TestMapOrdSortBy(t *testing.T) { 423 // Test case 1: Sort by key 424 m := g.MapOrd[string, int]{ 425 {"b", 2}, 426 {"c", 3}, 427 {"a", 1}, 428 } 429 sortedByKey := m.SortBy(func(a, b g.Pair[string, int]) cmp.Ordering { return cmp.Cmp(a.Key, b.Key) }) 430 expectedKeyOrder := []string{"a", "b", "c"} 431 for i, p := range sortedByKey { 432 if p.Key != expectedKeyOrder[i] { 433 t.Errorf("Expected key at index %d to be %s, got %s", i, expectedKeyOrder[i], p.Key) 434 } 435 } 436 437 // Test case 2: Sort by value 438 m2 := g.MapOrd[string, int]{ 439 {"a", 3}, 440 {"b", 1}, 441 {"c", 2}, 442 } 443 sortedByValue := m2.SortBy(func(a, b g.Pair[string, int]) cmp.Ordering { return cmp.Cmp(a.Value, b.Value) }) 444 expectedValueOrder := []int{1, 2, 3} 445 for i, p := range sortedByValue { 446 if p.Value != expectedValueOrder[i] { 447 t.Errorf("Expected value at index %d to be %d, got %d", i, expectedValueOrder[i], p.Value) 448 } 449 } 450 } 451 452 func TestSortByKey(t *testing.T) { 453 // Create a sample MapOrd to test sorting by keys 454 mo := g.MapOrd[string, int]{ 455 {"b", 2}, 456 {"a", 1}, 457 {"c", 3}, 458 } 459 460 // Sort the MapOrd by keys using the custom comparison function 461 mo.SortByKey(cmp.Cmp) 462 463 // Expected sorted order by keys 464 expected := g.MapOrd[string, int]{ 465 {"a", 1}, 466 {"b", 2}, 467 {"c", 3}, 468 } 469 470 // Check if the MapOrd is sorted as expected 471 if !slices.Equal(mo, expected) { 472 t.Errorf("SortByKey failed: expected %v, got %v", expected, mo) 473 } 474 } 475 476 func TestSortByValue(t *testing.T) { 477 // Create a sample MapOrd to test sorting by values 478 mo := g.MapOrd[string, int]{ 479 {"a", 2}, 480 {"b", 1}, 481 {"c", 3}, 482 } 483 484 // Define a custom comparison function for integers 485 customIntCmp := func(a, b int) cmp.Ordering { 486 if a < b { 487 return cmp.Less 488 } else if a > b { 489 return cmp.Greater 490 } 491 return cmp.Equal 492 } 493 494 // Sort the MapOrd by values using the custom comparison function 495 mo.SortByValue(customIntCmp) 496 497 // Expected sorted order by values 498 expected := g.MapOrd[string, int]{ 499 {"b", 1}, 500 {"a", 2}, 501 {"c", 3}, 502 } 503 504 // Check if the MapOrd is sorted as expected 505 if !slices.Equal(mo, expected) { 506 t.Errorf("SortByValue failed: expected %v, got %v", expected, mo) 507 } 508 } 509 510 func TestSortIterByKey(t *testing.T) { 511 // Create a sample MapOrd to test sorting by keys 512 mo := g.MapOrd[string, int]{ 513 {"b", 2}, 514 {"a", 1}, 515 {"c", 3}, 516 } 517 518 // Sort the MapOrd by keys using the custom comparison function 519 mo = mo.Iter().SortByKey(cmp.Cmp).Collect() 520 521 // Expected sorted order by keys 522 expected := g.MapOrd[string, int]{ 523 {"a", 1}, 524 {"b", 2}, 525 {"c", 3}, 526 } 527 528 // Check if the MapOrd is sorted as expected 529 if !slices.Equal(mo, expected) { 530 t.Errorf("SortByKey failed: expected %v, got %v", expected, mo) 531 } 532 } 533 534 func TestSortIterByValue(t *testing.T) { 535 // Create a sample MapOrd to test sorting by values 536 mo := g.MapOrd[string, int]{ 537 {"a", 2}, 538 {"b", 1}, 539 {"c", 3}, 540 } 541 542 // Define a custom comparison function for integers 543 customIntCmp := func(a, b int) cmp.Ordering { 544 if a < b { 545 return cmp.Less 546 } else if a > b { 547 return cmp.Greater 548 } 549 return cmp.Equal 550 } 551 552 // Sort the MapOrd by values using the custom comparison function 553 mo = mo.Iter().SortByValue(customIntCmp).Collect() 554 555 // Expected sorted order by values 556 expected := g.MapOrd[string, int]{ 557 {"b", 1}, 558 {"a", 2}, 559 {"c", 3}, 560 } 561 562 // Check if the MapOrd is sorted as expected 563 if !slices.Equal(mo, expected) { 564 t.Errorf("SortByValue failed: expected %v, got %v", expected, mo) 565 } 566 } 567 568 func TestMapOrdFromMap(t *testing.T) { 569 // Test case 1: Map with elements 570 m := g.NewMap[string, int]() 571 m.Set("a", 1) 572 m.Set("b", 2) 573 m.Set("c", 3) 574 575 mapOrd := g.MapOrdFromMap(m). 576 SortBy(func(a, b g.Pair[string, int]) cmp.Ordering { return cmp.Cmp(a.Value, b.Value) }) 577 578 expected := []g.Pair[string, int]{ 579 {"a", 1}, 580 {"b", 2}, 581 {"c", 3}, 582 } 583 584 for i, p := range mapOrd { 585 if p != expected[i] { 586 t.Errorf("Expected mapOrd[%d] to be %v, got %v", i, expected[i], p) 587 } 588 } 589 590 // Test case 2: Empty Map 591 m2 := g.NewMap[string, int]() 592 mapOrd2 := g.MapOrdFromMap(m2) 593 if len(mapOrd2) != 0 { 594 t.Errorf("Expected mapOrd2 to be empty") 595 } 596 } 597 598 func TestMapOrdFromStd(t *testing.T) { 599 // Test case 1: Map with elements 600 inputMap := map[string]int{"a": 1, "b": 2, "c": 3} 601 orderedMap := g.MapOrdFromStd(inputMap) 602 if len(orderedMap) != len(inputMap) { 603 t.Errorf("Expected ordered map to have length %d, got %d", len(inputMap), orderedMap.Len()) 604 } 605 for key, value := range inputMap { 606 if orderedMap.Get(key).Some() != value { 607 t.Errorf("Expected ordered map to have key-value pair %s:%d", key, value) 608 } 609 } 610 611 // Test case 2: Empty Map 612 emptyMap := map[string]int{} 613 orderedEmptyMap := g.MapOrdFromStd(emptyMap) 614 if orderedEmptyMap.Len() != 0 { 615 t.Errorf("Expected ordered map of an empty map to be empty") 616 } 617 } 618 619 func TestMapOrdEq(t *testing.T) { 620 // Test case 1: Equal maps 621 m1 := g.NewMapOrd[string, int]() 622 m1.Set("a", 1) 623 m1.Set("b", 2) 624 m1.Set("c", 3) 625 626 m2 := g.NewMapOrd[string, int]() 627 m2.Set("a", 1) 628 m2.Set("b", 2) 629 m2.Set("c", 3) 630 631 if !m1.Eq(m2) { 632 t.Errorf("Expected maps to be equal") 633 } 634 635 // Test case 2: Unequal maps (different lengths) 636 m3 := g.NewMapOrd[string, int]() 637 m3.Set("a", 1) 638 m3.Set("b", 2) 639 640 if m1.Eq(m3) { 641 t.Errorf("Expected maps to be unequal") 642 } 643 644 // Test case 3: Unequal maps (different values) 645 m4 := g.NewMapOrd[string, int]() 646 m4.Set("a", 1) 647 m4.Set("b", 3) 648 m4.Set("c", 3) 649 650 if m1.Eq(m4) { 651 t.Errorf("Expected maps to be unequal") 652 } 653 } 654 655 func TestMapOrdIterInspect(t *testing.T) { 656 // Define an ordered map to iterate over 657 mo := g.NewMapOrd[int, string]() 658 mo.Set(1, "one") 659 mo.Set(2, "two") 660 mo.Set(3, "three") 661 662 // Define a slice to store the inspected key-value pairs 663 inspectedPairs := g.NewMapOrd[int, string]() 664 665 // Create a new iterator with Inspect and collect the key-value pairs 666 mo.Iter().Inspect(func(k int, v string) { 667 inspectedPairs.Set(k, v) 668 }).Collect() 669 670 if mo.Len() != inspectedPairs.Len() { 671 t.Errorf("Expected inspected pairs to have length %d, got %d", mo.Len(), inspectedPairs.Len()) 672 } 673 674 if mo.Ne(inspectedPairs) { 675 t.Errorf("Expected inspected pairs to be equal to the original map") 676 } 677 } 678 679 func TestMapOrdIterChain(t *testing.T) { 680 // Define the first ordered map to iterate over 681 iter1 := g.NewMapOrd[int, string]() 682 iter1.Set(1, "a") 683 684 // Define the second ordered map to iterate over 685 iter2 := g.NewMapOrd[int, string]() 686 iter2.Set(2, "b") 687 688 // Concatenate the iterators and collect the elements 689 chainedIter := iter1.Iter().Chain(iter2.Iter()) 690 collected := chainedIter.Collect() 691 692 // Verify the concatenated elements 693 expected := g.NewMapOrd[int, string]() 694 expected.Set(1, "a") 695 expected.Set(2, "b") 696 697 if !collected.Eq(expected) { 698 t.Errorf("Concatenated map does not match expected map") 699 } 700 } 701 702 func TestMapOrdIterCount(t *testing.T) { 703 // Create a new ordered map 704 seq := g.NewMapOrd[int, string]() 705 seq.Set(1, "a") 706 seq.Set(2, "b") 707 seq.Set(3, "c") 708 709 // Count the number of iterations 710 count := seq.Iter().Count() 711 712 // Verify the count 713 expectedCount := g.Int(3) // Since there are 3 elements in the ordered map 714 if count != expectedCount { 715 t.Errorf("Expected count to be %d, but got %d", expectedCount, count) 716 } 717 } 718 719 func TestMapOrdIterSkip(t *testing.T) { 720 // Create a new ordered map 721 seq := g.NewMapOrd[int, string]() 722 seq.Set(1, "a") 723 seq.Set(2, "b") 724 seq.Set(3, "c") 725 seq.Set(4, "d") 726 727 // Skip the first two elements 728 skipped := seq.Iter().Skip(2) 729 730 // Collect the elements after skipping 731 collected := skipped.Collect() 732 733 // Verify the collected elements 734 expected := g.NewMapOrd[int, string]() 735 expected.Set(3, "c") 736 expected.Set(4, "d") 737 738 if !collected.Eq(expected) { 739 t.Errorf("Expected %v, but got %v", expected, collected) 740 } 741 } 742 743 func TestMapOrdIterExclude(t *testing.T) { 744 // Create a new ordered map 745 mo := g.NewMapOrd[int, int]() 746 mo.Set(1, 1) 747 mo.Set(2, 2) 748 mo.Set(3, 3) 749 mo.Set(4, 4) 750 mo.Set(5, 5) 751 752 // Exclude even values 753 notEven := mo.Iter().Exclude(func(k, v int) bool { 754 return v%2 == 0 755 }) 756 757 // Collect the resulting elements 758 collected := notEven.Collect() 759 760 // Verify the collected elements 761 expected := g.NewMapOrd[int, int]() 762 expected.Set(1, 1) 763 expected.Set(3, 3) 764 expected.Set(5, 5) 765 766 if !collected.Eq(expected) { 767 t.Errorf("Expected %v, but got %v", expected, collected) 768 } 769 } 770 771 func TestMapOrdIterFilter(t *testing.T) { 772 // Create a new ordered map 773 mo := g.NewMapOrd[int, int]() 774 mo.Set(1, 1) 775 mo.Set(2, 2) 776 mo.Set(3, 3) 777 mo.Set(4, 4) 778 mo.Set(5, 5) 779 780 // Filter even values 781 even := mo.Iter().Filter(func(k, v int) bool { 782 return v%2 == 0 783 }) 784 785 // Collect the resulting elements 786 collected := even.Collect() 787 788 // Verify the collected elements 789 expected := g.NewMapOrd[int, int]() 790 expected.Set(2, 2) 791 expected.Set(4, 4) 792 793 if !collected.Eq(expected) { 794 t.Errorf("Expected %v, but got %v", expected, collected) 795 } 796 } 797 798 func TestMapOrdIterFind(t *testing.T) { 799 // Create a new ordered map 800 mo := g.NewMapOrd[int, int]() 801 mo.Set(1, 1) 802 mo.Set(2, 2) 803 mo.Set(3, 3) 804 mo.Set(4, 4) 805 mo.Set(5, 5) 806 807 // Find the first even value 808 found := mo.Iter().Find(func(k, v int) bool { 809 return v%2 == 0 810 }).Some() 811 812 // Verify the found element 813 expected := g.Pair[int, int]{2, 2} 814 815 if !reflect.DeepEqual(found, expected) { 816 t.Errorf("Expected %v, but got %v", expected, found) 817 } 818 } 819 820 func TestMapOrdIterMap(t *testing.T) { 821 // Create a new ordered map 822 mo := g.NewMapOrd[int, int]() 823 mo.Set(1, 1) 824 mo.Set(2, 2) 825 mo.Set(3, 3) 826 mo.Set(4, 4) 827 mo.Set(5, 5) 828 829 // Map each key-value pair to its square 830 squared := mo.Iter().Map(func(k, v int) (int, int) { 831 return k * k, v * v 832 }) 833 834 // Collect the resulting elements 835 collected := squared.Collect() 836 837 // Verify the collected elements 838 expected := g.NewMapOrd[int, int]() 839 expected.Set(1, 1) 840 expected.Set(4, 4) 841 expected.Set(9, 9) 842 expected.Set(16, 16) 843 expected.Set(25, 25) 844 845 if !collected.Eq(expected) { 846 t.Errorf("Expected %v, but got %v", expected, collected) 847 } 848 } 849 850 func TestMapOrdIterTake(t *testing.T) { 851 // Create a new ordered map 852 mo := g.NewMapOrd[int, int]() 853 mo.Set(1, 1) 854 mo.Set(2, 2) 855 mo.Set(3, 3) 856 mo.Set(4, 4) 857 mo.Set(5, 5) 858 859 // Take the first 3 elements 860 taken := mo.Iter().Take(3) 861 862 // Collect the resulting elements 863 collected := taken.Collect() 864 865 // Verify the collected elements 866 expected := g.NewMapOrd[int, int]() 867 expected.Set(1, 1) 868 expected.Set(2, 2) 869 expected.Set(3, 3) 870 871 if !collected.Eq(expected) { 872 t.Errorf("Expected %v, but got %v", expected, collected) 873 } 874 } 875 876 func TestMapOrdIterToChannel(t *testing.T) { 877 // Create a new ordered map 878 mo := g.NewMapOrd[int, int]() 879 mo.Set(1, 1) 880 mo.Set(2, 2) 881 mo.Set(3, 3) 882 mo.Set(4, 4) 883 mo.Set(5, 5) 884 885 // Convert the iterator to a channel 886 ctx, cancel := context.WithCancel(context.Background()) 887 defer cancel() // Ensure cancellation to avoid goroutine leaks. 888 889 ch := mo.Iter().ToChan(ctx) 890 891 // Collect elements from the channel 892 collected := g.NewMapOrd[int, int]() 893 for pair := range ch { 894 collected.Set(pair.Key, pair.Value) 895 } 896 897 // Verify the collected elements 898 expected := g.NewMapOrd[int, int]() 899 expected.Set(1, 1) 900 expected.Set(2, 2) 901 expected.Set(3, 3) 902 expected.Set(4, 4) 903 expected.Set(5, 5) 904 905 if !collected.Eq(expected) { 906 t.Errorf("Expected %v, but got %v", expected, collected) 907 } 908 } 909 910 func TestMapOrdIterUnzip(t *testing.T) { 911 // Create a new ordered map 912 mo := g.NewMapOrd[string, int]() 913 mo.Set("a", 1) 914 mo.Set("b", 2) 915 mo.Set("c", 3) 916 917 // Unzip the ordered map 918 keys, values := mo.Iter().Unzip() 919 920 // Verify the keys 921 expectedKeys := g.SliceOf("a", "b", "c") 922 if keys.Collect().Ne(expectedKeys) { 923 t.Errorf("Expected keys %v, but got %v", expectedKeys, keys) 924 } 925 926 // Verify the values 927 expectedValues := g.SliceOf(1, 2, 3) 928 if values.Collect().Ne(expectedValues) { 929 t.Errorf("Expected values %v, but got %v", expectedValues, values) 930 } 931 }