Debian Codegraph @

src/cmd/compile/internal/test/free_test.go

raw
1 // Copyright 2025 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 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 // TODO(thepudds): we get 8 allocs for slice-no-alias instead of 1 with -race. This
22 // might be expected given some allocation optimizations are already disabled
23 // under race, but if not, we might need to update walk.
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 }