File size: 1,030 Bytes
e36aeda | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | package main
import (
"runtime"
"testing"
)
func checkDivByZero(f func()) (divByZero bool) {
defer func() {
if r := recover(); r != nil {
if e, ok := r.(runtime.Error); ok && e.Error() == "runtime error: integer divide by zero" {
divByZero = true
}
}
}()
f()
return false
}
//go:noinline
func div_a(i uint, s []int) int {
return s[i%uint(len(s))]
}
//go:noinline
func div_b(i uint, j uint) uint {
return i / j
}
//go:noinline
func div_c(i int) int {
return 7 / (i - i)
}
func TestDivByZero(t *testing.T) {
if got := checkDivByZero(func() { div_b(7, 0) }); !got {
t.Errorf("expected div by zero for b(7, 0), got no error\n")
}
if got := checkDivByZero(func() { div_b(7, 7) }); got {
t.Errorf("expected no error for b(7, 7), got div by zero\n")
}
if got := checkDivByZero(func() { div_a(4, nil) }); !got {
t.Errorf("expected div by zero for a(4, nil), got no error\n")
}
if got := checkDivByZero(func() { div_c(5) }); !got {
t.Errorf("expected div by zero for c(5), got no error\n")
}
}
|