github.com/goproxy0/go@v0.0.0-20171111080102-49cc0c489d2c/src/cmd/compile/internal/gc/testdata/gen/zeroGen.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 zeroing operations
    16  // zero the data they are supposed to and clobber no adjacent values.
    17  
    18  // run as `go run zeroGen.go`.  A file called zero.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}
    22  var usizes = [...]int{8, 16, 24, 32, 64, 256}
    23  
    24  func main() {
    25  	w := new(bytes.Buffer)
    26  	fmt.Fprintf(w, "// run\n")
    27  	fmt.Fprintf(w, "// Code generated by gen/zeroGen.go. DO NOT EDIT.\n\n")
    28  	fmt.Fprintf(w, "package main\n")
    29  	fmt.Fprintf(w, "import \"fmt\"\n")
    30  
    31  	for _, s := range sizes {
    32  		// type for test
    33  		fmt.Fprintf(w, "type T%d struct {\n", s)
    34  		fmt.Fprintf(w, "  pre [8]byte\n")
    35  		fmt.Fprintf(w, "  mid [%d]byte\n", s)
    36  		fmt.Fprintf(w, "  post [8]byte\n")
    37  		fmt.Fprintf(w, "}\n")
    38  
    39  		// function being tested
    40  		fmt.Fprintf(w, "//go:noinline\n")
    41  		fmt.Fprintf(w, "func zero%d_ssa(x *[%d]byte) {\n", s, s)
    42  		fmt.Fprintf(w, "  *x = [%d]byte{}\n", s)
    43  		fmt.Fprintf(w, "}\n")
    44  
    45  		// testing harness
    46  		fmt.Fprintf(w, "func testZero%d() {\n", s)
    47  		fmt.Fprintf(w, "  a := T%d{[8]byte{255,255,255,255,255,255,255,255},[%d]byte{", s, s)
    48  		for i := 0; i < s; i++ {
    49  			fmt.Fprintf(w, "255,")
    50  		}
    51  		fmt.Fprintf(w, "},[8]byte{255,255,255,255,255,255,255,255}}\n")
    52  		fmt.Fprintf(w, "  zero%d_ssa(&a.mid)\n", s)
    53  		fmt.Fprintf(w, "  want := T%d{[8]byte{255,255,255,255,255,255,255,255},[%d]byte{", s, s)
    54  		for i := 0; i < s; i++ {
    55  			fmt.Fprintf(w, "0,")
    56  		}
    57  		fmt.Fprintf(w, "},[8]byte{255,255,255,255,255,255,255,255}}\n")
    58  		fmt.Fprintf(w, "  if a != want {\n")
    59  		fmt.Fprintf(w, "    fmt.Printf(\"zero%d got=%%v, want %%v\\n\", a, want)\n", s)
    60  		fmt.Fprintf(w, "    failed=true\n")
    61  		fmt.Fprintf(w, "  }\n")
    62  		fmt.Fprintf(w, "}\n")
    63  	}
    64  
    65  	for _, s := range usizes {
    66  		// type for test
    67  		fmt.Fprintf(w, "type T%du1 struct {\n", s)
    68  		fmt.Fprintf(w, "  b   bool\n")
    69  		fmt.Fprintf(w, "  val [%d]byte\n", s)
    70  		fmt.Fprintf(w, "}\n")
    71  
    72  		fmt.Fprintf(w, "type T%du2 struct {\n", s)
    73  		fmt.Fprintf(w, "  i   uint16\n")
    74  		fmt.Fprintf(w, "  val [%d]byte\n", s)
    75  		fmt.Fprintf(w, "}\n")
    76  
    77  		// function being tested
    78  		fmt.Fprintf(w, "//go:noinline\n")
    79  		fmt.Fprintf(w, "func zero%du1_ssa(t *T%du1) {\n", s, s)
    80  		fmt.Fprintf(w, "  t.val = [%d]byte{}\n", s)
    81  		fmt.Fprintf(w, "}\n")
    82  
    83  		// function being tested
    84  		fmt.Fprintf(w, "//go:noinline\n")
    85  		fmt.Fprintf(w, "func zero%du2_ssa(t *T%du2) {\n", s, s)
    86  		fmt.Fprintf(w, "  t.val = [%d]byte{}\n", s)
    87  		fmt.Fprintf(w, "}\n")
    88  
    89  		// testing harness
    90  		fmt.Fprintf(w, "func testZero%du() {\n", s)
    91  		fmt.Fprintf(w, "  a := T%du1{false, [%d]byte{", s, s)
    92  		for i := 0; i < s; i++ {
    93  			fmt.Fprintf(w, "255,")
    94  		}
    95  		fmt.Fprintf(w, "}}\n")
    96  		fmt.Fprintf(w, "  zero%du1_ssa(&a)\n", s)
    97  		fmt.Fprintf(w, "  want := T%du1{false, [%d]byte{", s, s)
    98  		for i := 0; i < s; i++ {
    99  			fmt.Fprintf(w, "0,")
   100  		}
   101  		fmt.Fprintf(w, "}}\n")
   102  		fmt.Fprintf(w, "  if a != want {\n")
   103  		fmt.Fprintf(w, "    fmt.Printf(\"zero%du2 got=%%v, want %%v\\n\", a, want)\n", s)
   104  		fmt.Fprintf(w, "    failed=true\n")
   105  		fmt.Fprintf(w, "  }\n")
   106  		fmt.Fprintf(w, "  b := T%du2{15, [%d]byte{", s, s)
   107  		for i := 0; i < s; i++ {
   108  			fmt.Fprintf(w, "255,")
   109  		}
   110  		fmt.Fprintf(w, "}}\n")
   111  		fmt.Fprintf(w, "  zero%du2_ssa(&b)\n", s)
   112  		fmt.Fprintf(w, "  wantb := T%du2{15, [%d]byte{", s, s)
   113  		for i := 0; i < s; i++ {
   114  			fmt.Fprintf(w, "0,")
   115  		}
   116  		fmt.Fprintf(w, "}}\n")
   117  		fmt.Fprintf(w, "  if b != wantb {\n")
   118  		fmt.Fprintf(w, "    fmt.Printf(\"zero%du2 got=%%v, want %%v\\n\", b, wantb)\n", s)
   119  		fmt.Fprintf(w, "    failed=true\n")
   120  		fmt.Fprintf(w, "  }\n")
   121  		fmt.Fprintf(w, "}\n")
   122  	}
   123  
   124  	// boilerplate at end
   125  	fmt.Fprintf(w, "var failed bool\n")
   126  	fmt.Fprintf(w, "func main() {\n")
   127  	for _, s := range sizes {
   128  		fmt.Fprintf(w, "  testZero%d()\n", s)
   129  	}
   130  	for _, s := range usizes {
   131  		fmt.Fprintf(w, "  testZero%du()\n", s)
   132  	}
   133  	fmt.Fprintf(w, "  if failed {\n")
   134  	fmt.Fprintf(w, "    panic(\"failed\")\n")
   135  	fmt.Fprintf(w, "  }\n")
   136  	fmt.Fprintf(w, "}\n")
   137  
   138  	// gofmt result
   139  	b := w.Bytes()
   140  	src, err := format.Source(b)
   141  	if err != nil {
   142  		fmt.Printf("%s\n", b)
   143  		panic(err)
   144  	}
   145  
   146  	// write to file
   147  	err = ioutil.WriteFile("../zero.go", src, 0666)
   148  	if err != nil {
   149  		log.Fatalf("can't write output: %v\n", err)
   150  	}
   151  }