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.
14 var equalIntTests = []struct {
40 var equalFloatTests = []struct {
52 []float64{1, 2, math.NaN()},
53 []float64{1, 2, math.NaN()},
59 func TestEqual(t *testing.T) {
60 for _, test := range equalIntTests {
61 if got := Equal(test.s1, test.s2); got != test.want {
62 t.Errorf("Equal(%v, %v) = %t, want %t", test.s1, test.s2, got, test.want)
65 for _, test := range equalFloatTests {
66 if got := Equal(test.s1, test.s2); got != test.wantEqual {
67 t.Errorf("Equal(%v, %v) = %t, want %t", test.s1, test.s2, got, test.wantEqual)
72 // equal is simply ==.
73 func equal[T comparable](v1, v2 T) bool {
77 // equalNaN is like == except that all NaNs are equal.
78 func equalNaN[T comparable](v1, v2 T) bool {
79 isNaN := func(f T) bool { return f != f }
80 return v1 == v2 || (isNaN(v1) && isNaN(v2))
83 // offByOne returns true if integers v1 and v2 differ by 1.
84 func offByOne(v1, v2 int) bool {
85 return v1 == v2+1 || v1 == v2-1
88 func TestEqualFunc(t *testing.T) {
89 for _, test := range equalIntTests {
90 if got := EqualFunc(test.s1, test.s2, equal[int]); got != test.want {
91 t.Errorf("EqualFunc(%v, %v, equal[int]) = %t, want %t", test.s1, test.s2, got, test.want)
94 for _, test := range equalFloatTests {
95 if got := EqualFunc(test.s1, test.s2, equal[float64]); got != test.wantEqual {
96 t.Errorf("Equal(%v, %v, equal[float64]) = %t, want %t", test.s1, test.s2, got, test.wantEqual)
98 if got := EqualFunc(test.s1, test.s2, equalNaN[float64]); got != test.wantEqualNaN {
99 t.Errorf("Equal(%v, %v, equalNaN[float64]) = %t, want %t", test.s1, test.s2, got, test.wantEqualNaN)
105 if EqualFunc(s1, s1, offByOne) {
106 t.Errorf("EqualFunc(%v, %v, offByOne) = true, want false", s1, s1)
108 if !EqualFunc(s1, s2, offByOne) {
109 t.Errorf("EqualFunc(%v, %v, offByOne) = false, want true", s1, s2)
112 s3 := []string{"a", "b", "c"}
113 s4 := []string{"A", "B", "C"}
114 if !EqualFunc(s3, s4, strings.EqualFold) {
115 t.Errorf("EqualFunc(%v, %v, strings.EqualFold) = false, want true", s3, s4)
118 cmpIntString := func(v1 int, v2 string) bool {
119 return string(rune(v1)-1+'a') == v2
121 if !EqualFunc(s1, s3, cmpIntString) {
122 t.Errorf("EqualFunc(%v, %v, cmpIntString) = false, want true", s1, s3)
126 var indexTests = []struct {
158 func TestIndex(t *testing.T) {
159 for _, test := range indexTests {
160 if got := Index(test.s, test.v); got != test.want {
161 t.Errorf("Index(%v, %v) = %d, want %d", test.s, test.v, got, test.want)
166 func equalToIndex[T any](f func(T, T) bool, v1 T) func(T) bool {
167 return func(v2 T) bool {
172 func TestIndexFunc(t *testing.T) {
173 for _, test := range indexTests {
174 if got := IndexFunc(test.s, equalToIndex(equal[int], test.v)); got != test.want {
175 t.Errorf("IndexFunc(%v, equalToIndex(equal[int], %v)) = %d, want %d", test.s, test.v, got, test.want)
179 s1 := []string{"hi", "HI"}
180 if got := IndexFunc(s1, equalToIndex(equal[string], "HI")); got != 1 {
181 t.Errorf("IndexFunc(%v, equalToIndex(equal[string], %q)) = %d, want %d", s1, "HI", got, 1)
183 if got := IndexFunc(s1, equalToIndex(strings.EqualFold, "HI")); got != 0 {
184 t.Errorf("IndexFunc(%v, equalToIndex(strings.EqualFold, %q)) = %d, want %d", s1, "HI", got, 0)
188 func TestContains(t *testing.T) {
189 for _, test := range indexTests {
190 if got := Contains(test.s, test.v); got != (test.want != -1) {
191 t.Errorf("Contains(%v, %v) = %t, want %t", test.s, test.v, got, test.want != -1)
196 func TestContainsFunc(t *testing.T) {
197 for _, test := range indexTests {
198 if got := ContainsFunc(test.s, equalToIndex(equal[int], test.v)); got != (test.want != -1) {
199 t.Errorf("ContainsFunc(%v, equalToIndex(equal[int], %v)) = %t, want %t", test.s, test.v, got, test.want != -1)
203 s1 := []string{"hi", "HI"}
204 if got := ContainsFunc(s1, equalToIndex(equal[string], "HI")); got != true {
205 t.Errorf("ContainsFunc(%v, equalToContains(equal[string], %q)) = %t, want %t", s1, "HI", got, true)
207 if got := ContainsFunc(s1, equalToIndex(equal[string], "hI")); got != false {
208 t.Errorf("ContainsFunc(%v, equalToContains(strings.EqualFold, %q)) = %t, want %t", s1, "hI", got, false)
210 if got := ContainsFunc(s1, equalToIndex(strings.EqualFold, "hI")); got != true {
211 t.Errorf("ContainsFunc(%v, equalToContains(strings.EqualFold, %q)) = %t, want %t", s1, "hI", got, true)
215 var insertTests = []struct {
243 []int{1, 2, 4, 5, 3},
247 func TestInsert(t *testing.T) {
249 if got := Insert(s, 0); !Equal(got, s) {
250 t.Errorf("Insert(%v, 0) = %v, want %v", s, got, s)
252 for _, test := range insertTests {
253 copy := Clone(test.s)
254 if got := Insert(copy, test.i, test.add...); !Equal(got, test.want) {
255 t.Errorf("Insert(%v, %d, %v...) = %v, want %v", test.s, test.i, test.add, got, test.want)
260 var deleteTests = []struct {
297 func TestDelete(t *testing.T) {
298 for _, test := range deleteTests {
299 copy := Clone(test.s)
300 if got := Delete(copy, test.i, test.j); !Equal(got, test.want) {
301 t.Errorf("Delete(%v, %d, %d) = %v, want %v", test.s, test.i, test.j, got, test.want)
306 func panics(f func()) (b bool) {
308 if x := recover(); x != nil {
316 func TestDeletePanics(t *testing.T) {
317 for _, test := range []struct {
322 {"with negative first index", []int{42}, -2, 1},
323 {"with negative second index", []int{42}, 1, -1},
324 {"with out-of-bounds first index", []int{42}, 2, 3},
325 {"with out-of-bounds second index", []int{42}, 0, 2},
326 {"with invalid i>j", []int{42}, 1, 0},
328 if !panics(func() { Delete(test.s, test.i, test.j) }) {
329 t.Errorf("Delete %s: got no panic, want panic", test.name)
334 func TestClone(t *testing.T) {
338 t.Errorf("Clone(%v) = %v, want %v", s1, s2, s1)
341 want := []int{1, 2, 3}
342 if !Equal(s2, want) {
343 t.Errorf("Clone(%v) changed unexpectedly to %v", want, s2)
345 if got := Clone([]int(nil)); got != nil {
346 t.Errorf("Clone(nil) = %#v, want nil", got)
348 if got := Clone(s1[:0]); got == nil || len(got) != 0 {
349 t.Errorf("Clone(%v) = %#v, want %#v", s1[:0], got, s1[:0])
353 var compactTests = []struct {
385 []int{1, 2, 2, 3, 3, 4},
390 func TestCompact(t *testing.T) {
391 for _, test := range compactTests {
392 copy := Clone(test.s)
393 if got := Compact(copy); !Equal(got, test.want) {
394 t.Errorf("Compact(%v) = %v, want %v", test.s, got, test.want)
399 func BenchmarkCompact(b *testing.B) {
400 for _, c := range compactTests {
401 b.Run(c.name, func(b *testing.B) {
402 ss := make([]int, 0, 64)
403 for k := 0; k < b.N; k++ {
405 ss = append(ss, c.s...)
413 func TestCompactFunc(t *testing.T) {
414 for _, test := range compactTests {
415 copy := Clone(test.s)
416 if got := CompactFunc(copy, equal[int]); !Equal(got, test.want) {
417 t.Errorf("CompactFunc(%v, equal[int]) = %v, want %v", test.s, got, test.want)
421 s1 := []string{"a", "a", "A", "B", "b"}
423 want := []string{"a", "B"}
424 if got := CompactFunc(copy, strings.EqualFold); !Equal(got, want) {
425 t.Errorf("CompactFunc(%v, strings.EqualFold) = %v, want %v", s1, got, want)
429 func TestGrow(t *testing.T) {
433 s2 := Grow(copy, 1000)
435 t.Errorf("Grow(%v) = %v, want %v", s1, s2, s1)
437 if cap(s2) < 1000+len(s1) {
438 t.Errorf("after Grow(%v) cap = %d, want >= %d", s1, cap(s2), 1000+len(s1))
441 // Test mutation of elements between length and capacity.
443 s3 := Grow(copy[:1], 2)[:3]
445 t.Errorf("Grow should not mutate elements between length and capacity")
447 s3 = Grow(copy[:1], 1000)[:3]
449 t.Errorf("Grow should not mutate elements between length and capacity")
452 // Test number of allocations.
453 if n := testing.AllocsPerRun(100, func() { Grow(s2, cap(s2)-len(s2)) }); n != 0 {
454 t.Errorf("Grow should not allocate when given sufficient capacity; allocated %v times", n)
456 if n := testing.AllocsPerRun(100, func() { Grow(s2, cap(s2)-len(s2)+1) }); n != 1 {
459 errorf = t.Logf // this allocates multiple times in race detector mode
461 errorf("Grow should allocate once when given insufficient capacity; allocated %v times", n)
464 // Test for negative growth sizes.
467 defer func() { gotPanic = recover() != nil }()
471 t.Errorf("Grow(-1) did not panic; expected a panic")
475 func TestClip(t *testing.T) {
476 s1 := []int{1, 2, 3, 4, 5, 6}[:3]
479 t.Errorf("len(%v) = %d, want 3", s1, len(s1))
482 t.Errorf("cap(%v[:3]) = %d, want >= 6", orig, cap(s1))
486 t.Errorf("Clip(%v) = %v, want %v", s1, s2, s1)
489 t.Errorf("cap(Clip(%v)) = %d, want 3", orig, cap(s2))
493 // naiveReplace is a baseline implementation to the Replace function.
494 func naiveReplace[S ~[]E, E any](s S, i, j int, v ...E) S {
496 s = Insert(s, i, v...)
500 func TestReplace(t *testing.T) {
501 for _, test := range []struct {
505 {}, // all zero value
507 s: []int{1, 2, 3, 4},
513 s: []int{1, 2, 3, 4},
514 v: []int{5, 6, 7, 8},
520 s := make([]int, 3, 20)
526 v: []int{3, 4, 5, 6, 7},
531 ss, vv := Clone(test.s), Clone(test.v)
532 want := naiveReplace(ss, test.i, test.j, vv...)
533 got := Replace(test.s, test.i, test.j, test.v...)
534 if !Equal(got, want) {
535 t.Errorf("Replace(%v, %v, %v, %v) = %v, want %v", test.s, test.i, test.j, test.v, got, want)
540 func TestReplacePanics(t *testing.T) {
541 for _, test := range []struct {
546 {"indexes out of order", []int{1, 2}, []int{3}, 2, 1},
547 {"large index", []int{1, 2}, []int{3}, 1, 10},
548 {"negative index", []int{1, 2}, []int{3}, -1, 2},
550 ss, vv := Clone(test.s), Clone(test.v)
551 if !panics(func() { Replace(ss, test.i, test.j, vv...) }) {
552 t.Errorf("Replace %s: should have panicked", test.name)
557 func BenchmarkReplace(b *testing.B) {
566 return make([]int, 100)
569 return make([]int, 20)
577 return make([]int, 100)
580 return make([]int, 20)
587 for _, c := range cases {
588 b.Run("naive-"+c.name, func(b *testing.B) {
589 for k := 0; k < b.N; k++ {
592 _ = naiveReplace(s, c.i, c.j, v...)
595 b.Run("optimized-"+c.name, func(b *testing.B) {
596 for k := 0; k < b.N; k++ {
599 _ = Replace(s, c.i, c.j, v...)