github.com/euank/go@v0.0.0-20160829210321-495514729181/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_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}
    22  
    23  func main() {
    24  	w := new(bytes.Buffer)
    25  	fmt.Fprintf(w, "// run\n")
    26  	fmt.Fprintf(w, "// autogenerated from gen/zeroGen.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 zero%d_ssa(x *[%d]byte) {\n", s, s)
    40  		fmt.Fprintf(w, "  switch{}\n")
    41  		fmt.Fprintf(w, "  *x = [%d]byte{}\n", s)
    42  		fmt.Fprintf(w, "}\n")
    43  
    44  		// testing harness
    45  		fmt.Fprintf(w, "func testZero%d() {\n", s)
    46  		fmt.Fprintf(w, "  a := T%d{[8]byte{255,255,255,255,255,255,255,255},[%d]byte{", s, s)
    47  		for i := 0; i < s; i++ {
    48  			fmt.Fprintf(w, "255,")
    49  		}
    50  		fmt.Fprintf(w, "},[8]byte{255,255,255,255,255,255,255,255}}\n")
    51  		fmt.Fprintf(w, "  zero%d_ssa(&a.mid)\n", s)
    52  		fmt.Fprintf(w, "  want := T%d{[8]byte{255,255,255,255,255,255,255,255},[%d]byte{", s, s)
    53  		for i := 0; i < s; i++ {
    54  			fmt.Fprintf(w, "0,")
    55  		}
    56  		fmt.Fprintf(w, "},[8]byte{255,255,255,255,255,255,255,255}}\n")
    57  		fmt.Fprintf(w, "  if a != want {\n")
    58  		fmt.Fprintf(w, "    fmt.Printf(\"zero%d got=%%v, want %%v\\n\", a, want)\n", s)
    59  		fmt.Fprintf(w, "    failed=true\n")
    60  		fmt.Fprintf(w, "  }\n")
    61  		fmt.Fprintf(w, "}\n")
    62  	}
    63  
    64  	// boilerplate at end
    65  	fmt.Fprintf(w, "var failed bool\n")
    66  	fmt.Fprintf(w, "func main() {\n")
    67  	for _, s := range sizes {
    68  		fmt.Fprintf(w, "  testZero%d()\n", s)
    69  	}
    70  	fmt.Fprintf(w, "  if failed {\n")
    71  	fmt.Fprintf(w, "    panic(\"failed\")\n")
    72  	fmt.Fprintf(w, "  }\n")
    73  	fmt.Fprintf(w, "}\n")
    74  
    75  	// gofmt result
    76  	b := w.Bytes()
    77  	src, err := format.Source(b)
    78  	if err != nil {
    79  		fmt.Printf("%s\n", b)
    80  		panic(err)
    81  	}
    82  
    83  	// write to file
    84  	err = ioutil.WriteFile("../zero_ssa.go", src, 0666)
    85  	if err != nil {
    86  		log.Fatalf("can't write output: %v\n", err)
    87  	}
    88  }