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