blob: 431bb537bd5b1b321dca8d843e8fbd28281aec5d [file] [log] [blame]
Dave Cheney787976c2013-05-31 23:03:22 +10001// 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.
4
5package testing_test
6
7import (
Dmitriy Vyukovc3922f02014-02-17 06:29:56 +04008 "bytes"
9 "runtime"
10 "sync/atomic"
Dave Cheney787976c2013-05-31 23:03:22 +100011 "testing"
Dmitriy Vyukovc3922f02014-02-17 06:29:56 +040012 "text/template"
Dave Cheney787976c2013-05-31 23:03:22 +100013)
14
15var roundDownTests = []struct {
16 v, expected int
17}{
18 {1, 1},
19 {9, 1},
Dave Cheneya28609d2013-06-02 09:13:12 +100020 {10, 10},
Dave Cheney787976c2013-05-31 23:03:22 +100021 {11, 10},
Dave Cheneya28609d2013-06-02 09:13:12 +100022 {100, 100},
23 {101, 100},
24 {999, 100},
25 {1000, 1000},
26 {1001, 1000},
Dave Cheney787976c2013-05-31 23:03:22 +100027}
28
29func TestRoundDown10(t *testing.T) {
30 for _, tt := range roundDownTests {
31 actual := testing.RoundDown10(tt.v)
32 if tt.expected != actual {
Dave Cheneya28609d2013-06-02 09:13:12 +100033 t.Errorf("roundDown10(%d): expected %d, actual %d", tt.v, tt.expected, actual)
34 }
35 }
36}
37
38var roundUpTests = []struct {
39 v, expected int
40}{
41 {0, 1},
42 {1, 1},
43 {2, 2},
Josh Bleecher Snydera12cc712014-06-24 08:39:30 -070044 {3, 3},
Dave Cheneya28609d2013-06-02 09:13:12 +100045 {5, 5},
46 {9, 10},
47 {999, 1000},
48 {1000, 1000},
49 {1400, 2000},
50 {1700, 2000},
Josh Bleecher Snydera12cc712014-06-24 08:39:30 -070051 {2700, 3000},
Dave Cheneya28609d2013-06-02 09:13:12 +100052 {4999, 5000},
53 {5000, 5000},
54 {5001, 10000},
55}
56
57func 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)
Dave Cheney787976c2013-05-31 23:03:22 +100062 }
63 }
64}
Dmitriy Vyukovc3922f02014-02-17 06:29:56 +040065
66func TestRunParallel(t *testing.T) {
67 testing.Benchmark(func(b *testing.B) {
68 procs := uint32(0)
69 iters := uint64(0)
70 b.SetParallelism(3)
71 b.RunParallel(func(pb *testing.PB) {
72 atomic.AddUint32(&procs, 1)
73 for pb.Next() {
74 atomic.AddUint64(&iters, 1)
75 }
76 })
77 if want := uint32(3 * runtime.GOMAXPROCS(0)); procs != want {
78 t.Errorf("got %v procs, want %v", procs, want)
79 }
80 if iters != uint64(b.N) {
81 t.Errorf("got %v iters, want %v", iters, b.N)
82 }
83 })
84}
85
86func 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.
91 b.Log("log")
92 b.Error("error")
Dmitriy Vyukovc3922f02014-02-17 06:29:56 +040093 })
94 })
95}
96
97func 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.
105 var buf bytes.Buffer
106 for pb.Next() {
107 // The loop body is executed b.N times total across all goroutines.
108 buf.Reset()
109 templ.Execute(&buf, "World")
110 }
111 })
112 })
113}