GoPLS Viewer

Home|gopls/cmd/cover/testdata/main.go
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
8package main
9
10import (
11    "fmt"
12    "os"
13)
14
15func main() {
16    testAll()
17    verify()
18}
19
20type block struct {
21    count uint32
22    line  uint32
23}
24
25var counters = make(map[block]bool)
26
27// check records the location and expected value for a counter.
28func check(linecount 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.
38func checkVal(linecount uint32val intint {
39    b := block{
40        count,
41        line,
42    }
43    counters[b] = true
44    return val
45}
46
47var PASS = true
48
49// verify checks the expected counts against the actual. It runs after the test has completed.
50func verify() {
51    for b := range counters {
52        gotindex := 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.lineb.countgotindex)
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.
70func 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.
87func count(line uint32) (uint32int) {
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        lohi := 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 00
110    }
111    return coverTest.Count[index], index
112}
113
MembersX
count.indexLo
check.line
check.count
PASS
verify
verify.RangeStmt_970.b
verify.RangeStmt_970.BlockStmt.got
verifyPanic
count.RangeStmt_2373.i
check.b
checkVal.b
count
fmt
os
checkVal.line
checkVal.count
verify.RangeStmt_970.BlockStmt.index
count.line
main
block
block.count
block.line
check
checkVal
checkVal.val
Members
X