]> Cypherpunks repositories - gostls13.git/commitdiff
testing: add Coverage function
authorRuss Cox <rsc@golang.org>
Wed, 23 Jul 2014 02:56:35 +0000 (22:56 -0400)
committerRuss Cox <rsc@golang.org>
Wed, 23 Jul 2014 02:56:35 +0000 (22:56 -0400)
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

doc/go1.4.txt
src/pkg/testing/cover.go

index 689c73115a433872bd9956254a28632ea23bf1c7..3cf595f199cdce90b6508e4a2bc3110ccc8f3da1 100644 (file)
@@ -11,5 +11,6 @@ encoding/gob: remove unsafe (CL 102680045)
 misc: deleted editor support; refer to https://code.google.com/p/go-wiki/wiki/IDEsAndTextEditorPlugins instead (CL 105470043)
 runtime/race: freebsd is supported (CL 107270043)
 syscall: Setuid, Setgid are disabled on linux platforms. On linux those syscalls operate on the calling thread, not the whole process. This does not match the semantics of other platforms, nor the expectations of the caller, so the operations have been disabled until issue 1435 is resolved (CL 106170043)
+testing: add Coverage (CL 98150043)
 text/scanner: add IsIdentRune field of Scanner. (CL 108030044)
 time: use the micro symbol (ยต (U+00B5)) to print microsecond duration (CL 105030046)
index dd29364d87e56dbd4adc4551588dc96bc79fdc4e..eb7249dccde6bc9e2b3e4220ee4c7304c02f6e28 100644 (file)
@@ -34,6 +34,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 _, 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.