1 // Copyright 2013 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.
15 var roundDownTests
= []struct {
29 func TestRoundDown10(t
*testing
.T
) {
30 for _
, tt
:= range roundDownTests
{
31 actual
:= testing
.RoundDown10(tt
.v
)
32 if tt
.expected
!= actual
{
33 t
.Errorf("roundDown10(%d): expected %d, actual %d", tt
.v
, tt
.expected
, actual
)
38 var roundUpTests
= []struct {
57 func TestRoundUp(t
*testing
.T
) {
58 for _
, tt
:= range roundUpTests
{
59 actual
:= testing
.RoundUp(tt
.v
)
60 if tt
.expected
!= actual
{
61 t
.Errorf("roundUp(%d): expected %d, actual %d", tt
.v
, tt
.expected
, actual
)
66 func TestRunParallel(t
*testing
.T
) {
67 testing
.Benchmark(func(b
*testing
.B
) {
71 b
.RunParallel(func(pb
*testing
.PB
) {
72 atomic
.AddUint32(&procs
, 1)
74 atomic
.AddUint64(&iters
, 1)
77 if want
:= uint32(3 * runtime
.GOMAXPROCS(0)); procs
!= want
{
78 t
.Errorf("got %v procs, want %v", procs
, want
)
80 if iters
!= uint64(b
.N
) {
81 t
.Errorf("got %v iters, want %v", iters
, b
.N
)
86 func TestRunParallelFail(t
*testing
.T
) {
87 testing
.Benchmark(func(b
*testing
.B
) {
88 b
.RunParallel(func(pb
*testing
.PB
) {
89 // The function must be able to log/abort
90 // w/o crashing/deadlocking the whole benchmark.
97 func ExampleB_RunParallel() {
98 // Parallel benchmark for text/template.Template.Execute on a single object.
99 testing
.Benchmark(func(b
*testing
.B
) {
100 templ
:= template
.Must(template
.New("test").Parse("Hello, {{.}}!"))
101 // RunParallel will create GOMAXPROCS goroutines
102 // and distribute work among them.
103 b
.RunParallel(func(pb
*testing
.PB
) {
104 // Each goroutine has its own bytes.Buffer.
107 // The loop body is executed b.N times total across all goroutines.
109 templ
.Execute(&buf
, "World")