...

Source file src/cmd/compile/internal/base/hashdebug_test.go

Documentation: cmd/compile/internal/base

     1  // Copyright 2022 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package base
     6  
     7  import (
     8  	"bytes"
     9  	"internal/bisect"
    10  	"strings"
    11  	"testing"
    12  )
    13  
    14  func TestHashDebugGossahashY(t *testing.T) {
    15  	hd := NewHashDebug("GOSSAHASH", "y", new(bytes.Buffer))
    16  	if hd == nil {
    17  		t.Errorf("NewHashDebug should not return nil for GOSSASHASH=y")
    18  	}
    19  	if !hd.MatchPkgFunc("anything", "anyfunc", nil) {
    20  		t.Errorf("NewHashDebug should return yes for everything for GOSSASHASH=y")
    21  	}
    22  }
    23  
    24  func TestHashDebugGossahashN(t *testing.T) {
    25  	hd := NewHashDebug("GOSSAHASH", "n", new(bytes.Buffer))
    26  	if hd == nil {
    27  		t.Errorf("NewHashDebug should not return nil for GOSSASHASH=n")
    28  	}
    29  	if hd.MatchPkgFunc("anything", "anyfunc", nil) {
    30  		t.Errorf("NewHashDebug should return no for everything for GOSSASHASH=n")
    31  	}
    32  }
    33  
    34  func TestHashDebugGossahashEmpty(t *testing.T) {
    35  	hd := NewHashDebug("GOSSAHASH", "", nil)
    36  	if hd != nil {
    37  		t.Errorf("NewHashDebug should return nil for GOSSASHASH=\"\"")
    38  	}
    39  }
    40  
    41  func TestHashDebugMagic(t *testing.T) {
    42  	hd := NewHashDebug("FOOXYZZY", "y", nil)
    43  	hd0 := NewHashDebug("FOOXYZZY0", "n", nil)
    44  	if hd == nil {
    45  		t.Errorf("NewHashDebug should have succeeded for FOOXYZZY")
    46  	}
    47  	if hd0 == nil {
    48  		t.Errorf("NewHashDebug should have succeeded for FOOXYZZY0")
    49  	}
    50  }
    51  
    52  func TestHash(t *testing.T) {
    53  	h0 := bisect.Hash("bar", "0")
    54  	h1 := bisect.Hash("bar", "1")
    55  	t.Logf(`These values are used in other tests: Hash("bar", "0")=%#64b, Hash("bar", "1")=%#64b`, h0, h1)
    56  	if h0 == h1 {
    57  		t.Errorf("Hashes 0x%x and 0x%x should differ", h0, h1)
    58  	}
    59  }
    60  
    61  func TestHashMatch(t *testing.T) {
    62  	b := new(bytes.Buffer)
    63  	hd := NewHashDebug("GOSSAHASH", "v1110", b)
    64  	check := hd.MatchPkgFunc("bar", "0", func() string { return "note" })
    65  	msg := b.String()
    66  	t.Logf("message was '%s'", msg)
    67  	if !check {
    68  		t.Errorf("GOSSAHASH=1110 should have matched for 'bar', '0'")
    69  	}
    70  	wantPrefix(t, msg, "bar.0: note [bisect-match ")
    71  	wantContains(t, msg, "\nGOSSAHASH triggered bar.0: note ")
    72  }
    73  
    74  func TestYMatch(t *testing.T) {
    75  	b := new(bytes.Buffer)
    76  	hd := NewHashDebug("GOSSAHASH", "vy", b)
    77  	check := hd.MatchPkgFunc("bar", "0", nil)
    78  	msg := b.String()
    79  	t.Logf("message was '%s'", msg)
    80  	if !check {
    81  		t.Errorf("GOSSAHASH=y should have matched for 'bar', '0'")
    82  	}
    83  	wantPrefix(t, msg, "bar.0 [bisect-match ")
    84  	wantContains(t, msg, "\nGOSSAHASH triggered bar.0 010100100011100101011110")
    85  }
    86  
    87  func TestNMatch(t *testing.T) {
    88  	b := new(bytes.Buffer)
    89  	hd := NewHashDebug("GOSSAHASH", "vn", b)
    90  	check := hd.MatchPkgFunc("bar", "0", nil)
    91  	msg := b.String()
    92  	t.Logf("message was '%s'", msg)
    93  	if check {
    94  		t.Errorf("GOSSAHASH=n should NOT have matched for 'bar', '0'")
    95  	}
    96  	wantPrefix(t, msg, "bar.0 [DISABLED] [bisect-match ")
    97  	wantContains(t, msg, "\nGOSSAHASH triggered bar.0 [DISABLED] 010100100011100101011110")
    98  }
    99  
   100  func TestHashNoMatch(t *testing.T) {
   101  	b := new(bytes.Buffer)
   102  	hd := NewHashDebug("GOSSAHASH", "01110", b)
   103  	check := hd.MatchPkgFunc("bar", "0", nil)
   104  	msg := b.String()
   105  	t.Logf("message was '%s'", msg)
   106  	if check {
   107  		t.Errorf("GOSSAHASH=001100 should NOT have matched for 'bar', '0'")
   108  	}
   109  	if msg != "" {
   110  		t.Errorf("Message should have been empty, instead %s", msg)
   111  	}
   112  
   113  }
   114  
   115  func TestHashSecondMatch(t *testing.T) {
   116  	b := new(bytes.Buffer)
   117  	hd := NewHashDebug("GOSSAHASH", "01110/11110", b)
   118  
   119  	check := hd.MatchPkgFunc("bar", "0", nil)
   120  	msg := b.String()
   121  	t.Logf("message was '%s'", msg)
   122  	if !check {
   123  		t.Errorf("GOSSAHASH=001100, GOSSAHASH0=0011 should have matched for 'bar', '0'")
   124  	}
   125  	wantContains(t, msg, "\nGOSSAHASH0 triggered bar")
   126  }
   127  
   128  func wantPrefix(t *testing.T, got, want string) {
   129  	t.Helper()
   130  	if !strings.HasPrefix(got, want) {
   131  		t.Errorf("want prefix %q, got:\n%s", want, got)
   132  	}
   133  }
   134  
   135  func wantContains(t *testing.T, got, want string) {
   136  	t.Helper()
   137  	if !strings.Contains(got, want) {
   138  		t.Errorf("want contains %q, got:\n%s", want, got)
   139  	}
   140  }
   141  

View as plain text