modernc.org/gc@v1.0.1-0.20240304020402-f0dba7c97c2b/testdata/errchk/test/fixedbugs/bug369.go (about)

     1  // +build !nacl,!windows
     2  // run
     3  
     4  // Copyright 2011 The Go Authors. All rights reserved.
     5  // Use of this source code is governed by a BSD-style
     6  // license that can be found in the LICENSE file.
     7  
     8  // Test that compiling with optimization turned on produces faster code.
     9  
    10  package main
    11  
    12  import (
    13  	"fmt"
    14  	"os"
    15  	"os/exec"
    16  	"path/filepath"
    17  )
    18  
    19  func main() {
    20  	err := os.Chdir(filepath.Join(".", "fixedbugs", "bug369.dir"))
    21  	check(err)
    22  
    23  	run("go", "tool", "compile", "-N", "-o", "slow.o", "pkg.go")
    24  	run("go", "tool", "compile", "-o", "fast.o", "pkg.go")
    25  	run("go", "tool", "compile", "-o", "main.o", "main.go")
    26  	run("go", "tool", "link", "-o", "a.exe", "main.o")
    27  	run("." + string(filepath.Separator) + "a.exe")
    28  
    29  	os.Remove("slow.o")
    30  	os.Remove("fast.o")
    31  	os.Remove("main.o")
    32  	os.Remove("a.exe")
    33  }
    34  
    35  func run(name string, args ...string) {
    36  	cmd := exec.Command(name, args...)
    37  	out, err := cmd.CombinedOutput()
    38  	if err != nil {
    39  		fmt.Println(string(out))
    40  		fmt.Println(err)
    41  		os.Exit(1)
    42  	}
    43  }
    44  
    45  func check(err error) {
    46  	if err != nil {
    47  		fmt.Println(err)
    48  		os.Exit(1)
    49  	}
    50  }