Merge from mainline (167278:168000).
[official-gcc/graphite-test-results.git] / gcc / testsuite / go.test / test / index.go
bloba91294cffbb6b2273d928ffac2b5095256ce2091
1 // $G $D/$F.go && $L $F.$A &&
2 // ./$A.out -pass 0 >tmp.go && $G tmp.go && $L -o tmp1.$A tmp.$A && ./tmp1.$A &&
3 // ./$A.out -pass 1 >tmp.go && errchk $G -e tmp.go &&
4 // ./$A.out -pass 2 >tmp.go && errchk $G -e tmp.go
5 // rm -f tmp.go
7 // Copyright 2010 The Go Authors. All rights reserved.
8 // Use of this source code is governed by a BSD-style
9 // license that can be found in the LICENSE file.
11 // Generate test of index and slice bounds checks.
13 package main
15 import (
16 "bufio"
17 "flag"
18 "fmt"
19 "os"
22 const prolog = `
24 package main
26 import (
27 "runtime"
30 type quad struct { x, y, z, w int }
32 const (
33 cj = 11
34 ci int = 12
35 ci32 int32 = 13
36 ci64 int64 = 14
37 ci64big int64 = 1<<31
38 ci64bigger int64 = 1<<32
39 chuge = 1<<100
41 cnj = -2
42 cni int = -3
43 cni32 int32 = -4
44 cni64 int64 = -5
45 cni64big int64 = -1<<31
46 cni64bigger int64 = -1<<32
47 cnhuge = -1<<100
50 var j int = 20
51 var i int = 21
52 var i32 int32 = 22
53 var i64 int64 = 23
54 var i64big int64 = 1<<31
55 var i64bigger int64 = 1<<32
56 var huge uint64 = 1<<64 - 1
58 var nj int = -10
59 var ni int = -11
60 var ni32 int32 = -12
61 var ni64 int64 = -13
62 var ni64big int64 = -1<<31
63 var ni64bigger int64 = -1<<32
64 var nhuge int64 = -1<<63
66 var si []int = make([]int, 10)
67 var ai [10]int
68 var pai *[10]int = &ai
70 var sq []quad = make([]quad, 10)
71 var aq [10]quad
72 var paq *[10]quad = &aq
74 type T struct {
75 si []int
76 ai [10]int
77 pai *[10]int
78 sq []quad
79 aq [10]quad
80 paq *[10]quad
83 var t = T{si, ai, pai, sq, aq, paq}
85 var pt = &T{si, ai, pai, sq, aq, paq}
87 // test that f panics
88 func test(f func(), s string) {
89 defer func() {
90 if err := recover(); err == nil {
91 _, file, line, _ := runtime.Caller(2)
92 bug()
93 print(file, ":", line, ": ", s, " did not panic\n")
95 }()
96 f()
99 var X interface{}
100 func use(y interface{}) {
101 X = y
104 var didBug = false
106 func bug() {
107 if !didBug {
108 didBug = true
109 println("BUG")
113 func main() {
116 // Passes:
117 // 0 - dynamic checks
118 // 1 - static checks of invalid constants (cannot assign to types)
119 // 2 - static checks of array bounds
120 var pass = flag.Int("pass", 0, "which test (0,1,2)")
122 func testExpr(b *bufio.Writer, expr string) {
123 if *pass == 0 {
124 fmt.Fprintf(b, "\ttest(func(){use(%s)}, %q)\n", expr, expr)
125 } else {
126 fmt.Fprintf(b, "\tuse(%s) // ERROR \"index|overflow\"\n", expr)
130 func main() {
131 b := bufio.NewWriter(os.Stdout)
133 flag.Parse()
135 if *pass == 0 {
136 fmt.Fprint(b, "// $G $D/$F.go && $L $F.$A && ./$A.out\n\n")
137 } else {
138 fmt.Fprint(b, "// errchk $G -e $D/$F.go\n\n")
140 fmt.Fprint(b, prolog)
142 var choices = [][]string{
143 // Direct value, fetch from struct, fetch from struct pointer.
144 // The last two cases get us to oindex_const_sudo in gsubr.c.
145 []string{"", "t.", "pt."},
147 // Array, pointer to array, slice.
148 []string{"a", "pa", "s"},
150 // Element is int, element is quad (struct).
151 // This controls whether we end up in gsubr.c (i) or cgen.c (q).
152 []string{"i", "q"},
154 // Variable or constant.
155 []string{"", "c"},
157 // Positive or negative.
158 []string{"", "n"},
160 // Size of index.
161 []string{"j", "i", "i32", "i64", "i64big", "i64bigger", "huge"},
164 forall(choices, func(x []string) {
165 p, a, e, c, n, i := x[0], x[1], x[2], x[3], x[4], x[5]
167 // Pass: dynamic=0, static=1, 2.
168 // Which cases should be caught statically?
169 // Only constants, obviously.
170 // Beyond that, must be one of these:
171 // indexing into array or pointer to array
172 // negative constant
173 // large constant
174 thisPass := 0
175 if c == "c" && (a == "a" || a == "pa" || n == "n" || i == "i64big" || i == "i64bigger" || i == "huge") {
176 if i == "huge" {
177 // Due to a detail of 6g's internals,
178 // the huge constant errors happen in an
179 // earlier pass than the others and inhibits
180 // the next pass from running.
181 // So run it as a separate check.
182 thisPass = 1
183 } else {
184 thisPass = 2
188 // Only print the test case if it is appropriate for this pass.
189 if thisPass == *pass {
190 pae := p+a+e
191 cni := c+n+i
193 // Index operation
194 testExpr(b, pae + "[" + cni + "]")
196 // Slice operation.
197 // Low index 0 is a special case in ggen.c
198 // so test both 0 and 1.
199 testExpr(b, pae + "[0:" + cni + "]")
200 testExpr(b, pae + "[1:" + cni + "]")
201 testExpr(b, pae + "[" + cni + ":]")
202 testExpr(b, pae + "[" + cni + ":" + cni + "]")
206 fmt.Fprintln(b, "}")
207 b.Flush()
210 func forall(choices [][]string, f func([]string)) {
211 x := make([]string, len(choices))
213 var recurse func(d int)
214 recurse = func(d int) {
215 if d >= len(choices) {
216 f(x)
217 return
219 for _, x[d] = range choices[d] {
220 recurse(d+1)
223 recurse(0)