allocs_test.go 817 B

1234567891011121314151617181920212223242526272829
  1. // Copyright 2014 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. package testing_test
  5. import "testing"
  6. var global any
  7. var allocsPerRunTests = []struct {
  8. name string
  9. fn func()
  10. allocs float64
  11. }{
  12. {"alloc *byte", func() { global = new(*byte) }, 1},
  13. {"alloc complex128", func() { global = new(complex128) }, 1},
  14. {"alloc float64", func() { global = new(float64) }, 1},
  15. {"alloc int32", func() { global = new(int32) }, 1},
  16. {"alloc byte", func() { global = new(byte) }, 1},
  17. }
  18. func TestAllocsPerRun(t *testing.T) {
  19. for _, tt := range allocsPerRunTests {
  20. if allocs := testing.AllocsPerRun(100, tt.fn); allocs != tt.allocs {
  21. t.Errorf("AllocsPerRun(100, %s) = %v, want %v", tt.name, allocs, tt.allocs)
  22. }
  23. }
  24. }