| 1 |
// Copyright 2022 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. |
| 4 |
|
| 5 |
//go:build !race && !asan && !msan |
| 6 |
|
| 7 |
package test |
| 8 |
|
| 9 |
import ( |
| 10 |
"internal/testenv" |
| 11 |
"testing" |
| 12 |
) |
| 13 |
|
| 14 |
func TestAppendOfMake(t *testing.T) { |
| 15 |
testenv.SkipIfOptimizationOff(t) |
| 16 |
for n := 32; n < 33; n++ { // avoid stack allocation of make() |
| 17 |
b := make([]byte, n) |
| 18 |
f := func() { |
| 19 |
b = append(b[:0], make([]byte, n)...) |
| 20 |
} |
| 21 |
if n := testing.AllocsPerRun(10, f); n > 0 { |
| 22 |
t.Errorf("got %f allocs, want 0", n) |
| 23 |
} |
| 24 |
type S []byte |
| 25 |
|
| 26 |
s := make(S, n) |
| 27 |
g := func() { |
| 28 |
s = append(s[:0], make(S, n)...) |
| 29 |
} |
| 30 |
if n := testing.AllocsPerRun(10, g); n > 0 { |
| 31 |
t.Errorf("got %f allocs, want 0", n) |
| 32 |
} |
| 33 |
h := func() { |
| 34 |
s = append(s[:0], make([]byte, n)...) |
| 35 |
} |
| 36 |
if n := testing.AllocsPerRun(10, h); n > 0 { |
| 37 |
t.Errorf("got %f allocs, want 0", n) |
| 38 |
} |
| 39 |
i := func() { |
| 40 |
b = append(b[:0], make(S, n)...) |
| 41 |
} |
| 42 |
if n := testing.AllocsPerRun(10, i); n > 0 { |
| 43 |
t.Errorf("got %f allocs, want 0", n) |
| 44 |
} |
| 45 |
} |
| 46 |
} |