...
1
2
3
4
5 package test
6
7 import (
8 "internal/testenv"
9 "os"
10 "path/filepath"
11 "regexp"
12 "testing"
13 )
14
15
16
17 func TestInst(t *testing.T) {
18 testenv.MustHaveGoBuild(t)
19 testenv.MustHaveGoRun(t)
20
21
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
35
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
41
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
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 }
61
View as plain text