github.com/ltltlt/go-source-code@v0.0.0-20190830023027-95be009773aa/cmd/compile/internal/gc/ssa_test.go (about)

     1  // Copyright 2015 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 gc
     6  
     7  import (
     8  	"bytes"
     9  	"internal/testenv"
    10  	"io/ioutil"
    11  	"os"
    12  	"os/exec"
    13  	"path/filepath"
    14  	"runtime"
    15  	"strings"
    16  	"testing"
    17  )
    18  
    19  // TODO: move all these tests elsewhere?
    20  // Perhaps teach test/run.go how to run them with a new action verb.
    21  func runTest(t *testing.T, filename string, flags ...string) {
    22  	t.Parallel()
    23  	doTest(t, filename, "run", flags...)
    24  }
    25  func buildTest(t *testing.T, filename string, flags ...string) {
    26  	t.Parallel()
    27  	doTest(t, filename, "build", flags...)
    28  }
    29  func doTest(t *testing.T, filename string, kind string, flags ...string) {
    30  	testenv.MustHaveGoBuild(t)
    31  	gotool := testenv.GoToolPath(t)
    32  
    33  	var stdout, stderr bytes.Buffer
    34  	args := []string{kind}
    35  	if len(flags) == 0 {
    36  		args = append(args, "-gcflags=-d=ssa/check/on")
    37  	} else {
    38  		args = append(args, flags...)
    39  	}
    40  	args = append(args, filepath.Join("testdata", filename))
    41  	cmd := exec.Command(gotool, args...)
    42  	cmd.Stdout = &stdout
    43  	cmd.Stderr = &stderr
    44  	err := cmd.Run()
    45  	if err != nil {
    46  		t.Fatalf("Failed: %v:\nOut: %s\nStderr: %s\n", err, &stdout, &stderr)
    47  	}
    48  	if s := stdout.String(); s != "" {
    49  		t.Errorf("Stdout = %s\nWant empty", s)
    50  	}
    51  	if s := stderr.String(); strings.Contains(s, "SSA unimplemented") {
    52  		t.Errorf("Unimplemented message found in stderr:\n%s", s)
    53  	}
    54  }
    55  
    56  // runGenTest runs a test-generator, then runs the generated test.
    57  // Generated test can either fail in compilation or execution.
    58  // The environment variable parameter(s) is passed to the run
    59  // of the generated test.
    60  func runGenTest(t *testing.T, filename, tmpname string, ev ...string) {
    61  	testenv.MustHaveGoRun(t)
    62  	gotool := testenv.GoToolPath(t)
    63  	var stdout, stderr bytes.Buffer
    64  	cmd := exec.Command(gotool, "run", filepath.Join("testdata", filename))
    65  	cmd.Stdout = &stdout
    66  	cmd.Stderr = &stderr
    67  	if err := cmd.Run(); err != nil {
    68  		t.Fatalf("Failed: %v:\nOut: %s\nStderr: %s\n", err, &stdout, &stderr)
    69  	}
    70  	// Write stdout into a temporary file
    71  	tmpdir, ok := ioutil.TempDir("", tmpname)
    72  	if ok != nil {
    73  		t.Fatalf("Failed to create temporary directory")
    74  	}
    75  	defer os.RemoveAll(tmpdir)
    76  
    77  	rungo := filepath.Join(tmpdir, "run.go")
    78  	ok = ioutil.WriteFile(rungo, stdout.Bytes(), 0600)
    79  	if ok != nil {
    80  		t.Fatalf("Failed to create temporary file " + rungo)
    81  	}
    82  
    83  	stdout.Reset()
    84  	stderr.Reset()
    85  	cmd = exec.Command(gotool, "run", "-gcflags=-d=ssa/check/on", rungo)
    86  	cmd.Stdout = &stdout
    87  	cmd.Stderr = &stderr
    88  	cmd.Env = append(cmd.Env, ev...)
    89  	err := cmd.Run()
    90  	if err != nil {
    91  		t.Fatalf("Failed: %v:\nOut: %s\nStderr: %s\n", err, &stdout, &stderr)
    92  	}
    93  	if s := stderr.String(); s != "" {
    94  		t.Errorf("Stderr = %s\nWant empty", s)
    95  	}
    96  	if s := stdout.String(); s != "" {
    97  		t.Errorf("Stdout = %s\nWant empty", s)
    98  	}
    99  }
   100  
   101  func TestGenFlowGraph(t *testing.T) {
   102  	runGenTest(t, "flowgraph_generator1.go", "ssa_fg_tmp1")
   103  	if runtime.GOOS != "windows" {
   104  		runGenTest(t, "flowgraph_generator1.go", "ssa_fg_tmp2", "GO_SSA_PHI_LOC_CUTOFF=0")
   105  	}
   106  }
   107  
   108  // TestShortCircuit tests OANDAND and OOROR expressions and short circuiting.
   109  func TestShortCircuit(t *testing.T) { runTest(t, "short.go") }
   110  
   111  // TestBreakContinue tests that continue and break statements do what they say.
   112  func TestBreakContinue(t *testing.T) { runTest(t, "break.go") }
   113  
   114  // TestTypeAssertion tests type assertions.
   115  func TestTypeAssertion(t *testing.T) { runTest(t, "assert.go") }
   116  
   117  // TestArithmetic tests that both backends have the same result for arithmetic expressions.
   118  func TestArithmetic(t *testing.T) { runTest(t, "arith.go") }
   119  
   120  // TestFP tests that both backends have the same result for floating point expressions.
   121  func TestFP(t *testing.T) { runTest(t, "fp.go") }
   122  
   123  func TestFPSoftFloat(t *testing.T) {
   124  	runTest(t, "fp.go", "-gcflags=-d=softfloat,ssa/check/on")
   125  }
   126  
   127  // TestArithmeticBoundary tests boundary results for arithmetic operations.
   128  func TestArithmeticBoundary(t *testing.T) { runTest(t, "arithBoundary.go") }
   129  
   130  // TestArithmeticConst tests results for arithmetic operations against constants.
   131  func TestArithmeticConst(t *testing.T) { runTest(t, "arithConst.go") }
   132  
   133  func TestChan(t *testing.T) { runTest(t, "chan.go") }
   134  
   135  // TestComparisonsConst tests results for comparison operations against constants.
   136  func TestComparisonsConst(t *testing.T) { runTest(t, "cmpConst.go") }
   137  
   138  func TestCompound(t *testing.T) { runTest(t, "compound.go") }
   139  
   140  func TestCtl(t *testing.T) { runTest(t, "ctl.go") }
   141  
   142  func TestLoadStore(t *testing.T) { runTest(t, "loadstore.go") }
   143  
   144  func TestMap(t *testing.T) { runTest(t, "map.go") }
   145  
   146  func TestRegalloc(t *testing.T) { runTest(t, "regalloc.go") }
   147  
   148  func TestString(t *testing.T) { runTest(t, "string.go") }
   149  
   150  func TestDeferNoReturn(t *testing.T) { buildTest(t, "deferNoReturn.go") }
   151  
   152  // TestClosure tests closure related behavior.
   153  func TestClosure(t *testing.T) { runTest(t, "closure.go") }
   154  
   155  func TestArray(t *testing.T) { runTest(t, "array.go") }
   156  
   157  func TestAppend(t *testing.T) { runTest(t, "append.go") }
   158  
   159  func TestZero(t *testing.T) { runTest(t, "zero.go") }
   160  
   161  func TestAddressed(t *testing.T) { runTest(t, "addressed.go") }
   162  
   163  func TestCopy(t *testing.T) { runTest(t, "copy.go") }
   164  
   165  func TestUnsafe(t *testing.T) { runTest(t, "unsafe.go") }
   166  
   167  func TestPhi(t *testing.T) { runTest(t, "phi.go") }
   168  
   169  func TestSlice(t *testing.T) { runTest(t, "slice.go") }
   170  
   171  func TestNamedReturn(t *testing.T) { runTest(t, "namedReturn.go") }
   172  
   173  func TestDuplicateLoad(t *testing.T) { runTest(t, "dupLoad.go") }
   174  
   175  func TestSqrt(t *testing.T) { runTest(t, "sqrt_const.go") }