aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/compile/internal/base/hashdebug_test.go
diff options
context:
space:
mode:
authorRuss Cox <rsc@golang.org>2023-05-08 23:18:38 -0400
committerRuss Cox <rsc@golang.org>2023-05-09 16:07:06 +0000
commitda5a3146ec903cdcb779d501be4ff88fd775820e (patch)
tree12c4fce48a5d3d1ea05fb08a5cf7578e77bb268d /src/cmd/compile/internal/base/hashdebug_test.go
parentffc4cc05f596a38c19f0d7e1ee91f17527ac3b37 (diff)
downloadgo-da5a3146ec903cdcb779d501be4ff88fd775820e.tar.xz
cmd/compile: use more of internal/bisect in HashDebug
Using more of internal/bisect gives us more that will be deleted from base/hashdebug.go when we have updated the tools that need the old protocol. It is also cheaper: there is no allocation to make a decision about whether to enable, and no locking unless printing is needed. Change-Id: I43ec398461205a1a9e988512a134ed6b3a3b1587 Reviewed-on: https://go-review.googlesource.com/c/go/+/493736 Run-TryBot: Russ Cox <rsc@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: David Chase <drchase@google.com>
Diffstat (limited to 'src/cmd/compile/internal/base/hashdebug_test.go')
-rw-r--r--src/cmd/compile/internal/base/hashdebug_test.go110
1 files changed, 42 insertions, 68 deletions
diff --git a/src/cmd/compile/internal/base/hashdebug_test.go b/src/cmd/compile/internal/base/hashdebug_test.go
index 0b83712532..e6ced4d148 100644
--- a/src/cmd/compile/internal/base/hashdebug_test.go
+++ b/src/cmd/compile/internal/base/hashdebug_test.go
@@ -6,26 +6,27 @@ package base
import (
"bytes"
+ "internal/bisect"
"strings"
"testing"
)
func TestHashDebugGossahashY(t *testing.T) {
- hd := NewHashDebug("GOSSAHASH", "y", new(bufferWithSync))
+ hd := NewHashDebug("GOSSAHASH", "y", new(bytes.Buffer))
if hd == nil {
t.Errorf("NewHashDebug should not return nil for GOSSASHASH=y")
}
- if !hd.DebugHashMatch("anything") {
+ if !hd.MatchPkgFunc("anything", "anyfunc") {
t.Errorf("NewHashDebug should return yes for everything for GOSSASHASH=y")
}
}
func TestHashDebugGossahashN(t *testing.T) {
- hd := NewHashDebug("GOSSAHASH", "n", new(bufferWithSync))
+ hd := NewHashDebug("GOSSAHASH", "n", new(bytes.Buffer))
if hd == nil {
t.Errorf("NewHashDebug should not return nil for GOSSASHASH=n")
}
- if hd.DebugHashMatch("anything") {
+ if hd.MatchPkgFunc("anything", "anyfunc") {
t.Errorf("NewHashDebug should return no for everything for GOSSASHASH=n")
}
}
@@ -49,74 +50,61 @@ func TestHashDebugMagic(t *testing.T) {
}
func TestHash(t *testing.T) {
- h0 := hashOf("bar", 0)
- h1 := hashOf("bar", 1)
- t.Logf(`These values are used in other tests: hashOf("bar,0)"=0x%x, hashOf("bar,1)"=0x%x`, h0, h1)
+ h0 := bisect.Hash("bar", "0")
+ h1 := bisect.Hash("bar", "1")
+ t.Logf(`These values are used in other tests: Hash("bar", "0")=%#64b, Hash("bar", "1")=%#64b`, h0, h1)
if h0 == h1 {
t.Errorf("Hashes 0x%x and 0x%x should differ", h0, h1)
}
}
func TestHashMatch(t *testing.T) {
- ws := new(bufferWithSync)
- hd := NewHashDebug("GOSSAHASH", "0011", ws)
- check := hd.DebugHashMatch("bar")
- msg := ws.String()
+ b := new(bytes.Buffer)
+ hd := NewHashDebug("GOSSAHASH", "1110", b)
+ check := hd.MatchPkgFunc("bar", "0")
+ msg := b.String()
t.Logf("message was '%s'", msg)
if !check {
- t.Errorf("GOSSAHASH=0011 should have matched for 'bar'")
+ t.Errorf("GOSSAHASH=1110 should have matched for 'bar', '0'")
}
- wantPrefix(t, msg, "GOSSAHASH triggered bar ")
- wantContains(t, msg, "\nbar [bisect-match ")
-}
-
-func TestHashMatchParam(t *testing.T) {
- ws := new(bufferWithSync)
- hd := NewHashDebug("GOSSAHASH", "1010", ws)
- check := hd.DebugHashMatchParam("bar", 1)
- msg := ws.String()
- t.Logf("message was '%s'", msg)
- if !check {
- t.Errorf("GOSSAHASH=1010 should have matched for 'bar', 1")
- }
- wantPrefix(t, msg, "GOSSAHASH triggered bar:1 ")
- wantContains(t, msg, "\nbar:1 [bisect-match ")
+ wantPrefix(t, msg, "bar.0 [bisect-match ")
+ wantContains(t, msg, "\nGOSSAHASH triggered bar.0 ")
}
func TestYMatch(t *testing.T) {
- ws := new(bufferWithSync)
- hd := NewHashDebug("GOSSAHASH", "y", ws)
- check := hd.DebugHashMatch("bar")
- msg := ws.String()
+ b := new(bytes.Buffer)
+ hd := NewHashDebug("GOSSAHASH", "y", b)
+ check := hd.MatchPkgFunc("bar", "0")
+ msg := b.String()
t.Logf("message was '%s'", msg)
if !check {
- t.Errorf("GOSSAHASH=y should have matched for 'bar'")
+ t.Errorf("GOSSAHASH=y should have matched for 'bar', '0'")
}
- wantPrefix(t, msg, "GOSSAHASH triggered bar 110101000010000100000011")
- wantContains(t, msg, "\nbar [bisect-match ")
+ wantPrefix(t, msg, "bar.0 [bisect-match ")
+ wantContains(t, msg, "\nGOSSAHASH triggered bar.0 010100100011100101011110")
}
func TestNMatch(t *testing.T) {
- ws := new(bufferWithSync)
- hd := NewHashDebug("GOSSAHASH", "n", ws)
- check := hd.DebugHashMatch("bar")
- msg := ws.String()
+ b := new(bytes.Buffer)
+ hd := NewHashDebug("GOSSAHASH", "n", b)
+ check := hd.MatchPkgFunc("bar", "0")
+ msg := b.String()
t.Logf("message was '%s'", msg)
if check {
- t.Errorf("GOSSAHASH=n should NOT have matched for 'bar'")
+ t.Errorf("GOSSAHASH=n should NOT have matched for 'bar', '0'")
}
- wantPrefix(t, msg, "GOSSAHASH triggered bar 110101000010000100000011")
- wantContains(t, msg, "\nbar [bisect-match ")
+ wantPrefix(t, msg, "bar.0 [bisect-match ")
+ wantContains(t, msg, "\nGOSSAHASH triggered bar.0 010100100011100101011110")
}
func TestHashNoMatch(t *testing.T) {
- ws := new(bufferWithSync)
- hd := NewHashDebug("GOSSAHASH", "001100", ws)
- check := hd.DebugHashMatch("bar")
- msg := ws.String()
+ b := new(bytes.Buffer)
+ hd := NewHashDebug("GOSSAHASH", "01110", b)
+ check := hd.MatchPkgFunc("bar", "0")
+ msg := b.String()
t.Logf("message was '%s'", msg)
if check {
- t.Errorf("GOSSAHASH=001100 should NOT have matched for 'bar'")
+ t.Errorf("GOSSAHASH=001100 should NOT have matched for 'bar', '0'")
}
if msg != "" {
t.Errorf("Message should have been empty, instead %s", msg)
@@ -125,41 +113,27 @@ func TestHashNoMatch(t *testing.T) {
}
func TestHashSecondMatch(t *testing.T) {
- ws := new(bufferWithSync)
- hd := NewHashDebug("GOSSAHASH", "001100/0011", ws)
+ b := new(bytes.Buffer)
+ hd := NewHashDebug("GOSSAHASH", "01110/11110", b)
- check := hd.DebugHashMatch("bar")
- msg := ws.String()
+ check := hd.MatchPkgFunc("bar", "0")
+ msg := b.String()
t.Logf("message was '%s'", msg)
if !check {
- t.Errorf("GOSSAHASH=001100, GOSSAHASH0=0011 should have matched for 'bar'")
+ t.Errorf("GOSSAHASH=001100, GOSSAHASH0=0011 should have matched for 'bar', '0'")
}
- wantPrefix(t, msg, "GOSSAHASH0 triggered bar")
-}
-
-type bufferWithSync struct {
- b bytes.Buffer
-}
-
-func (ws *bufferWithSync) Sync() error {
- return nil
-}
-
-func (ws *bufferWithSync) Write(p []byte) (n int, err error) {
- return (&ws.b).Write(p)
-}
-
-func (ws *bufferWithSync) String() string {
- return strings.TrimSpace((&ws.b).String())
+ wantContains(t, msg, "\nGOSSAHASH0 triggered bar")
}
func wantPrefix(t *testing.T, got, want string) {
+ t.Helper()
if !strings.HasPrefix(got, want) {
t.Errorf("want prefix %q, got:\n%s", want, got)
}
}
func wantContains(t *testing.T, got, want string) {
+ t.Helper()
if !strings.Contains(got, want) {
t.Errorf("want contains %q, got:\n%s", want, got)
}