-// $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()
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
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"},
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()
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
}
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")
fmt.Printf("%v/%v: expected %g error; got %g\n", t.f, t.g, t.out, x)
}
}
+ if bad {
+ panic("zerodivide")
+ }
}