link_test.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. package big
  5. import (
  6. "bytes"
  7. "internal/testenv"
  8. "os"
  9. "os/exec"
  10. "path/filepath"
  11. "testing"
  12. )
  13. // Tests that the linker is able to remove references to Float, Rat,
  14. // and Int if unused (notably, not used by init).
  15. func TestLinkerGC(t *testing.T) {
  16. if testing.Short() {
  17. t.Skip("skipping in short mode")
  18. }
  19. t.Parallel()
  20. tmp := t.TempDir()
  21. goBin := testenv.GoToolPath(t)
  22. goFile := filepath.Join(tmp, "x.go")
  23. file := []byte(`package main
  24. import _ "math/big"
  25. func main() {}
  26. `)
  27. if err := os.WriteFile(goFile, file, 0644); err != nil {
  28. t.Fatal(err)
  29. }
  30. cmd := exec.Command(goBin, "build", "-o", "x.exe", "x.go")
  31. cmd.Dir = tmp
  32. if out, err := cmd.CombinedOutput(); err != nil {
  33. t.Fatalf("compile: %v, %s", err, out)
  34. }
  35. cmd = exec.Command(goBin, "tool", "nm", "x.exe")
  36. cmd.Dir = tmp
  37. nm, err := cmd.CombinedOutput()
  38. if err != nil {
  39. t.Fatalf("nm: %v, %s", err, nm)
  40. }
  41. const want = "runtime.main"
  42. if !bytes.Contains(nm, []byte(want)) {
  43. // Test the test.
  44. t.Errorf("expected symbol %q not found", want)
  45. }
  46. bad := []string{
  47. "math/big.(*Float)",
  48. "math/big.(*Rat)",
  49. "math/big.(*Int)",
  50. }
  51. for _, sym := range bad {
  52. if bytes.Contains(nm, []byte(sym)) {
  53. t.Errorf("unexpected symbol %q found", sym)
  54. }
  55. }
  56. if t.Failed() {
  57. t.Logf("Got: %s", nm)
  58. }
  59. }