github.com/euank/go@v0.0.0-20160829210321-495514729181/src/cmd/compile/internal/gc/testdata/gen/copyGen.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 main
     6  
     7  import (
     8  	"bytes"
     9  	"fmt"
    10  	"go/format"
    11  	"io/ioutil"
    12  	"log"
    13  )
    14  
    15  // This program generates tests to verify that copying operations
    16  // copy the data they are supposed to and clobber no adjacent values.
    17  
    18  // run as `go run copyGen.go`.  A file called copy_ssa.go
    19  // will be written into the parent directory containing the tests.
    20  
    21  var sizes = [...]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 16, 17, 23, 24, 25, 31, 32, 33, 63, 64, 65, 1023, 1024, 1025, 1024 + 7, 1024 + 8, 1024 + 9, 1024 + 15, 1024 + 16, 1024 + 17}
    22  
    23  func main() {
    24  	w := new(bytes.Buffer)
    25  	fmt.Fprintf(w, "// run\n")
    26  	fmt.Fprintf(w, "// autogenerated from gen/copyGen.go - do not edit!\n")
    27  	fmt.Fprintf(w, "package main\n")
    28  	fmt.Fprintf(w, "import \"fmt\"\n")
    29  
    30  	for _, s := range sizes {
    31  		// type for test
    32  		fmt.Fprintf(w, "type T%d struct {\n", s)
    33  		fmt.Fprintf(w, "  pre [8]byte\n")
    34  		fmt.Fprintf(w, "  mid [%d]byte\n", s)
    35  		fmt.Fprintf(w, "  post [8]byte\n")
    36  		fmt.Fprintf(w, "}\n")
    37  
    38  		// function being tested
    39  		fmt.Fprintf(w, "func t%dcopy_ssa(y, x *[%d]byte) {\n", s, s)
    40  		fmt.Fprintf(w, "  switch{}\n")
    41  		fmt.Fprintf(w, "  *y = *x\n")
    42  		fmt.Fprintf(w, "}\n")
    43  
    44  		// testing harness
    45  		fmt.Fprintf(w, "func testCopy%d() {\n", s)
    46  		fmt.Fprintf(w, "  a := T%d{[8]byte{201, 202, 203, 204, 205, 206, 207, 208},[%d]byte{", s, s)
    47  		for i := 0; i < s; i++ {
    48  			fmt.Fprintf(w, "%d,", i%100)
    49  		}
    50  		fmt.Fprintf(w, "},[8]byte{211, 212, 213, 214, 215, 216, 217, 218}}\n")
    51  		fmt.Fprintf(w, "  x := [%d]byte{", s)
    52  		for i := 0; i < s; i++ {
    53  			fmt.Fprintf(w, "%d,", 100+i%100)
    54  		}
    55  		fmt.Fprintf(w, "}\n")
    56  		fmt.Fprintf(w, "  t%dcopy_ssa(&a.mid, &x)\n", s)
    57  		fmt.Fprintf(w, "  want := T%d{[8]byte{201, 202, 203, 204, 205, 206, 207, 208},[%d]byte{", s, s)
    58  		for i := 0; i < s; i++ {
    59  			fmt.Fprintf(w, "%d,", 100+i%100)
    60  		}
    61  		fmt.Fprintf(w, "},[8]byte{211, 212, 213, 214, 215, 216, 217, 218}}\n")
    62  		fmt.Fprintf(w, "  if a != want {\n")
    63  		fmt.Fprintf(w, "    fmt.Printf(\"t%dcopy got=%%v, want %%v\\n\", a, want)\n", s)
    64  		fmt.Fprintf(w, "    failed=true\n")
    65  		fmt.Fprintf(w, "  }\n")
    66  		fmt.Fprintf(w, "}\n")
    67  	}
    68  
    69  	// boilerplate at end
    70  	fmt.Fprintf(w, "var failed bool\n")
    71  	fmt.Fprintf(w, "func main() {\n")
    72  	for _, s := range sizes {
    73  		fmt.Fprintf(w, "  testCopy%d()\n", s)
    74  	}
    75  	fmt.Fprintf(w, "  if failed {\n")
    76  	fmt.Fprintf(w, "    panic(\"failed\")\n")
    77  	fmt.Fprintf(w, "  }\n")
    78  	fmt.Fprintf(w, "}\n")
    79  
    80  	// gofmt result
    81  	b := w.Bytes()
    82  	src, err := format.Source(b)
    83  	if err != nil {
    84  		fmt.Printf("%s\n", b)
    85  		panic(err)
    86  	}
    87  
    88  	// write to file
    89  	err = ioutil.WriteFile("../copy_ssa.go", src, 0666)
    90  	if err != nil {
    91  		log.Fatalf("can't write output: %v\n", err)
    92  	}
    93  }