copy(s2[i:], v)
return s2
}
- s2 := make(S, tot)
+ // Use append rather than make so that we bump the size of
+ // the slice up to the next storage class.
+ // This is what Grow does but we don't call Grow because
+ // that might copy the values twice.
+ s2 := append(S(nil), make(S, tot)...)
copy(s2, s[:i])
copy(s2[i:], v)
copy(s2[i+len(v):], s[i:])
t.Errorf("Insert(%v, %d, %v...) = %v, want %v", test.s, test.i, test.add, got, test.want)
}
}
+
+ if !testenv.OptimizationOff() && !race.Enabled {
+ // Allocations should be amortized.
+ const count = 50
+ n := testing.AllocsPerRun(10, func() {
+ s := []int{1, 2, 3}
+ for i := 0; i < count; i++ {
+ s = Insert(s, 0, 1)
+ }
+ })
+ if n > count/2 {
+ t.Errorf("too many allocations inserting %d elements: got %v, want less than %d", count, n, count/2)
+ }
+ }
}
var deleteTests = []struct {