You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

23 lines
534 B

package testutil
import "testing"
const defaultAllocRuns = 10
// MaxAlloc checks that f does not allocate more than n.
func MaxAlloc(t *testing.T, n int, f func()) {
t.Helper()
if Race {
t.Skip("Skipped (race detector conflicts with allocation tests)")
}
avg := testing.AllocsPerRun(defaultAllocRuns, f)
if avg > float64(n) {
t.Errorf("Allocated %f bytes per run, expected less than %d", avg, n)
}
}
// ZeroAlloc checks that f does not allocate.
func ZeroAlloc(t *testing.T, f func()) {
t.Helper()
MaxAlloc(t, 0, f)
}