]> Cypherpunks.ru repositories - gostls13.git/blob - test/rotate.go
test: avoid interface conversion in rotate.go
[gostls13.git] / test / rotate.go
1 // $G $D/$F.go && $L $F.$A &&
2 // ./$A.out >tmp.go && $G tmp.go && $L -o $A.out1 tmp.$A && ./$A.out1
3 // rm -f tmp.go $A.out1
4
5 // Copyright 2012 The Go Authors.  All rights reserved.
6 // Use of this source code is governed by a BSD-style
7 // license that can be found in the LICENSE file.
8
9 // Generate test of shift and rotate by constants.
10 // The output is compiled and run.
11 //
12 // The output takes around a gigabyte of memory to compile, link, and run
13 // but it is only done during ./run, not in normal builds using run.go.
14
15 package main
16
17 import (
18         "bufio"
19         "flag"
20         "fmt"
21         "os"
22         "strings"
23 )
24
25 func main() {
26         flag.Parse()
27
28         b := bufio.NewWriter(os.Stdout)
29         defer b.Flush()
30
31         fmt.Fprintf(b, "%s\n", prolog)
32
33         for logBits := uint(3); logBits <= 6; logBits++ {
34                 typ := fmt.Sprintf("int%d", 1<<logBits)
35                 fmt.Fprint(b, strings.Replace(checkFunc, "XXX", typ, -1))
36                 fmt.Fprint(b, strings.Replace(checkFunc, "XXX", "u"+typ, -1))
37                 for mode := 0; mode < 1<<2; mode++ {
38                         gentest(b, 1<<logBits, mode&1 != 0, mode&2 != 0)
39                 }
40         }
41 }
42
43 const prolog = `
44
45 package main
46
47 import (
48         "fmt"
49         "os"
50 )
51
52 var (
53         i8 int8 = 0x12
54         i16 int16 = 0x1234
55         i32 int32 = 0x12345678
56         i64 int64 = 0x123456789abcdef0
57         ui8 uint8 = 0x12
58         ui16 uint16 = 0x1234
59         ui32 uint32 = 0x12345678
60         ui64 uint64 = 0x123456789abcdef0
61
62         ni8 = ^i8
63         ni16 = ^i16
64         ni32 = ^i32
65         ni64 = ^i64
66         nui8 = ^ui8
67         nui16 = ^ui16
68         nui32 = ^ui32
69         nui64 = ^ui64
70 )
71
72 var nfail = 0
73
74 func main() {
75         if nfail > 0 {
76                 fmt.Printf("BUG\n")
77         }
78 }
79
80 `
81
82 const checkFunc = `
83 func check_XXX(desc string, have, want XXX) {
84         if have != want {
85                 nfail++
86                 fmt.Printf("%s = %T(%#x), want %T(%#x)\n", desc, have, have, want, want)
87                 if nfail >= 100 {
88                         fmt.Printf("BUG: stopping after 100 failures\n")
89                         os.Exit(0)
90                 }
91         }
92 }
93 `
94
95 var (
96         uop = [2]func(x, y uint64) uint64{
97                 func(x, y uint64) uint64 {
98                         return x | y
99                 },
100                 func(x, y uint64) uint64 {
101                         return x ^ y
102                 },
103         }
104         iop = [2]func(x, y int64) int64{
105                 func(x, y int64) int64 {
106                         return x | y
107                 },
108                 func(x, y int64) int64 {
109                         return x ^ y
110                 },
111         }
112         cop = [2]byte{'|', '^'}
113 )
114
115 func gentest(b *bufio.Writer, bits uint, unsigned, inverted bool) {
116         fmt.Fprintf(b, "func init() {\n")
117         defer fmt.Fprintf(b, "}\n")
118         n := 0
119
120         // Generate tests for left/right and right/left.
121         for l := uint(0); l <= bits; l++ {
122                 for r := uint(0); r <= bits; r++ {
123                         for o, op := range cop {
124                                 typ := fmt.Sprintf("int%d", bits)
125                                 v := fmt.Sprintf("i%d", bits)
126                                 if unsigned {
127                                         typ = "u" + typ
128                                         v = "u" + v
129                                 }
130                                 v0 := int64(0x123456789abcdef0)
131                                 if inverted {
132                                         v = "n" + v
133                                         v0 = ^v0
134                                 }
135                                 expr1 := fmt.Sprintf("%s<<%d %c %s>>%d", v, l, op, v, r)
136                                 expr2 := fmt.Sprintf("%s>>%d %c %s<<%d", v, r, op, v, l)
137
138                                 var result string
139                                 if unsigned {
140                                         v := uint64(v0) >> (64 - bits)
141                                         v = uop[o](v<<l, v>>r)
142                                         v <<= 64 - bits
143                                         v >>= 64 - bits
144                                         result = fmt.Sprintf("%#x", v)
145                                 } else {
146                                         v := int64(v0) >> (64 - bits)
147                                         v = iop[o](v<<l, v>>r)
148                                         v <<= 64 - bits
149                                         v >>= 64 - bits
150                                         result = fmt.Sprintf("%#x", v)
151                                 }
152
153                                 fmt.Fprintf(b, "\tcheck_%s(%q, %s, %s(%s))\n", typ, expr1, expr1, typ, result)
154                                 fmt.Fprintf(b, "\tcheck_%s(%q, %s, %s(%s))\n", typ, expr2, expr2, typ, result)
155
156                                 // Chop test into multiple functions so that there's not one
157                                 // enormous function to compile/link.
158                                 // All the functions are named init so we don't have to do
159                                 // anything special to call them.  ☺
160                                 if n++; n >= 50 {
161                                         fmt.Fprintf(b, "}\n")
162                                         fmt.Fprintf(b, "func init() {\n")
163                                         n = 0
164                                 }
165                         }
166                 }
167         }
168 }