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.
15 var equalIntTests = []struct {
41 var equalFloatTests = []struct {
53 []float64{1, 2, math.NaN()},
54 []float64{1, 2, math.NaN()},
60 func TestEqual(t *testing.T) {
61 for _, test := range equalIntTests {
62 if got := Equal(test.s1, test.s2); got != test.want {
63 t.Errorf("Equal(%v, %v) = %t, want %t", test.s1, test.s2, got, test.want)
66 for _, test := range equalFloatTests {
67 if got := Equal(test.s1, test.s2); got != test.wantEqual {
68 t.Errorf("Equal(%v, %v) = %t, want %t", test.s1, test.s2, got, test.wantEqual)
73 // equal is simply ==.
74 func equal[T comparable](v1, v2 T) bool {
78 // equalNaN is like == except that all NaNs are equal.
79 func equalNaN[T comparable](v1, v2 T) bool {
80 isNaN := func(f T) bool { return f != f }
81 return v1 == v2 || (isNaN(v1) && isNaN(v2))
84 // offByOne returns true if integers v1 and v2 differ by 1.
85 func offByOne(v1, v2 int) bool {
86 return v1 == v2+1 || v1 == v2-1
89 func TestEqualFunc(t *testing.T) {
90 for _, test := range equalIntTests {
91 if got := EqualFunc(test.s1, test.s2, equal[int]); got != test.want {
92 t.Errorf("EqualFunc(%v, %v, equal[int]) = %t, want %t", test.s1, test.s2, got, test.want)
95 for _, test := range equalFloatTests {
96 if got := EqualFunc(test.s1, test.s2, equal[float64]); got != test.wantEqual {
97 t.Errorf("Equal(%v, %v, equal[float64]) = %t, want %t", test.s1, test.s2, got, test.wantEqual)
99 if got := EqualFunc(test.s1, test.s2, equalNaN[float64]); got != test.wantEqualNaN {
100 t.Errorf("Equal(%v, %v, equalNaN[float64]) = %t, want %t", test.s1, test.s2, got, test.wantEqualNaN)
106 if EqualFunc(s1, s1, offByOne) {
107 t.Errorf("EqualFunc(%v, %v, offByOne) = true, want false", s1, s1)
109 if !EqualFunc(s1, s2, offByOne) {
110 t.Errorf("EqualFunc(%v, %v, offByOne) = false, want true", s1, s2)
113 s3 := []string{"a", "b", "c"}
114 s4 := []string{"A", "B", "C"}
115 if !EqualFunc(s3, s4, strings.EqualFold) {
116 t.Errorf("EqualFunc(%v, %v, strings.EqualFold) = false, want true", s3, s4)
119 cmpIntString := func(v1 int, v2 string) bool {
120 return string(rune(v1)-1+'a') == v2
122 if !EqualFunc(s1, s3, cmpIntString) {
123 t.Errorf("EqualFunc(%v, %v, cmpIntString) = false, want true", s1, s3)
127 func BenchmarkEqualFunc_Large(b *testing.B) {
128 type Large [4 * 1024]byte
130 xs := make([]Large, 1024)
131 ys := make([]Large, 1024)
132 for i := 0; i < b.N; i++ {
133 _ = EqualFunc(xs, ys, func(x, y Large) bool { return x == y })
137 var indexTests = []struct {
169 func TestIndex(t *testing.T) {
170 for _, test := range indexTests {
171 if got := Index(test.s, test.v); got != test.want {
172 t.Errorf("Index(%v, %v) = %d, want %d", test.s, test.v, got, test.want)
177 func equalToIndex[T any](f func(T, T) bool, v1 T) func(T) bool {
178 return func(v2 T) bool {
183 func BenchmarkIndex_Large(b *testing.B) {
184 type Large [4 * 1024]byte
186 ss := make([]Large, 1024)
187 for i := 0; i < b.N; i++ {
188 _ = Index(ss, Large{1})
192 func TestIndexFunc(t *testing.T) {
193 for _, test := range indexTests {
194 if got := IndexFunc(test.s, equalToIndex(equal[int], test.v)); got != test.want {
195 t.Errorf("IndexFunc(%v, equalToIndex(equal[int], %v)) = %d, want %d", test.s, test.v, got, test.want)
199 s1 := []string{"hi", "HI"}
200 if got := IndexFunc(s1, equalToIndex(equal[string], "HI")); got != 1 {
201 t.Errorf("IndexFunc(%v, equalToIndex(equal[string], %q)) = %d, want %d", s1, "HI", got, 1)
203 if got := IndexFunc(s1, equalToIndex(strings.EqualFold, "HI")); got != 0 {
204 t.Errorf("IndexFunc(%v, equalToIndex(strings.EqualFold, %q)) = %d, want %d", s1, "HI", got, 0)
208 func BenchmarkIndexFunc_Large(b *testing.B) {
209 type Large [4 * 1024]byte
211 ss := make([]Large, 1024)
212 for i := 0; i < b.N; i++ {
213 _ = IndexFunc(ss, func(e Large) bool {
219 func TestContains(t *testing.T) {
220 for _, test := range indexTests {
221 if got := Contains(test.s, test.v); got != (test.want != -1) {
222 t.Errorf("Contains(%v, %v) = %t, want %t", test.s, test.v, got, test.want != -1)
227 func TestContainsFunc(t *testing.T) {
228 for _, test := range indexTests {
229 if got := ContainsFunc(test.s, equalToIndex(equal[int], test.v)); got != (test.want != -1) {
230 t.Errorf("ContainsFunc(%v, equalToIndex(equal[int], %v)) = %t, want %t", test.s, test.v, got, test.want != -1)
234 s1 := []string{"hi", "HI"}
235 if got := ContainsFunc(s1, equalToIndex(equal[string], "HI")); got != true {
236 t.Errorf("ContainsFunc(%v, equalToContains(equal[string], %q)) = %t, want %t", s1, "HI", got, true)
238 if got := ContainsFunc(s1, equalToIndex(equal[string], "hI")); got != false {
239 t.Errorf("ContainsFunc(%v, equalToContains(strings.EqualFold, %q)) = %t, want %t", s1, "hI", got, false)
241 if got := ContainsFunc(s1, equalToIndex(strings.EqualFold, "hI")); got != true {
242 t.Errorf("ContainsFunc(%v, equalToContains(strings.EqualFold, %q)) = %t, want %t", s1, "hI", got, true)
246 var insertTests = []struct {
274 []int{1, 2, 4, 5, 3},
278 func TestInsert(t *testing.T) {
280 if got := Insert(s, 0); !Equal(got, s) {
281 t.Errorf("Insert(%v, 0) = %v, want %v", s, got, s)
283 for _, test := range insertTests {
284 copy := Clone(test.s)
285 if got := Insert(copy, test.i, test.add...); !Equal(got, test.want) {
286 t.Errorf("Insert(%v, %d, %v...) = %v, want %v", test.s, test.i, test.add, got, test.want)
290 if !testenv.OptimizationOff() && !race.Enabled {
291 // Allocations should be amortized.
293 n := testing.AllocsPerRun(10, func() {
295 for i := 0; i < count; i++ {
300 t.Errorf("too many allocations inserting %d elements: got %v, want less than %d", count, n, count/2)
305 func TestInsertOverlap(t *testing.T) {
308 want := make([]int, 2*N)
309 for n := 0; n <= N; n++ { // length
310 for i := 0; i <= n; i++ { // insertion point
311 for x := 0; x <= N; x++ { // start of inserted data
312 for y := x; y <= N; y++ { // end of inserted data
313 for k := 0; k < N; k++ {
317 want = append(want, a[:i]...)
318 want = append(want, a[x:y]...)
319 want = append(want, a[i:n]...)
320 got := Insert(a[:n], i, a[x:y]...)
321 if !Equal(got, want) {
322 t.Errorf("Insert with overlap failed n=%d i=%d x=%d y=%d, got %v want %v", n, i, x, y, got, want)
330 var deleteTests = []struct {
367 func TestDelete(t *testing.T) {
368 for _, test := range deleteTests {
369 copy := Clone(test.s)
370 if got := Delete(copy, test.i, test.j); !Equal(got, test.want) {
371 t.Errorf("Delete(%v, %d, %d) = %v, want %v", test.s, test.i, test.j, got, test.want)
376 var deleteFuncTests = []struct {
383 func(int) bool { return true },
388 func(int) bool { return true },
393 func(int) bool { return false },
398 func(i int) bool { return i > 2 },
403 func(i int) bool { return i < 2 },
408 func(i int) bool { return i >= 10 },
413 func TestDeleteFunc(t *testing.T) {
414 for i, test := range deleteFuncTests {
415 copy := Clone(test.s)
416 if got := DeleteFunc(copy, test.fn); !Equal(got, test.want) {
417 t.Errorf("DeleteFunc case %d: got %v, want %v", i, got, test.want)
422 func panics(f func()) (b bool) {
424 if x := recover(); x != nil {
432 func TestDeletePanics(t *testing.T) {
433 for _, test := range []struct {
438 {"with negative first index", []int{42}, -2, 1},
439 {"with negative second index", []int{42}, 1, -1},
440 {"with out-of-bounds first index", []int{42}, 2, 3},
441 {"with out-of-bounds second index", []int{42}, 0, 2},
442 {"with invalid i>j", []int{42}, 1, 0},
444 if !panics(func() { Delete(test.s, test.i, test.j) }) {
445 t.Errorf("Delete %s: got no panic, want panic", test.name)
450 func TestClone(t *testing.T) {
454 t.Errorf("Clone(%v) = %v, want %v", s1, s2, s1)
457 want := []int{1, 2, 3}
458 if !Equal(s2, want) {
459 t.Errorf("Clone(%v) changed unexpectedly to %v", want, s2)
461 if got := Clone([]int(nil)); got != nil {
462 t.Errorf("Clone(nil) = %#v, want nil", got)
464 if got := Clone(s1[:0]); got == nil || len(got) != 0 {
465 t.Errorf("Clone(%v) = %#v, want %#v", s1[:0], got, s1[:0])
469 var compactTests = []struct {
501 []int{1, 2, 2, 3, 3, 4},
506 func TestCompact(t *testing.T) {
507 for _, test := range compactTests {
508 copy := Clone(test.s)
509 if got := Compact(copy); !Equal(got, test.want) {
510 t.Errorf("Compact(%v) = %v, want %v", test.s, got, test.want)
515 func BenchmarkCompact(b *testing.B) {
516 for _, c := range compactTests {
517 b.Run(c.name, func(b *testing.B) {
518 ss := make([]int, 0, 64)
519 for k := 0; k < b.N; k++ {
521 ss = append(ss, c.s...)
528 func BenchmarkCompact_Large(b *testing.B) {
529 type Large [4 * 1024]byte
531 ss := make([]Large, 1024)
532 for i := 0; i < b.N; i++ {
537 func TestCompactFunc(t *testing.T) {
538 for _, test := range compactTests {
539 copy := Clone(test.s)
540 if got := CompactFunc(copy, equal[int]); !Equal(got, test.want) {
541 t.Errorf("CompactFunc(%v, equal[int]) = %v, want %v", test.s, got, test.want)
545 s1 := []string{"a", "a", "A", "B", "b"}
547 want := []string{"a", "B"}
548 if got := CompactFunc(copy, strings.EqualFold); !Equal(got, want) {
549 t.Errorf("CompactFunc(%v, strings.EqualFold) = %v, want %v", s1, got, want)
553 func BenchmarkCompactFunc_Large(b *testing.B) {
554 type Large [4 * 1024]byte
556 ss := make([]Large, 1024)
557 for i := 0; i < b.N; i++ {
558 _ = CompactFunc(ss, func(a, b Large) bool { return a == b })
562 func TestGrow(t *testing.T) {
566 s2 := Grow(copy, 1000)
568 t.Errorf("Grow(%v) = %v, want %v", s1, s2, s1)
570 if cap(s2) < 1000+len(s1) {
571 t.Errorf("after Grow(%v) cap = %d, want >= %d", s1, cap(s2), 1000+len(s1))
574 // Test mutation of elements between length and capacity.
576 s3 := Grow(copy[:1], 2)[:3]
578 t.Errorf("Grow should not mutate elements between length and capacity")
580 s3 = Grow(copy[:1], 1000)[:3]
582 t.Errorf("Grow should not mutate elements between length and capacity")
585 // Test number of allocations.
586 if n := testing.AllocsPerRun(100, func() { Grow(s2, cap(s2)-len(s2)) }); n != 0 {
587 t.Errorf("Grow should not allocate when given sufficient capacity; allocated %v times", n)
589 if n := testing.AllocsPerRun(100, func() { Grow(s2, cap(s2)-len(s2)+1) }); n != 1 {
591 if race.Enabled || testenv.OptimizationOff() {
592 errorf = t.Logf // this allocates multiple times in race detector mode
594 errorf("Grow should allocate once when given insufficient capacity; allocated %v times", n)
597 // Test for negative growth sizes.
600 defer func() { gotPanic = recover() != nil }()
604 t.Errorf("Grow(-1) did not panic; expected a panic")
608 func TestClip(t *testing.T) {
609 s1 := []int{1, 2, 3, 4, 5, 6}[:3]
612 t.Errorf("len(%v) = %d, want 3", s1, len(s1))
615 t.Errorf("cap(%v[:3]) = %d, want >= 6", orig, cap(s1))
619 t.Errorf("Clip(%v) = %v, want %v", s1, s2, s1)
622 t.Errorf("cap(Clip(%v)) = %d, want 3", orig, cap(s2))
626 func TestReverse(t *testing.T) {
627 even := []int{3, 1, 4, 1, 5, 9} // len = 6
629 if want := []int{9, 5, 1, 4, 1, 3}; !Equal(even, want) {
630 t.Errorf("Reverse(even) = %v, want %v", even, want)
633 odd := []int{3, 1, 4, 1, 5, 9, 2} // len = 7
635 if want := []int{2, 9, 5, 1, 4, 1, 3}; !Equal(odd, want) {
636 t.Errorf("Reverse(odd) = %v, want %v", odd, want)
639 words := strings.Fields("one two three")
641 if want := strings.Fields("three two one"); !Equal(words, want) {
642 t.Errorf("Reverse(words) = %v, want %v", words, want)
645 singleton := []string{"one"}
647 if want := []string{"one"}; !Equal(singleton, want) {
648 t.Errorf("Reverse(singeleton) = %v, want %v", singleton, want)
654 // naiveReplace is a baseline implementation to the Replace function.
655 func naiveReplace[S ~[]E, E any](s S, i, j int, v ...E) S {
657 s = Insert(s, i, v...)
661 func TestReplace(t *testing.T) {
662 for _, test := range []struct {
666 {}, // all zero value
668 s: []int{1, 2, 3, 4},
674 s: []int{1, 2, 3, 4},
675 v: []int{5, 6, 7, 8},
681 s := make([]int, 3, 20)
687 v: []int{3, 4, 5, 6, 7},
692 ss, vv := Clone(test.s), Clone(test.v)
693 want := naiveReplace(ss, test.i, test.j, vv...)
694 got := Replace(test.s, test.i, test.j, test.v...)
695 if !Equal(got, want) {
696 t.Errorf("Replace(%v, %v, %v, %v) = %v, want %v", test.s, test.i, test.j, test.v, got, want)
701 func TestReplacePanics(t *testing.T) {
702 for _, test := range []struct {
707 {"indexes out of order", []int{1, 2}, []int{3}, 2, 1},
708 {"large index", []int{1, 2}, []int{3}, 1, 10},
709 {"negative index", []int{1, 2}, []int{3}, -1, 2},
711 ss, vv := Clone(test.s), Clone(test.v)
712 if !panics(func() { Replace(ss, test.i, test.j, vv...) }) {
713 t.Errorf("Replace %s: should have panicked", test.name)
718 func TestReplaceOverlap(t *testing.T) {
721 want := make([]int, 2*N)
722 for n := 0; n <= N; n++ { // length
723 for i := 0; i <= n; i++ { // insertion point 1
724 for j := i; j <= n; j++ { // insertion point 2
725 for x := 0; x <= N; x++ { // start of inserted data
726 for y := x; y <= N; y++ { // end of inserted data
727 for k := 0; k < N; k++ {
731 want = append(want, a[:i]...)
732 want = append(want, a[x:y]...)
733 want = append(want, a[j:n]...)
734 got := Replace(a[:n], i, j, a[x:y]...)
735 if !Equal(got, want) {
736 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)
745 func BenchmarkReplace(b *testing.B) {
754 return make([]int, 100)
757 return make([]int, 20)
765 return make([]int, 100)
768 return make([]int, 20)
775 for _, c := range cases {
776 b.Run("naive-"+c.name, func(b *testing.B) {
777 for k := 0; k < b.N; k++ {
780 _ = naiveReplace(s, c.i, c.j, v...)
783 b.Run("optimized-"+c.name, func(b *testing.B) {
784 for k := 0; k < b.N; k++ {
787 _ = Replace(s, c.i, c.j, v...)
794 func TestRotate(t *testing.T) {
796 s := make([]int, 0, N)
797 for n := 0; n < N; n++ {
798 for r := 0; r < n; r++ {
800 for i := 0; i < n; i++ {
804 for i := 0; i < n; i++ {
806 t.Errorf("expected n=%d r=%d i:%d want:%d got:%d", n, r, i, (i+r)%n, s[i])
813 func TestInsertGrowthRate(t *testing.T) {
818 for i := 0; i < N; i++ {
819 b = Insert(b, len(b)-1, 0)
825 want := int(math.Log(N) / math.Log(1.25)) // 1.25 == growth rate for large slices
827 t.Errorf("too many grows. got:%d want:%d", nGrow, want)
831 func TestReplaceGrowthRate(t *testing.T) {
836 for i := 0; i < N; i++ {
837 b = Replace(b, len(b)-2, len(b)-1, 0, 0)
843 want := int(math.Log(N) / math.Log(1.25)) // 1.25 == growth rate for large slices
845 t.Errorf("too many grows. got:%d want:%d", nGrow, want)