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.
5 // Test runner for coverage test. This file is not coverage-annotated; test.go is.
6 // It knows the coverage counter is called "coverTest".
25 var counters = make(map[block]bool)
27 // check records the location and expected value for a counter.
28 func check(line, count uint32) {
36 // checkVal is a version of check that returns its extra argument,
37 // so it can be used in conditionals.
38 func checkVal(line, count uint32, val int) int {
49 // verify checks the expected counts against the actual. It runs after the test has completed.
51 for b := range counters {
52 got, index := count(b.line)
53 if b.count == anything && got != 0 {
57 fmt.Fprintf(os.Stderr, "test_go:%d expected count %d got %d [counter %d]\n", b.line, b.count, got, index)
63 fmt.Fprintf(os.Stderr, "FAIL\n")
68 // verifyPanic is a special check for the known counter that should be
69 // after the panic call in testPanic.
71 if coverTest.Count[panicIndex-1] != 1 {
72 // Sanity check for test before panic.
73 fmt.Fprintf(os.Stderr, "bad before panic")
76 if coverTest.Count[panicIndex] != 0 {
77 fmt.Fprintf(os.Stderr, "bad at panic: %d should be 0\n", coverTest.Count[panicIndex])
80 if coverTest.Count[panicIndex+1] != 1 {
81 fmt.Fprintf(os.Stderr, "bad after panic")
86 // count returns the count and index for the counter at the specified line.
87 func count(line uint32) (uint32, int) {
88 // Linear search is fine. Choose perfect fit over approximate.
89 // We can have a closing brace for a range on the same line as a condition for an "else if"
90 // and we don't want that brace to steal the count for the condition on the "if".
91 // Therefore we test for a perfect (lo==line && hi==line) match, but if we can't
92 // find that we take the first imperfect match.
94 indexLo := uint32(1e9)
95 for i := range coverTest.Count {
96 lo, hi := coverTest.Pos[3*i], coverTest.Pos[3*i+1]
97 if lo == line && line == hi {
98 return coverTest.Count[i], i
100 // Choose the earliest match (the counters are in unpredictable order).
101 if lo <= line && line <= hi && indexLo > lo {
107 fmt.Fprintln(os.Stderr, "cover_test: no counter for line", line)
111 return coverTest.Count[index], index