mirror of
https://github.com/golang/go
synced 2024-11-23 22:40:04 -07:00
cmd/cover: fix sorting of profile segment boundaries, again
This is a refinement of CL 114855, which fixed the empty clause case, but broke some other cases where segment boundaries can coincide for other reasons. Fixes #25767. Change-Id: I2a387c83f9d651c8358f3e11b03f6167af0eb8bf Reviewed-on: https://go-review.googlesource.com/116976 Run-TryBot: David Symonds <dsymonds@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Rob Pike <r@golang.org>
This commit is contained in:
parent
d8a1465ca8
commit
aadaec5045
@ -153,6 +153,7 @@ type Boundary struct {
|
||||
Start bool // Is this the start of a block?
|
||||
Count int // Event count from the cover profile.
|
||||
Norm float64 // Count normalized to [0..1].
|
||||
Index int // Order in input file.
|
||||
}
|
||||
|
||||
// Boundaries returns a Profile as a set of Boundary objects within the provided src.
|
||||
@ -168,8 +169,10 @@ func (p *Profile) Boundaries(src []byte) (boundaries []Boundary) {
|
||||
divisor := math.Log(float64(max))
|
||||
|
||||
// boundary returns a Boundary, populating the Norm field with a normalized Count.
|
||||
index := 0
|
||||
boundary := func(offset int, start bool, count int) Boundary {
|
||||
b := Boundary{Offset: offset, Start: start, Count: count}
|
||||
b := Boundary{Offset: offset, Start: start, Count: count, Index: index}
|
||||
index++
|
||||
if !start || count == 0 {
|
||||
return b
|
||||
}
|
||||
@ -209,10 +212,9 @@ func (b boundariesByPos) Len() int { return len(b) }
|
||||
func (b boundariesByPos) Swap(i, j int) { b[i], b[j] = b[j], b[i] }
|
||||
func (b boundariesByPos) Less(i, j int) bool {
|
||||
if b[i].Offset == b[j].Offset {
|
||||
// Boundaries at the same offset should be ordered Start < !Start.
|
||||
// They represent empty sections of code (e.g. a switch/select clause
|
||||
// without a body).
|
||||
return b[i].Start && !b[j].Start
|
||||
// Boundaries at the same offset should be ordered according to
|
||||
// their original position.
|
||||
return b[i].Index < b[j].Index
|
||||
}
|
||||
return b[i].Offset < b[j].Offset
|
||||
}
|
||||
|
12
src/cmd/cover/testdata/html/html.go
vendored
12
src/cmd/cover/testdata/html/html.go
vendored
@ -1,5 +1,7 @@
|
||||
package html
|
||||
|
||||
import "fmt"
|
||||
|
||||
// This file is tested by html_test.go.
|
||||
// The comments below are markers for extracting the annotated source
|
||||
// from the HTML output.
|
||||
@ -16,3 +18,13 @@ func f() {
|
||||
}
|
||||
|
||||
// END f
|
||||
|
||||
// https://golang.org/issue/25767
|
||||
// START g
|
||||
func g() {
|
||||
if false {
|
||||
fmt.Printf("Hello")
|
||||
}
|
||||
}
|
||||
|
||||
// END g
|
||||
|
8
src/cmd/cover/testdata/html/html.golden
vendored
8
src/cmd/cover/testdata/html/html.golden
vendored
@ -8,3 +8,11 @@ func f() <span class="cov8" title="1">{
|
||||
}
|
||||
|
||||
// END f
|
||||
// START g
|
||||
func g() <span class="cov8" title="1">{
|
||||
if false </span><span class="cov0" title="0">{
|
||||
fmt.Printf("Hello")
|
||||
}</span>
|
||||
}
|
||||
|
||||
// END g
|
||||
|
1
src/cmd/cover/testdata/html/html_test.go
vendored
1
src/cmd/cover/testdata/html/html_test.go
vendored
@ -4,4 +4,5 @@ import "testing"
|
||||
|
||||
func TestAll(t *testing.T) {
|
||||
f()
|
||||
g()
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user