Source file src/cmd/compile/internal/test/testdata/gen/zeroGen.go

     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  	"log"
    12  	"os"
    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, "// Code generated by gen/zeroGen.go. DO NOT EDIT.\n\n")
    27  	fmt.Fprintf(w, "package main\n")
    28  	fmt.Fprintf(w, "import \"testing\"\n")
    29  
    30  	for _, s := range sizes {
    31  		// type for test
    32  		fmt.Fprintf(w, "type Z%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, "//go:noinline\n")
    40  		fmt.Fprintf(w, "func zero%d_ssa(x *[%d]byte) {\n", s, s)
    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(t *testing.T) {\n", s)
    46  		fmt.Fprintf(w, "  a := Z%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 := Z%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, "    t.Errorf(\"zero%d got=%%v, want %%v\\n\", a, want)\n", s)
    59  		fmt.Fprintf(w, "  }\n")
    60  		fmt.Fprintf(w, "}\n")
    61  	}
    62  
    63  	for _, s := range usizes {
    64  		// type for test
    65  		fmt.Fprintf(w, "type Z%du1 struct {\n", s)
    66  		fmt.Fprintf(w, "  b   bool\n")
    67  		fmt.Fprintf(w, "  val [%d]byte\n", s)
    68  		fmt.Fprintf(w, "}\n")
    69  
    70  		fmt.Fprintf(w, "type Z%du2 struct {\n", s)
    71  		fmt.Fprintf(w, "  i   uint16\n")
    72  		fmt.Fprintf(w, "  val [%d]byte\n", s)
    73  		fmt.Fprintf(w, "}\n")
    74  
    75  		// function being tested
    76  		fmt.Fprintf(w, "//go:noinline\n")
    77  		fmt.Fprintf(w, "func zero%du1_ssa(t *Z%du1) {\n", s, s)
    78  		fmt.Fprintf(w, "  t.val = [%d]byte{}\n", s)
    79  		fmt.Fprintf(w, "}\n")
    80  
    81  		// function being tested
    82  		fmt.Fprintf(w, "//go:noinline\n")
    83  		fmt.Fprintf(w, "func zero%du2_ssa(t *Z%du2) {\n", s, s)
    84  		fmt.Fprintf(w, "  t.val = [%d]byte{}\n", s)
    85  		fmt.Fprintf(w, "}\n")
    86  
    87  		// testing harness
    88  		fmt.Fprintf(w, "func testZero%du(t *testing.T) {\n", s)
    89  		fmt.Fprintf(w, "  a := Z%du1{false, [%d]byte{", s, s)
    90  		for i := 0; i < s; i++ {
    91  			fmt.Fprintf(w, "255,")
    92  		}
    93  		fmt.Fprintf(w, "}}\n")
    94  		fmt.Fprintf(w, "  zero%du1_ssa(&a)\n", s)
    95  		fmt.Fprintf(w, "  want := Z%du1{false, [%d]byte{", s, s)
    96  		for i := 0; i < s; i++ {
    97  			fmt.Fprintf(w, "0,")
    98  		}
    99  		fmt.Fprintf(w, "}}\n")
   100  		fmt.Fprintf(w, "  if a != want {\n")
   101  		fmt.Fprintf(w, "    t.Errorf(\"zero%du2 got=%%v, want %%v\\n\", a, want)\n", s)
   102  		fmt.Fprintf(w, "  }\n")
   103  		fmt.Fprintf(w, "  b := Z%du2{15, [%d]byte{", s, s)
   104  		for i := 0; i < s; i++ {
   105  			fmt.Fprintf(w, "255,")
   106  		}
   107  		fmt.Fprintf(w, "}}\n")
   108  		fmt.Fprintf(w, "  zero%du2_ssa(&b)\n", s)
   109  		fmt.Fprintf(w, "  wantb := Z%du2{15, [%d]byte{", s, s)
   110  		for i := 0; i < s; i++ {
   111  			fmt.Fprintf(w, "0,")
   112  		}
   113  		fmt.Fprintf(w, "}}\n")
   114  		fmt.Fprintf(w, "  if b != wantb {\n")
   115  		fmt.Fprintf(w, "    t.Errorf(\"zero%du2 got=%%v, want %%v\\n\", b, wantb)\n", s)
   116  		fmt.Fprintf(w, "  }\n")
   117  		fmt.Fprintf(w, "}\n")
   118  	}
   119  
   120  	// boilerplate at end
   121  	fmt.Fprintf(w, "func TestZero(t *testing.T) {\n")
   122  	for _, s := range sizes {
   123  		fmt.Fprintf(w, "  testZero%d(t)\n", s)
   124  	}
   125  	for _, s := range usizes {
   126  		fmt.Fprintf(w, "  testZero%du(t)\n", s)
   127  	}
   128  	fmt.Fprintf(w, "}\n")
   129  
   130  	// gofmt result
   131  	b := w.Bytes()
   132  	src, err := format.Source(b)
   133  	if err != nil {
   134  		fmt.Printf("%s\n", b)
   135  		panic(err)
   136  	}
   137  
   138  	// write to file
   139  	err = os.WriteFile("../zero_test.go", src, 0666)
   140  	if err != nil {
   141  		log.Fatalf("can't write output: %v\n", err)
   142  	}
   143  }
   144  

View as plain text