| |
| |
| |
|
|
| package test |
|
|
| import ( |
| "internal/testenv" |
| "testing" |
| "unsafe" |
| ) |
|
|
| |
| |
| const maxStackSize = 32 |
|
|
| |
| func genericUse[T any](s []T) { |
| |
| } |
|
|
| func TestStackAllocation(t *testing.T) { |
| testenv.SkipIfOptimizationOff(t) |
|
|
| type testCase struct { |
| f func(int) |
| elemSize uintptr |
| } |
|
|
| for _, tc := range []testCase{ |
| { |
| f: func(n int) { |
| genericUse(make([]int, n)) |
| }, |
| elemSize: unsafe.Sizeof(int(0)), |
| }, |
| { |
| f: func(n int) { |
| genericUse(make([]*byte, n)) |
| }, |
| elemSize: unsafe.Sizeof((*byte)(nil)), |
| }, |
| { |
| f: func(n int) { |
| genericUse(make([]string, n)) |
| }, |
| elemSize: unsafe.Sizeof(""), |
| }, |
| } { |
| max := maxStackSize / int(tc.elemSize) |
| if n := testing.AllocsPerRun(10, func() { |
| tc.f(max) |
| }); n != 0 { |
| t.Fatalf("unexpected allocation: %f", n) |
| } |
| if n := testing.AllocsPerRun(10, func() { |
| tc.f(max + 1) |
| }); n != 1 { |
| t.Fatalf("unexpected allocation: %f", n) |
| } |
| } |
| } |
|
|