]> 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 8ea93c66d766861bcde3a2a31ce3a6d51fa11cc4..2fc583ff909d048738d70590c55e94a4207770cd 100644 (file)
@@ -536,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
@@ -639,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
@@ -649,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)
@@ -908,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
@@ -916,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...) }) {
@@ -1055,3 +1085,101 @@ func TestInference(t *testing.T) {
                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)
+               }
+       }
+}