]> Cypherpunks.ru repositories - gostls13.git/blobdiff - src/slices/slices_test.go
slices: update doc for Delete and Replace
[gostls13.git] / src / slices / slices_test.go
index 8f683a7ae662edbad6336e67fc071843fe17889b..2fc583ff909d048738d70590c55e94a4207770cd 100644 (file)
@@ -2,13 +2,14 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-package slices
+package slices_test
 
 import (
        "cmp"
        "internal/race"
        "internal/testenv"
        "math"
+       . "slices"
        "strings"
        "testing"
 )
@@ -535,6 +536,26 @@ func TestInsertOverlap(t *testing.T) {
        }
 }
 
+func TestInsertPanics(t *testing.T) {
+       a := [3]int{}
+       for _, test := range []struct {
+               name string
+               s    []int
+               i    int
+               v    []int
+       }{
+               // There are no values.
+               {"with negative index", a[:1:1], -1, nil},
+               {"with out-of-bounds index and > cap", a[:1:1], 2, nil},
+               {"with out-of-bounds index and = cap", a[:1:2], 2, nil},
+               {"with out-of-bounds index and < cap", a[:1:3], 2, nil},
+       } {
+               if !panics(func() { Insert(test.s, test.i, test.v...) }) {
+                       t.Errorf("Insert %s: got no panic, want panic", test.name)
+               }
+       }
+}
+
 var deleteTests = []struct {
        s    []int
        i, j int
@@ -638,6 +659,10 @@ func panics(f func()) (b bool) {
 }
 
 func TestDeletePanics(t *testing.T) {
+       s := []int{0, 1, 2, 3, 4}
+       s = s[0:2]
+       _ = s[0:4] // this is a valid slice of s
+
        for _, test := range []struct {
                name string
                s    []int
@@ -648,6 +673,7 @@ func TestDeletePanics(t *testing.T) {
                {"with out-of-bounds first index", []int{42}, 2, 3},
                {"with out-of-bounds second index", []int{42}, 0, 2},
                {"with invalid i>j", []int{42}, 1, 0},
+               {"s[i:j] is valid and j > len(s)", s, 0, 4},
        } {
                if !panics(func() { Delete(test.s, test.i, test.j) }) {
                        t.Errorf("Delete %s: got no panic, want panic", test.name)
@@ -856,7 +882,7 @@ func TestReverse(t *testing.T) {
                t.Errorf("Reverse(singeleton) = %v, want %v", singleton, want)
        }
 
-       Reverse[string](nil)
+       Reverse[[]string](nil)
 }
 
 // naiveReplace is a baseline implementation to the Replace function.
@@ -907,6 +933,10 @@ func TestReplace(t *testing.T) {
 }
 
 func TestReplacePanics(t *testing.T) {
+       s := []int{0, 1, 2, 3, 4}
+       s = s[0:2]
+       _ = s[0:4] // this is a valid slice of s
+
        for _, test := range []struct {
                name string
                s, v []int
@@ -915,6 +945,7 @@ func TestReplacePanics(t *testing.T) {
                {"indexes out of order", []int{1, 2}, []int{3}, 2, 1},
                {"large index", []int{1, 2}, []int{3}, 1, 10},
                {"negative index", []int{1, 2}, []int{3}, -1, 2},
+               {"s[i:j] is valid and j > len(s)", s, nil, 0, 4},
        } {
                ss, vv := Clone(test.s), Clone(test.v)
                if !panics(func() { Replace(ss, test.i, test.j, vv...) }) {
@@ -999,25 +1030,6 @@ func BenchmarkReplace(b *testing.B) {
 
 }
 
-func TestRotate(t *testing.T) {
-       const N = 10
-       s := make([]int, 0, N)
-       for n := 0; n < N; n++ {
-               for r := 0; r < n; r++ {
-                       s = s[:0]
-                       for i := 0; i < n; i++ {
-                               s = append(s, i)
-                       }
-                       rotateLeft(s, r)
-                       for i := 0; i < n; i++ {
-                               if s[i] != (i+r)%n {
-                                       t.Errorf("expected n=%d r=%d i:%d want:%d got:%d", n, r, i, (i+r)%n, s[i])
-                               }
-                       }
-               }
-       }
-}
-
 func TestInsertGrowthRate(t *testing.T) {
        b := make([]byte, 1)
        maxCap := cap(b)
@@ -1053,3 +1065,121 @@ func TestReplaceGrowthRate(t *testing.T) {
                t.Errorf("too many grows. got:%d want:%d", nGrow, want)
        }
 }
+
+func apply[T any](v T, f func(T)) {
+       f(v)
+}
+
+// Test type inference with a named slice type.
+func TestInference(t *testing.T) {
+       s1 := []int{1, 2, 3}
+       apply(s1, Reverse)
+       if want := []int{3, 2, 1}; !Equal(s1, want) {
+               t.Errorf("Reverse(%v) = %v, want %v", []int{1, 2, 3}, s1, want)
+       }
+
+       type S []int
+       s2 := S{4, 5, 6}
+       apply(s2, Reverse)
+       if want := (S{6, 5, 4}); !Equal(s2, want) {
+               t.Errorf("Reverse(%v) = %v, want %v", S{4, 5, 6}, s2, want)
+       }
+}
+
+func TestConcat(t *testing.T) {
+       cases := []struct {
+               s    [][]int
+               want []int
+       }{
+               {
+                       s:    [][]int{nil},
+                       want: nil,
+               },
+               {
+                       s:    [][]int{{1}},
+                       want: []int{1},
+               },
+               {
+                       s:    [][]int{{1}, {2}},
+                       want: []int{1, 2},
+               },
+               {
+                       s:    [][]int{{1}, nil, {2}},
+                       want: []int{1, 2},
+               },
+       }
+       for _, tc := range cases {
+               got := Concat(tc.s...)
+               if !Equal(tc.want, got) {
+                       t.Errorf("Concat(%v) = %v, want %v", tc.s, got, tc.want)
+               }
+               var sink []int
+               allocs := testing.AllocsPerRun(5, func() {
+                       sink = Concat(tc.s...)
+               })
+               _ = sink
+               if allocs > 1 {
+                       errorf := t.Errorf
+                       if testenv.OptimizationOff() || race.Enabled {
+                               errorf = t.Logf
+                       }
+                       errorf("Concat(%v) allocated %v times; want 1", tc.s, allocs)
+               }
+       }
+}
+
+func TestConcat_too_large(t *testing.T) {
+       // Use zero length element to minimize memory in testing
+       type void struct{}
+       cases := []struct {
+               lengths     []int
+               shouldPanic bool
+       }{
+               {
+                       lengths:     []int{0, 0},
+                       shouldPanic: false,
+               },
+               {
+                       lengths:     []int{math.MaxInt, 0},
+                       shouldPanic: false,
+               },
+               {
+                       lengths:     []int{0, math.MaxInt},
+                       shouldPanic: false,
+               },
+               {
+                       lengths:     []int{math.MaxInt - 1, 1},
+                       shouldPanic: false,
+               },
+               {
+                       lengths:     []int{math.MaxInt - 1, 1, 1},
+                       shouldPanic: true,
+               },
+               {
+                       lengths:     []int{math.MaxInt, 1},
+                       shouldPanic: true,
+               },
+               {
+                       lengths:     []int{math.MaxInt, math.MaxInt},
+                       shouldPanic: true,
+               },
+       }
+       for _, tc := range cases {
+               var r any
+               ss := make([][]void, 0, len(tc.lengths))
+               for _, l := range tc.lengths {
+                       s := make([]void, l)
+                       ss = append(ss, s)
+               }
+               func() {
+                       defer func() {
+                               r = recover()
+                       }()
+                       _ = Concat(ss...)
+               }()
+               if didPanic := r != nil; didPanic != tc.shouldPanic {
+                       t.Errorf("slices.Concat(lens(%v)) got panic == %v",
+                               tc.lengths, didPanic)
+               }
+       }
+}