...

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

Documentation: cmd/compile/internal/test/testdata/gen

     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 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, "// Code generated by gen/copyGen.go. DO NOT EDIT.\n\n")
    28  	fmt.Fprintf(w, "package main\n")
    29  	fmt.Fprintf(w, "import \"testing\"\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 t%dcopy_ssa(y, x *[%d]byte) {\n", s, s)
    42  		fmt.Fprintf(w, "  *y = *x\n")
    43  		fmt.Fprintf(w, "}\n")
    44  
    45  		// testing harness
    46  		fmt.Fprintf(w, "func testCopy%d(t *testing.T) {\n", s)
    47  		fmt.Fprintf(w, "  a := T%d{[8]byte{201, 202, 203, 204, 205, 206, 207, 208},[%d]byte{", s, s)
    48  		for i := 0; i < s; i++ {
    49  			fmt.Fprintf(w, "%d,", i%100)
    50  		}
    51  		fmt.Fprintf(w, "},[8]byte{211, 212, 213, 214, 215, 216, 217, 218}}\n")
    52  		fmt.Fprintf(w, "  x := [%d]byte{", s)
    53  		for i := 0; i < s; i++ {
    54  			fmt.Fprintf(w, "%d,", 100+i%100)
    55  		}
    56  		fmt.Fprintf(w, "}\n")
    57  		fmt.Fprintf(w, "  t%dcopy_ssa(&a.mid, &x)\n", s)
    58  		fmt.Fprintf(w, "  want := T%d{[8]byte{201, 202, 203, 204, 205, 206, 207, 208},[%d]byte{", s, s)
    59  		for i := 0; i < s; i++ {
    60  			fmt.Fprintf(w, "%d,", 100+i%100)
    61  		}
    62  		fmt.Fprintf(w, "},[8]byte{211, 212, 213, 214, 215, 216, 217, 218}}\n")
    63  		fmt.Fprintf(w, "  if a != want {\n")
    64  		fmt.Fprintf(w, "    t.Errorf(\"t%dcopy got=%%v, want %%v\\n\", a, want)\n", s)
    65  		fmt.Fprintf(w, "  }\n")
    66  		fmt.Fprintf(w, "}\n")
    67  	}
    68  
    69  	for _, s := range usizes {
    70  		// function being tested
    71  		fmt.Fprintf(w, "//go:noinline\n")
    72  		fmt.Fprintf(w, "func tu%dcopy_ssa(docopy bool, data [%d]byte, x *[%d]byte) {\n", s, s, s)
    73  		fmt.Fprintf(w, "  if docopy {\n")
    74  		fmt.Fprintf(w, "    *x = data\n")
    75  		fmt.Fprintf(w, "  }\n")
    76  		fmt.Fprintf(w, "}\n")
    77  
    78  		// testing harness
    79  		fmt.Fprintf(w, "func testUnalignedCopy%d(t *testing.T) {\n", s)
    80  		fmt.Fprintf(w, "  var a [%d]byte\n", s)
    81  		fmt.Fprintf(w, "  t%d := [%d]byte{", s, s)
    82  		for i := 0; i < s; i++ {
    83  			fmt.Fprintf(w, " %d,", s+i)
    84  		}
    85  		fmt.Fprintf(w, "}\n")
    86  		fmt.Fprintf(w, "  tu%dcopy_ssa(true, t%d, &a)\n", s, s)
    87  		fmt.Fprintf(w, "  want%d := [%d]byte{", s, s)
    88  		for i := 0; i < s; i++ {
    89  			fmt.Fprintf(w, " %d,", s+i)
    90  		}
    91  		fmt.Fprintf(w, "}\n")
    92  		fmt.Fprintf(w, "  if a != want%d {\n", s)
    93  		fmt.Fprintf(w, "    t.Errorf(\"tu%dcopy got=%%v, want %%v\\n\", a, want%d)\n", s, s)
    94  		fmt.Fprintf(w, "  }\n")
    95  		fmt.Fprintf(w, "}\n")
    96  	}
    97  
    98  	// boilerplate at end
    99  	fmt.Fprintf(w, "func TestCopy(t *testing.T) {\n")
   100  	for _, s := range sizes {
   101  		fmt.Fprintf(w, "  testCopy%d(t)\n", s)
   102  	}
   103  	for _, s := range usizes {
   104  		fmt.Fprintf(w, "  testUnalignedCopy%d(t)\n", s)
   105  	}
   106  	fmt.Fprintf(w, "}\n")
   107  
   108  	// gofmt result
   109  	b := w.Bytes()
   110  	src, err := format.Source(b)
   111  	if err != nil {
   112  		fmt.Printf("%s\n", b)
   113  		panic(err)
   114  	}
   115  
   116  	// write to file
   117  	err = os.WriteFile("../copy_test.go", src, 0666)
   118  	if err != nil {
   119  		log.Fatalf("can't write output: %v\n", err)
   120  	}
   121  }
   122  

View as plain text