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 var deleteTests = []struct {
342 func TestDelete(t *testing.T) {
343 for _, test := range deleteTests {
344 copy := Clone(test.s)
345 if got := Delete(copy, test.i, test.j); !Equal(got, test.want) {
346 t.Errorf("Delete(%v, %d, %d) = %v, want %v", test.s, test.i, test.j, got, test.want)
351 var deleteFuncTests = []struct {
358 func(int) bool { return true },
363 func(int) bool { return true },
368 func(int) bool { return false },
373 func(i int) bool { return i > 2 },
378 func(i int) bool { return i < 2 },
383 func(i int) bool { return i >= 10 },
388 func TestDeleteFunc(t *testing.T) {
389 for i, test := range deleteFuncTests {
390 copy := Clone(test.s)
391 if got := DeleteFunc(copy, test.fn); !Equal(got, test.want) {
392 t.Errorf("DeleteFunc case %d: got %v, want %v", i, got, test.want)
397 func panics(f func()) (b bool) {
399 if x := recover(); x != nil {
407 func TestDeletePanics(t *testing.T) {
408 for _, test := range []struct {
413 {"with negative first index", []int{42}, -2, 1},
414 {"with negative second index", []int{42}, 1, -1},
415 {"with out-of-bounds first index", []int{42}, 2, 3},
416 {"with out-of-bounds second index", []int{42}, 0, 2},
417 {"with invalid i>j", []int{42}, 1, 0},
419 if !panics(func() { Delete(test.s, test.i, test.j) }) {
420 t.Errorf("Delete %s: got no panic, want panic", test.name)
425 func TestClone(t *testing.T) {
429 t.Errorf("Clone(%v) = %v, want %v", s1, s2, s1)
432 want := []int{1, 2, 3}
433 if !Equal(s2, want) {
434 t.Errorf("Clone(%v) changed unexpectedly to %v", want, s2)
436 if got := Clone([]int(nil)); got != nil {
437 t.Errorf("Clone(nil) = %#v, want nil", got)
439 if got := Clone(s1[:0]); got == nil || len(got) != 0 {
440 t.Errorf("Clone(%v) = %#v, want %#v", s1[:0], got, s1[:0])
444 var compactTests = []struct {
476 []int{1, 2, 2, 3, 3, 4},
481 func TestCompact(t *testing.T) {
482 for _, test := range compactTests {
483 copy := Clone(test.s)
484 if got := Compact(copy); !Equal(got, test.want) {
485 t.Errorf("Compact(%v) = %v, want %v", test.s, got, test.want)
490 func BenchmarkCompact(b *testing.B) {
491 for _, c := range compactTests {
492 b.Run(c.name, func(b *testing.B) {
493 ss := make([]int, 0, 64)
494 for k := 0; k < b.N; k++ {
496 ss = append(ss, c.s...)
503 func BenchmarkCompact_Large(b *testing.B) {
504 type Large [4 * 1024]byte
506 ss := make([]Large, 1024)
507 for i := 0; i < b.N; i++ {
512 func TestCompactFunc(t *testing.T) {
513 for _, test := range compactTests {
514 copy := Clone(test.s)
515 if got := CompactFunc(copy, equal[int]); !Equal(got, test.want) {
516 t.Errorf("CompactFunc(%v, equal[int]) = %v, want %v", test.s, got, test.want)
520 s1 := []string{"a", "a", "A", "B", "b"}
522 want := []string{"a", "B"}
523 if got := CompactFunc(copy, strings.EqualFold); !Equal(got, want) {
524 t.Errorf("CompactFunc(%v, strings.EqualFold) = %v, want %v", s1, got, want)
528 func BenchmarkCompactFunc_Large(b *testing.B) {
529 type Large [4 * 1024]byte
531 ss := make([]Large, 1024)
532 for i := 0; i < b.N; i++ {
533 _ = CompactFunc(ss, func(a, b Large) bool { return a == b })
537 func TestGrow(t *testing.T) {
541 s2 := Grow(copy, 1000)
543 t.Errorf("Grow(%v) = %v, want %v", s1, s2, s1)
545 if cap(s2) < 1000+len(s1) {
546 t.Errorf("after Grow(%v) cap = %d, want >= %d", s1, cap(s2), 1000+len(s1))
549 // Test mutation of elements between length and capacity.
551 s3 := Grow(copy[:1], 2)[:3]
553 t.Errorf("Grow should not mutate elements between length and capacity")
555 s3 = Grow(copy[:1], 1000)[:3]
557 t.Errorf("Grow should not mutate elements between length and capacity")
560 // Test number of allocations.
561 if n := testing.AllocsPerRun(100, func() { Grow(s2, cap(s2)-len(s2)) }); n != 0 {
562 t.Errorf("Grow should not allocate when given sufficient capacity; allocated %v times", n)
564 if n := testing.AllocsPerRun(100, func() { Grow(s2, cap(s2)-len(s2)+1) }); n != 1 {
566 if race.Enabled || testenv.OptimizationOff() {
567 errorf = t.Logf // this allocates multiple times in race detector mode
569 errorf("Grow should allocate once when given insufficient capacity; allocated %v times", n)
572 // Test for negative growth sizes.
575 defer func() { gotPanic = recover() != nil }()
579 t.Errorf("Grow(-1) did not panic; expected a panic")
583 func TestClip(t *testing.T) {
584 s1 := []int{1, 2, 3, 4, 5, 6}[:3]
587 t.Errorf("len(%v) = %d, want 3", s1, len(s1))
590 t.Errorf("cap(%v[:3]) = %d, want >= 6", orig, cap(s1))
594 t.Errorf("Clip(%v) = %v, want %v", s1, s2, s1)
597 t.Errorf("cap(Clip(%v)) = %d, want 3", orig, cap(s2))
601 // naiveReplace is a baseline implementation to the Replace function.
602 func naiveReplace[S ~[]E, E any](s S, i, j int, v ...E) S {
604 s = Insert(s, i, v...)
608 func TestReplace(t *testing.T) {
609 for _, test := range []struct {
613 {}, // all zero value
615 s: []int{1, 2, 3, 4},
621 s: []int{1, 2, 3, 4},
622 v: []int{5, 6, 7, 8},
628 s := make([]int, 3, 20)
634 v: []int{3, 4, 5, 6, 7},
639 ss, vv := Clone(test.s), Clone(test.v)
640 want := naiveReplace(ss, test.i, test.j, vv...)
641 got := Replace(test.s, test.i, test.j, test.v...)
642 if !Equal(got, want) {
643 t.Errorf("Replace(%v, %v, %v, %v) = %v, want %v", test.s, test.i, test.j, test.v, got, want)
648 func TestReplacePanics(t *testing.T) {
649 for _, test := range []struct {
654 {"indexes out of order", []int{1, 2}, []int{3}, 2, 1},
655 {"large index", []int{1, 2}, []int{3}, 1, 10},
656 {"negative index", []int{1, 2}, []int{3}, -1, 2},
658 ss, vv := Clone(test.s), Clone(test.v)
659 if !panics(func() { Replace(ss, test.i, test.j, vv...) }) {
660 t.Errorf("Replace %s: should have panicked", test.name)
665 func BenchmarkReplace(b *testing.B) {
674 return make([]int, 100)
677 return make([]int, 20)
685 return make([]int, 100)
688 return make([]int, 20)
695 for _, c := range cases {
696 b.Run("naive-"+c.name, func(b *testing.B) {
697 for k := 0; k < b.N; k++ {
700 _ = naiveReplace(s, c.i, c.j, v...)
703 b.Run("optimized-"+c.name, func(b *testing.B) {
704 for k := 0; k < b.N; k++ {
707 _ = Replace(s, c.i, c.j, v...)