github.com/guyezi/gofrontend@v0.0.0-20200228202240-7a62a49e62c0/libgo/go/runtime/checkptr_test.go (about)

     1  // Copyright 2020 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 runtime_test
     6  
     7  import (
     8  	"internal/testenv"
     9  	"os/exec"
    10  	"runtime"
    11  	"strings"
    12  	"testing"
    13  )
    14  
    15  func TestCheckPtr(t *testing.T) {
    16  	if runtime.Compiler == "gccgo" {
    17  		t.Skip("gccgo does not have -d=checkptr")
    18  	}
    19  	t.Parallel()
    20  	testenv.MustHaveGoRun(t)
    21  
    22  	exe, err := buildTestProg(t, "testprog", "-gcflags=all=-d=checkptr=1")
    23  	if err != nil {
    24  		t.Fatal(err)
    25  	}
    26  
    27  	testCases := []struct {
    28  		cmd  string
    29  		want string
    30  	}{
    31  		{"CheckPtrAlignment", "fatal error: checkptr: unsafe pointer conversion\n"},
    32  		{"CheckPtrArithmetic", "fatal error: checkptr: unsafe pointer arithmetic\n"},
    33  		{"CheckPtrSize", "fatal error: checkptr: unsafe pointer conversion\n"},
    34  		{"CheckPtrSmall", "fatal error: checkptr: unsafe pointer arithmetic\n"},
    35  	}
    36  
    37  	for _, tc := range testCases {
    38  		tc := tc
    39  		t.Run(tc.cmd, func(t *testing.T) {
    40  			t.Parallel()
    41  			got, err := testenv.CleanCmdEnv(exec.Command(exe, tc.cmd)).CombinedOutput()
    42  			if err != nil {
    43  				t.Log(err)
    44  			}
    45  			if !strings.HasPrefix(string(got), tc.want) {
    46  				t.Errorf("output:\n%s\n\nwant output starting with: %s", got, tc.want)
    47  			}
    48  		})
    49  	}
    50  }