3 // Copyright 2010 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 that zero division causes a panic.
18 type ErrorTest
struct {
26 i8
, j8
, k8
int8 = 0, 0, 1
27 i16
, j16
, k16
int16 = 0, 0, 1
28 i32
, j32
, k32
int32 = 0, 0, 1
29 i64
, j64
, k64
int64 = 0, 0, 1
31 u
, v
, w
uint = 0, 0, 1
32 u8
, v8
, w8
uint8 = 0, 0, 1
33 u16
, v16
, w16
uint16 = 0, 0, 1
34 u32
, v32
, w32
uint32 = 0, 0, 1
35 u64
, v64
, w64
uint64 = 0, 0, 1
36 up
, vp
, wp
uintptr = 0, 0, 1
38 f
, g
, h
float64 = 0, 0, 1
39 f32
, g32
, h32
float32 = 0, 0, 1
40 f64
, g64
, h64
, inf
, negInf
, nan
float64 = 0, 0, 1, math
.Inf(1), math
.Inf(-1), math
.NaN()
42 c
, d
, e complex128
= 0 + 0i
, 0 + 0i
, 1 + 1i
43 c64
, d64
, e64 complex64
= 0 + 0i
, 0 + 0i
, 1 + 1i
44 c128
, d128
, e128 complex128
= 0 + 0i
, 0 + 0i
, 1 + 1i
47 // Fool gccgo into thinking that these variables can change.
107 // We could assign to _ but the compiler optimizes it too easily.
108 func use(v
interface{}) {
112 // Verify error/no error for all types.
113 var errorTests
= []ErrorTest
{
114 // All integer divide by zero should error.
115 ErrorTest
{"int 0/0", func() { use(i
/ j
) }, "divide"},
116 ErrorTest
{"int8 0/0", func() { use(i8
/ j8
) }, "divide"},
117 ErrorTest
{"int16 0/0", func() { use(i16
/ j16
) }, "divide"},
118 ErrorTest
{"int32 0/0", func() { use(i32
/ j32
) }, "divide"},
119 ErrorTest
{"int64 0/0", func() { use(i64
/ j64
) }, "divide"},
121 ErrorTest
{"int 1/0", func() { use(k
/ j
) }, "divide"},
122 ErrorTest
{"int8 1/0", func() { use(k8
/ j8
) }, "divide"},
123 ErrorTest
{"int16 1/0", func() { use(k16
/ j16
) }, "divide"},
124 ErrorTest
{"int32 1/0", func() { use(k32
/ j32
) }, "divide"},
125 ErrorTest
{"int64 1/0", func() { use(k64
/ j64
) }, "divide"},
127 ErrorTest
{"uint 0/0", func() { use(u
/ v
) }, "divide"},
128 ErrorTest
{"uint8 0/0", func() { use(u8
/ v8
) }, "divide"},
129 ErrorTest
{"uint16 0/0", func() { use(u16
/ v16
) }, "divide"},
130 ErrorTest
{"uint32 0/0", func() { use(u32
/ v32
) }, "divide"},
131 ErrorTest
{"uint64 0/0", func() { use(u64
/ v64
) }, "divide"},
132 ErrorTest
{"uintptr 0/0", func() { use(up
/ vp
) }, "divide"},
134 ErrorTest
{"uint 1/0", func() { use(w
/ v
) }, "divide"},
135 ErrorTest
{"uint8 1/0", func() { use(w8
/ v8
) }, "divide"},
136 ErrorTest
{"uint16 1/0", func() { use(w16
/ v16
) }, "divide"},
137 ErrorTest
{"uint32 1/0", func() { use(w32
/ v32
) }, "divide"},
138 ErrorTest
{"uint64 1/0", func() { use(w64
/ v64
) }, "divide"},
139 ErrorTest
{"uintptr 1/0", func() { use(wp
/ vp
) }, "divide"},
141 // All float64ing divide by zero should not error.
142 ErrorTest
{"float64 0/0", func() { use(f
/ g
) }, ""},
143 ErrorTest
{"float32 0/0", func() { use(f32
/ g32
) }, ""},
144 ErrorTest
{"float64 0/0", func() { use(f64
/ g64
) }, ""},
146 ErrorTest
{"float64 1/0", func() { use(h
/ g
) }, ""},
147 ErrorTest
{"float32 1/0", func() { use(h32
/ g32
) }, ""},
148 ErrorTest
{"float64 1/0", func() { use(h64
/ g64
) }, ""},
149 ErrorTest
{"float64 inf/0", func() { use(inf
/ g64
) }, ""},
150 ErrorTest
{"float64 -inf/0", func() { use(negInf
/ g64
) }, ""},
151 ErrorTest
{"float64 nan/0", func() { use(nan
/ g64
) }, ""},
153 // All complex divide by zero should not error.
154 ErrorTest
{"complex 0/0", func() { use(c
/ d
) }, ""},
155 ErrorTest
{"complex64 0/0", func() { use(c64
/ d64
) }, ""},
156 ErrorTest
{"complex128 0/0", func() { use(c128
/ d128
) }, ""},
158 ErrorTest
{"complex 1/0", func() { use(e
/ d
) }, ""},
159 ErrorTest
{"complex64 1/0", func() { use(e64
/ d64
) }, ""},
160 ErrorTest
{"complex128 1/0", func() { use(e128
/ d128
) }, ""},
163 func error_(fn
func()) (error
string) {
165 if e
:= recover(); e
!= nil {
166 error
= e
.(runtime
.Error
).Error()
173 type FloatTest
struct {
178 var float64Tests
= []FloatTest
{
179 FloatTest
{0, 0, nan
},
180 FloatTest
{nan
, 0, nan
},
181 FloatTest
{inf
, 0, inf
},
182 FloatTest
{negInf
, 0, negInf
},
185 func alike(a
, b
float64) bool {
187 case math
.IsNaN(a
) && math
.IsNaN(b
):
190 return math
.Signbit(a
) == math
.Signbit(b
)
197 for _
, t
:= range errorTests
{
203 case t
.err
== "" && err
== "":
205 case t
.err
!= "" && err
== "":
210 fmt
.Printf("%s: expected %q; got no error\n", t
.name
, t
.err
)
211 case t
.err
== "" && err
!= "":
216 fmt
.Printf("%s: expected no error; got %q\n", t
.name
, err
)
217 case t
.err
!= "" && err
!= "":
218 if strings
.Index(err
, t
.err
) < 0 {
223 fmt
.Printf("%s: expected %q; got %q\n", t
.name
, t
.err
, err
)
229 // At this point we know we don't error on the values we're testing
230 for _
, t
:= range float64Tests
{
232 if !alike(x
, t
.out
) {
237 fmt
.Printf("%v/%v: expected %g error; got %g\n", t
.f
, t
.g
, t
.out
, x
)