github.com/stingnevermore/go@v0.0.0-20180120041312-3810f5bfed72/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.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  var usizes = [...]int{2, 3, 4, 5, 6, 7}
    24  
    25  func main() {
    26  	w := new(bytes.Buffer)
    27  	fmt.Fprintf(w, "// run\n")
    28  	fmt.Fprintf(w, "// Code generated by gen/copyGen.go. DO NOT EDIT.\n\n")
    29  	fmt.Fprintf(w, "package main\n")
    30  	fmt.Fprintf(w, "import \"fmt\"\n")
    31  
    32  	for _, s := range sizes {
    33  		// type for test
    34  		fmt.Fprintf(w, "type T%d struct {\n", s)
    35  		fmt.Fprintf(w, "  pre [8]byte\n")
    36  		fmt.Fprintf(w, "  mid [%d]byte\n", s)
    37  		fmt.Fprintf(w, "  post [8]byte\n")
    38  		fmt.Fprintf(w, "}\n")
    39  
    40  		// function being tested
    41  		fmt.Fprintf(w, "//go:noinline\n")
    42  		fmt.Fprintf(w, "func t%dcopy_ssa(y, x *[%d]byte) {\n", s, s)
    43  		fmt.Fprintf(w, "  *y = *x\n")
    44  		fmt.Fprintf(w, "}\n")
    45  
    46  		// testing harness
    47  		fmt.Fprintf(w, "func testCopy%d() {\n", s)
    48  		fmt.Fprintf(w, "  a := T%d{[8]byte{201, 202, 203, 204, 205, 206, 207, 208},[%d]byte{", s, s)
    49  		for i := 0; i < s; i++ {
    50  			fmt.Fprintf(w, "%d,", i%100)
    51  		}
    52  		fmt.Fprintf(w, "},[8]byte{211, 212, 213, 214, 215, 216, 217, 218}}\n")
    53  		fmt.Fprintf(w, "  x := [%d]byte{", s)
    54  		for i := 0; i < s; i++ {
    55  			fmt.Fprintf(w, "%d,", 100+i%100)
    56  		}
    57  		fmt.Fprintf(w, "}\n")
    58  		fmt.Fprintf(w, "  t%dcopy_ssa(&a.mid, &x)\n", s)
    59  		fmt.Fprintf(w, "  want := T%d{[8]byte{201, 202, 203, 204, 205, 206, 207, 208},[%d]byte{", s, s)
    60  		for i := 0; i < s; i++ {
    61  			fmt.Fprintf(w, "%d,", 100+i%100)
    62  		}
    63  		fmt.Fprintf(w, "},[8]byte{211, 212, 213, 214, 215, 216, 217, 218}}\n")
    64  		fmt.Fprintf(w, "  if a != want {\n")
    65  		fmt.Fprintf(w, "    fmt.Printf(\"t%dcopy got=%%v, want %%v\\n\", a, want)\n", s)
    66  		fmt.Fprintf(w, "    failed=true\n")
    67  		fmt.Fprintf(w, "  }\n")
    68  		fmt.Fprintf(w, "}\n")
    69  	}
    70  
    71  	for _, s := range usizes {
    72  		// function being tested
    73  		fmt.Fprintf(w, "//go:noinline\n")
    74  		fmt.Fprintf(w, "func tu%dcopy_ssa(docopy bool, data [%d]byte, x *[%d]byte) {\n", s, s, s)
    75  		fmt.Fprintf(w, "  if docopy {\n")
    76  		fmt.Fprintf(w, "    *x = data\n")
    77  		fmt.Fprintf(w, "  }\n")
    78  		fmt.Fprintf(w, "}\n")
    79  
    80  		// testing harness
    81  		fmt.Fprintf(w, "func testUnalignedCopy%d() {\n", s)
    82  		fmt.Fprintf(w, "  var a [%d]byte\n", s)
    83  		fmt.Fprintf(w, "  t%d := [%d]byte{", s, s)
    84  		for i := 0; i < s; i++ {
    85  			fmt.Fprintf(w, " %d,", s+i)
    86  		}
    87  		fmt.Fprintf(w, "}\n")
    88  		fmt.Fprintf(w, "  tu%dcopy_ssa(true, t%d, &a)\n", s, s)
    89  		fmt.Fprintf(w, "  want%d := [%d]byte{", s, s)
    90  		for i := 0; i < s; i++ {
    91  			fmt.Fprintf(w, " %d,", s+i)
    92  		}
    93  		fmt.Fprintf(w, "}\n")
    94  		fmt.Fprintf(w, "  if a != want%d {\n", s)
    95  		fmt.Fprintf(w, "    fmt.Printf(\"tu%dcopy got=%%v, want %%v\\n\", a, want%d)\n", s, s)
    96  		fmt.Fprintf(w, "    failed=true\n")
    97  		fmt.Fprintf(w, "  }\n")
    98  		fmt.Fprintf(w, "}\n")
    99  	}
   100  
   101  	// boilerplate at end
   102  	fmt.Fprintf(w, "var failed bool\n")
   103  	fmt.Fprintf(w, "func main() {\n")
   104  	for _, s := range sizes {
   105  		fmt.Fprintf(w, "  testCopy%d()\n", s)
   106  	}
   107  	for _, s := range usizes {
   108  		fmt.Fprintf(w, "  testUnalignedCopy%d()\n", s)
   109  	}
   110  	fmt.Fprintf(w, "  if failed {\n")
   111  	fmt.Fprintf(w, "    panic(\"failed\")\n")
   112  	fmt.Fprintf(w, "  }\n")
   113  	fmt.Fprintf(w, "}\n")
   114  
   115  	// gofmt result
   116  	b := w.Bytes()
   117  	src, err := format.Source(b)
   118  	if err != nil {
   119  		fmt.Printf("%s\n", b)
   120  		panic(err)
   121  	}
   122  
   123  	// write to file
   124  	err = ioutil.WriteFile("../copy.go", src, 0666)
   125  	if err != nil {
   126  		log.Fatalf("can't write output: %v\n", err)
   127  	}
   128  }