1
2
3
4
5 package test
6
7 import (
8 "internal/asan"
9 "internal/goexperiment"
10 "internal/msan"
11 "internal/race"
12 "testing"
13 )
14
15 func TestFreeAppendAllocations(t *testing.T) {
16 t.Run("slice-no-alias", func(t *testing.T) {
17 if !goexperiment.RuntimeFreegc {
18 t.Skip("skipping allocation test when runtime.freegc is disabled")
19 }
20 if race.Enabled || msan.Enabled || asan.Enabled {
21
22
23
24 t.Skip("skipping allocation test under race detector and other sanitizers")
25 }
26
27 allocs := testing.AllocsPerRun(100, func() {
28 var s []int64
29 for i := range 100 {
30 s = append(s, int64(i))
31 }
32 _ = s
33 })
34 t.Logf("allocs: %v", allocs)
35 if allocs != 1 {
36 t.Errorf("allocs: %v, want 1", allocs)
37 }
38 })
39
40 t.Run("slice-aliased", func(t *testing.T) {
41 allocs := testing.AllocsPerRun(100, func() {
42 var s []int64
43 var alias []int64
44 for i := range 100 {
45 s = append(s, int64(i))
46 alias = s
47 }
48 _ = alias
49 })
50 t.Logf("allocs: %v", allocs)
51 if allocs < 2 {
52 t.Errorf("allocs: %v, want >= 2", allocs)
53 }
54 })
55 }
56
View as plain text