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 var indexTests = []struct {
159 func TestIndex(t *testing.T) {
160 for _, test := range indexTests {
161 if got := Index(test.s, test.v); got != test.want {
162 t.Errorf("Index(%v, %v) = %d, want %d", test.s, test.v, got, test.want)
167 func equalToIndex[T any](f func(T, T) bool, v1 T) func(T) bool {
168 return func(v2 T) bool {
173 func TestIndexFunc(t *testing.T) {
174 for _, test := range indexTests {
175 if got := IndexFunc(test.s, equalToIndex(equal[int], test.v)); got != test.want {
176 t.Errorf("IndexFunc(%v, equalToIndex(equal[int], %v)) = %d, want %d", test.s, test.v, got, test.want)
180 s1 := []string{"hi", "HI"}
181 if got := IndexFunc(s1, equalToIndex(equal[string], "HI")); got != 1 {
182 t.Errorf("IndexFunc(%v, equalToIndex(equal[string], %q)) = %d, want %d", s1, "HI", got, 1)
184 if got := IndexFunc(s1, equalToIndex(strings.EqualFold, "HI")); got != 0 {
185 t.Errorf("IndexFunc(%v, equalToIndex(strings.EqualFold, %q)) = %d, want %d", s1, "HI", got, 0)
189 func TestContains(t *testing.T) {
190 for _, test := range indexTests {
191 if got := Contains(test.s, test.v); got != (test.want != -1) {
192 t.Errorf("Contains(%v, %v) = %t, want %t", test.s, test.v, got, test.want != -1)
197 func TestContainsFunc(t *testing.T) {
198 for _, test := range indexTests {
199 if got := ContainsFunc(test.s, equalToIndex(equal[int], test.v)); got != (test.want != -1) {
200 t.Errorf("ContainsFunc(%v, equalToIndex(equal[int], %v)) = %t, want %t", test.s, test.v, got, test.want != -1)
204 s1 := []string{"hi", "HI"}
205 if got := ContainsFunc(s1, equalToIndex(equal[string], "HI")); got != true {
206 t.Errorf("ContainsFunc(%v, equalToContains(equal[string], %q)) = %t, want %t", s1, "HI", got, true)
208 if got := ContainsFunc(s1, equalToIndex(equal[string], "hI")); got != false {
209 t.Errorf("ContainsFunc(%v, equalToContains(strings.EqualFold, %q)) = %t, want %t", s1, "hI", got, false)
211 if got := ContainsFunc(s1, equalToIndex(strings.EqualFold, "hI")); got != true {
212 t.Errorf("ContainsFunc(%v, equalToContains(strings.EqualFold, %q)) = %t, want %t", s1, "hI", got, true)
216 var insertTests = []struct {
244 []int{1, 2, 4, 5, 3},
248 func TestInsert(t *testing.T) {
250 if got := Insert(s, 0); !Equal(got, s) {
251 t.Errorf("Insert(%v, 0) = %v, want %v", s, got, s)
253 for _, test := range insertTests {
254 copy := Clone(test.s)
255 if got := Insert(copy, test.i, test.add...); !Equal(got, test.want) {
256 t.Errorf("Insert(%v, %d, %v...) = %v, want %v", test.s, test.i, test.add, got, test.want)
261 var deleteTests = []struct {
298 func TestDelete(t *testing.T) {
299 for _, test := range deleteTests {
300 copy := Clone(test.s)
301 if got := Delete(copy, test.i, test.j); !Equal(got, test.want) {
302 t.Errorf("Delete(%v, %d, %d) = %v, want %v", test.s, test.i, test.j, got, test.want)
307 func panics(f func()) (b bool) {
309 if x := recover(); x != nil {
317 func TestDeletePanics(t *testing.T) {
318 for _, test := range []struct {
323 {"with negative first index", []int{42}, -2, 1},
324 {"with negative second index", []int{42}, 1, -1},
325 {"with out-of-bounds first index", []int{42}, 2, 3},
326 {"with out-of-bounds second index", []int{42}, 0, 2},
327 {"with invalid i>j", []int{42}, 1, 0},
329 if !panics(func() { Delete(test.s, test.i, test.j) }) {
330 t.Errorf("Delete %s: got no panic, want panic", test.name)
335 func TestClone(t *testing.T) {
339 t.Errorf("Clone(%v) = %v, want %v", s1, s2, s1)
342 want := []int{1, 2, 3}
343 if !Equal(s2, want) {
344 t.Errorf("Clone(%v) changed unexpectedly to %v", want, s2)
346 if got := Clone([]int(nil)); got != nil {
347 t.Errorf("Clone(nil) = %#v, want nil", got)
349 if got := Clone(s1[:0]); got == nil || len(got) != 0 {
350 t.Errorf("Clone(%v) = %#v, want %#v", s1[:0], got, s1[:0])
354 var compactTests = []struct {
386 []int{1, 2, 2, 3, 3, 4},
391 func TestCompact(t *testing.T) {
392 for _, test := range compactTests {
393 copy := Clone(test.s)
394 if got := Compact(copy); !Equal(got, test.want) {
395 t.Errorf("Compact(%v) = %v, want %v", test.s, got, test.want)
400 func BenchmarkCompact(b *testing.B) {
401 for _, c := range compactTests {
402 b.Run(c.name, func(b *testing.B) {
403 ss := make([]int, 0, 64)
404 for k := 0; k < b.N; k++ {
406 ss = append(ss, c.s...)
414 func TestCompactFunc(t *testing.T) {
415 for _, test := range compactTests {
416 copy := Clone(test.s)
417 if got := CompactFunc(copy, equal[int]); !Equal(got, test.want) {
418 t.Errorf("CompactFunc(%v, equal[int]) = %v, want %v", test.s, got, test.want)
422 s1 := []string{"a", "a", "A", "B", "b"}
424 want := []string{"a", "B"}
425 if got := CompactFunc(copy, strings.EqualFold); !Equal(got, want) {
426 t.Errorf("CompactFunc(%v, strings.EqualFold) = %v, want %v", s1, got, want)
430 func TestGrow(t *testing.T) {
434 s2 := Grow(copy, 1000)
436 t.Errorf("Grow(%v) = %v, want %v", s1, s2, s1)
438 if cap(s2) < 1000+len(s1) {
439 t.Errorf("after Grow(%v) cap = %d, want >= %d", s1, cap(s2), 1000+len(s1))
442 // Test mutation of elements between length and capacity.
444 s3 := Grow(copy[:1], 2)[:3]
446 t.Errorf("Grow should not mutate elements between length and capacity")
448 s3 = Grow(copy[:1], 1000)[:3]
450 t.Errorf("Grow should not mutate elements between length and capacity")
453 // Test number of allocations.
454 if n := testing.AllocsPerRun(100, func() { Grow(s2, cap(s2)-len(s2)) }); n != 0 {
455 t.Errorf("Grow should not allocate when given sufficient capacity; allocated %v times", n)
457 if n := testing.AllocsPerRun(100, func() { Grow(s2, cap(s2)-len(s2)+1) }); n != 1 {
459 if race.Enabled || testenv.OptimizationOff() {
460 errorf = t.Logf // this allocates multiple times in race detector mode
462 errorf("Grow should allocate once when given insufficient capacity; allocated %v times", n)
465 // Test for negative growth sizes.
468 defer func() { gotPanic = recover() != nil }()
472 t.Errorf("Grow(-1) did not panic; expected a panic")
476 func TestClip(t *testing.T) {
477 s1 := []int{1, 2, 3, 4, 5, 6}[:3]
480 t.Errorf("len(%v) = %d, want 3", s1, len(s1))
483 t.Errorf("cap(%v[:3]) = %d, want >= 6", orig, cap(s1))
487 t.Errorf("Clip(%v) = %v, want %v", s1, s2, s1)
490 t.Errorf("cap(Clip(%v)) = %d, want 3", orig, cap(s2))
494 // naiveReplace is a baseline implementation to the Replace function.
495 func naiveReplace[S ~[]E, E any](s S, i, j int, v ...E) S {
497 s = Insert(s, i, v...)
501 func TestReplace(t *testing.T) {
502 for _, test := range []struct {
506 {}, // all zero value
508 s: []int{1, 2, 3, 4},
514 s: []int{1, 2, 3, 4},
515 v: []int{5, 6, 7, 8},
521 s := make([]int, 3, 20)
527 v: []int{3, 4, 5, 6, 7},
532 ss, vv := Clone(test.s), Clone(test.v)
533 want := naiveReplace(ss, test.i, test.j, vv...)
534 got := Replace(test.s, test.i, test.j, test.v...)
535 if !Equal(got, want) {
536 t.Errorf("Replace(%v, %v, %v, %v) = %v, want %v", test.s, test.i, test.j, test.v, got, want)
541 func TestReplacePanics(t *testing.T) {
542 for _, test := range []struct {
547 {"indexes out of order", []int{1, 2}, []int{3}, 2, 1},
548 {"large index", []int{1, 2}, []int{3}, 1, 10},
549 {"negative index", []int{1, 2}, []int{3}, -1, 2},
551 ss, vv := Clone(test.s), Clone(test.v)
552 if !panics(func() { Replace(ss, test.i, test.j, vv...) }) {
553 t.Errorf("Replace %s: should have panicked", test.name)
558 func BenchmarkReplace(b *testing.B) {
567 return make([]int, 100)
570 return make([]int, 20)
578 return make([]int, 100)
581 return make([]int, 20)
588 for _, c := range cases {
589 b.Run("naive-"+c.name, func(b *testing.B) {
590 for k := 0; k < b.N; k++ {
593 _ = naiveReplace(s, c.i, c.j, v...)
596 b.Run("optimized-"+c.name, func(b *testing.B) {
597 for k := 0; k < b.N; k++ {
600 _ = Replace(s, c.i, c.j, v...)