]> Cypherpunks.ru repositories - gostls13.git/blobdiff - test/index.go
cmd/compile/internal/inline: score call sites exposed by inlines
[gostls13.git] / test / index.go
index 122b2a5724ac9e4264eaaeab8a042a98e8bf4485..91195ad632bea77a97f4f69457b89e74c4eea7e5 100644 (file)
@@ -1,27 +1,19 @@
-// $G $D/$F.go && $L $F.$A &&
-// ./$A.out -pass 0 >tmp.go && $G tmp.go && $L -o $A.out1 tmp.$A && ./$A.out1 &&
-// ./$A.out -pass 1 >tmp.go && errchk $G -e tmp.go &&
-// ./$A.out -pass 2 >tmp.go && errchk $G -e tmp.go
-// rm -f tmp.go $A.out1
+// skip
 
-// NOTE: This test is not run by 'run.go' and so not run by all.bash.
-// To run this test you must use the ./run shell script.
-
-// Copyright 2010 The Go Authors.  All rights reserved.
+// Copyright 2010 The Go Authors. All rights reserved.
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
 // Generate test of index and slice bounds checks.
-// The output is compiled and run.
+// The actual tests are index0.go, index1.go, index2.go.
 
 package main
 
 import (
        "bufio"
-       "flag"
        "fmt"
        "os"
-       "runtime"
+       "unsafe"
 )
 
 const prolog = `
@@ -44,6 +36,8 @@ const (
        ci64big int64 = 1<<31
        ci64bigger int64 = 1<<32
        chuge = 1<<100
+       cfgood = 2.0
+       cfbad = 2.1
 
        cnj = -2
        cni int = -3
@@ -54,6 +48,8 @@ const (
        cni64big int64 = -1<<31
        cni64bigger int64 = -1<<32
        cnhuge = -1<<100
+       cnfgood = -2.0
+       cnfbad = -2.1
 )
 
 var j int = 100020
@@ -65,6 +61,8 @@ var i64 int64 = 100023
 var i64big int64 = 1<<31
 var i64bigger int64 = 1<<32
 var huge uint64 = 1<<64 - 1
+var fgood float64 = 2.0
+var fbad float64 = 2.1
 
 var nj int = -10
 var ni int = -11
@@ -75,6 +73,8 @@ var ni64 int64 = -13
 var ni64big int64 = -1<<31
 var ni64bigger int64 = -1<<32
 var nhuge int64 = -1<<63
+var nfgood float64 = -2.0
+var nfbad float64 = -2.1
 
 var si []int = make([]int, 10)
 var ai [10]int
@@ -155,40 +155,37 @@ func bug() {
 func main() {
 `
 
-// Passes:
+// pass variable set in index[012].go
 //     0 - dynamic checks
 //     1 - static checks of invalid constants (cannot assign to types)
 //     2 - static checks of array bounds
-var pass = flag.Int("pass", 0, "which test (0,1,2)")
 
 func testExpr(b *bufio.Writer, expr string) {
-       if *pass == 0 {
+       if pass == 0 {
                fmt.Fprintf(b, "\ttest(func(){use(%s)}, %q)\n", expr, expr)
        } else {
-               fmt.Fprintf(b, "\tuse(%s)  // ERROR \"index|overflow\"\n", expr)
+               fmt.Fprintf(b, "\tuse(%s)  // ERROR \"index|overflow|truncated|must be integer\"\n", expr)
        }
 }
 
 func main() {
        b := bufio.NewWriter(os.Stdout)
 
-       flag.Parse()
-       
-       if *pass == 0 {
-               fmt.Fprint(b, "// $G $D/$F.go && $L $F.$A && ./$A.out\n\n")
+       if pass == 0 {
+               fmt.Fprint(b, "// run\n\n")
        } else {
-               fmt.Fprint(b, "// errchk $G -e $D/$F.go\n\n")
+               fmt.Fprint(b, "// errorcheck\n\n")
        }
        fmt.Fprint(b, prolog)
-       
+
        var choices = [][]string{
                // Direct value, fetch from struct, fetch from struct pointer.
                // The last two cases get us to oindex_const_sudo in gsubr.c.
                []string{"", "t.", "pt."},
-               
+
                // Array, pointer to array, slice.
                []string{"a", "pa", "s"},
-               
+
                // Element is int, element is quad (struct).
                // This controls whether we end up in gsubr.c (i) or cgen.c (q).
                []string{"i", "q"},
@@ -203,9 +200,9 @@ func main() {
                []string{"", "n"},
 
                // Size of index.
-               []string{"j", "i", "i8", "i16", "i32", "i64", "i64big", "i64bigger", "huge"},
+               []string{"j", "i", "i8", "i16", "i32", "i64", "i64big", "i64bigger", "huge", "fgood", "fbad"},
        }
-       
+
        forall(choices, func(x []string) {
                p, a, e, big, c, n, i := x[0], x[1], x[2], x[3], x[4], x[5], x[6]
 
@@ -217,43 +214,67 @@ func main() {
                //      negative constant
                //      large constant
                thisPass := 0
-               if c == "c" && (a == "a" || a == "pa" || n == "n" || i == "i64big" || i == "i64bigger" || i == "huge") {
+               if c == "c" && (a == "a" || a == "pa" || n == "n" || i == "i64big" || i == "i64bigger" || i == "huge" || i == "fbad") {
                        if i == "huge" {
-                               // Due to a detail of 6g's internals,
+                               // Due to a detail of gc's internals,
                                // the huge constant errors happen in an
                                // earlier pass than the others and inhibits
                                // the next pass from running.
                                // So run it as a separate check.
                                thisPass = 1
-                       } else if i == "i64big" || i == "i64bigger" && runtime.GOARCH == "amd64" {
-                               // On amd64, these huge numbers do fit in an int, so they are not
-                               // rejected at compile time.
+                       } else if a == "s" && n == "" && (i == "i64big" || i == "i64bigger") && unsafe.Sizeof(int(0)) > 4 {
+                               // If int is 64 bits, these huge
+                               // numbers do fit in an int, so they
+                               // are not rejected at compile time.
                                thisPass = 0
                        } else {
                                thisPass = 2
                        }
                }
-               
+
+               pae := p + a + e + big
+               cni := c + n + i
+
                // If we're using the big-len data, positive int8 and int16 cannot overflow.
                if big == "b" && n == "" && (i == "i8" || i == "i16") {
+                       if pass == 0 {
+                               fmt.Fprintf(b, "\tuse(%s[%s])\n", pae, cni)
+                               fmt.Fprintf(b, "\tuse(%s[0:%s])\n", pae, cni)
+                               fmt.Fprintf(b, "\tuse(%s[1:%s])\n", pae, cni)
+                               fmt.Fprintf(b, "\tuse(%s[%s:])\n", pae, cni)
+                               fmt.Fprintf(b, "\tuse(%s[%s:%s])\n", pae, cni, cni)
+                       }
+                       return
+               }
+
+               // Float variables cannot be used as indices.
+               if c == "" && (i == "fgood" || i == "fbad") {
+                       return
+               }
+               // Integral float constant is ok.
+               if c == "c" && n == "" && i == "fgood" {
+                       if pass == 0 {
+                               fmt.Fprintf(b, "\tuse(%s[%s])\n", pae, cni)
+                               fmt.Fprintf(b, "\tuse(%s[0:%s])\n", pae, cni)
+                               fmt.Fprintf(b, "\tuse(%s[1:%s])\n", pae, cni)
+                               fmt.Fprintf(b, "\tuse(%s[%s:])\n", pae, cni)
+                               fmt.Fprintf(b, "\tuse(%s[%s:%s])\n", pae, cni, cni)
+                       }
                        return
                }
 
                // Only print the test case if it is appropriate for this pass.
-               if thisPass == *pass {
-                       pae := p+a+e+big
-                       cni := c+n+i
-                       
+               if thisPass == pass {
                        // Index operation
-                       testExpr(b, pae + "[" + cni + "]")
-                       
+                       testExpr(b, pae+"["+cni+"]")
+
                        // Slice operation.
                        // Low index 0 is a special case in ggen.c
                        // so test both 0 and 1.
-                       testExpr(b, pae + "[0:" + cni + "]")
-                       testExpr(b, pae + "[1:" + cni + "]")
-                       testExpr(b, pae + "[" + cni + ":]")
-                       testExpr(b, pae + "[" + cni + ":" + cni + "]")
+                       testExpr(b, pae+"[0:"+cni+"]")
+                       testExpr(b, pae+"[1:"+cni+"]")
+                       testExpr(b, pae+"["+cni+":]")
+                       testExpr(b, pae+"["+cni+":"+cni+"]")
                }
        })
 
@@ -263,7 +284,7 @@ func main() {
 
 func forall(choices [][]string, f func([]string)) {
        x := make([]string, len(choices))
-       
+
        var recurse func(d int)
        recurse = func(d int) {
                if d >= len(choices) {
@@ -271,7 +292,7 @@ func forall(choices [][]string, f func([]string)) {
                        return
                }
                for _, x[d] = range choices[d] {
-                       recurse(d+1)
+                       recurse(d + 1)
                }
        }
        recurse(0)