| 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. |
| 4 | |
| 5 | // Test runner for coverage test. This file is not coverage-annotated; test.go is. |
| 6 | // It knows the coverage counter is called "coverTest". |
| 7 | |
| 8 | package main |
| 9 | |
| 10 | import ( |
| 11 | "fmt" |
| 12 | "os" |
| 13 | ) |
| 14 | |
| 15 | func main() { |
| 16 | testAll() |
| 17 | verify() |
| 18 | } |
| 19 | |
| 20 | type block struct { |
| 21 | count uint32 |
| 22 | line uint32 |
| 23 | } |
| 24 | |
| 25 | var counters = make(map[block]bool) |
| 26 | |
| 27 | // check records the location and expected value for a counter. |
| 28 | func check(line, count uint32) { |
| 29 | b := block{ |
| 30 | count, |
| 31 | line, |
| 32 | } |
| 33 | counters[b] = true |
| 34 | } |
| 35 | |
| 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 { |
| 39 | b := block{ |
| 40 | count, |
| 41 | line, |
| 42 | } |
| 43 | counters[b] = true |
| 44 | return val |
| 45 | } |
| 46 | |
| 47 | var PASS = true |
| 48 | |
| 49 | // verify checks the expected counts against the actual. It runs after the test has completed. |
| 50 | func verify() { |
| 51 | for b := range counters { |
| 52 | got, index := count(b.line) |
| 53 | if b.count == anything && got != 0 { |
| 54 | got = anything |
| 55 | } |
| 56 | if got != b.count { |
| 57 | fmt.Fprintf(os.Stderr, "test_go:%d expected count %d got %d [counter %d]\n", b.line, b.count, got, index) |
| 58 | PASS = false |
| 59 | } |
| 60 | } |
| 61 | verifyPanic() |
| 62 | if !PASS { |
| 63 | fmt.Fprintf(os.Stderr, "FAIL\n") |
| 64 | os.Exit(2) |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | // verifyPanic is a special check for the known counter that should be |
| 69 | // after the panic call in testPanic. |
| 70 | func verifyPanic() { |
| 71 | if coverTest.Count[panicIndex-1] != 1 { |
| 72 | // Sanity check for test before panic. |
| 73 | fmt.Fprintf(os.Stderr, "bad before panic") |
| 74 | PASS = false |
| 75 | } |
| 76 | if coverTest.Count[panicIndex] != 0 { |
| 77 | fmt.Fprintf(os.Stderr, "bad at panic: %d should be 0\n", coverTest.Count[panicIndex]) |
| 78 | PASS = false |
| 79 | } |
| 80 | if coverTest.Count[panicIndex+1] != 1 { |
| 81 | fmt.Fprintf(os.Stderr, "bad after panic") |
| 82 | PASS = false |
| 83 | } |
| 84 | } |
| 85 | |
| 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. |
| 93 | index := -1 |
| 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 |
| 99 | } |
| 100 | // Choose the earliest match (the counters are in unpredictable order). |
| 101 | if lo <= line && line <= hi && indexLo > lo { |
| 102 | index = i |
| 103 | indexLo = lo |
| 104 | } |
| 105 | } |
| 106 | if index == -1 { |
| 107 | fmt.Fprintln(os.Stderr, "cover_test: no counter for line", line) |
| 108 | PASS = false |
| 109 | return 0, 0 |
| 110 | } |
| 111 | return coverTest.Count[index], index |
| 112 | } |
| 113 |
Members