Source file
src/testing/loop_test.go
1
2
3
4
5 package testing
6
7 func TestBenchmarkBLoop(t *T) {
8 var initialStart highPrecisionTime
9 var firstStart highPrecisionTime
10 var lastStart highPrecisionTime
11 var runningEnd bool
12 runs := 0
13 iters := 0
14 finalBN := 0
15 bRet := Benchmark(func(b *B) {
16 initialStart = b.start
17 runs++
18 for b.Loop() {
19 if iters == 0 {
20 firstStart = b.start
21 }
22 lastStart = b.start
23 iters++
24 }
25 finalBN = b.N
26 runningEnd = b.timerOn
27 })
28
29 if runs != 1 {
30 t.Errorf("want runs == 1, got %d", runs)
31 }
32
33 if iters == 0 {
34 t.Fatalf("no iterations ran")
35 }
36
37 if finalBN != iters || bRet.N != iters {
38 t.Errorf("benchmark iterations mismatch: %d loop iterations, final b.N=%d, bRet.N=%d", iters, finalBN, bRet.N)
39 }
40
41 if bRet.T < benchTime.d {
42 t.Fatalf("benchmark ran for %s, want >= %s", bRet.T, benchTime.d)
43 }
44
45 if firstStart == initialStart {
46 t.Errorf("b.Loop did not reset the timer")
47 }
48 if lastStart != firstStart {
49 t.Errorf("timer was reset during iteration")
50 }
51
52 if runningEnd {
53 t.Errorf("timer was still running after last iteration")
54 }
55 }
56
57
58
View as plain text