github.com/enetx/g@v1.0.80/tests/slice_test.go (about) 1 package g_test 2 3 import ( 4 "fmt" 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 TestSliceUnpack(t *testing.T) { 15 tests := []struct { 16 name string 17 slice g.Slice[int] 18 vars []*int 19 expected []int 20 }{ 21 { 22 name: "Unpack with valid indices", 23 slice: g.Slice[int]{1, 2, 3, 4, 5}, 24 vars: []*int{new(int), new(int), new(int)}, 25 expected: []int{1, 2, 3}, 26 }, 27 { 28 name: "Unpack with invalid indices", 29 slice: g.Slice[int]{1, 2, 3}, 30 vars: []*int{new(int), new(int), new(int), new(int)}, 31 expected: []int{1, 2, 3, 0}, // Expecting zero value for the fourth variable 32 }, 33 { 34 name: "Unpack with empty slice", 35 slice: g.Slice[int]{}, 36 vars: []*int{new(int)}, 37 expected: []int{0}, // Expecting zero value for the only variable 38 }, 39 { 40 name: "Unpack with nil slice", 41 slice: nil, 42 vars: []*int{new(int)}, 43 expected: []int{0}, // Expecting zero value for the only variable 44 }, 45 } 46 47 for _, test := range tests { 48 t.Run(test.name, func(t *testing.T) { 49 test.slice.Unpack(test.vars...) 50 for i, v := range test.vars { 51 if *v != test.expected[i] { 52 t.Errorf("Expected %d but got %d", test.expected[i], *v) 53 } 54 } 55 }) 56 } 57 } 58 59 func TestSubSliceWithStep(t *testing.T) { 60 slice := g.SliceOf(1, 2, 3, 4, 5, 6, 7, 8, 9) 61 62 testCases := []struct { 63 start g.Int 64 end g.Int 65 step g.Int 66 expected g.Slice[int] 67 }{ 68 {1, 7, 2, g.SliceOf(2, 4, 6)}, 69 {0, 9, 3, g.SliceOf(1, 4, 7)}, 70 {2, 6, 1, g.SliceOf(3, 4, 5, 6)}, 71 {0, 9, 2, g.SliceOf(1, 3, 5, 7, 9)}, 72 {0, 9, 4, g.SliceOf(1, 5, 9)}, 73 {6, 1, -2, g.SliceOf(7, 5, 3)}, 74 {8, 1, -3, g.SliceOf(9, 6, 3)}, 75 {8, 0, -2, g.SliceOf(9, 7, 5, 3)}, 76 {8, 0, -1, g.SliceOf(9, 8, 7, 6, 5, 4, 3, 2)}, 77 {8, 0, -4, g.SliceOf(9, 5)}, 78 {-1, -6, -2, g.SliceOf(9, 7, 5)}, 79 {-2, -9, -3, g.SliceOf(8, 5, 2)}, 80 {-1, -8, -2, g.SliceOf(9, 7, 5, 3)}, 81 {-3, -10, -2, g.SliceOf(7, 5, 3, 1)}, 82 {-1, -10, -1, g.SliceOf(9, 8, 7, 6, 5, 4, 3, 2, 1)}, 83 {-5, -1, -1, g.Slice[int]{}}, 84 {-1, -5, -1, g.SliceOf(9, 8, 7, 6)}, 85 } 86 87 for _, tc := range testCases { 88 t.Run(fmt.Sprintf("start:%d_end:%d_step:%d", tc.start, tc.end, tc.step), func(t *testing.T) { 89 result := slice.SubSlice(tc.start, tc.end, tc.step) 90 91 if !result.Eq(tc.expected) { 92 t.Errorf("Expected %v, but got %v", tc.expected, result) 93 } 94 }) 95 } 96 } 97 98 // func TestSortInts(t *testing.T) { 99 // slice := g.Slice[int]{5, 2, 8, 1, 6} 100 // slice.Sort() 101 102 // expected := g.Slice[int]{1, 2, 5, 6, 8} 103 104 // if !reflect.DeepEqual(slice, expected) { 105 // t.Errorf("Expected %v but got %v", expected, slice) 106 // } 107 // } 108 109 // func TestSortStrings(t *testing.T) { 110 // slice := g.Slice[string]{"apple", "orange", "banana", "grape"} 111 // slice.Sort() 112 113 // expected := g.Slice[string]{"apple", "banana", "grape", "orange"} 114 115 // if !reflect.DeepEqual(slice, expected) { 116 // t.Errorf("Expected %v but got %v", expected, slice) 117 // } 118 // } 119 120 // func TestSliceSortFloats(t *testing.T) { 121 // slice := g.Slice[float64]{5.6, 2.3, 8.9, 1.2, 6.7} 122 // slice.Sort() 123 124 // expected := g.Slice[float64]{1.2, 2.3, 5.6, 6.7, 8.9} 125 126 // if !reflect.DeepEqual(slice, expected) { 127 // t.Errorf("Expected %v but got %v", expected, slice) 128 // } 129 // } 130 131 func TestSliceInsert(t *testing.T) { 132 // Test insertion in the middle 133 slice := g.Slice[string]{"a", "b", "c", "d"} 134 newSlice := slice.Insert(2, "e", "f") 135 expected := g.Slice[string]{"a", "b", "e", "f", "c", "d"} 136 if !newSlice.Eq(expected) { 137 t.Errorf("Insert(2) failed. Expected %v, but got %v", expected, newSlice) 138 } 139 140 // Test insertion at the start 141 slice = g.Slice[string]{"a", "b", "c", "d"} 142 newSlice = slice.Insert(0, "x", "y") 143 expected = g.Slice[string]{"x", "y", "a", "b", "c", "d"} 144 if !newSlice.Eq(expected) { 145 t.Errorf("Insert(0) failed. Expected %v, but got %v", expected, newSlice) 146 } 147 148 // Test insertion at the end 149 slice = g.Slice[string]{"a", "b", "c", "d"} 150 newSlice = slice.Insert(slice.Len(), "x", "y") 151 expected = g.Slice[string]{"a", "b", "c", "d", "x", "y"} 152 if !newSlice.Eq(expected) { 153 t.Errorf("Insert(end) failed. Expected %v, but got %v", expected, newSlice) 154 } 155 156 // Test insertion with negative index 157 slice = g.Slice[string]{"a", "b", "c", "d"} 158 newSlice = slice.Insert(-2, "x", "y") 159 expected = g.Slice[string]{"a", "b", "x", "y", "c", "d"} 160 if !newSlice.Eq(expected) { 161 t.Errorf("Insert(-2) failed. Expected %v, but got %v", expected, newSlice) 162 } 163 164 // Test insertion at index 0 with an empty slice 165 slice = g.Slice[string]{} 166 newSlice = slice.Insert(0, "x", "y") 167 expected = g.Slice[string]{"x", "y"} 168 if !newSlice.Eq(expected) { 169 t.Errorf("Insert(0) with empty slice failed. Expected %v, but got %v", expected, newSlice) 170 } 171 } 172 173 func TestSliceInsertInPlace(t *testing.T) { 174 // Test insertion in the middle 175 slice := g.Slice[string]{"a", "b", "c", "d"} 176 slice.InsertInPlace(2, "e", "f") 177 expected := g.Slice[string]{"a", "b", "e", "f", "c", "d"} 178 if !slice.Eq(expected) { 179 t.Errorf("InsertInPlace(2) failed. Expected %v, but got %v", expected, slice) 180 } 181 182 // Test insertion at the start 183 slice = g.Slice[string]{"a", "b", "c", "d"} 184 slice.InsertInPlace(0, "x", "y") 185 expected = g.Slice[string]{"x", "y", "a", "b", "c", "d"} 186 if !slice.Eq(expected) { 187 t.Errorf("InsertInPlace(0) failed. Expected %v, but got %v", expected, slice) 188 } 189 190 // Test insertion at the end 191 slice = g.Slice[string]{"a", "b", "c", "d"} 192 slice.InsertInPlace(slice.Len(), "x", "y") 193 expected = g.Slice[string]{"a", "b", "c", "d", "x", "y"} 194 if !slice.Eq(expected) { 195 t.Errorf("InsertInPlace(end) failed. Expected %v, but got %v", expected, slice) 196 } 197 198 // Test insertion with negative index 199 slice = g.Slice[string]{"a", "b", "c", "d"} 200 slice.InsertInPlace(-2, "x", "y") 201 expected = g.Slice[string]{"a", "b", "x", "y", "c", "d"} 202 if !slice.Eq(expected) { 203 t.Errorf("InsertInPlace(-2) failed. Expected %v, but got %v", expected, slice) 204 } 205 206 // Test insertion at index 0 with an empty slice 207 slice = g.Slice[string]{} 208 slice.InsertInPlace(0, "x", "y") 209 expected = g.Slice[string]{"x", "y"} 210 if !slice.Eq(expected) { 211 t.Errorf("InsertInPlace(0) with empty slice failed. Expected %v, but got %v", expected, slice) 212 } 213 } 214 215 func TestSliceToSlice(t *testing.T) { 216 sl := g.NewSlice[int]().Append(1, 2, 3, 4, 5) 217 slice := sl.Std() 218 219 if len(slice) != sl.Len().Std() { 220 t.Errorf("Expected length %d, but got %d", sl.Len(), len(slice)) 221 } 222 223 for i, v := range sl { 224 if v != slice[i] { 225 t.Errorf("Expected value %d at index %d, but got %d", v, i, slice[i]) 226 } 227 } 228 } 229 230 func TestSliceShuffle(t *testing.T) { 231 sl := g.Slice[int]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} 232 sl.Shuffle() 233 234 if sl.Len() != 10 { 235 t.Error("Expected length of 10, got ", sl.Len()) 236 } 237 } 238 239 func TestSliceReverse(t *testing.T) { 240 sl := g.Slice[int]{1, 2, 3, 4, 5} 241 sl.Reverse() 242 243 if !reflect.DeepEqual(sl, g.Slice[int]{5, 4, 3, 2, 1}) { 244 t.Errorf("Expected %v, got %v", g.Slice[int]{5, 4, 3, 2, 1}, sl) 245 } 246 } 247 248 func TestSliceIndex(t *testing.T) { 249 // Test case: Function returns an index for known types (Int) 250 slInt := g.Slice[g.Int]{1, 2, 3, 4, 5} 251 index := slInt.Index(3) 252 if index != 2 { 253 t.Errorf("Expected index 2, got %d", index) 254 } 255 256 // Test case: Function returns -1 for unknown types (String) 257 slString := g.Slice[g.String]{"a", "b", "c"} 258 index = slString.Index("d") 259 if index != -1 { 260 t.Errorf("Expected index -1, got %d", index) 261 } 262 263 // Test case: Function returns an index for known types (Float) 264 slFloat := g.Slice[g.Float]{1.1, 2.2, 3.3} 265 index = slFloat.Index(2.2) 266 if index != 1 { 267 t.Errorf("Expected index 1, got %d", index) 268 } 269 270 // Test case: Function returns -1 for empty slice (Bool) 271 emptySliceBool := g.Slice[bool]{} 272 index = emptySliceBool.Index(true) 273 if index != -1 { 274 t.Errorf("Expected index -1 for empty slice, got %d", index) 275 } 276 277 // Test case: Function returns an index for known types (Byte) 278 slByte := g.Slice[byte]{byte('a'), byte('b'), byte('c')} 279 index = slByte.Index(byte('b')) 280 if index != 1 { 281 t.Errorf("Expected index 1, got %d", index) 282 } 283 284 // Test case: Function returns an index for known types (String) 285 slString2 := g.Slice[string]{"apple", "banana", "cherry"} 286 index = slString2.Index("banana") 287 if index != 1 { 288 t.Errorf("Expected index 1, got %d", index) 289 } 290 291 // Test case: Function returns an index for known types (Int) 292 slInt2 := g.Slice[int]{10, 20, 30} 293 index = slInt2.Index(20) 294 if index != 1 { 295 t.Errorf("Expected index 1, got %d", index) 296 } 297 298 // Test case: Function returns an index for known types (Int8) 299 slInt8 := g.Slice[int8]{1, 2, 3} 300 index = slInt8.Index(int8(2)) 301 if index != 1 { 302 t.Errorf("Expected index 1, got %d", index) 303 } 304 305 // Test case: Function returns an index for known types (Int16) 306 slInt16 := g.Slice[int16]{1, 2, 3} 307 index = slInt16.Index(int16(2)) 308 if index != 1 { 309 t.Errorf("Expected index 1, got %d", index) 310 } 311 312 // Test case: Function returns an index for known types (Int32) 313 slInt32 := g.Slice[int32]{1, 2, 3} 314 index = slInt32.Index(int32(2)) 315 if index != 1 { 316 t.Errorf("Expected index 1, got %d", index) 317 } 318 319 // Test case: Function returns an index for known types (Int64) 320 slInt64 := g.Slice[int64]{1, 2, 3} 321 index = slInt64.Index(int64(2)) 322 if index != 1 { 323 t.Errorf("Expected index 1, got %d", index) 324 } 325 326 // Test case: Function returns an index for known types (Uint) 327 slUint := g.Slice[uint]{1, 2, 3, 4, 5} 328 index = slUint.Index(uint(3)) 329 if index != 2 { 330 t.Errorf("Expected index 2, got %d", index) 331 } 332 333 // Test case: Function returns an index for known types (Uint8) 334 slUint8 := g.Slice[uint8]{1, 2, 3} 335 index = slUint8.Index(uint8(2)) 336 if index != 1 { 337 t.Errorf("Expected index 1, got %d", index) 338 } 339 340 // Test case: Function returns an index for known types (Uint16) 341 slUint16 := g.Slice[uint16]{1, 2, 3} 342 index = slUint16.Index(uint16(2)) 343 if index != 1 { 344 t.Errorf("Expected index 1, got %d", index) 345 } 346 347 // Test case: Function returns an index for known types (Uint32) 348 slUint32 := g.Slice[uint32]{1, 2, 3} 349 index = slUint32.Index(uint32(2)) 350 if index != 1 { 351 t.Errorf("Expected index 1, got %d", index) 352 } 353 354 // Test case: Function returns an index for known types (Uint64) 355 slUint64 := g.Slice[uint64]{1, 2, 3} 356 index = slUint64.Index(uint64(2)) 357 if index != 1 { 358 t.Errorf("Expected index 1, got %d", index) 359 } 360 361 // Test case: Function returns an index for known types (Float32) 362 slFloat32 := g.Slice[float32]{1.1, 2.2, 3.3} 363 index = slFloat32.Index(float32(2.2)) 364 if index != 1 { 365 t.Errorf("Expected index 1, got %d", index) 366 } 367 368 // Test case: Function returns an index for known types (Float64) 369 slFloat64 := g.Slice[float64]{1.1, 2.2, 3.3} 370 index = slFloat64.Index(float64(2.2)) 371 if index != 1 { 372 t.Errorf("Expected index 1, got %d", index) 373 } 374 } 375 376 func TestSliceIndexFunc(t *testing.T) { 377 // Define a custom slice type 378 type customType struct { 379 Value int 380 } 381 382 // Create a slice with custom type 383 slCustom := g.Slice[customType]{{Value: 1}, {Value: 2}, {Value: 3}} 384 385 // Test case: Function returns an index for custom types using IndexFunc 386 index := slCustom.Index(customType{Value: 2}) 387 if index != 1 { 388 t.Errorf("Expected index 1, got %d", index) 389 } 390 391 // Test case: Function returns -1 for custom types not found using IndexFunc 392 index = slCustom.Index(customType{Value: 4}) 393 if index != -1 { 394 t.Errorf("Expected index -1, got %d", index) 395 } 396 } 397 398 func TestSliceRandomSample(t *testing.T) { 399 sl := g.Slice[int]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} 400 result := sl.RandomSample(5) 401 402 if result.Len() != 5 { 403 t.Errorf("Expected result length to be 5, got %d", result.Len()) 404 } 405 406 for _, item := range result { 407 if !sl.Contains(item) { 408 t.Errorf("Expected result to contain only items from the original slice, got %d", item) 409 } 410 } 411 } 412 413 func TestSliceAddUnique(t *testing.T) { 414 sl := g.Slice[int]{1, 2, 3} 415 sl = sl.AddUnique(4, 5, 6) 416 417 if !sl.Contains(4) { 418 t.Error("AddUnique failed") 419 } 420 421 sl = sl.AddUnique(4, 5, 6) 422 if sl.Len() != 6 { 423 t.Error("AddUnique failed") 424 } 425 } 426 427 func TestSliceSortBy(t *testing.T) { 428 sl1 := g.NewSlice[int]().Append(3, 1, 4, 1, 5) 429 expected1 := g.NewSlice[int]().Append(1, 1, 3, 4, 5) 430 431 sl1.SortBy(func(a, b int) cmp.Ordering { return cmp.Cmp(a, b) }) 432 433 if !sl1.Eq(expected1) { 434 t.Errorf("SortBy failed: expected %v, but got %v", expected1, sl1) 435 } 436 437 sl2 := g.NewSlice[string]().Append("foo", "bar", "baz") 438 expected2 := g.NewSlice[string]().Append("foo", "baz", "bar") 439 440 sl2.SortBy(func(a, b string) cmp.Ordering { return cmp.Cmp(b, a) }) 441 442 if !sl2.Eq(expected2) { 443 t.Errorf("SortBy failed: expected %v, but got %v", expected2, sl2) 444 } 445 446 sl3 := g.NewSlice[int]() 447 expected3 := g.NewSlice[int]() 448 449 sl3.SortBy(func(a, b int) cmp.Ordering { return cmp.Cmp(a, b) }) 450 451 if !sl3.Eq(expected3) { 452 t.Errorf("SortBy failed: expected %v, but got %v", expected3, sl3) 453 } 454 } 455 456 func TestSliceJoin(t *testing.T) { 457 sl := g.Slice[string]{"1", "2", "3", "4", "5"} 458 str := sl.Join(",") 459 460 if !strings.EqualFold("1,2,3,4,5", str.Std()) { 461 t.Errorf("Expected 1,2,3,4,5, got %s", str.Std()) 462 } 463 } 464 465 func TestSliceToStringSlice(t *testing.T) { 466 sl := g.Slice[int]{1, 2, 3} 467 result := sl.ToStringSlice() 468 expected := []string{"1", "2", "3"} 469 470 if !reflect.DeepEqual(result, expected) { 471 t.Errorf("Expected %v, got %v", expected, result) 472 } 473 } 474 475 func TestSliceAdd(t *testing.T) { 476 sl := g.Slice[int]{1, 2, 3}.Append(4, 5, 6) 477 478 if !reflect.DeepEqual(sl, g.Slice[int]{1, 2, 3, 4, 5, 6}) { 479 t.Error("Add failed") 480 } 481 } 482 483 func TestSliceClone(t *testing.T) { 484 sl := g.Slice[int]{1, 2, 3} 485 slClone := sl.Clone() 486 487 if !sl.Eq(slClone) { 488 t.Errorf("Clone() failed, expected %v, got %v", sl, slClone) 489 } 490 } 491 492 func TestSliceCut(t *testing.T) { 493 slice := g.Slice[int]{1, 2, 3, 4, 5} 494 495 // Test normal range 496 newSlice := slice.Cut(1, 3) 497 expected := g.Slice[int]{1, 4, 5} 498 if !newSlice.Eq(expected) { 499 t.Errorf("Cut(1, 3) failed. Expected %v, but got %v", expected, newSlice) 500 } 501 502 // Test range with negative indices 503 newSlice = slice.Cut(-3, -2) 504 expected = g.Slice[int]{1, 2, 4, 5} 505 if !newSlice.Eq(expected) { 506 t.Errorf("Cut(-3, -2) failed. Expected %v, but got %v", expected, newSlice) 507 } 508 509 // Test empty range 510 newSlice = slice.Cut(0, 5) 511 expected = g.Slice[int]{} 512 if !newSlice.Eq(expected) { 513 t.Errorf("Cut(3, 2) failed. Expected %v, but got %v", expected, newSlice) 514 } 515 } 516 517 func TestSliceCutInPlace(t *testing.T) { 518 slice := g.Slice[int]{1, 2, 3, 4, 5} 519 520 // Test normal range 521 slice.CutInPlace(1, 3) 522 expected := g.Slice[int]{1, 4, 5} 523 if !slice.Eq(expected) { 524 t.Errorf("CutInPlace(1, 3) failed. Expected %v, but got %v", expected, slice) 525 } 526 527 // Test range with negative indices 528 slice = g.Slice[int]{1, 2, 3, 4, 5} // Restore the original slice 529 slice.CutInPlace(-3, -2) 530 expected = g.Slice[int]{1, 2, 4, 5} 531 if !slice.Eq(expected) { 532 t.Errorf("CutInPlace(-3, -2) failed. Expected %v, but got %v", expected, slice) 533 } 534 535 // Test empty range 536 slice = g.Slice[int]{1, 2, 3, 4, 5} // Restore the original slice 537 slice.CutInPlace(0, 5) 538 expected = g.Slice[int]{} 539 if !slice.Eq(expected) { 540 t.Errorf("CutInPlace(0, 5) failed. Expected %v, but got %v", expected, slice) 541 } 542 } 543 544 func TestSliceLast(t *testing.T) { 545 sl := g.Slice[int]{1, 2, 3, 4, 5} 546 if sl.Last() != 5 { 547 t.Error("Last() failed") 548 } 549 } 550 551 func TestSliceLen(t *testing.T) { 552 sl := g.Slice[int]{1, 2, 3, 4, 5} 553 if sl.Len() != 5 { 554 t.Errorf("Expected 5, got %d", sl.Len()) 555 } 556 } 557 558 func TestSlicePop(t *testing.T) { 559 sl := g.Slice[int]{1, 2, 3, 4, 5} 560 last, sl := sl.Pop() 561 562 if last != 5 { 563 t.Errorf("Expected 5, got %v", last) 564 } 565 566 if sl.Len() != 4 { 567 t.Errorf("Expected 4, got %v", sl.Len()) 568 } 569 570 r := g.Slice[int]{1, 2, 3, 4} 571 if sl.Ne(r) { 572 t.Errorf("Expected %v, got %v", r, sl) 573 } 574 } 575 576 func TestSliceMaxInt(t *testing.T) { 577 sl := g.Slice[g.Int]{1, 2, 3, 4, 5} 578 if max := cmp.Max(sl...); max != 5 { 579 t.Errorf("Max() = %d, want: %d.", max, 5) 580 } 581 } 582 583 func TestSliceMaxFloats(t *testing.T) { 584 sl := g.Slice[g.Float]{2.2, 2.8, 2.1, 2.7} 585 if max := cmp.Max(sl...); max != 2.8 { 586 t.Errorf("Max() = %f, want: %f.", max, 2.8) 587 } 588 } 589 590 func TestSliceMinFloat(t *testing.T) { 591 sl := g.Slice[g.Float]{2.2, 2.8, 2.1, 2.7} 592 if min := cmp.Min(sl...); min != 2.1 { 593 t.Errorf("Min() = %f; want: %f", min, 2.1) 594 } 595 } 596 597 func TestSliceMinInt(t *testing.T) { 598 sl := g.Slice[g.Int]{1, 2, 3, 4, 5} 599 if min := cmp.Min(sl...); min != 1 { 600 t.Errorf("Min() = %d; want: %d", min, 1) 601 } 602 } 603 604 func TestSliceDelete(t *testing.T) { 605 sl := g.Slice[int]{1, 2, 3, 4, 5} 606 sl = sl.Delete(2) 607 608 if !reflect.DeepEqual(sl, g.Slice[int]{1, 2, 4, 5}) { 609 t.Errorf("Delete(2) = %v, want %v", sl, g.Slice[int]{1, 2, 4, 5}) 610 } 611 612 sl = g.Slice[int]{1, 2, 3, 4, 5} 613 sl = sl.Delete(-2) 614 615 if !reflect.DeepEqual(sl, g.Slice[int]{1, 2, 3, 5}) { 616 t.Errorf("Delete(2) = %v, want %v", sl, g.Slice[int]{1, 2, 3, 5}) 617 } 618 } 619 620 func TestSliceSFill(t *testing.T) { 621 sl := g.Slice[int]{1, 2, 3, 4, 5} 622 sl.Fill(0) 623 624 for _, v := range sl { 625 if v != 0 { 626 t.Errorf("Expected all elements to be 0, but found %d", v) 627 } 628 } 629 } 630 631 func TestSliceSet(t *testing.T) { 632 sl := g.NewSlice[int](5) 633 634 sl.Set(0, 1) 635 sl.Set(0, 1) 636 sl.Set(2, 2) 637 sl.Set(4, 3) 638 639 if !reflect.DeepEqual(sl, g.Slice[int]{1, 0, 2, 0, 3}) { 640 t.Errorf("Set() = %v, want %v", sl, g.Slice[int]{1, 0, 2, 0, 3}) 641 } 642 } 643 644 func TestSliceReplace(t *testing.T) { 645 tests := []struct { 646 name string 647 input g.Slice[string] 648 i, j g.Int 649 values []string 650 expected g.Slice[string] 651 }{ 652 { 653 name: "basic test", 654 input: g.Slice[string]{"a", "b", "c", "d"}, 655 i: 1, 656 j: 3, 657 values: []string{"e", "f"}, 658 expected: g.Slice[string]{"a", "e", "f", "d"}, 659 }, 660 { 661 name: "replace at start", 662 input: g.Slice[string]{"a", "b", "c", "d"}, 663 i: 0, 664 j: 2, 665 values: []string{"e", "f"}, 666 expected: g.Slice[string]{"e", "f", "c", "d"}, 667 }, 668 { 669 name: "replace at end", 670 input: g.Slice[string]{"a", "b", "c", "d"}, 671 i: 2, 672 j: 4, 673 values: []string{"e", "f"}, 674 expected: g.Slice[string]{"a", "b", "e", "f"}, 675 }, 676 { 677 name: "replace with more values", 678 input: g.Slice[string]{"a", "b", "c", "d"}, 679 i: 1, 680 j: 2, 681 values: []string{"e", "f", "g", "h"}, 682 expected: g.Slice[string]{"a", "e", "f", "g", "h", "c", "d"}, 683 }, 684 { 685 name: "replace with fewer values", 686 input: g.Slice[string]{"a", "b", "c", "d"}, 687 i: 1, 688 j: 3, 689 values: []string{"e"}, 690 expected: g.Slice[string]{"a", "e", "d"}, 691 }, 692 { 693 name: "replace entire slice", 694 input: g.Slice[string]{"a", "b", "c", "d"}, 695 i: 0, 696 j: 4, 697 values: []string{"e", "f", "g", "h"}, 698 expected: g.Slice[string]{"e", "f", "g", "h"}, 699 }, 700 } 701 702 for _, tt := range tests { 703 t.Run(tt.name, func(t *testing.T) { 704 result := tt.input.Replace(tt.i, tt.j, tt.values...) 705 if !reflect.DeepEqual(result, tt.expected) { 706 t.Errorf("expected %v, got %v", tt.expected, result) 707 } 708 }) 709 } 710 } 711 712 func TestSliceReplaceM(t *testing.T) { 713 // Test replacement in the middle 714 slice := g.Slice[string]{"a", "b", "c", "d"} 715 newSlice := slice.Replace(1, 1, "zz", "xx") 716 expected := g.Slice[string]{"a", "zz", "xx", "b", "c", "d"} 717 if !newSlice.Eq(expected) { 718 t.Errorf("Replace(1, 1) failed. Expected %v, but got %v", expected, newSlice) 719 } 720 721 // Test replacement with same start and end indices 722 slice = g.Slice[string]{"a", "b", "c", "d"} 723 newSlice = slice.Replace(2, 2, "zz", "xx") 724 expected = g.Slice[string]{"a", "b", "zz", "xx", "c", "d"} 725 if !newSlice.Eq(expected) { 726 t.Errorf("Replace(2, 2) failed. Expected %v, but got %v", expected, newSlice) 727 } 728 729 // Test replacement from i to the end 730 slice = g.Slice[string]{"a", "b", "c", "d"} 731 newSlice = slice.Replace(2, slice.Len(), "zz", "xx") 732 expected = g.Slice[string]{"a", "b", "zz", "xx"} 733 if !newSlice.Eq(expected) { 734 t.Errorf("Replace(2, end) failed. Expected %v, but got %v", expected, newSlice) 735 } 736 737 // Test replacement from the start to j 738 slice = g.Slice[string]{"a", "b", "c", "d"} 739 newSlice = slice.Replace(0, 2, "zz", "xx") 740 expected = g.Slice[string]{"zz", "xx", "c", "d"} 741 if !newSlice.Eq(expected) { 742 t.Errorf("Replace(start, 2) failed. Expected %v, but got %v", expected, newSlice) 743 } 744 745 // Test empty replacement 746 slice = g.Slice[string]{"a", "b", "c", "d"} 747 newSlice = slice.Replace(2, 2) // No replacement, should remain unchanged 748 expected = g.Slice[string]{"a", "b", "c", "d"} 749 if !newSlice.Eq(expected) { 750 t.Errorf("Replace(2, 2) failed. Expected %v, but got %v", expected, newSlice) 751 } 752 753 // Test replacement from negative index to positive index 754 slice = g.Slice[string]{"a", "b", "c", "d"} 755 newSlice = slice.Replace(-2, 2, "zz", "xx") 756 expected = g.Slice[string]{"a", "b", "zz", "xx", "c", "d"} 757 if !newSlice.Eq(expected) { 758 t.Errorf("Replace(-2, 2) failed. Expected %v, but got %v", expected, newSlice) 759 } 760 761 // Test replacement from positive index to negative index 762 slice = g.Slice[string]{"a", "b", "c", "d"} 763 newSlice = slice.Replace(1, -1, "zz", "xx") 764 expected = g.Slice[string]{"a", "zz", "xx", "d"} 765 if !newSlice.Eq(expected) { 766 t.Errorf("Replace(1, -1) failed. Expected %v, but got %v", expected, newSlice) 767 } 768 769 // Test replacement from negative index to negative index 770 slice = g.Slice[string]{"a", "b", "c", "d"} 771 newSlice = slice.Replace(-3, -2, "zz", "xx") 772 expected = g.Slice[string]{"a", "zz", "xx", "c", "d"} 773 if !newSlice.Eq(expected) { 774 t.Errorf("Replace(-3, -2) failed. Expected %v, but got %v", expected, newSlice) 775 } 776 777 // Test replacement from negative index to positive index including negative values 778 slice = g.Slice[string]{"a", "b", "c", "d"} 779 newSlice = slice.Replace(-3, 3, "zz", "xx") 780 expected = g.Slice[string]{"a", "zz", "xx", "d"} 781 if !newSlice.Eq(expected) { 782 t.Errorf("Replace(-3, 3) failed. Expected %v, but got %v", expected, newSlice) 783 } 784 785 // Test replacement with empty slice 786 slice = g.Slice[string]{"a", "b", "c", "d"} 787 newSlice = slice.Replace(1, 3) 788 expected = g.Slice[string]{"a", "d"} 789 if !newSlice.Eq(expected) { 790 t.Errorf("Replace(1, 3) with empty slice failed. Expected %v, but got %v", expected, newSlice) 791 } 792 } 793 794 func TestSliceReplaceInPlace(t *testing.T) { 795 tests := []struct { 796 name string 797 input g.Slice[string] 798 i, j g.Int 799 values []string 800 expected g.Slice[string] 801 }{ 802 { 803 name: "basic test", 804 input: g.Slice[string]{"a", "b", "c", "d"}, 805 i: 1, 806 j: 3, 807 values: []string{"e", "f"}, 808 expected: g.Slice[string]{"a", "e", "f", "d"}, 809 }, 810 { 811 name: "replace at start", 812 input: g.Slice[string]{"a", "b", "c", "d"}, 813 i: 0, 814 j: 2, 815 values: []string{"e", "f"}, 816 expected: g.Slice[string]{"e", "f", "c", "d"}, 817 }, 818 { 819 name: "replace at end", 820 input: g.Slice[string]{"a", "b", "c", "d"}, 821 i: 2, 822 j: 4, 823 values: []string{"e", "f"}, 824 expected: g.Slice[string]{"a", "b", "e", "f"}, 825 }, 826 { 827 name: "replace with more values", 828 input: g.Slice[string]{"a", "b", "c", "d"}, 829 i: 1, 830 j: 2, 831 values: []string{"e", "f", "g", "h"}, 832 expected: g.Slice[string]{"a", "e", "f", "g", "h", "c", "d"}, 833 }, 834 { 835 name: "replace with fewer values", 836 input: g.Slice[string]{"a", "b", "c", "d"}, 837 i: 1, 838 j: 3, 839 values: []string{"e"}, 840 expected: g.Slice[string]{"a", "e", "d"}, 841 }, 842 { 843 name: "replace entire slice", 844 input: g.Slice[string]{"a", "b", "c", "d"}, 845 i: 0, 846 j: 4, 847 values: []string{"e", "f", "g", "h"}, 848 expected: g.Slice[string]{"e", "f", "g", "h"}, 849 }, 850 } 851 852 for _, tt := range tests { 853 t.Run(tt.name, func(t *testing.T) { 854 sl := &tt.input 855 sl.ReplaceInPlace(tt.i, tt.j, tt.values...) 856 if !reflect.DeepEqual(*sl, tt.expected) { 857 t.Errorf("expected %v, got %v", tt.expected, *sl) 858 } 859 }) 860 } 861 } 862 863 func TestSliceReplaceInPlaceM(t *testing.T) { 864 // Test replacement in the middle 865 slice := g.Slice[string]{"a", "b", "c", "d"} 866 slice.ReplaceInPlace(1, 1, "zz", "xx") 867 expected := g.Slice[string]{"a", "zz", "xx", "b", "c", "d"} 868 if !slice.Eq(expected) { 869 t.Errorf("ReplaceInPlace(1, 1) failed. Expected %v, but got %v", expected, slice) 870 } 871 872 // Test replacement with same start and end indices 873 slice = g.Slice[string]{"a", "b", "c", "d"} 874 slice.ReplaceInPlace(2, 2, "zz", "xx") 875 expected = g.Slice[string]{"a", "b", "zz", "xx", "c", "d"} 876 if !slice.Eq(expected) { 877 t.Errorf("ReplaceInPlace(2, 2) failed. Expected %v, but got %v", expected, slice) 878 } 879 880 // Test replacement from i to the end 881 slice = g.Slice[string]{"a", "b", "c", "d"} 882 slice.ReplaceInPlace(2, slice.Len(), "zz", "xx") 883 expected = g.Slice[string]{"a", "b", "zz", "xx"} 884 if !slice.Eq(expected) { 885 t.Errorf("ReplaceInPlace(2, end) failed. Expected %v, but got %v", expected, slice) 886 } 887 888 // Test replacement from the start to j 889 slice = g.Slice[string]{"a", "b", "c", "d"} 890 slice.ReplaceInPlace(0, 2, "zz", "xx") 891 expected = g.Slice[string]{"zz", "xx", "c", "d"} 892 if !slice.Eq(expected) { 893 t.Errorf("ReplaceInPlace(start, 2) failed. Expected %v, but got %v", expected, slice) 894 } 895 896 // Test empty replacement 897 slice = g.Slice[string]{"a", "b", "c", "d"} 898 slice.ReplaceInPlace(2, 2) // No replacement, should remain unchanged 899 expected = g.Slice[string]{"a", "b", "c", "d"} 900 if !slice.Eq(expected) { 901 t.Errorf("ReplaceInPlace(2, 2) failed. Expected %v, but got %v", expected, slice) 902 } 903 904 // Test replacement from negative index to positive index 905 slice = g.Slice[string]{"a", "b", "c", "d"} 906 slice.ReplaceInPlace(-2, 2, "zz", "xx") 907 expected = g.Slice[string]{"a", "b", "zz", "xx", "c", "d"} 908 if !slice.Eq(expected) { 909 t.Errorf("ReplaceInPlace(-2, 2) failed. Expected %v, but got %v", expected, slice) 910 } 911 912 // Test replacement from positive index to negative index 913 slice = g.Slice[string]{"a", "b", "c", "d"} 914 slice.ReplaceInPlace(1, -1, "zz", "xx") 915 expected = g.Slice[string]{"a", "zz", "xx", "d"} 916 if !slice.Eq(expected) { 917 t.Errorf("ReplaceInPlace(1, -1) failed. Expected %v, but got %v", expected, slice) 918 } 919 920 // Test replacement from negative index to negative index 921 slice = g.Slice[string]{"a", "b", "c", "d"} 922 slice.ReplaceInPlace(-3, -2, "zz", "xx") 923 expected = g.Slice[string]{"a", "zz", "xx", "c", "d"} 924 if !slice.Eq(expected) { 925 t.Errorf("ReplaceInPlace(-3, -2) failed. Expected %v, but got %v", expected, slice) 926 } 927 928 // Test replacement from negative index to positive index including negative values 929 slice = g.Slice[string]{"a", "b", "c", "d"} 930 slice.ReplaceInPlace(-3, 3, "zz", "xx") 931 expected = g.Slice[string]{"a", "zz", "xx", "d"} 932 if !slice.Eq(expected) { 933 t.Errorf("ReplaceInPlace(-3, 3) failed. Expected %v, but got %v", expected, slice) 934 } 935 936 // Test replacement with empty slice 937 slice = g.Slice[string]{"a", "b", "c", "d"} 938 slice.ReplaceInPlace(1, 3) 939 expected = g.Slice[string]{"a", "d"} 940 if !slice.Eq(expected) { 941 t.Errorf("ReplaceInPlace(1, 3) with empty slice failed. Expected %v, but got %v", expected, slice) 942 } 943 } 944 945 func TestSliceContainsAny(t *testing.T) { 946 testCases := []struct { 947 sl g.Slice[int] 948 other g.Slice[int] 949 expect bool 950 }{ 951 {g.Slice[int]{1, 2, 3, 4, 5}, g.Slice[int]{6, 7, 8, 9, 10}, false}, 952 {g.Slice[int]{1, 2, 3, 4, 5}, g.Slice[int]{5, 6, 7, 8, 9}, true}, 953 {g.Slice[int]{}, g.Slice[int]{1, 2, 3, 4, 5}, false}, 954 {g.Slice[int]{1, 2, 3, 4, 5}, g.Slice[int]{}, false}, 955 {g.Slice[int]{1, 2, 3, 4, 5}, g.Slice[int]{1, 2, 3, 4, 5}, true}, 956 {g.Slice[int]{1, 2, 3, 4, 5}, g.Slice[int]{6}, false}, 957 {g.Slice[int]{1, 2, 3, 4, 5}, g.Slice[int]{1}, true}, 958 {g.Slice[int]{1, 2, 3, 4, 5}, g.Slice[int]{6, 7, 8, 9, 0, 3}, true}, 959 } 960 961 for _, tc := range testCases { 962 if result := tc.sl.ContainsAny(tc.other...); result != tc.expect { 963 t.Errorf("ContainsAny(%v, %v) = %v; want %v", tc.sl, tc.other, result, tc.expect) 964 } 965 } 966 } 967 968 func TestSliceContainsAll(t *testing.T) { 969 testCases := []struct { 970 sl g.Slice[int] 971 other g.Slice[int] 972 expect bool 973 }{ 974 {g.Slice[int]{1, 2, 3, 4, 5}, g.Slice[int]{1, 2, 3}, true}, 975 {g.Slice[int]{1, 2, 3, 4, 5}, g.Slice[int]{1, 2, 3, 6}, false}, 976 {g.Slice[int]{}, g.Slice[int]{1, 2, 3, 4, 5}, false}, 977 {g.Slice[int]{1, 2, 3, 4, 5}, g.Slice[int]{}, false}, 978 {g.Slice[int]{1, 2, 3, 4, 5}, g.Slice[int]{1, 2, 3, 4, 5}, true}, 979 {g.Slice[int]{1, 2, 3, 4, 5}, g.Slice[int]{6}, false}, 980 {g.Slice[int]{1, 2, 3, 4, 5}, g.Slice[int]{1}, true}, 981 {g.Slice[int]{1, 2, 3, 4, 5}, g.Slice[int]{1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 5, 5}, true}, 982 } 983 984 for _, tc := range testCases { 985 if result := tc.sl.ContainsAll(tc.other...); result != tc.expect { 986 t.Errorf("ContainsAll(%v, %v) = %v; want %v", tc.sl, tc.other, result, tc.expect) 987 } 988 } 989 } 990 991 func TestSliceSubSlice(t *testing.T) { 992 // Test with an empty slice 993 emptySlice := g.Slice[int]{} 994 emptySubSlice := emptySlice.SubSlice(0, 0) 995 if !emptySubSlice.Empty() { 996 t.Errorf("Expected empty slice for empty source slice, but got: %v", emptySubSlice) 997 } 998 999 // Test with a non-empty slice 1000 slice := g.Slice[int]{1, 2, 3, 4, 5} 1001 1002 // Test a valid range within bounds 1003 subSlice := slice.SubSlice(1, 4) 1004 expected := g.Slice[int]{2, 3, 4} 1005 if !subSlice.Eq(expected) { 1006 t.Errorf("Expected subSlice: %v, but got: %v", expected, subSlice) 1007 } 1008 1009 // Test with a single negative index 1010 subSlice = slice.SubSlice(-2, slice.Len()) 1011 expected = g.Slice[int]{4, 5} 1012 if !subSlice.Eq(expected) { 1013 t.Errorf("Expected subSlice: %v, but got: %v", expected, subSlice) 1014 } 1015 1016 // Test with a negative start and end index 1017 subSlice = slice.SubSlice(-3, -1) 1018 expected = g.Slice[int]{3, 4} 1019 if !subSlice.Eq(expected) { 1020 t.Errorf("Expected subSlice: %v, but got: %v", expected, subSlice) 1021 } 1022 } 1023 1024 func TestSubSliceOutOfBoundsStartIndex(t *testing.T) { 1025 slice := g.Slice[int]{1, 2, 3, 4, 5} 1026 1027 // Test with start index beyond slice length (should panic) 1028 defer func() { 1029 if r := recover(); r == nil { 1030 t.Errorf("Expected panic for start index beyond slice length, but no panic occurred") 1031 } 1032 }() 1033 _ = slice.SubSlice(10, slice.Len()) 1034 } 1035 1036 func TestSubSliceOutOfBoundsNegativeStartIndex(t *testing.T) { 1037 slice := g.Slice[int]{1, 2, 3, 4, 5} 1038 1039 // Test with a negative start index beyond slice length (should panic) 1040 defer func() { 1041 if r := recover(); r == nil { 1042 t.Errorf("Expected panic for negative start index beyond slice length, but no panic occurred") 1043 } 1044 }() 1045 _ = slice.SubSlice(-10, slice.Len()) 1046 } 1047 1048 func TestSubSliceOutOfBoundsEndIndex(t *testing.T) { 1049 slice := g.Slice[int]{1, 2, 3, 4, 5} 1050 1051 // Test with an end index beyond slice length (should panic) 1052 defer func() { 1053 if r := recover(); r == nil { 1054 t.Errorf("Expected panic for end index beyond slice length, but no panic occurred") 1055 } 1056 }() 1057 _ = slice.SubSlice(2, 10) 1058 } 1059 1060 func TestGrowSlice(t *testing.T) { 1061 // Initialize a slice with some elements. 1062 initialSlice := g.SliceOf(1, 2, 3) 1063 1064 // Check the initial capacity of the slice. 1065 var initialCapacity g.Int = initialSlice.Cap() 1066 1067 // Grow the slice to accommodate more elements. 1068 var newCapacity g.Int = initialCapacity + 5 1069 grownSlice := initialSlice.Grow(newCapacity - initialCapacity) 1070 1071 // Check if the capacity of the grown slice is as expected. 1072 if grownSlice.Cap() != newCapacity { 1073 t.Errorf("Grow method failed: Expected capacity %d, got %d", newCapacity, grownSlice.Cap()) 1074 } 1075 1076 // Append new elements to the grown slice. 1077 for i := 0; i < 5; i++ { 1078 grownSlice = grownSlice.Append(i + 4) 1079 } 1080 1081 // Check if the length of the grown slice is correct. 1082 if grownSlice.Len() != newCapacity { 1083 t.Errorf("Grow method failed: Expected length %d, got %d", newCapacity, grownSlice.Len()) 1084 } 1085 } 1086 1087 func TestSliceNotEmpty(t *testing.T) { 1088 // Test case 1: Slice with elements 1089 sl1 := g.SliceOf(1, 2, 3) 1090 if !sl1.NotEmpty() { 1091 t.Errorf("Test case 1 failed: Expected slice to be not empty") 1092 } 1093 1094 // Test case 2: Empty slice 1095 sl2 := g.NewSlice[g.Int]() 1096 if sl2.NotEmpty() { 1097 t.Errorf("Test case 2 failed: Expected slice to be empty") 1098 } 1099 } 1100 1101 func TestSliceAppendInPlace(t *testing.T) { 1102 // Create a slice with initial elements 1103 initialSlice := g.Slice[int]{1, 2, 3} 1104 1105 // Append additional elements using AppendInPlace 1106 initialSlice.AppendInPlace(4, 5, 6) 1107 1108 // Verify that the slice has the expected elements 1109 expected := g.Slice[int]{1, 2, 3, 4, 5, 6} 1110 if !initialSlice.Eq(expected) { 1111 t.Errorf("AppendInPlace failed. Expected: %v, Got: %v", expected, initialSlice) 1112 } 1113 } 1114 1115 func TestSliceString(t *testing.T) { 1116 // Create a slice with some elements 1117 sl := g.SliceOf(1, 2, 3, 4, 5) 1118 1119 // Define the expected string representation 1120 expected := "Slice[1, 2, 3, 4, 5]" 1121 1122 // Get the string representation using the String method 1123 result := sl.String() 1124 1125 // Compare the result with the expected value 1126 if result != expected { 1127 t.Errorf("Slice String method failed. Expected: %s, Got: %s", expected, result) 1128 } 1129 } 1130 1131 func TestSliceEq(t *testing.T) { 1132 // Test case: Function returns true for equal slices of known types (Int) 1133 slInt1 := g.Slice[g.Int]{1, 2, 3} 1134 slInt2 := g.Slice[g.Int]{1, 2, 3} 1135 if !slInt1.Eq(slInt2) { 1136 t.Errorf("Test 1: Expected slices to be equal") 1137 } 1138 1139 // Test case: Function returns false for unequal slices of known types (String) 1140 slString1 := g.Slice[g.String]{"a", "b", "c"} 1141 slString2 := g.Slice[g.String]{"a", "x", "c"} 1142 if slString1.Eq(slString2) { 1143 t.Errorf("Test 2: Expected slices to be unequal") 1144 } 1145 1146 // Test case: Function returns true for empty slices 1147 emptySlice1 := g.Slice[g.Float]{} 1148 emptySlice2 := g.Slice[g.Float]{} 1149 if !emptySlice1.Eq(emptySlice2) { 1150 t.Errorf("Test 3: Expected empty slices to be equal") 1151 } 1152 1153 // Test case: Function returns false for slices of different lengths 1154 slFloat1 := g.Slice[g.Float]{1.1, 2.2, 3.3} 1155 slFloat2 := g.Slice[g.Float]{1.1, 2.2} 1156 if slFloat1.Eq(slFloat2) { 1157 t.Errorf("Test 4: Expected slices of different lengths to be unequal") 1158 } 1159 1160 // Test case: Function returns true for equal slices of string type 1161 slString3 := g.Slice[string]{"apple", "banana", "cherry"} 1162 slString4 := g.Slice[string]{"apple", "banana", "cherry"} 1163 if !slString3.Eq(slString4) { 1164 t.Errorf("Test 5: Expected slices to be equal") 1165 } 1166 1167 // Test case: Function returns false for unequal slices of int type 1168 slInt3 := g.Slice[int]{10, 20, 30} 1169 slInt4 := g.Slice[int]{10, 20, 40} 1170 if slInt3.Eq(slInt4) { 1171 t.Errorf("Test 6: Expected slices to be unequal") 1172 } 1173 1174 // Test case: Function returns true for equal slices of float64 type 1175 slFloat64_1 := g.Slice[float64]{1.1, 2.2, 3.3} 1176 slFloat64_2 := g.Slice[float64]{1.1, 2.2, 3.3} 1177 if !slFloat64_1.Eq(slFloat64_2) { 1178 t.Errorf("Test 7: Expected slices to be equal") 1179 } 1180 1181 // Test case: Function returns true for equal slices of bool type 1182 slBool1 := g.Slice[bool]{true, false, true} 1183 slBool2 := g.Slice[bool]{true, false, true} 1184 if !slBool1.Eq(slBool2) { 1185 t.Errorf("Test 8: Expected slices to be equal") 1186 } 1187 1188 // Test case: Function returns false for unequal slices of byte type 1189 slByte1 := g.Slice[byte]{1, 2, 3} 1190 slByte2 := g.Slice[byte]{1, 2, 4} 1191 if slByte1.Eq(slByte2) { 1192 t.Errorf("Test 9: Expected slices to be unequal") 1193 } 1194 1195 // Test case: Function returns true for equal slices of int8 type 1196 slInt81 := g.Slice[int8]{1, 2, 3} 1197 slInt82 := g.Slice[int8]{1, 2, 3} 1198 if !slInt81.Eq(slInt82) { 1199 t.Errorf("Test 10: Expected slices to be equal") 1200 } 1201 1202 // Test case: Function returns true for equal slices of int16 type 1203 slInt161 := g.Slice[int16]{1, 2, 3} 1204 slInt162 := g.Slice[int16]{1, 2, 3} 1205 if !slInt161.Eq(slInt162) { 1206 t.Errorf("Test 11: Expected slices to be equal") 1207 } 1208 1209 // Test case: Function returns true for equal slices of int32 type 1210 slInt321 := g.Slice[int32]{1, 2, 3} 1211 slInt322 := g.Slice[int32]{1, 2, 3} 1212 if !slInt321.Eq(slInt322) { 1213 t.Errorf("Test 12: Expected slices to be equal") 1214 } 1215 1216 // Test case: Function returns true for equal slices of int64 type 1217 slInt641 := g.Slice[int64]{1, 2, 3} 1218 slInt642 := g.Slice[int64]{1, 2, 3} 1219 if !slInt641.Eq(slInt642) { 1220 t.Errorf("Test 13: Expected slices to be equal") 1221 } 1222 1223 // Test case: Function returns true for equal slices of uint type 1224 slUint1 := g.Slice[uint]{1, 2, 3} 1225 slUint2 := g.Slice[uint]{1, 2, 3} 1226 if !slUint1.Eq(slUint2) { 1227 t.Errorf("Test 14: Expected slices to be equal") 1228 } 1229 1230 // Test case: Function returns true for equal slices of uint8 type 1231 slUint81 := g.Slice[uint8]{1, 2, 3} 1232 slUint82 := g.Slice[uint8]{1, 2, 3} 1233 if !slUint81.Eq(slUint82) { 1234 t.Errorf("Test 15: Expected slices to be equal") 1235 } 1236 1237 // Test case: Function returns true for equal slices of uint16 type 1238 slUint161 := g.Slice[uint16]{1, 2, 3} 1239 slUint162 := g.Slice[uint16]{1, 2, 3} 1240 if !slUint161.Eq(slUint162) { 1241 t.Errorf("Test 16: Expected slices to be equal") 1242 } 1243 1244 // Test case: Function returns true for equal slices of uint32 type 1245 slUint321 := g.Slice[uint32]{1, 2, 3} 1246 slUint322 := g.Slice[uint32]{1, 2, 3} 1247 if !slUint321.Eq(slUint322) { 1248 t.Errorf("Test 17: Expected slices to be equal") 1249 } 1250 1251 // Test case: Function returns true for equal slices of uint64 type 1252 slUint641 := g.Slice[uint64]{1, 2, 3} 1253 slUint642 := g.Slice[uint64]{1, 2, 3} 1254 if !slUint641.Eq(slUint642) { 1255 t.Errorf("Test 18: Expected slices to be equal") 1256 } 1257 1258 // Test case: Function returns true for equal slices of float32 type 1259 slFloat321 := g.Slice[float32]{1.1, 2.2, 3.3} 1260 slFloat322 := g.Slice[float32]{1.1, 2.2, 3.3} 1261 if !slFloat321.Eq(slFloat322) { 1262 t.Errorf("Test 19: Expected slices to be equal") 1263 } 1264 } 1265 1266 func TestSliceLastIndex(t *testing.T) { 1267 // Create a slice with some elements 1268 sl := g.SliceOf(1, 2, 3, 4, 5) 1269 1270 // Get the last index of the slice 1271 lastIndex := sl.LastIndex() 1272 1273 // Check if the last index is correct 1274 expectedLastIndex := sl.Len() - 1 1275 if lastIndex != expectedLastIndex { 1276 t.Errorf("Slice LastIndex method failed. Expected: %d, Got: %d", expectedLastIndex, lastIndex) 1277 } 1278 1279 // Create an empty slice 1280 emptySlice := g.NewSlice[int]() 1281 1282 // Get the last index of the empty slice 1283 emptyLastIndex := emptySlice.LastIndex() 1284 1285 // Check if the last index of an empty slice is 0 1286 if emptyLastIndex != 0 { 1287 t.Errorf("Slice LastIndex method failed for empty slice. Expected: 0, Got: %d", emptyLastIndex) 1288 } 1289 } 1290 1291 func TestSliceRandom(t *testing.T) { 1292 // Create a slice with some elements 1293 sl := g.SliceOf(1, 2, 3, 4, 5) 1294 1295 // Get a random element from the slice 1296 randomElement := sl.Random() 1297 1298 // Check if the random element is within the slice 1299 found := false 1300 sl.Iter().ForEach(func(v int) { 1301 if v == randomElement { 1302 found = true 1303 } 1304 }) 1305 1306 if !found { 1307 t.Errorf("Slice Random method failed. Random element %d not found in the slice", randomElement) 1308 } 1309 1310 // Test for an empty slice 1311 emptySlice := g.NewSlice[int]() 1312 1313 // Get a random element from the empty slice 1314 emptyRandomElement := emptySlice.Random() 1315 1316 // Check if the random element from an empty slice is zero value 1317 if emptyRandomElement != 0 { 1318 t.Errorf("Slice Random method failed for empty slice. Expected: 0, Got: %d", emptyRandomElement) 1319 } 1320 } 1321 1322 func TestSliceAddUniqueInPlace(t *testing.T) { 1323 // Test cases for Int 1324 testAddUniqueInPlace(t, g.SliceOf(1, 2, 3, 4, 5), []int{3, 4, 5, 6, 7}, []int{1, 2, 3, 4, 5, 6, 7}) 1325 1326 // Test cases for Float 1327 testAddUniqueInPlace( 1328 t, 1329 g.SliceOf(1.1, 2.2, 3.3, 4.4, 5.5), 1330 []float64{3.3, 4.4, 5.5, 6.6, 7.7}, 1331 []float64{1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7}, 1332 ) 1333 1334 // Test cases for String 1335 testAddUniqueInPlace( 1336 t, 1337 g.SliceOf("apple", "banana", "orange", "grape"), 1338 []string{"orange", "grape", "kiwi"}, 1339 []string{"apple", "banana", "orange", "grape", "kiwi"}, 1340 ) 1341 1342 // Add more test cases for other types as needed 1343 } 1344 1345 func testAddUniqueInPlace[T comparable](t *testing.T, sl g.Slice[T], elems, expected []T) { 1346 sl.AddUniqueInPlace(elems...) 1347 if !sl.Eq(g.SliceOf(expected...)) { 1348 t.Errorf("Slice AddUniqueInPlace method failed for type %T. Expected: %v, Got: %v", sl[0], expected, sl) 1349 } 1350 } 1351 1352 func TestSliceAsAny(t *testing.T) { 1353 // Test cases for Int 1354 testSliceAsAny(t, g.SliceOf(1, 2, 3), []any{1, 2, 3}) 1355 1356 // Test cases for Float 1357 testSliceAsAny(t, g.SliceOf(1.1, 2.2, 3.3), []any{1.1, 2.2, 3.3}) 1358 1359 // Test cases for String 1360 testSliceAsAny(t, g.SliceOf("apple", "banana", "orange"), []any{"apple", "banana", "orange"}) 1361 1362 // Add more test cases for other types as needed 1363 } 1364 1365 func testSliceAsAny[T any](t *testing.T, sl g.Slice[T], expected []any) { 1366 result := sl.AsAny() 1367 if !result.Eq(g.SliceOf(expected...)) { 1368 t.Errorf("Slice AsAny method failed for type %T. Expected: %v, Got: %v", sl[0], expected, result) 1369 } 1370 } 1371 1372 func TestSliceContainsBy(t *testing.T) { 1373 // Test case 1: Slice contains the element that satisfies the provided function 1374 sl1 := g.Slice[g.Int]{1, 2, 3, 4, 5} 1375 contains1 := sl1.ContainsBy(f.Eq(g.Int(3))) 1376 1377 if !contains1 { 1378 t.Errorf("Test 1: Expected true, got false") 1379 } 1380 1381 // Test case 2: Slice does not contain the element that satisfies the provided function 1382 sl2 := g.Slice[g.String]{"apple", "banana", "cherry"} 1383 contains2 := sl2.ContainsBy(f.Eq(g.String("orange"))) 1384 1385 if contains2 { 1386 t.Errorf("Test 2: Expected false, got true") 1387 } 1388 1389 // Test case 3: Slice contains the element that satisfies the provided function (using custom struct) 1390 type Person struct { 1391 Name string 1392 Age int 1393 } 1394 1395 sl3 := g.Slice[Person]{{Name: "Alice", Age: 30}, {Name: "Bob", Age: 25}, {Name: "Charlie", Age: 35}} 1396 1397 contains3 := sl3.ContainsBy(func(x Person) bool { return x.Name == "Bob" && x.Age == 25 }) 1398 if !contains3 { 1399 t.Errorf("Test 3: Expected true, got false") 1400 } 1401 } 1402 1403 func TestSliceEqNeBy(t *testing.T) { 1404 // Test case 1: Slices are equal using the equality function 1405 sl1 := g.Slice[g.Int]{1, 2, 3} 1406 sl2 := g.Slice[g.Int]{1, 2, 3} 1407 1408 eq1 := sl1.EqBy(sl2, func(x, y g.Int) bool { return x.Eq(y) }) 1409 1410 if !eq1 { 1411 t.Errorf("Test 1: Expected true, got false") 1412 } 1413 1414 // Test case 2: Slices are not equal using the equality function 1415 sl3 := g.Slice[g.String]{"apple", "banana", "cherry"} 1416 sl4 := g.Slice[g.String]{"apple", "orange", "cherry"} 1417 1418 eq2 := sl3.EqBy(sl4, func(x, y g.String) bool { return x.Eq(y) }) 1419 1420 if eq2 { 1421 t.Errorf("Test 2: Expected false, got true") 1422 } 1423 1424 // Test case 3: Slices are equal using the equality function (using custom struct) 1425 type Person struct { 1426 Name string 1427 Age int 1428 } 1429 1430 sl5 := g.Slice[Person]{{Name: "Alice", Age: 30}, {Name: "Bob", Age: 25}} 1431 sl6 := g.Slice[Person]{{Name: "Alice", Age: 30}, {Name: "Bob", Age: 25}} 1432 1433 eq3 := sl5.EqBy(sl6, func(x, y Person) bool { 1434 return x.Name == y.Name && x.Age == y.Age 1435 }) 1436 1437 if !eq3 { 1438 t.Errorf("Test 3: Expected true, got false") 1439 } 1440 1441 // Additional tests for NeBy 1442 1443 // Test case 4: Slices are not equal using the equality function 1444 sl7 := g.Slice[g.Int]{1, 2, 3} 1445 sl8 := g.Slice[g.Int]{1, 2, 4} 1446 1447 ne1 := sl7.NeBy(sl8, func(x, y g.Int) bool { return x.Eq(y) }) 1448 1449 if !ne1 { 1450 t.Errorf("Test 4: Expected true, got false") 1451 } 1452 1453 // Test case 5: Slices are equal using the equality function 1454 sl9 := g.Slice[g.String]{"apple", "banana", "cherry"} 1455 sl10 := g.Slice[g.String]{"apple", "banana", "cherry"} 1456 1457 ne2 := sl9.NeBy(sl10, func(x, y g.String) bool { return x.Eq(y) }) 1458 1459 if ne2 { 1460 t.Errorf("Test 5: Expected false, got true") 1461 } 1462 } 1463 1464 func TestSliceIndexBy(t *testing.T) { 1465 // Test case 1: Element satisfying the custom comparison function exists in the slice 1466 sl1 := g.Slice[int]{1, 2, 3, 4, 5} 1467 index1 := sl1.IndexBy(f.Eq(3)) 1468 1469 expectedIndex1 := g.Int(2) 1470 if index1 != expectedIndex1 { 1471 t.Errorf("Test 1: Expected index %d, got %d", expectedIndex1, index1) 1472 } 1473 1474 // Test case 2: Element satisfying the custom comparison function doesn't exist in the slice 1475 sl2 := g.Slice[string]{"apple", "banana", "cherry"} 1476 index2 := sl2.IndexBy(f.Eq("orange")) 1477 1478 expectedIndex2 := g.Int(-1) 1479 if index2 != expectedIndex2 { 1480 t.Errorf("Test 2: Expected index %d, got %d", expectedIndex2, index2) 1481 } 1482 1483 // Test case 3: Element satisfying the custom comparison function exists in the slice (using custom struct) 1484 type Person struct { 1485 Name string 1486 Age int 1487 } 1488 1489 sl3 := g.Slice[Person]{{Name: "Alice", Age: 30}, {Name: "Bob", Age: 25}} 1490 index3 := sl3.IndexBy(func(x Person) bool { return x.Name == "Bob" && x.Age == 25 }) 1491 1492 expectedIndex3 := g.Int(1) 1493 if index3 != expectedIndex3 { 1494 t.Errorf("Test 3: Expected index %d, got %d", expectedIndex3, index3) 1495 } 1496 } 1497 1498 func TestSliceSwap(t *testing.T) { 1499 // Define a slice to test 1500 s := g.Slice[int]{1, 2, 3, 4, 5} 1501 1502 // Test swapping two elements 1503 s.Swap(1, 3) 1504 expected := g.Slice[int]{1, 4, 3, 2, 5} 1505 if !s.Eq(expected) { 1506 t.Errorf("Swap failed: got %v, want %v", s, expected) 1507 } 1508 1509 // Test swapping two elements 1510 s.Swap(-1, 0) 1511 expected = g.Slice[int]{5, 4, 3, 2, 1} 1512 if !s.Eq(expected) { 1513 t.Errorf("Swap failed: got %v, want %v", s, expected) 1514 } 1515 } 1516 1517 func TestSliceMaxBy(t *testing.T) { 1518 // Test case 1: Maximum integer 1519 s := g.Slice[int]{3, 1, 4, 2, 5} 1520 maxInt := s.MaxBy(cmp.Cmp) 1521 expectedMaxInt := 5 1522 if maxInt != expectedMaxInt { 1523 t.Errorf("s.MaxBy(IntCompare) = %d; want %d", maxInt, expectedMaxInt) 1524 } 1525 } 1526 1527 func TestSliceMinBy(t *testing.T) { 1528 // Test case 1: Minimum integer 1529 s := g.Slice[int]{3, 1, 4, 2, 5} 1530 minInt := s.MinBy(cmp.Cmp) 1531 expectedMinInt := 1 1532 if minInt != expectedMinInt { 1533 t.Errorf("s.MinBy(IntCompare) = %d; want %d", minInt, expectedMinInt) 1534 } 1535 }