testing: add Coverage function
I've found this very useful for generating
good test case lists for -short mode for
the disassemblers.
Fixes #7959.
LGTM=r
R=r
CC=golang-codereviews
https://golang.org/cl/98150043
diff --git a/src/pkg/testing/cover.go b/src/pkg/testing/cover.go
index dd29364..eb7249d 100644
--- a/src/pkg/testing/cover.go
+++ b/src/pkg/testing/cover.go
@@ -34,6 +34,29 @@
CoveredPackages string
}
+// Coverage reports the current code coverage as a fraction in the range [0, 1].
+// If coverage is not enabled, Coverage returns 0.
+//
+// When running a large set of sequential test cases, checking Coverage after each one
+// can be useful for identifying which test cases exercise new code paths.
+// It is not a replacement for the reports generated by 'go test -cover' and
+// 'go tool cover'.
+func Coverage() float64 {
+ var n, d int64
+ for _, counters := range cover.Counters {
+ for _, c := range counters {
+ if c > 0 {
+ n++
+ }
+ d++
+ }
+ }
+ if d == 0 {
+ return 0
+ }
+ return float64(n) / float64(d)
+}
+
// RegisterCover records the coverage data accumulators for the tests.
// NOTE: This function is internal to the testing infrastructure and may change.
// It is not covered (yet) by the Go 1 compatibility guidelines.