Debian Codegraph @

src/cmd/compile/internal/test/inst_test.go

raw
1 // Copyright 2021 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 test
6
7 import (
8 "internal/testenv"
9 "os"
10 "path/filepath"
11 "regexp"
12 "testing"
13 )
14
15 // TestInst tests that only one instantiation of Sort is created, even though generic
16 // Sort is used for multiple pointer types across two packages.
17 func TestInst(t *testing.T) {
18 testenv.MustHaveGoBuild(t)
19 testenv.MustHaveGoRun(t)
20
21 // Build ptrsort.go, which uses package mysort.
22 var output []byte
23 var err error
24 filename := "ptrsort.go"
25 exename := "ptrsort"
26 outname := "ptrsort.out"
27 gotool := testenv.GoToolPath(t)
28 dest := filepath.Join(t.TempDir(), exename)
29 cmd := testenv.Command(t, gotool, "build", "-o", dest, filepath.Join("testdata", filename))
30 if output, err = cmd.CombinedOutput(); err != nil {
31 t.Fatalf("Failed: %v:\nOutput: %s\n", err, output)
32 }
33
34 // Test that there is exactly one shape-based instantiation of Sort in
35 // the executable.
36 cmd = testenv.Command(t, gotool, "tool", "nm", dest)
37 if output, err = cmd.CombinedOutput(); err != nil {
38 t.Fatalf("Failed: %v:\nOut: %s\n", err, output)
39 }
40 // Look for shape-based instantiation of Sort, but ignore any extra wrapper
41 // ending in "-tramp" (which are created on riscv).
42 re := regexp.MustCompile(`\bSort\[.*shape.*\][^-]`)
43 r := re.FindAllIndex(output, -1)
44 if len(r) != 1 {
45 t.Fatalf("Wanted 1 instantiations of Sort function, got %d\n", len(r))
46 }
47
48 // Actually run the test and make sure output is correct.
49 cmd = testenv.Command(t, gotool, "run", filepath.Join("testdata", filename))
50 if output, err = cmd.CombinedOutput(); err != nil {
51 t.Fatalf("Failed: %v:\nOut: %s\n", err, output)
52 }
53 out, err := os.ReadFile(filepath.Join("testdata", outname))
54 if err != nil {
55 t.Fatalf("Could not find %s\n", outname)
56 }
57 if string(out) != string(output) {
58 t.Fatalf("Wanted output %v, got %v\n", string(out), string(output))
59 }
60 }