// Insert panics if i is out of range.
// This function is O(len(s) + len(v)).
func Insert[S ~[]E, E any](s S, i int, v ...E) S {
+ n := len(s)
m := len(v)
if m == 0 {
+ // Panic if i is not in the range [0:n] inclusive.
+ // See issue 63913.
+ _ = s[:n:n][i:]
return s
}
- n := len(s)
if i == n {
return append(s, v...)
}
}
}
+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