~ubuntu-branches/ubuntu/vivid/golang/vivid

« back to all changes in this revision

Viewing changes to test/index.go

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2013-08-20 14:06:23 UTC
  • mfrom: (14.1.23 saucy-proposed)
  • Revision ID: package-import@ubuntu.com-20130820140623-b414jfxi3m0qkmrq
Tags: 2:1.1.2-2ubuntu1
* Merge from Debian unstable (LP: #1211749, #1202027). Remaining changes:
  - 016-armhf-elf-header.patch: Use correct ELF header for armhf binaries.
  - d/control,control.cross: Update Breaks/Replaces for Ubuntu
    versions to ensure smooth upgrades, regenerate control file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
1
// skip
6
2
 
7
3
// Copyright 2010 The Go Authors.  All rights reserved.
8
4
// Use of this source code is governed by a BSD-style
9
5
// license that can be found in the LICENSE file.
10
6
 
11
7
// Generate test of index and slice bounds checks.
12
 
// The output is compiled and run.
 
8
// The actual tests are index0.go, index1.go, index2.go.
13
9
 
14
10
package main
15
11
 
16
12
import (
17
13
        "bufio"
18
 
        "flag"
19
14
        "fmt"
20
15
        "os"
 
16
        "unsafe"
21
17
)
22
18
 
23
19
const prolog = `
40
36
        ci64big int64 = 1<<31
41
37
        ci64bigger int64 = 1<<32
42
38
        chuge = 1<<100
 
39
        cfgood = 2.0
 
40
        cfbad = 2.1
43
41
 
44
42
        cnj = -2
45
43
        cni int = -3
50
48
        cni64big int64 = -1<<31
51
49
        cni64bigger int64 = -1<<32
52
50
        cnhuge = -1<<100
 
51
        cnfgood = -2.0
 
52
        cnfbad = -2.1
53
53
)
54
54
 
55
55
var j int = 100020
61
61
var i64big int64 = 1<<31
62
62
var i64bigger int64 = 1<<32
63
63
var huge uint64 = 1<<64 - 1
 
64
var fgood float64 = 2.0
 
65
var fbad float64 = 2.1
64
66
 
65
67
var nj int = -10
66
68
var ni int = -11
71
73
var ni64big int64 = -1<<31
72
74
var ni64bigger int64 = -1<<32
73
75
var nhuge int64 = -1<<63
 
76
var nfgood float64 = -2.0
 
77
var nfbad float64 = -2.1
74
78
 
75
79
var si []int = make([]int, 10)
76
80
var ai [10]int
151
155
func main() {
152
156
`
153
157
 
154
 
// Passes:
 
158
// pass variable set in index[012].go
155
159
//      0 - dynamic checks
156
160
//      1 - static checks of invalid constants (cannot assign to types)
157
161
//      2 - static checks of array bounds
158
 
var pass = flag.Int("pass", 0, "which test (0,1,2)")
159
162
 
160
163
func testExpr(b *bufio.Writer, expr string) {
161
 
        if *pass == 0 {
 
164
        if pass == 0 {
162
165
                fmt.Fprintf(b, "\ttest(func(){use(%s)}, %q)\n", expr, expr)
163
166
        } else {
164
 
                fmt.Fprintf(b, "\tuse(%s)  // ERROR \"index|overflow\"\n", expr)
 
167
                fmt.Fprintf(b, "\tuse(%s)  // ERROR \"index|overflow|truncated\"\n", expr)
165
168
        }
166
169
}
167
170
 
168
171
func main() {
169
172
        b := bufio.NewWriter(os.Stdout)
170
173
 
171
 
        flag.Parse()
172
 
        
173
 
        if *pass == 0 {
174
 
                fmt.Fprint(b, "// $G $D/$F.go && $L $F.$A && ./$A.out\n\n")
 
174
        if pass == 0 {
 
175
                fmt.Fprint(b, "// run\n\n")
175
176
        } else {
176
 
                fmt.Fprint(b, "// errchk $G -e $D/$F.go\n\n")
 
177
                fmt.Fprint(b, "// errorcheck\n\n")
177
178
        }
178
179
        fmt.Fprint(b, prolog)
179
 
        
 
180
 
180
181
        var choices = [][]string{
181
182
                // Direct value, fetch from struct, fetch from struct pointer.
182
183
                // The last two cases get us to oindex_const_sudo in gsubr.c.
183
184
                []string{"", "t.", "pt."},
184
 
                
 
185
 
185
186
                // Array, pointer to array, slice.
186
187
                []string{"a", "pa", "s"},
187
 
                
 
188
 
188
189
                // Element is int, element is quad (struct).
189
190
                // This controls whether we end up in gsubr.c (i) or cgen.c (q).
190
191
                []string{"i", "q"},
199
200
                []string{"", "n"},
200
201
 
201
202
                // Size of index.
202
 
                []string{"j", "i", "i8", "i16", "i32", "i64", "i64big", "i64bigger", "huge"},
 
203
                []string{"j", "i", "i8", "i16", "i32", "i64", "i64big", "i64bigger", "huge", "fgood", "fbad"},
203
204
        }
204
 
        
 
205
 
205
206
        forall(choices, func(x []string) {
206
207
                p, a, e, big, c, n, i := x[0], x[1], x[2], x[3], x[4], x[5], x[6]
207
208
 
213
214
                //      negative constant
214
215
                //      large constant
215
216
                thisPass := 0
216
 
                if c == "c" && (a == "a" || a == "pa" || n == "n" || i == "i64big" || i == "i64bigger" || i == "huge") {
 
217
                if c == "c" && (a == "a" || a == "pa" || n == "n" || i == "i64big" || i == "i64bigger" || i == "huge" || i == "fbad") {
217
218
                        if i == "huge" {
218
219
                                // Due to a detail of 6g's internals,
219
220
                                // the huge constant errors happen in an
221
222
                                // the next pass from running.
222
223
                                // So run it as a separate check.
223
224
                                thisPass = 1
 
225
                        } else if a == "s" && n == "" && (i == "i64big" || i == "i64bigger") && unsafe.Sizeof(int(0)) > 4 {
 
226
                                // If int is 64 bits, these huge
 
227
                                // numbers do fit in an int, so they
 
228
                                // are not rejected at compile time.
 
229
                                thisPass = 0
224
230
                        } else {
225
231
                                thisPass = 2
226
232
                        }
227
233
                }
228
 
                
 
234
 
 
235
                pae := p + a + e + big
 
236
                cni := c + n + i
 
237
 
229
238
                // If we're using the big-len data, positive int8 and int16 cannot overflow.
230
239
                if big == "b" && n == "" && (i == "i8" || i == "i16") {
 
240
                        if pass == 0 {
 
241
                                fmt.Fprintf(b, "\tuse(%s[%s])\n", pae, cni)
 
242
                                fmt.Fprintf(b, "\tuse(%s[0:%s])\n", pae, cni)
 
243
                                fmt.Fprintf(b, "\tuse(%s[1:%s])\n", pae, cni)
 
244
                                fmt.Fprintf(b, "\tuse(%s[%s:])\n", pae, cni)
 
245
                                fmt.Fprintf(b, "\tuse(%s[%s:%s])\n", pae, cni, cni)
 
246
                        }
 
247
                        return
 
248
                }
 
249
 
 
250
                // Float variables cannot be used as indices.
 
251
                if c == "" && (i == "fgood" || i == "fbad") {
 
252
                        return
 
253
                }
 
254
                // Integral float constat is ok.
 
255
                if c == "c" && n == "" && i == "fgood" {
 
256
                        if pass == 0 {
 
257
                                fmt.Fprintf(b, "\tuse(%s[%s])\n", pae, cni)
 
258
                                fmt.Fprintf(b, "\tuse(%s[0:%s])\n", pae, cni)
 
259
                                fmt.Fprintf(b, "\tuse(%s[1:%s])\n", pae, cni)
 
260
                                fmt.Fprintf(b, "\tuse(%s[%s:])\n", pae, cni)
 
261
                                fmt.Fprintf(b, "\tuse(%s[%s:%s])\n", pae, cni, cni)
 
262
                        }
231
263
                        return
232
264
                }
233
265
 
234
266
                // Only print the test case if it is appropriate for this pass.
235
 
                if thisPass == *pass {
236
 
                        pae := p+a+e+big
237
 
                        cni := c+n+i
238
 
                        
 
267
                if thisPass == pass {
239
268
                        // Index operation
240
 
                        testExpr(b, pae + "[" + cni + "]")
241
 
                        
 
269
                        testExpr(b, pae+"["+cni+"]")
 
270
 
242
271
                        // Slice operation.
243
272
                        // Low index 0 is a special case in ggen.c
244
273
                        // so test both 0 and 1.
245
 
                        testExpr(b, pae + "[0:" + cni + "]")
246
 
                        testExpr(b, pae + "[1:" + cni + "]")
247
 
                        testExpr(b, pae + "[" + cni + ":]")
248
 
                        testExpr(b, pae + "[" + cni + ":" + cni + "]")
 
274
                        testExpr(b, pae+"[0:"+cni+"]")
 
275
                        testExpr(b, pae+"[1:"+cni+"]")
 
276
                        testExpr(b, pae+"["+cni+":]")
 
277
                        testExpr(b, pae+"["+cni+":"+cni+"]")
249
278
                }
250
279
        })
251
280
 
255
284
 
256
285
func forall(choices [][]string, f func([]string)) {
257
286
        x := make([]string, len(choices))
258
 
        
 
287
 
259
288
        var recurse func(d int)
260
289
        recurse = func(d int) {
261
290
                if d >= len(choices) {
263
292
                        return
264
293
                }
265
294
                for _, x[d] = range choices[d] {
266
 
                        recurse(d+1)
 
295
                        recurse(d + 1)
267
296
                }
268
297
        }
269
298
        recurse(0)