github.com/enetx/g@v1.0.80/tests/slice_iter_test.go (about) 1 package g_test 2 3 import ( 4 "context" 5 "reflect" 6 "strings" 7 "testing" 8 9 "github.com/enetx/g" 10 "github.com/enetx/g/cmp" 11 "github.com/enetx/g/f" 12 ) 13 14 func TestSliceIterFromChan(t *testing.T) { 15 // Create a channel and populate it with some test data 16 ch := make(chan int) 17 go func() { 18 defer close(ch) 19 for i := 1; i <= 5; i++ { 20 ch <- i 21 } 22 }() 23 24 // Convert the channel into an iterator 25 iter := g.FromChan(ch) 26 27 // Create a slice to collect elements from the iterator 28 var collected []int 29 30 // Define a function to be used as a callback for iterator 31 yield := func(v int) bool { 32 if v == 3 { 33 return false // Return false when element equals 3 to test premature exit 34 } 35 collected = append(collected, v) 36 return true 37 } 38 39 // Iterate through the elements using the iterator and collect them 40 iter(yield) 41 42 // Define the expected result 43 expected := []int{1, 2} 44 45 // Compare the collected elements with the expected result 46 if len(collected) != len(expected) { 47 t.Errorf("Length mismatch: expected %d elements, got %d", len(expected), len(collected)) 48 return 49 } 50 51 for i, v := range collected { 52 if v != expected[i] { 53 t.Errorf("Element mismatch at index %d: expected %d, got %d", i, expected[i], v) 54 } 55 } 56 } 57 58 func TestSliceIterPartition(t *testing.T) { 59 // Test case 1: Basic partitioning with integers 60 slice1 := g.Slice[int]{1, 2, 3, 4, 5} 61 isEven := func(val int) bool { 62 return val%2 == 0 63 } 64 65 evens, odds := slice1.Iter().Partition(isEven) 66 expectedEvens := g.Slice[int]{2, 4} 67 expectedOdds := g.Slice[int]{1, 3, 5} 68 69 if !reflect.DeepEqual(evens, expectedEvens) { 70 t.Errorf("Expected evens %v, but got %v", expectedEvens, evens) 71 } 72 73 if !reflect.DeepEqual(odds, expectedOdds) { 74 t.Errorf("Expected odds %v, but got %v", expectedOdds, odds) 75 } 76 77 // Test case 2: Partitioning with strings 78 slice2 := g.Slice[string]{"apple", "banana", "cherry", "date"} 79 hasA := func(val string) bool { 80 return strings.Contains(val, "a") 81 } 82 83 withA, withoutA := slice2.Iter().Partition(hasA) 84 expectedWithA := g.Slice[string]{"apple", "banana", "date"} 85 expectedWithoutA := g.Slice[string]{"cherry"} 86 87 if !reflect.DeepEqual(withA, expectedWithA) { 88 t.Errorf("Expected withA %v, but got %v", expectedWithA, withA) 89 } 90 91 if !reflect.DeepEqual(withoutA, expectedWithoutA) { 92 t.Errorf("Expected withoutA %v, but got %v", expectedWithoutA, withoutA) 93 } 94 95 // Test case 3: Partitioning an empty slice 96 emptySlice := g.Slice[int]{} 97 all, none := emptySlice.Iter().Partition(func(_ int) bool { return true }) 98 99 if len(all) != 0 { 100 t.Errorf("Expected empty slice for 'all', but got %v", all) 101 } 102 103 if len(none) != 0 { 104 t.Errorf("Expected empty slice for 'none', but got %v", none) 105 } 106 } 107 108 func TestSliceIterCombinations(t *testing.T) { 109 // Test case 1: Combinations of integers 110 slice1 := g.Slice[int]{0, 1, 2, 3} 111 combs1 := slice1.Iter().Combinations(3).Collect() 112 expectedCombs1 := []g.Slice[int]{ 113 {0, 1, 2}, 114 {0, 1, 3}, 115 {0, 2, 3}, 116 {1, 2, 3}, 117 } 118 119 if !reflect.DeepEqual(combs1, expectedCombs1) { 120 t.Errorf("Test case 1 failed: expected %v, but got %v", expectedCombs1, combs1) 121 } 122 123 // Test case 2: Combinations of strings 124 p1 := g.SliceOf[g.String]("a", "b") 125 p2 := g.SliceOf[g.String]("c", "d") 126 combs2 := p1.Iter().Chain(p2.Iter()).Map(g.String.Upper).Combinations(2).Collect() 127 expectedCombs2 := []g.Slice[g.String]{ 128 {"A", "B"}, 129 {"A", "C"}, 130 {"A", "D"}, 131 {"B", "C"}, 132 {"B", "D"}, 133 {"C", "D"}, 134 } 135 136 if !reflect.DeepEqual(combs2, expectedCombs2) { 137 t.Errorf("Test case 2 failed: expected %v, but got %v", expectedCombs2, combs2) 138 } 139 140 // Test case 3: Combinations of mixed types 141 p3 := g.SliceOf[any]("x", "y") 142 p4 := g.SliceOf[any](1, 2) 143 combs3 := p3.Iter().Chain(p4.Iter()).Combinations(2).Collect() 144 expectedCombs3 := []g.Slice[any]{ 145 {"x", "y"}, 146 {"x", 1}, 147 {"x", 2}, 148 {"y", 1}, 149 {"y", 2}, 150 {1, 2}, 151 } 152 153 if !reflect.DeepEqual(combs3, expectedCombs3) { 154 t.Errorf("Test case 3 failed: expected %v, but got %v", expectedCombs3, combs3) 155 } 156 157 // Test case 4: Empty slice 158 emptySlice := g.Slice[int]{} 159 combs4 := emptySlice.Iter().Combinations(2).Collect() 160 expectedCombs4 := []g.Slice[int]{} 161 162 if !reflect.DeepEqual(combs4, expectedCombs4) { 163 t.Errorf("Test case 4 failed: expected %v, but got %v", expectedCombs4, combs4) 164 } 165 166 // Test case 5: Combinations with k greater than slice length 167 slice5 := g.Slice[int]{1, 2, 3} 168 combs5 := slice5.Iter().Combinations(4).Collect() 169 expectedCombs5 := []g.Slice[int]{} 170 171 if !reflect.DeepEqual(combs5, expectedCombs5) { 172 t.Errorf("Test case 5 failed: expected %v, but got %v", expectedCombs5, combs5) 173 } 174 } 175 176 func TestSliceIterSortBy(t *testing.T) { 177 sl1 := g.NewSlice[int]().Append(3, 1, 4, 1, 5) 178 expected1 := g.NewSlice[int]().Append(1, 1, 3, 4, 5) 179 180 actual1 := sl1.Iter().SortBy(cmp.Cmp).Collect() 181 182 if !actual1.Eq(expected1) { 183 t.Errorf("SortBy failed: expected %v, but got %v", expected1, actual1) 184 } 185 186 sl2 := g.NewSlice[string]().Append("foo", "bar", "baz") 187 expected2 := g.NewSlice[string]().Append("foo", "baz", "bar") 188 189 actual2 := sl2.Iter().SortBy(func(a, b string) cmp.Ordering { return cmp.Cmp(b, a) }).Collect() 190 191 if !actual2.Eq(expected2) { 192 t.Errorf("SortBy failed: expected %v, but got %v", expected2, actual2) 193 } 194 195 sl3 := g.NewSlice[int]() 196 expected3 := g.NewSlice[int]() 197 198 actual3 := sl3.Iter().SortBy(cmp.Cmp).Collect() 199 200 if !actual3.Eq(expected3) { 201 t.Errorf("SortBy failed: expected %v, but got %v", expected3, actual3) 202 } 203 } 204 205 func TestSliceIterDedup(t *testing.T) { 206 // Test case 1: Dedup with consecutive duplicate elements for int 207 sliceInt := g.Slice[int]{1, 2, 2, 3, 4, 4, 4, 5} 208 expectedResultInt := g.Slice[int]{1, 2, 3, 4, 5} 209 210 iterInt := sliceInt.Iter().Dedup() 211 resultInt := iterInt.Collect() 212 213 if !reflect.DeepEqual(resultInt, expectedResultInt) { 214 t.Errorf("Dedup failed for int. Expected %v, got %v", expectedResultInt, resultInt) 215 } 216 217 // Test case 2: Dedup with consecutive duplicate elements for string 218 sliceString := g.Slice[string]{"apple", "orange", "orange", "banana", "banana", "grape"} 219 expectedResultString := g.Slice[string]{"apple", "orange", "banana", "grape"} 220 221 iterString := sliceString.Iter().Dedup() 222 resultString := iterString.Collect() 223 224 if !reflect.DeepEqual(resultString, expectedResultString) { 225 t.Errorf("Dedup failed for string. Expected %v, got %v", expectedResultString, resultString) 226 } 227 228 // Test case 3: Dedup with consecutive duplicate elements for float64 229 sliceFloat64 := g.Slice[float64]{1.2, 2.3, 2.3, 3.4, 4.5, 4.5, 4.5, 5.6} 230 expectedResultFloat64 := g.Slice[float64]{1.2, 2.3, 3.4, 4.5, 5.6} 231 232 iterFloat64 := sliceFloat64.Iter().Dedup() 233 resultFloat64 := iterFloat64.Collect() 234 235 if !reflect.DeepEqual(resultFloat64, expectedResultFloat64) { 236 t.Errorf("Dedup failed for float64. Expected %v, got %v", expectedResultFloat64, resultFloat64) 237 } 238 239 // Test case 4: Dedup with consecutive duplicate elements for custom non-comparable struct 240 type myStruct struct { 241 val []int 242 } 243 244 sliceStruct := g.Slice[myStruct]{ 245 {val: []int{1}}, 246 {val: []int{2}}, 247 {val: []int{2}}, 248 {val: []int{3}}, 249 {val: []int{3}}, 250 {val: []int{4}}, 251 } 252 253 expectedResultStruct := g.Slice[myStruct]{{val: []int{1}}, {val: []int{2}}, {val: []int{3}}, {val: []int{4}}} 254 255 iterStruct := sliceStruct.Iter().Dedup() 256 resultStruct := iterStruct.Collect() 257 258 if !reflect.DeepEqual(resultStruct, expectedResultStruct) { 259 t.Errorf("Dedup failed for custom struct. Expected %v, got %v", expectedResultStruct, resultStruct) 260 } 261 } 262 263 func TestSliceIterStepBy(t *testing.T) { 264 // Test case 1: StepBy with a step size of 3 265 slice := g.Slice[int]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} 266 expectedResult := g.Slice[int]{1, 4, 7, 10} 267 268 iter := slice.Iter().StepBy(3) 269 result := iter.Collect() 270 271 if !reflect.DeepEqual(result, expectedResult) { 272 t.Errorf("StepBy failed. Expected %v, got %v", expectedResult, result) 273 } 274 275 // Test case 2: StepBy with a step size of 2 276 slice = g.Slice[int]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} 277 expectedResult = g.Slice[int]{1, 3, 5, 7, 9} 278 279 iter = slice.Iter().StepBy(2) 280 result = iter.Collect() 281 282 if !reflect.DeepEqual(result, expectedResult) { 283 t.Errorf("StepBy failed. Expected %v, got %v", expectedResult, result) 284 } 285 286 // Test case 3: StepBy with a step size larger than the slice length 287 slice = g.Slice[int]{1, 2, 3, 4, 5} 288 expectedResult = g.Slice[int]{1} 289 290 iter = slice.Iter().StepBy(10) 291 result = iter.Collect() 292 293 if !reflect.DeepEqual(result, expectedResult) { 294 t.Errorf("StepBy failed. Expected %v, got %v", expectedResult, result) 295 } 296 297 // Test case 4: StepBy with a step size of 1 298 slice = g.Slice[int]{1, 2, 3, 4, 5} 299 expectedResult = g.Slice[int]{1, 2, 3, 4, 5} 300 301 iter = slice.Iter().StepBy(1) 302 result = iter.Collect() 303 304 if !reflect.DeepEqual(result, expectedResult) { 305 t.Errorf("StepBy failed. Expected %v, got %v", expectedResult, result) 306 } 307 } 308 309 func TestSliceIterPermutations(t *testing.T) { 310 // Test case 1: Single element slice 311 slice1 := g.SliceOf(1) 312 perms1 := slice1.Iter().Permutations().Collect() 313 expectedPerms1 := []g.Slice[int]{slice1} 314 315 if !reflect.DeepEqual(perms1, expectedPerms1) { 316 t.Errorf("expected %v, but got %v", expectedPerms1, perms1) 317 } 318 319 // Test case 2: Two-element string slice 320 slice2 := g.SliceOf("a", "b") 321 perms2 := slice2.Iter().Permutations().Collect() 322 expectedPerms2 := []g.Slice[string]{ 323 {"a", "b"}, 324 {"b", "a"}, 325 } 326 327 if !reflect.DeepEqual(perms2, expectedPerms2) { 328 t.Errorf("expected %v, but got %v", expectedPerms2, perms2) 329 } 330 331 // Test case 3: Three-element float64 slice 332 slice3 := g.SliceOf(1.0, 2.0, 3.0) 333 perms3 := slice3.Iter().Permutations().Collect() 334 expectedPerms3 := []g.Slice[float64]{ 335 {1.0, 2.0, 3.0}, 336 {1.0, 3.0, 2.0}, 337 {2.0, 1.0, 3.0}, 338 {2.0, 3.0, 1.0}, 339 {3.0, 1.0, 2.0}, 340 {3.0, 2.0, 1.0}, 341 } 342 343 if !reflect.DeepEqual(perms3, expectedPerms3) { 344 t.Errorf("expected %v, but got %v", expectedPerms3, perms3) 345 } 346 347 // Additional Test case 4: Empty slice 348 slice4 := g.Slice[any]{} 349 perms4 := slice4.Iter().Permutations().Collect() 350 expectedPerms4 := []g.Slice[any]{slice4} 351 352 if !reflect.DeepEqual(perms4, expectedPerms4) { 353 t.Errorf("expected %v, but got %v", expectedPerms4, perms4) 354 } 355 356 // Additional Test case 5: Four-element mixed-type slice 357 slice5 := g.SliceOf[any]("a", 1, 2.5, true) 358 perms5 := slice5.Iter().Permutations().Collect() 359 expectedPerms5 := []g.Slice[any]{ 360 {"a", 1, 2.5, true}, 361 {"a", 1, true, 2.5}, 362 {"a", 2.5, 1, true}, 363 {"a", 2.5, true, 1}, 364 {"a", true, 1, 2.5}, 365 {"a", true, 2.5, 1}, 366 {1, "a", 2.5, true}, 367 {1, "a", true, 2.5}, 368 {1, 2.5, "a", true}, 369 {1, 2.5, true, "a"}, 370 {1, true, "a", 2.5}, 371 {1, true, 2.5, "a"}, 372 {2.5, "a", 1, true}, 373 {2.5, "a", true, 1}, 374 {2.5, 1, "a", true}, 375 {2.5, 1, true, "a"}, 376 {2.5, true, "a", 1}, 377 {2.5, true, 1, "a"}, 378 {true, "a", 1, 2.5}, 379 {true, "a", 2.5, 1}, 380 {true, 1, "a", 2.5}, 381 {true, 1, 2.5, "a"}, 382 {true, 2.5, "a", 1}, 383 {true, 2.5, 1, "a"}, 384 } 385 386 if !reflect.DeepEqual(perms5, expectedPerms5) { 387 t.Errorf("expected %v, but got %v", expectedPerms5, perms5) 388 } 389 } 390 391 func TestSliceIterChunks(t *testing.T) { 392 tests := []struct { 393 name string 394 input g.Slice[int] 395 expected []g.Slice[int] 396 size g.Int 397 }{ 398 { 399 name: "empty slice", 400 input: g.NewSlice[int](), 401 expected: []g.Slice[int]{g.NewSlice[int]()}, 402 size: 2, 403 }, 404 { 405 name: "single chunk", 406 input: g.NewSlice[int]().Append(1, 2, 3), 407 expected: []g.Slice[int]{g.NewSlice[int]().Append(1, 2, 3)}, 408 size: 3, 409 }, 410 { 411 name: "multiple chunks", 412 input: g.NewSlice[int]().Append(1, 2, 3, 4, 5, 6), 413 expected: []g.Slice[int]{ 414 g.NewSlice[int]().Append(1, 2), 415 g.NewSlice[int]().Append(3, 4), 416 g.NewSlice[int]().Append(5, 6), 417 }, 418 size: 2, 419 }, 420 { 421 name: "last chunk is smaller", 422 input: g.NewSlice[int]().Append(1, 2, 3, 4, 5), 423 expected: []g.Slice[int]{ 424 g.NewSlice[int]().Append(1, 2), 425 g.NewSlice[int]().Append(3, 4), 426 g.NewSlice[int]().Append(5), 427 }, 428 size: 2, 429 }, 430 { 431 name: "chunk size bigger than slice length", 432 input: g.NewSlice[int]().Append(1, 2, 3, 4), 433 expected: []g.Slice[int]{g.NewSlice[int]().Append(1, 2, 3, 4)}, 434 size: 5, 435 }, 436 } 437 438 for _, tt := range tests { 439 t.Run(tt.name, func(t *testing.T) { 440 result := tt.input.Iter().Chunks(tt.size).Collect() 441 442 if !reflect.DeepEqual(result, tt.expected) { 443 t.Errorf("Expected %d, but got %d", tt.expected, result) 444 return 445 } 446 447 for i, chunk := range result { 448 if !chunk.Eq(tt.expected[i]) { 449 t.Errorf("Chunk %d does not match the expected result", i) 450 } 451 } 452 }) 453 } 454 } 455 456 func TestSliceIterAll(t *testing.T) { 457 sl1 := g.NewSlice[int]() 458 sl2 := g.NewSlice[int]().Append(1, 2, 3) 459 sl3 := g.NewSlice[int]().Append(2, 4, 6) 460 461 testCases := []struct { 462 f func(int) bool 463 name string 464 sl g.Slice[int] 465 want bool 466 }{ 467 { 468 name: "empty slice", 469 f: func(x int) bool { return x%2 == 0 }, 470 sl: sl1, 471 want: true, 472 }, 473 { 474 name: "all elements satisfy the condition", 475 f: func(x int) bool { return x%2 != 0 }, 476 sl: sl2, 477 want: false, 478 }, 479 { 480 name: "not all elements satisfy the condition", 481 f: func(x int) bool { return x%2 == 0 }, 482 sl: sl3, 483 want: true, 484 }, 485 } 486 487 for _, tc := range testCases { 488 t.Run(tc.name, func(t *testing.T) { 489 got := tc.sl.Iter().All(tc.f) 490 if got != tc.want { 491 t.Errorf("got %v, want %v", got, tc.want) 492 } 493 }) 494 } 495 } 496 497 func TestSliceIterAny(t *testing.T) { 498 sl1 := g.NewSlice[int]() 499 f1 := func(x int) bool { return x > 0 } 500 501 if sl1.Iter().Any(f1) { 502 t.Errorf("Expected false for empty slice, got true") 503 } 504 505 sl2 := g.NewSlice[int]().Append(1, 2, 3) 506 f2 := func(x int) bool { return x < 1 } 507 508 if sl2.Iter().Any(f2) { 509 t.Errorf("Expected false for slice with no matching elements, got true") 510 } 511 512 sl3 := g.NewSlice[string]().Append("foo", "bar") 513 f3 := func(x string) bool { return x == "bar" } 514 515 if !sl3.Iter().Any(f3) { 516 t.Errorf("Expected true for slice with one matching element, got false") 517 } 518 519 sl4 := g.NewSlice[int]().Append(1, 2, 3, 4, 5) 520 f4 := func(x int) bool { return x%2 == 0 } 521 522 if !sl4.Iter().Any(f4) { 523 t.Errorf("Expected true for slice with multiple matching elements, got false") 524 } 525 } 526 527 func TestSliceIterFold(t *testing.T) { 528 sl := g.Slice[int]{1, 2, 3, 4, 5} 529 sum := sl.Iter().Fold(0, func(index, value int) int { return index + value }) 530 531 if sum != 15 { 532 t.Errorf("Expected %d, got %d", 15, sum) 533 } 534 } 535 536 func TestSliceIterFilter(t *testing.T) { 537 var sl g.Slice[int] 538 539 sl = sl.Append(1, 2, 3, 4, 5) 540 result := sl.Iter().Filter(func(v int) bool { return v%2 == 0 }).Collect() 541 542 if result.Len() != 2 { 543 t.Errorf("Expected 2, got %d", result.Len()) 544 } 545 546 if result[0] != 2 { 547 t.Errorf("Expected 2, got %d", result[0]) 548 } 549 550 if result[1] != 4 { 551 t.Errorf("Expected 4, got %d", result[1]) 552 } 553 } 554 555 func TestSliceIterMap(t *testing.T) { 556 sl := g.Slice[int]{1, 2, 3, 4, 5} 557 result := sl.Iter().Map(func(i int) int { return i * 2 }).Collect() 558 559 if result.Len() != sl.Len() { 560 t.Errorf("Expected %d, got %d", sl.Len(), result.Len()) 561 } 562 563 for i := range result.Len() { 564 if result[i] != sl[i]*2 { 565 t.Errorf("Expected %d, got %d", sl[i]*2, result[i]) 566 } 567 } 568 } 569 570 func TestSliceIterExcludeZeroValues(t *testing.T) { 571 sl := g.Slice[int]{1, 2, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 8, 0, 9, 0, 10} 572 sl = sl.Iter().Exclude(f.Zero).Collect() 573 574 if sl.Len() != 10 { 575 t.Errorf("Expected 10, got %d", sl.Len()) 576 } 577 578 for i := range sl.Len() { 579 if sl[i] == 0 { 580 t.Errorf("Expected non-zero value, got %d", sl[i]) 581 } 582 } 583 } 584 585 func TestSliceIterForEach(t *testing.T) { 586 sl1 := g.NewSlice[int]().Append(1, 2, 3, 4, 5) 587 sl2 := g.NewSlice[string]().Append("foo", "bar", "baz") 588 sl3 := g.NewSlice[float64]().Append(1.1, 2.2, 3.3, 4.4) 589 590 var result1 []int 591 592 sl1.Iter().ForEach(func(i int) { result1 = append(result1, i) }) 593 594 if !reflect.DeepEqual(result1, []int{1, 2, 3, 4, 5}) { 595 t.Errorf( 596 "ForEach failed for %v, expected %v, but got %v", 597 sl1, 598 []int{1, 2, 3, 4, 5}, 599 result1, 600 ) 601 } 602 603 var result2 []string 604 605 sl2.Iter().ForEach(func(s string) { result2 = append(result2, s) }) 606 607 if !reflect.DeepEqual(result2, []string{"foo", "bar", "baz"}) { 608 t.Errorf( 609 "ForEach failed for %v, expected %v, but got %v", 610 sl2, 611 []string{"foo", "bar", "baz"}, 612 result2, 613 ) 614 } 615 616 var result3 []float64 617 618 sl3.Iter().ForEach(func(f float64) { result3 = append(result3, f) }) 619 620 if !reflect.DeepEqual(result3, []float64{1.1, 2.2, 3.3, 4.4}) { 621 t.Errorf( 622 "ForEach failed for %v, expected %v, but got %v", 623 sl3, 624 []float64{1.1, 2.2, 3.3, 4.4}, 625 result3, 626 ) 627 } 628 } 629 630 func TestSliceIterZip(t *testing.T) { 631 s1 := g.SliceOf(1, 2, 3, 4) 632 s2 := g.SliceOf(5, 6, 7, 8) 633 expected := g.MapOrd[int, int]{{1, 5}, {2, 6}, {3, 7}, {4, 8}} 634 result := s1.Iter().Zip(s2.Iter()).Collect() 635 636 if !reflect.DeepEqual(result, expected) { 637 t.Errorf("Zip(%v, %v) = %v, expected %v", s1, s2, result, expected) 638 } 639 640 s3 := g.SliceOf(1, 2, 3) 641 s4 := g.SliceOf(4, 5) 642 expected = g.MapOrd[int, int]{{1, 4}, {2, 5}} 643 result = s3.Iter().Zip(s4.Iter()).Collect() 644 645 if !reflect.DeepEqual(result, expected) { 646 t.Errorf("Zip(%v, %v) = %v, expected %v", s3, s4, result, expected) 647 } 648 } 649 650 func TestSliceIterFlatten(t *testing.T) { 651 tests := []struct { 652 name string 653 input g.Slice[any] 654 expected g.Slice[any] 655 }{ 656 { 657 name: "Empty slice", 658 input: g.Slice[any]{}, 659 expected: g.Slice[any]{}, 660 }, 661 { 662 name: "Flat slice", 663 input: g.Slice[any]{1, "abc", 3.14}, 664 expected: g.Slice[any]{1, "abc", 3.14}, 665 }, 666 { 667 name: "Nested slice", 668 input: g.Slice[any]{ 669 1, 670 g.SliceOf(2, 3), 671 "abc", 672 g.SliceOf("def", "ghi"), 673 g.SliceOf(4.5, 6.7), 674 }, 675 expected: g.Slice[any]{1, 2, 3, "abc", "def", "ghi", 4.5, 6.7}, 676 }, 677 } 678 679 for _, tt := range tests { 680 t.Run(tt.name, func(t *testing.T) { 681 result := tt.input.Iter().Flatten().Collect() 682 if !reflect.DeepEqual(result, tt.expected) { 683 t.Errorf("Flatten() = %v, want %v", result, tt.expected) 684 } 685 }) 686 } 687 } 688 689 func TestSliceIterRange(t *testing.T) { 690 // Test scenario: Function stops at a specific value 691 t.Run("FunctionStopsAtThree", func(t *testing.T) { 692 slice := g.Slice[int]{1, 2, 3, 4, 5} 693 expected := []int{1, 2, 3} 694 695 var result []int 696 stopAtThree := func(val int) bool { 697 result = append(result, val) 698 return val != 3 699 } 700 701 slice.Iter().Range(stopAtThree) 702 703 if !reflect.DeepEqual(result, expected) { 704 t.Errorf("Expected: %v, Got: %v", expected, result) 705 } 706 }) 707 708 // Test scenario: Function always returns true 709 t.Run("FunctionAlwaysTrue", func(t *testing.T) { 710 slice := g.Slice[int]{1, 2, 3, 4, 5} 711 expected := []int{1, 2, 3, 4, 5} 712 713 var result []int 714 alwaysTrue := func(val int) bool { 715 result = append(result, val) 716 return true 717 } 718 719 slice.Iter().Range(alwaysTrue) 720 721 if !reflect.DeepEqual(result, expected) { 722 t.Errorf("Expected: %v, Got: %v", expected, result) 723 } 724 }) 725 726 // Test scenario: Empty slice 727 t.Run("EmptySlice", func(t *testing.T) { 728 emptySlice := g.Slice[int]{} 729 expected := []int{} 730 731 result := []int{} 732 anyFunc := func(val int) bool { 733 result = append(result, val) 734 return true 735 } 736 737 emptySlice.Iter().Range(anyFunc) 738 739 if !reflect.DeepEqual(result, expected) { 740 t.Errorf("Expected: %v, Got: %v", expected, result) 741 } 742 }) 743 } 744 745 func TestSliceIterCount(t *testing.T) { 746 // Test case 1: Count elements from the sequence 747 seq := g.Slice[int]{1, 2, 3, 4, 5} 748 count := seq.Iter().Count() 749 if count != 5 { 750 t.Errorf("Expected count to be %d, got %d", 5, count) 751 } 752 753 // Test case 2: Empty sequence 754 emptySeq := g.Slice[int]{} 755 emptyCount := emptySeq.Iter().Count() 756 if emptyCount != 0 { 757 t.Errorf("Expected count of an empty sequence to be %d, got %d", 0, emptyCount) 758 } 759 } 760 761 func TestSliceIterCycle(t *testing.T) { 762 // Test case 1: Cyclic behavior 763 seq := g.Slice[int]{1, 2, 3} 764 cycle := seq.Iter().Cycle().Take(9).Collect() 765 766 expected := []int{1, 2, 3, 1, 2, 3, 1, 2, 3} 767 for i := 0; i < len(expected); i++ { 768 if cycle[i] != expected[i] { 769 t.Errorf("Expected element at index %d to be %d, got %d", i, expected[i], cycle[i]) 770 } 771 } 772 } 773 774 func TestSliceIterEnumerate(t *testing.T) { 775 // Test case 1: Enumerate elements 776 seq := g.Slice[string]{"bbb", "ddd", "xxx", "aaa", "ccc"} 777 enumerated := seq.Iter().Enumerate().Collect() 778 779 expected := g.NewMapOrd[g.Int, string]() 780 expected.Set(0, "bbb").Set(1, "ddd").Set(2, "xxx").Set(3, "aaa").Set(4, "ccc") 781 782 for i, v := range enumerated { 783 if expected[i] != v { 784 t.Errorf("Expected element at index %d to be %v, got %v", i, expected[i], v) 785 } 786 } 787 } 788 789 func TestSliceIterSkip(t *testing.T) { 790 // Test case 1: Skip elements 791 seq := g.Slice[int]{1, 2, 3, 4, 5, 6} 792 skipped := seq.Iter().Skip(3).Collect() 793 expected := g.Slice[int]{4, 5, 6} 794 if len(skipped) != len(expected) { 795 t.Errorf("Expected skipped slice to have length %d, got %d", len(expected), len(skipped)) 796 } 797 for i := range expected { 798 if skipped[i] != expected[i] { 799 t.Errorf("Expected element at index %d to be %d, got %d", i, expected[i], skipped[i]) 800 } 801 } 802 803 // Test case 2: Skip all elements 804 seq2 := g.Slice[string]{"a", "b", "c"} 805 skipped2 := seq2.Iter().Skip(3).Collect() 806 if len(skipped2) != 0 { 807 t.Errorf("Expected skipped slice of all elements to be empty, got length %d", len(skipped2)) 808 } 809 } 810 811 func TestSliceIterUnique(t *testing.T) { 812 // Test case 1: Unique elements 813 seq := g.Slice[int]{1, 2, 3, 2, 4, 5, 3} 814 unique := seq.Iter().Unique().Collect() 815 816 expected := g.Slice[int]{1, 2, 3, 4, 5} 817 if len(unique) != len(expected) { 818 t.Errorf("Expected unique iterator length to be %d, got %d", len(expected), len(unique)) 819 } 820 for i, v := range unique { 821 if v != expected[i] { 822 t.Errorf("Expected element at index %d to be %d, got %d", i, expected[i], v) 823 } 824 } 825 } 826 827 func TestSliceIterFind(t *testing.T) { 828 // Test case 1: Element found 829 seq := g.Slice[int]{1, 2, 3, 4, 5} 830 found := seq.Iter().Find(func(i int) bool { 831 return i == 2 832 }) 833 if !found.IsSome() { 834 t.Error("Expected found option to be Some") 835 } 836 if found.Some() != 2 { 837 t.Errorf("Expected found element to be 2, got %d", found.Some()) 838 } 839 840 // Test case 2: Element not found 841 notFound := seq.Iter().Find(func(i int) bool { 842 return i == 6 843 }) 844 if notFound.IsSome() { 845 t.Error("Expected not found option to be None") 846 } 847 } 848 849 func TestSliceIterWindows(t *testing.T) { 850 // Test case 1: Windows of correct size 851 seq := g.Slice[int]{1, 2, 3, 4, 5, 6} 852 windows := seq.Iter().Windows(3).Collect() 853 854 expected := []g.Slice[int]{ 855 {1, 2, 3}, 856 {2, 3, 4}, 857 {3, 4, 5}, 858 {4, 5, 6}, 859 } 860 861 if len(windows) != len(expected) { 862 t.Errorf("Expected %d windows, got %d", len(expected), len(windows)) 863 } 864 865 for i, window := range windows { 866 if len(window) != len(expected[i]) { 867 t.Errorf("Expected window %d length to be %d, got %d", i, len(expected[i]), len(window)) 868 } 869 for j, v := range window { 870 if v != expected[i][j] { 871 t.Errorf("Expected window %d element at index %d to be %d, got %d", i, j, expected[i][j], v) 872 } 873 } 874 } 875 } 876 877 func TestSliceIterToChannel(t *testing.T) { 878 // Test case 1: Channel streaming without cancellation 879 seq := g.Slice[int]{1, 2, 3} 880 881 ch := seq.Iter().ToChan() 882 var result []int 883 for val := range ch { 884 result = append(result, val) 885 } 886 887 expected := []int{1, 2, 3} 888 if len(result) != len(expected) { 889 t.Errorf("Expected %d elements, got %d", len(expected), len(result)) 890 } 891 for i, v := range result { 892 if v != expected[i] { 893 t.Errorf("Expected element at index %d to be %d, got %d", i, expected[i], v) 894 } 895 } 896 897 // Test case 2: Channel streaming with cancellation 898 ctx, cancel := context.WithCancel(context.Background()) 899 cancel() // Ensure cancellation to avoid goroutine leaks. 900 901 ch = seq.Iter().ToChan(ctx) 902 var result2 []int 903 for val := range ch { 904 result2 = append(result2, val) 905 } 906 907 if len(result2) != 0 { 908 t.Error("Expected no elements due to cancellation, got some elements") 909 } 910 } 911 912 func TestSliceIterInspect(t *testing.T) { 913 // Define a slice to iterate over 914 s := g.Slice[int]{1, 2, 3} 915 916 // Define a slice to store the inspected elements 917 var inspectedElements g.Slice[int] 918 919 // Create a new iterator with Inspect and collect the elements 920 s.Iter().Inspect(func(v int) { 921 inspectedElements = append(inspectedElements, v) 922 }).Collect() 923 924 if inspectedElements.Len() != s.Len() { 925 t.Errorf("Expected %d inspected elements, got %d", s.Len(), inspectedElements.Len()) 926 } 927 928 if inspectedElements.Ne(s) { 929 t.Errorf("Expected %v, got %v", s, inspectedElements) 930 } 931 } 932 933 func TestSliceIterCounter(t *testing.T) { 934 sl1 := g.Slice[int]{1, 2, 3, 2, 1, 4, 5, 4, 4} 935 sl2 := g.Slice[string]{"apple", "banana", "orange", "apple", "apple", "orange", "grape"} 936 937 expected1 := g.NewMapOrd[int, g.Int]() 938 expected1. 939 Set(3, 1). 940 Set(5, 1). 941 Set(1, 2). 942 Set(2, 2). 943 Set(4, 3) 944 945 result1 := sl1.Iter().Counter().Collect() 946 if !result1.Eq(expected1) { 947 t.Errorf("Counter() returned %v, expected %v", result1, expected1) 948 } 949 950 // Test with string values 951 expected2 := g.NewMapOrd[string, g.Int]() 952 expected2. 953 Set("banana", 1). 954 Set("grape", 1). 955 Set("orange", 2). 956 Set("apple", 3) 957 958 result2 := sl2.Iter().Counter().Collect() 959 if !result2.Eq(expected2) { 960 t.Errorf("Counter() returned %v, expected %v", result2, expected2) 961 } 962 } 963 964 func TestSliceIntersperse(t *testing.T) { 965 // Test case 1: Intersperse strings with a comma 966 testSlice := g.Slice[string]{"apple", "banana", "orange"} 967 expected := g.Slice[string]{"apple", ", ", "banana", ", ", "orange"} 968 interspersed := testSlice.Iter().Intersperse(", ").Collect() 969 970 if interspersed.Ne(expected) { 971 t.Errorf("Test case 1 failed. Expected: %v, Got: %v", expected, interspersed) 972 } 973 974 // Test case 2: Intersperse strings with a dash 975 testSlice = g.Slice[string]{"apple", "banana", "orange"} 976 expected = g.Slice[string]{"apple", "-", "banana", "-", "orange"} 977 interspersed = testSlice.Iter().Intersperse("-").Collect() 978 979 if interspersed.Ne(expected) { 980 t.Errorf("Test case 2 failed. Expected: %v, Got: %v", expected, interspersed) 981 } 982 983 // Test case 3: Intersperse empty slice 984 emptySlice := g.Slice[string]{} // Create an empty slice of strings 985 expectedEmpty := g.Slice[string]{} // Expected empty slice 986 interspersedEmpty := emptySlice.Iter().Intersperse(", ").Collect() 987 988 if interspersedEmpty.Ne(expectedEmpty) { 989 t.Errorf("Test case 3 failed. Expected: %v, Got: %v", expectedEmpty, interspersedEmpty) 990 } 991 }