summaryrefslogtreecommitdiff
path: root/libgo/go/testing/cover.go
diff options
context:
space:
mode:
Diffstat (limited to 'libgo/go/testing/cover.go')
-rw-r--r--libgo/go/testing/cover.go28
1 files changed, 27 insertions, 1 deletions
diff --git a/libgo/go/testing/cover.go b/libgo/go/testing/cover.go
index dd29364d87..a4ce37f7c2 100644
--- a/libgo/go/testing/cover.go
+++ b/libgo/go/testing/cover.go
@@ -9,6 +9,7 @@ package testing
import (
"fmt"
"os"
+ "sync/atomic"
)
// CoverBlock records the coverage data for a single basic block.
@@ -34,6 +35,29 @@ type Cover struct {
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 i := range counters {
+ if atomic.LoadUint32(&counters[i]) > 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.
@@ -61,11 +85,13 @@ func coverReport() {
}
var active, total int64
+ var count uint32
for name, counts := range cover.Counters {
blocks := cover.Blocks[name]
- for i, count := range counts {
+ for i := range counts {
stmts := int64(blocks[i].Stmts)
total += stmts
+ count = atomic.LoadUint32(&counts[i]) // For -mode=atomic.
if count > 0 {
active += stmts
}