3 // NOTE: the actual tests to run are rotate[0123].go
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.
9 // Generate test of shift and rotate by constants.
10 // The output is compiled and run.
12 // The integer type depends on the value of mode (rotate direction,
28 b := bufio.NewWriter(os.Stdout)
31 fmt.Fprintf(b, "%s\n", prolog)
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 gentest(b, 1<<logBits, mode&1 != 0, mode&2 != 0)
53 i32 int32 = 0x12345678
54 i64 int64 = 0x123456789abcdef0
57 ui32 uint32 = 0x12345678
58 ui64 uint64 = 0x123456789abcdef0
81 func check_XXX(desc string, have, want XXX) {
84 fmt.Printf("%s = %T(%#x), want %T(%#x)\n", desc, have, have, want, want)
86 fmt.Printf("BUG: stopping after 100 failures\n")
94 uop = [2]func(x, y uint64) uint64{
95 func(x, y uint64) uint64 {
98 func(x, y uint64) uint64 {
102 iop = [2]func(x, y int64) int64{
103 func(x, y int64) int64 {
106 func(x, y int64) int64 {
110 cop = [2]byte{'|', '^'}
113 func gentest(b *bufio.Writer, bits uint, unsigned, inverted bool) {
114 fmt.Fprintf(b, "func init() {\n")
115 defer fmt.Fprintf(b, "}\n")
118 // Generate tests for left/right and right/left.
119 for l := uint(0); l <= bits; l++ {
120 for r := uint(0); r <= bits; r++ {
121 for o, op := range cop {
122 typ := fmt.Sprintf("int%d", bits)
123 v := fmt.Sprintf("i%d", bits)
128 v0 := int64(0x123456789abcdef0)
133 expr1 := fmt.Sprintf("%s<<%d %c %s>>%d", v, l, op, v, r)
134 expr2 := fmt.Sprintf("%s>>%d %c %s<<%d", v, r, op, v, l)
138 v := uint64(v0) >> (64 - bits)
139 v = uop[o](v<<l, v>>r)
142 result = fmt.Sprintf("%#x", v)
144 v := int64(v0) >> (64 - bits)
145 v = iop[o](v<<l, v>>r)
148 result = fmt.Sprintf("%#x", v)
151 fmt.Fprintf(b, "\tcheck_%s(%q, %s, %s(%s))\n", typ, expr1, expr1, typ, result)
152 fmt.Fprintf(b, "\tcheck_%s(%q, %s, %s(%s))\n", typ, expr2, expr2, typ, result)
154 // Chop test into multiple functions so that there's not one
155 // enormous function to compile/link.
156 // All the functions are named init so we don't have to do
157 // anything special to call them. ☺
159 fmt.Fprintf(b, "}\n")
160 fmt.Fprintf(b, "func init() {\n")