3 // Copyright 2013 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
7 // Test run-time behavior of 3-index slice expressions.
18 var bout *bufio.Writer
21 bout = bufio.NewWriter(os.Stdout)
23 fmt.Fprintf(bout, "%s", programTop)
24 fmt.Fprintf(bout, "func main() {\n")
42 parse := func(s string) (n int, isconst bool) {
51 n, _ = strconv.Atoi(s)
55 const Cap = 10 // cap of slice, array
57 for _, base := range []string{"array", "slice"} {
58 for _, i := range index {
59 iv, iconst := parse(i)
60 for _, j := range index {
61 jv, jconst := parse(j)
62 for _, k := range index {
63 kv, kconst := parse(k)
64 // Avoid errors that would make the program not compile.
65 // Those are tested by slice3err.go.
67 case iconst && jconst && iv > jv,
68 jconst && kconst && jv > kv,
69 iconst && kconst && iv > kv,
70 iconst && base == "array" && iv > Cap,
71 jconst && base == "array" && jv > Cap,
72 kconst && base == "array" && kv > Cap:
76 expr := base + "[" + i + ":" + j + ":" + k + "]"
77 var xbase, xlen, xcap int
78 if iv > jv || jv > kv || kv > Cap || iv < 0 || jv < 0 || kv < 0 {
79 xbase, xlen, xcap = -1, -1, -1
85 fmt.Fprintf(bout, "\tcheckSlice(%q, func() []byte { return %s }, %d, %d, %d)\n", expr, expr, xbase, xlen, xcap)
91 fmt.Fprintf(bout, "\tif !ok { os.Exit(1) }\n")
92 fmt.Fprintf(bout, "}\n")
108 array = new([10]byte)
129 func checkSlice(desc string, f func() []byte, xbase, xlen, xcap int) {
131 if err := recover(); err != nil {
134 println(desc, " unexpected panic: ", fmt.Sprint(err))
137 // "no panic" is checked below
142 arrayBase := uintptr(unsafe.Pointer(array))
143 raw := *(*[3]uintptr)(unsafe.Pointer(&x))
144 base, len, cap := raw[0] - arrayBase, raw[1], raw[2]
147 println(desc, "=", base, len, cap, "want panic")
150 if cap != 0 && base != uintptr(xbase) || base >= 10 || len != uintptr(xlen) || cap != uintptr(xcap) {
153 println(desc, "=", base, len, cap, "want", "0-9", xlen, xcap)
155 println(desc, "=", base, len, cap, "want", xbase, xlen, xcap)