1
0
mirror of https://github.com/golang/go synced 2024-11-18 23:05:06 -07:00
go/cmd/cover/testdata/main.go
Rob Pike c28528d489 go.tools/cmd/cover: add a test
Test the statistics work as expected for a simple program, which can be extended as needed. This is all a bit meta.

R=golang-dev, rsc
CC=golang-dev
https://golang.org/cl/10392050
2013-06-21 14:35:09 -07:00

65 lines
1.2 KiB
Go

// Copyright 2013 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Test runner for coverage test. This file is not coverage-annotated; test.go is.
// It knows the coverage counter is called "coverTest".
package main
import (
"fmt"
"os"
)
func main() {
testAll()
verify()
}
type block struct {
count uint32
line uint32
}
var counters = make(map[block]bool)
// check records the location and expected value for a counter.
func check(line, count uint32) {
b := block{
count,
line,
}
counters[b] = true
}
// verify checks the expected counts against the actual. It runs after the test has completed.
func verify() {
ok := true
for b := range counters {
got := count(b.line)
if b.count == anything && got != 0 {
got = anything
}
if got != b.count {
fmt.Fprintf(os.Stderr, "test_go:%d expected count %d got %d\n", b.line, b.count, got)
ok = false
}
}
if !ok {
fmt.Fprintf(os.Stderr, "FAIL\n")
os.Exit(2)
}
}
func count(line uint32) uint32 {
// Linear search is fine.
for i := range coverTest.Count {
lo, hi := coverTest.Pos[3*i], coverTest.Pos[3*i+1]
if lo <= line && line <= hi {
return coverTest.Count[i]
}
}
return 0
}