1 // Copyright 2021 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
17 var equalIntTests = []struct {
43 var equalFloatTests = []struct {
55 []float64{1, 2, math.NaN()},
56 []float64{1, 2, math.NaN()},
62 func TestEqual(t *testing.T) {
63 for _, test := range equalIntTests {
64 if got := Equal(test.s1, test.s2); got != test.want {
65 t.Errorf("Equal(%v, %v) = %t, want %t", test.s1, test.s2, got, test.want)
68 for _, test := range equalFloatTests {
69 if got := Equal(test.s1, test.s2); got != test.wantEqual {
70 t.Errorf("Equal(%v, %v) = %t, want %t", test.s1, test.s2, got, test.wantEqual)
75 // equal is simply ==.
76 func equal[T comparable](v1, v2 T) bool {
80 // equalNaN is like == except that all NaNs are equal.
81 func equalNaN[T comparable](v1, v2 T) bool {
82 isNaN := func(f T) bool { return f != f }
83 return v1 == v2 || (isNaN(v1) && isNaN(v2))
86 // offByOne returns true if integers v1 and v2 differ by 1.
87 func offByOne(v1, v2 int) bool {
88 return v1 == v2+1 || v1 == v2-1
91 func TestEqualFunc(t *testing.T) {
92 for _, test := range equalIntTests {
93 if got := EqualFunc(test.s1, test.s2, equal[int]); got != test.want {
94 t.Errorf("EqualFunc(%v, %v, equal[int]) = %t, want %t", test.s1, test.s2, got, test.want)
97 for _, test := range equalFloatTests {
98 if got := EqualFunc(test.s1, test.s2, equal[float64]); got != test.wantEqual {
99 t.Errorf("Equal(%v, %v, equal[float64]) = %t, want %t", test.s1, test.s2, got, test.wantEqual)
101 if got := EqualFunc(test.s1, test.s2, equalNaN[float64]); got != test.wantEqualNaN {
102 t.Errorf("Equal(%v, %v, equalNaN[float64]) = %t, want %t", test.s1, test.s2, got, test.wantEqualNaN)
108 if EqualFunc(s1, s1, offByOne) {
109 t.Errorf("EqualFunc(%v, %v, offByOne) = true, want false", s1, s1)
111 if !EqualFunc(s1, s2, offByOne) {
112 t.Errorf("EqualFunc(%v, %v, offByOne) = false, want true", s1, s2)
115 s3 := []string{"a", "b", "c"}
116 s4 := []string{"A", "B", "C"}
117 if !EqualFunc(s3, s4, strings.EqualFold) {
118 t.Errorf("EqualFunc(%v, %v, strings.EqualFold) = false, want true", s3, s4)
121 cmpIntString := func(v1 int, v2 string) bool {
122 return string(rune(v1)-1+'a') == v2
124 if !EqualFunc(s1, s3, cmpIntString) {
125 t.Errorf("EqualFunc(%v, %v, cmpIntString) = false, want true", s1, s3)
129 func BenchmarkEqualFunc_Large(b *testing.B) {
130 type Large [4 * 1024]byte
132 xs := make([]Large, 1024)
133 ys := make([]Large, 1024)
134 for i := 0; i < b.N; i++ {
135 _ = EqualFunc(xs, ys, func(x, y Large) bool { return x == y })
139 var compareIntTests = []struct {
190 []int{1, 2, 3, 8, 9},
195 var compareFloatTests = []struct {
210 []float64{math.NaN()},
211 []float64{math.NaN()},
215 []float64{1, 2, math.NaN()},
216 []float64{1, 2, math.NaN()},
220 []float64{1, math.NaN(), 3},
221 []float64{1, math.NaN(), 4},
225 []float64{1, math.NaN(), 3},
230 []float64{1, math.NaN(), 3},
231 []float64{1, 2, math.NaN()},
236 []float64{1, 2, math.NaN()},
241 []float64{1, math.NaN(), 3},
245 []float64{1, math.NaN(), 3, 4},
246 []float64{1, 2, math.NaN()},
251 func TestCompare(t *testing.T) {
252 intWant := func(want bool) string {
258 for _, test := range equalIntTests {
259 if got := Compare(test.s1, test.s2); (got == 0) != test.want {
260 t.Errorf("Compare(%v, %v) = %d, want %s", test.s1, test.s2, got, intWant(test.want))
263 for _, test := range equalFloatTests {
264 if got := Compare(test.s1, test.s2); (got == 0) != test.wantEqualNaN {
265 t.Errorf("Compare(%v, %v) = %d, want %s", test.s1, test.s2, got, intWant(test.wantEqualNaN))
269 for _, test := range compareIntTests {
270 if got := Compare(test.s1, test.s2); got != test.want {
271 t.Errorf("Compare(%v, %v) = %d, want %d", test.s1, test.s2, got, test.want)
274 for _, test := range compareFloatTests {
275 if got := Compare(test.s1, test.s2); got != test.want {
276 t.Errorf("Compare(%v, %v) = %d, want %d", test.s1, test.s2, got, test.want)
281 func equalToCmp[T comparable](eq func(T, T) bool) func(T, T) int {
282 return func(v1, v2 T) int {
290 func TestCompareFunc(t *testing.T) {
291 intWant := func(want bool) string {
297 for _, test := range equalIntTests {
298 if got := CompareFunc(test.s1, test.s2, equalToCmp(equal[int])); (got == 0) != test.want {
299 t.Errorf("CompareFunc(%v, %v, equalToCmp(equal[int])) = %d, want %s", test.s1, test.s2, got, intWant(test.want))
302 for _, test := range equalFloatTests {
303 if got := CompareFunc(test.s1, test.s2, equalToCmp(equal[float64])); (got == 0) != test.wantEqual {
304 t.Errorf("CompareFunc(%v, %v, equalToCmp(equal[float64])) = %d, want %s", test.s1, test.s2, got, intWant(test.wantEqual))
308 for _, test := range compareIntTests {
309 if got := CompareFunc(test.s1, test.s2, cmp.Compare[int]); got != test.want {
310 t.Errorf("CompareFunc(%v, %v, cmp[int]) = %d, want %d", test.s1, test.s2, got, test.want)
313 for _, test := range compareFloatTests {
314 if got := CompareFunc(test.s1, test.s2, cmp.Compare[float64]); got != test.want {
315 t.Errorf("CompareFunc(%v, %v, cmp[float64]) = %d, want %d", test.s1, test.s2, got, test.want)
321 if got := CompareFunc(s1, s2, equalToCmp(offByOne)); got != 0 {
322 t.Errorf("CompareFunc(%v, %v, offByOne) = %d, want 0", s1, s2, got)
325 s3 := []string{"a", "b", "c"}
326 s4 := []string{"A", "B", "C"}
327 if got := CompareFunc(s3, s4, strings.Compare); got != 1 {
328 t.Errorf("CompareFunc(%v, %v, strings.Compare) = %d, want 1", s3, s4, got)
331 compareLower := func(v1, v2 string) int {
332 return strings.Compare(strings.ToLower(v1), strings.ToLower(v2))
334 if got := CompareFunc(s3, s4, compareLower); got != 0 {
335 t.Errorf("CompareFunc(%v, %v, compareLower) = %d, want 0", s3, s4, got)
338 cmpIntString := func(v1 int, v2 string) int {
339 return strings.Compare(string(rune(v1)-1+'a'), v2)
341 if got := CompareFunc(s1, s3, cmpIntString); got != 0 {
342 t.Errorf("CompareFunc(%v, %v, cmpIntString) = %d, want 0", s1, s3, got)
346 var indexTests = []struct {
378 func TestIndex(t *testing.T) {
379 for _, test := range indexTests {
380 if got := Index(test.s, test.v); got != test.want {
381 t.Errorf("Index(%v, %v) = %d, want %d", test.s, test.v, got, test.want)
386 func equalToIndex[T any](f func(T, T) bool, v1 T) func(T) bool {
387 return func(v2 T) bool {
392 func BenchmarkIndex_Large(b *testing.B) {
393 type Large [4 * 1024]byte
395 ss := make([]Large, 1024)
396 for i := 0; i < b.N; i++ {
397 _ = Index(ss, Large{1})
401 func TestIndexFunc(t *testing.T) {
402 for _, test := range indexTests {
403 if got := IndexFunc(test.s, equalToIndex(equal[int], test.v)); got != test.want {
404 t.Errorf("IndexFunc(%v, equalToIndex(equal[int], %v)) = %d, want %d", test.s, test.v, got, test.want)
408 s1 := []string{"hi", "HI"}
409 if got := IndexFunc(s1, equalToIndex(equal[string], "HI")); got != 1 {
410 t.Errorf("IndexFunc(%v, equalToIndex(equal[string], %q)) = %d, want %d", s1, "HI", got, 1)
412 if got := IndexFunc(s1, equalToIndex(strings.EqualFold, "HI")); got != 0 {
413 t.Errorf("IndexFunc(%v, equalToIndex(strings.EqualFold, %q)) = %d, want %d", s1, "HI", got, 0)
417 func BenchmarkIndexFunc_Large(b *testing.B) {
418 type Large [4 * 1024]byte
420 ss := make([]Large, 1024)
421 for i := 0; i < b.N; i++ {
422 _ = IndexFunc(ss, func(e Large) bool {
428 func TestContains(t *testing.T) {
429 for _, test := range indexTests {
430 if got := Contains(test.s, test.v); got != (test.want != -1) {
431 t.Errorf("Contains(%v, %v) = %t, want %t", test.s, test.v, got, test.want != -1)
436 func TestContainsFunc(t *testing.T) {
437 for _, test := range indexTests {
438 if got := ContainsFunc(test.s, equalToIndex(equal[int], test.v)); got != (test.want != -1) {
439 t.Errorf("ContainsFunc(%v, equalToIndex(equal[int], %v)) = %t, want %t", test.s, test.v, got, test.want != -1)
443 s1 := []string{"hi", "HI"}
444 if got := ContainsFunc(s1, equalToIndex(equal[string], "HI")); got != true {
445 t.Errorf("ContainsFunc(%v, equalToContains(equal[string], %q)) = %t, want %t", s1, "HI", got, true)
447 if got := ContainsFunc(s1, equalToIndex(equal[string], "hI")); got != false {
448 t.Errorf("ContainsFunc(%v, equalToContains(strings.EqualFold, %q)) = %t, want %t", s1, "hI", got, false)
450 if got := ContainsFunc(s1, equalToIndex(strings.EqualFold, "hI")); got != true {
451 t.Errorf("ContainsFunc(%v, equalToContains(strings.EqualFold, %q)) = %t, want %t", s1, "hI", got, true)
455 var insertTests = []struct {
483 []int{1, 2, 4, 5, 3},
487 func TestInsert(t *testing.T) {
489 if got := Insert(s, 0); !Equal(got, s) {
490 t.Errorf("Insert(%v, 0) = %v, want %v", s, got, s)
492 for _, test := range insertTests {
493 copy := Clone(test.s)
494 if got := Insert(copy, test.i, test.add...); !Equal(got, test.want) {
495 t.Errorf("Insert(%v, %d, %v...) = %v, want %v", test.s, test.i, test.add, got, test.want)
499 if !testenv.OptimizationOff() && !race.Enabled {
500 // Allocations should be amortized.
502 n := testing.AllocsPerRun(10, func() {
504 for i := 0; i < count; i++ {
509 t.Errorf("too many allocations inserting %d elements: got %v, want less than %d", count, n, count/2)
514 func TestInsertOverlap(t *testing.T) {
517 want := make([]int, 2*N)
518 for n := 0; n <= N; n++ { // length
519 for i := 0; i <= n; i++ { // insertion point
520 for x := 0; x <= N; x++ { // start of inserted data
521 for y := x; y <= N; y++ { // end of inserted data
522 for k := 0; k < N; k++ {
526 want = append(want, a[:i]...)
527 want = append(want, a[x:y]...)
528 want = append(want, a[i:n]...)
529 got := Insert(a[:n], i, a[x:y]...)
530 if !Equal(got, want) {
531 t.Errorf("Insert with overlap failed n=%d i=%d x=%d y=%d, got %v want %v", n, i, x, y, got, want)
539 func TestInsertPanics(t *testing.T) {
541 for _, test := range []struct {
547 // There are no values.
548 {"with negative index", a[:1:1], -1, nil},
549 {"with out-of-bounds index and > cap", a[:1:1], 2, nil},
550 {"with out-of-bounds index and = cap", a[:1:2], 2, nil},
551 {"with out-of-bounds index and < cap", a[:1:3], 2, nil},
553 if !panics(func() { Insert(test.s, test.i, test.v...) }) {
554 t.Errorf("Insert %s: got no panic, want panic", test.name)
559 var deleteTests = []struct {
596 func TestDelete(t *testing.T) {
597 for _, test := range deleteTests {
598 copy := Clone(test.s)
599 if got := Delete(copy, test.i, test.j); !Equal(got, test.want) {
600 t.Errorf("Delete(%v, %d, %d) = %v, want %v", test.s, test.i, test.j, got, test.want)
605 var deleteFuncTests = []struct {
612 func(int) bool { return true },
617 func(int) bool { return true },
622 func(int) bool { return false },
627 func(i int) bool { return i > 2 },
632 func(i int) bool { return i < 2 },
637 func(i int) bool { return i >= 10 },
642 func TestDeleteFunc(t *testing.T) {
643 for i, test := range deleteFuncTests {
644 copy := Clone(test.s)
645 if got := DeleteFunc(copy, test.fn); !Equal(got, test.want) {
646 t.Errorf("DeleteFunc case %d: got %v, want %v", i, got, test.want)
651 func panics(f func()) (b bool) {
653 if x := recover(); x != nil {
661 func TestDeletePanics(t *testing.T) {
662 s := []int{0, 1, 2, 3, 4}
664 _ = s[0:4] // this is a valid slice of s
666 for _, test := range []struct {
671 {"with negative first index", []int{42}, -2, 1},
672 {"with negative second index", []int{42}, 1, -1},
673 {"with out-of-bounds first index", []int{42}, 2, 3},
674 {"with out-of-bounds second index", []int{42}, 0, 2},
675 {"with invalid i>j", []int{42}, 1, 0},
676 {"s[i:j] is valid and j > len(s)", s, 0, 4},
678 if !panics(func() { Delete(test.s, test.i, test.j) }) {
679 t.Errorf("Delete %s: got no panic, want panic", test.name)
684 func TestDeleteClearTail(t *testing.T) {
685 mem := []*int{new(int), new(int), new(int), new(int), new(int), new(int)}
686 s := mem[0:5] // there is 1 element beyond len(s), within cap(s)
690 if mem[3] != nil || mem[4] != nil {
691 // Check that potential memory leak is avoided
692 t.Errorf("Delete: want nil discarded elements, got %v, %v", mem[3], mem[4])
695 t.Errorf("Delete: want unchanged elements beyond original len, got nil")
699 func TestDeleteFuncClearTail(t *testing.T) {
700 mem := []*int{new(int), new(int), new(int), new(int), new(int), new(int)}
701 *mem[2], *mem[3] = 42, 42
702 s := mem[0:5] // there is 1 element beyond len(s), within cap(s)
704 s = DeleteFunc(s, func(i *int) bool {
705 return i != nil && *i == 42
708 if mem[3] != nil || mem[4] != nil {
709 // Check that potential memory leak is avoided
710 t.Errorf("DeleteFunc: want nil discarded elements, got %v, %v", mem[3], mem[4])
713 t.Errorf("DeleteFunc: want unchanged elements beyond original len, got nil")
717 func TestClone(t *testing.T) {
721 t.Errorf("Clone(%v) = %v, want %v", s1, s2, s1)
724 want := []int{1, 2, 3}
725 if !Equal(s2, want) {
726 t.Errorf("Clone(%v) changed unexpectedly to %v", want, s2)
728 if got := Clone([]int(nil)); got != nil {
729 t.Errorf("Clone(nil) = %#v, want nil", got)
731 if got := Clone(s1[:0]); got == nil || len(got) != 0 {
732 t.Errorf("Clone(%v) = %#v, want %#v", s1[:0], got, s1[:0])
736 var compactTests = []struct {
768 []int{1, 2, 2, 3, 3, 4},
773 func TestCompact(t *testing.T) {
774 for _, test := range compactTests {
775 copy := Clone(test.s)
776 if got := Compact(copy); !Equal(got, test.want) {
777 t.Errorf("Compact(%v) = %v, want %v", test.s, got, test.want)
782 func BenchmarkCompact(b *testing.B) {
783 for _, c := range compactTests {
784 b.Run(c.name, func(b *testing.B) {
785 ss := make([]int, 0, 64)
786 for k := 0; k < b.N; k++ {
788 ss = append(ss, c.s...)
795 func BenchmarkCompact_Large(b *testing.B) {
796 type Large [4 * 1024]byte
798 ss := make([]Large, 1024)
799 for i := 0; i < b.N; i++ {
804 func TestCompactFunc(t *testing.T) {
805 for _, test := range compactTests {
806 copy := Clone(test.s)
807 if got := CompactFunc(copy, equal[int]); !Equal(got, test.want) {
808 t.Errorf("CompactFunc(%v, equal[int]) = %v, want %v", test.s, got, test.want)
812 s1 := []string{"a", "a", "A", "B", "b"}
814 want := []string{"a", "B"}
815 if got := CompactFunc(copy, strings.EqualFold); !Equal(got, want) {
816 t.Errorf("CompactFunc(%v, strings.EqualFold) = %v, want %v", s1, got, want)
820 func TestCompactClearTail(t *testing.T) {
821 one, two, three, four := 1, 2, 3, 4
822 mem := []*int{&one, &one, &two, &two, &three, &four}
823 s := mem[0:5] // there is 1 element beyond len(s), within cap(s)
828 if want := []*int{&one, &two, &three}; !Equal(s, want) {
829 t.Errorf("Compact(%v) = %v, want %v", copy, s, want)
832 if mem[3] != nil || mem[4] != nil {
833 // Check that potential memory leak is avoided
834 t.Errorf("Compact: want nil discarded elements, got %v, %v", mem[3], mem[4])
837 t.Errorf("Compact: want unchanged element beyond original len, got %v", mem[5])
841 func TestCompactFuncClearTail(t *testing.T) {
842 a, b, c, d, e, f := 1, 1, 2, 2, 3, 4
843 mem := []*int{&a, &b, &c, &d, &e, &f}
844 s := mem[0:5] // there is 1 element beyond len(s), within cap(s)
847 s = CompactFunc(s, func(x, y *int) bool {
848 if x == nil || y == nil {
854 if want := []*int{&a, &c, &e}; !Equal(s, want) {
855 t.Errorf("CompactFunc(%v) = %v, want %v", copy, s, want)
858 if mem[3] != nil || mem[4] != nil {
859 // Check that potential memory leak is avoided
860 t.Errorf("CompactFunc: want nil discarded elements, got %v, %v", mem[3], mem[4])
863 t.Errorf("CompactFunc: want unchanged elements beyond original len, got %v", mem[5])
867 func BenchmarkCompactFunc_Large(b *testing.B) {
868 type Large [4 * 1024]byte
870 ss := make([]Large, 1024)
871 for i := 0; i < b.N; i++ {
872 _ = CompactFunc(ss, func(a, b Large) bool { return a == b })
876 func TestGrow(t *testing.T) {
880 s2 := Grow(copy, 1000)
882 t.Errorf("Grow(%v) = %v, want %v", s1, s2, s1)
884 if cap(s2) < 1000+len(s1) {
885 t.Errorf("after Grow(%v) cap = %d, want >= %d", s1, cap(s2), 1000+len(s1))
888 // Test mutation of elements between length and capacity.
890 s3 := Grow(copy[:1], 2)[:3]
892 t.Errorf("Grow should not mutate elements between length and capacity")
894 s3 = Grow(copy[:1], 1000)[:3]
896 t.Errorf("Grow should not mutate elements between length and capacity")
899 // Test number of allocations.
900 if n := testing.AllocsPerRun(100, func() { Grow(s2, cap(s2)-len(s2)) }); n != 0 {
901 t.Errorf("Grow should not allocate when given sufficient capacity; allocated %v times", n)
903 if n := testing.AllocsPerRun(100, func() { Grow(s2, cap(s2)-len(s2)+1) }); n != 1 {
905 if race.Enabled || testenv.OptimizationOff() {
906 errorf = t.Logf // this allocates multiple times in race detector mode
908 errorf("Grow should allocate once when given insufficient capacity; allocated %v times", n)
911 // Test for negative growth sizes.
914 defer func() { gotPanic = recover() != nil }()
918 t.Errorf("Grow(-1) did not panic; expected a panic")
922 func TestClip(t *testing.T) {
923 s1 := []int{1, 2, 3, 4, 5, 6}[:3]
926 t.Errorf("len(%v) = %d, want 3", s1, len(s1))
929 t.Errorf("cap(%v[:3]) = %d, want >= 6", orig, cap(s1))
933 t.Errorf("Clip(%v) = %v, want %v", s1, s2, s1)
936 t.Errorf("cap(Clip(%v)) = %d, want 3", orig, cap(s2))
940 func TestReverse(t *testing.T) {
941 even := []int{3, 1, 4, 1, 5, 9} // len = 6
943 if want := []int{9, 5, 1, 4, 1, 3}; !Equal(even, want) {
944 t.Errorf("Reverse(even) = %v, want %v", even, want)
947 odd := []int{3, 1, 4, 1, 5, 9, 2} // len = 7
949 if want := []int{2, 9, 5, 1, 4, 1, 3}; !Equal(odd, want) {
950 t.Errorf("Reverse(odd) = %v, want %v", odd, want)
953 words := strings.Fields("one two three")
955 if want := strings.Fields("three two one"); !Equal(words, want) {
956 t.Errorf("Reverse(words) = %v, want %v", words, want)
959 singleton := []string{"one"}
961 if want := []string{"one"}; !Equal(singleton, want) {
962 t.Errorf("Reverse(singeleton) = %v, want %v", singleton, want)
965 Reverse[[]string](nil)
968 // naiveReplace is a baseline implementation to the Replace function.
969 func naiveReplace[S ~[]E, E any](s S, i, j int, v ...E) S {
971 s = Insert(s, i, v...)
975 func TestReplace(t *testing.T) {
976 for _, test := range []struct {
980 {}, // all zero value
982 s: []int{1, 2, 3, 4},
988 s: []int{1, 2, 3, 4},
989 v: []int{5, 6, 7, 8},
995 s := make([]int, 3, 20)
1001 v: []int{3, 4, 5, 6, 7},
1006 ss, vv := Clone(test.s), Clone(test.v)
1007 want := naiveReplace(ss, test.i, test.j, vv...)
1008 got := Replace(test.s, test.i, test.j, test.v...)
1009 if !Equal(got, want) {
1010 t.Errorf("Replace(%v, %v, %v, %v) = %v, want %v", test.s, test.i, test.j, test.v, got, want)
1015 func TestReplacePanics(t *testing.T) {
1016 s := []int{0, 1, 2, 3, 4}
1018 _ = s[0:4] // this is a valid slice of s
1020 for _, test := range []struct {
1025 {"indexes out of order", []int{1, 2}, []int{3}, 2, 1},
1026 {"large index", []int{1, 2}, []int{3}, 1, 10},
1027 {"negative index", []int{1, 2}, []int{3}, -1, 2},
1028 {"s[i:j] is valid and j > len(s)", s, nil, 0, 4},
1030 ss, vv := Clone(test.s), Clone(test.v)
1031 if !panics(func() { Replace(ss, test.i, test.j, vv...) }) {
1032 t.Errorf("Replace %s: should have panicked", test.name)
1037 func TestReplaceGrow(t *testing.T) {
1038 // When Replace needs to allocate a new slice, we want the original slice
1039 // to not be changed.
1040 a, b, c, d, e, f := 1, 2, 3, 4, 5, 6
1041 mem := []*int{&a, &b, &c, &d, &e, &f}
1042 memcopy := Clone(mem)
1043 s := mem[0:5] // there is 1 element beyond len(s), within cap(s)
1047 // The new elements don't fit within cap(s), so Replace will allocate.
1049 s = Replace(s, 1, 3, &z, &z, &z, &z)
1051 if want := []*int{&a, &z, &z, &z, &z, &d, &e}; !Equal(s, want) {
1052 t.Errorf("Replace(%v, 1, 3, %v, %v, %v, %v) = %v, want %v", copy, &z, &z, &z, &z, s, want)
1055 if !Equal(original, copy) {
1056 t.Errorf("original slice has changed, got %v, want %v", original, copy)
1059 if !Equal(mem, memcopy) {
1060 // Changing the original tail s[len(s):cap(s)] is unwanted
1061 t.Errorf("original backing memory has changed, got %v, want %v", mem, memcopy)
1065 func TestReplaceClearTail(t *testing.T) {
1066 a, b, c, d, e, f := 1, 2, 3, 4, 5, 6
1067 mem := []*int{&a, &b, &c, &d, &e, &f}
1068 s := mem[0:5] // there is 1 element beyond len(s), within cap(s)
1072 s = Replace(s, 1, 4, &y, &z)
1074 if want := []*int{&a, &y, &z, &e}; !Equal(s, want) {
1075 t.Errorf("Replace(%v) = %v, want %v", copy, s, want)
1079 // Check that potential memory leak is avoided
1080 t.Errorf("Replace: want nil discarded element, got %v", mem[4])
1083 t.Errorf("Replace: want unchanged elements beyond original len, got %v", mem[5])
1087 func TestReplaceOverlap(t *testing.T) {
1090 want := make([]int, 2*N)
1091 for n := 0; n <= N; n++ { // length
1092 for i := 0; i <= n; i++ { // insertion point 1
1093 for j := i; j <= n; j++ { // insertion point 2
1094 for x := 0; x <= N; x++ { // start of inserted data
1095 for y := x; y <= N; y++ { // end of inserted data
1096 for k := 0; k < N; k++ {
1100 want = append(want, a[:i]...)
1101 want = append(want, a[x:y]...)
1102 want = append(want, a[j:n]...)
1103 got := Replace(a[:n], i, j, a[x:y]...)
1104 if !Equal(got, want) {
1105 t.Errorf("Insert with overlap failed n=%d i=%d j=%d x=%d y=%d, got %v want %v", n, i, j, x, y, got, want)
1114 func BenchmarkReplace(b *testing.B) {
1123 return make([]int, 100)
1126 return make([]int, 20)
1134 return make([]int, 100)
1137 return make([]int, 20)
1144 for _, c := range cases {
1145 b.Run("naive-"+c.name, func(b *testing.B) {
1146 for k := 0; k < b.N; k++ {
1149 _ = naiveReplace(s, c.i, c.j, v...)
1152 b.Run("optimized-"+c.name, func(b *testing.B) {
1153 for k := 0; k < b.N; k++ {
1156 _ = Replace(s, c.i, c.j, v...)
1163 func TestInsertGrowthRate(t *testing.T) {
1164 b := make([]byte, 1)
1168 for i := 0; i < N; i++ {
1169 b = Insert(b, len(b)-1, 0)
1170 if cap(b) > maxCap {
1175 want := int(math.Log(N) / math.Log(1.25)) // 1.25 == growth rate for large slices
1177 t.Errorf("too many grows. got:%d want:%d", nGrow, want)
1181 func TestReplaceGrowthRate(t *testing.T) {
1182 b := make([]byte, 2)
1186 for i := 0; i < N; i++ {
1187 b = Replace(b, len(b)-2, len(b)-1, 0, 0)
1188 if cap(b) > maxCap {
1193 want := int(math.Log(N) / math.Log(1.25)) // 1.25 == growth rate for large slices
1195 t.Errorf("too many grows. got:%d want:%d", nGrow, want)
1199 func apply[T any](v T, f func(T)) {
1203 // Test type inference with a named slice type.
1204 func TestInference(t *testing.T) {
1205 s1 := []int{1, 2, 3}
1207 if want := []int{3, 2, 1}; !Equal(s1, want) {
1208 t.Errorf("Reverse(%v) = %v, want %v", []int{1, 2, 3}, s1, want)
1214 if want := (S{6, 5, 4}); !Equal(s2, want) {
1215 t.Errorf("Reverse(%v) = %v, want %v", S{4, 5, 6}, s2, want)
1219 func TestConcat(t *testing.T) {
1233 s: [][]int{{1}, {2}},
1237 s: [][]int{{1}, nil, {2}},
1241 for _, tc := range cases {
1242 got := Concat(tc.s...)
1243 if !Equal(tc.want, got) {
1244 t.Errorf("Concat(%v) = %v, want %v", tc.s, got, tc.want)
1247 allocs := testing.AllocsPerRun(5, func() {
1248 sink = Concat(tc.s...)
1253 if testenv.OptimizationOff() || race.Enabled {
1256 errorf("Concat(%v) allocated %v times; want 1", tc.s, allocs)
1261 func TestConcat_too_large(t *testing.T) {
1262 // Use zero length element to minimize memory in testing
1269 lengths: []int{0, 0},
1273 lengths: []int{math.MaxInt, 0},
1277 lengths: []int{0, math.MaxInt},
1281 lengths: []int{math.MaxInt - 1, 1},
1285 lengths: []int{math.MaxInt - 1, 1, 1},
1289 lengths: []int{math.MaxInt, 1},
1293 lengths: []int{math.MaxInt, math.MaxInt},
1297 for _, tc := range cases {
1299 ss := make([][]void, 0, len(tc.lengths))
1300 for _, l := range tc.lengths {
1301 s := make([]void, l)
1310 if didPanic := r != nil; didPanic != tc.shouldPanic {
1311 t.Errorf("slices.Concat(lens(%v)) got panic == %v",
1312 tc.lengths, didPanic)