]> Cypherpunks.ru repositories - gostls13.git/blobdiff - test/zerodivide.go
cmd/compile/internal/inline: score call sites exposed by inlines
[gostls13.git] / test / zerodivide.go
index 1948528d2450a36278ab128095ca13f4d407de48..fd36d67d1ad6dc8b189473a7e45206edea866428 100644 (file)
@@ -1,21 +1,20 @@
-// $G $F.go && $L $F.$A && ./$A.out
+// run
 
 // 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.
 
+// Test that zero division causes a panic.
+
 package main
 
 import (
        "fmt"
        "math"
+       "runtime"
        "strings"
 )
 
-type Error interface {
-       String() string
-}
-
 type ErrorTest struct {
        name string
        fn   func()
@@ -29,6 +28,8 @@ var (
        i32, j32, k32 int32 = 0, 0, 1
        i64, j64, k64 int64 = 0, 0, 1
 
+       bb = []int16{2, 0}
+
        u, v, w       uint    = 0, 0, 1
        u8, v8, w8    uint8   = 0, 0, 1
        u16, v16, w16 uint16  = 0, 0, 1
@@ -125,6 +126,10 @@ var errorTests = []ErrorTest{
        ErrorTest{"int32 1/0", func() { use(k32 / j32) }, "divide"},
        ErrorTest{"int64 1/0", func() { use(k64 / j64) }, "divide"},
 
+       // From issue 5790, we should ensure that _ assignments
+       // still evaluate and generate zerodivide panics.
+       ErrorTest{"int16 _ = bb[0]/bb[1]", func() { _ = bb[0] / bb[1] }, "divide"},
+
        ErrorTest{"uint 0/0", func() { use(u / v) }, "divide"},
        ErrorTest{"uint8 0/0", func() { use(u8 / v8) }, "divide"},
        ErrorTest{"uint16 0/0", func() { use(u16 / v16) }, "divide"},
@@ -161,10 +166,10 @@ var errorTests = []ErrorTest{
        ErrorTest{"complex128 1/0", func() { use(e128 / d128) }, ""},
 }
 
-func error(fn func()) (error string) {
+func error_(fn func()) (error string) {
        defer func() {
                if e := recover(); e != nil {
-                       error = e.(Error).String()
+                       error = e.(runtime.Error).Error()
                }
        }()
        fn()
@@ -196,10 +201,7 @@ func alike(a, b float64) bool {
 func main() {
        bad := false
        for _, t := range errorTests {
-               if t.err != "" {
-                       continue
-               }
-               err := error(t.fn)
+               err := error_(t.fn)
                switch {
                case t.err == "" && err == "":
                        // fine
@@ -216,7 +218,7 @@ func main() {
                        }
                        fmt.Printf("%s: expected no error; got %q\n", t.name, err)
                case t.err != "" && err != "":
-                       if strings.Index(err, t.err) < 0 {
+                       if !strings.Contains(err, t.err) {
                                if !bad {
                                        bad = true
                                        fmt.Printf("BUG\n")
@@ -238,4 +240,7 @@ func main() {
                        fmt.Printf("%v/%v: expected %g error; got %g\n", t.f, t.g, t.out, x)
                }
        }
+       if bad {
+               panic("zerodivide")
+       }
 }