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
33 u, v, w uint = 0, 0, 1
34 u8, v8, w8 uint8 = 0, 0, 1
35 u16, v16, w16 uint16 = 0, 0, 1
36 u32, v32, w32 uint32 = 0, 0, 1
37 u64, v64, w64 uint64 = 0, 0, 1
38 up, vp, wp uintptr = 0, 0, 1
40 f, g, h float64 = 0, 0, 1
41 f32, g32, h32 float32 = 0, 0, 1
42 f64, g64, h64, inf, negInf, nan float64 = 0, 0, 1, math.Inf(1), math.Inf(-1), math.NaN()
44 c, d, e complex128 = 0 + 0i, 0 + 0i, 1 + 1i
45 c64, d64, e64 complex64 = 0 + 0i, 0 + 0i, 1 + 1i
46 c128, d128, e128 complex128 = 0 + 0i, 0 + 0i, 1 + 1i
49 // Fool gccgo into thinking that these variables can change.
109 // We could assign to _ but the compiler optimizes it too easily.
110 func use(v interface{}) {
114 // Verify error/no error for all types.
115 var errorTests = []ErrorTest{
116 // All integer divide by zero should error.
117 ErrorTest{"int 0/0", func() { use(i / j) }, "divide"},
118 ErrorTest{"int8 0/0", func() { use(i8 / j8) }, "divide"},
119 ErrorTest{"int16 0/0", func() { use(i16 / j16) }, "divide"},
120 ErrorTest{"int32 0/0", func() { use(i32 / j32) }, "divide"},
121 ErrorTest{"int64 0/0", func() { use(i64 / j64) }, "divide"},
123 ErrorTest{"int 1/0", func() { use(k / j) }, "divide"},
124 ErrorTest{"int8 1/0", func() { use(k8 / j8) }, "divide"},
125 ErrorTest{"int16 1/0", func() { use(k16 / j16) }, "divide"},
126 ErrorTest{"int32 1/0", func() { use(k32 / j32) }, "divide"},
127 ErrorTest{"int64 1/0", func() { use(k64 / j64) }, "divide"},
129 // From issue 5790, we should ensure that _ assignments
130 // still evaluate and generate zerodivide panics.
131 ErrorTest{"int16 _ = bb[0]/bb[1]", func() { _ = bb[0] / bb[1] }, "divide"},
133 ErrorTest{"uint 0/0", func() { use(u / v) }, "divide"},
134 ErrorTest{"uint8 0/0", func() { use(u8 / v8) }, "divide"},
135 ErrorTest{"uint16 0/0", func() { use(u16 / v16) }, "divide"},
136 ErrorTest{"uint32 0/0", func() { use(u32 / v32) }, "divide"},
137 ErrorTest{"uint64 0/0", func() { use(u64 / v64) }, "divide"},
138 ErrorTest{"uintptr 0/0", func() { use(up / vp) }, "divide"},
140 ErrorTest{"uint 1/0", func() { use(w / v) }, "divide"},
141 ErrorTest{"uint8 1/0", func() { use(w8 / v8) }, "divide"},
142 ErrorTest{"uint16 1/0", func() { use(w16 / v16) }, "divide"},
143 ErrorTest{"uint32 1/0", func() { use(w32 / v32) }, "divide"},
144 ErrorTest{"uint64 1/0", func() { use(w64 / v64) }, "divide"},
145 ErrorTest{"uintptr 1/0", func() { use(wp / vp) }, "divide"},
147 // All float64ing divide by zero should not error.
148 ErrorTest{"float64 0/0", func() { use(f / g) }, ""},
149 ErrorTest{"float32 0/0", func() { use(f32 / g32) }, ""},
150 ErrorTest{"float64 0/0", func() { use(f64 / g64) }, ""},
152 ErrorTest{"float64 1/0", func() { use(h / g) }, ""},
153 ErrorTest{"float32 1/0", func() { use(h32 / g32) }, ""},
154 ErrorTest{"float64 1/0", func() { use(h64 / g64) }, ""},
155 ErrorTest{"float64 inf/0", func() { use(inf / g64) }, ""},
156 ErrorTest{"float64 -inf/0", func() { use(negInf / g64) }, ""},
157 ErrorTest{"float64 nan/0", func() { use(nan / g64) }, ""},
159 // All complex divide by zero should not error.
160 ErrorTest{"complex 0/0", func() { use(c / d) }, ""},
161 ErrorTest{"complex64 0/0", func() { use(c64 / d64) }, ""},
162 ErrorTest{"complex128 0/0", func() { use(c128 / d128) }, ""},
164 ErrorTest{"complex 1/0", func() { use(e / d) }, ""},
165 ErrorTest{"complex64 1/0", func() { use(e64 / d64) }, ""},
166 ErrorTest{"complex128 1/0", func() { use(e128 / d128) }, ""},
169 func error_(fn func()) (error string) {
171 if e := recover(); e != nil {
172 error = e.(runtime.Error).Error()
179 type FloatTest struct {
184 var float64Tests = []FloatTest{
185 FloatTest{0, 0, nan},
186 FloatTest{nan, 0, nan},
187 FloatTest{inf, 0, inf},
188 FloatTest{negInf, 0, negInf},
191 func alike(a, b float64) bool {
193 case math.IsNaN(a) && math.IsNaN(b):
196 return math.Signbit(a) == math.Signbit(b)
203 for _, t := range errorTests {
206 case t.err == "" && err == "":
208 case t.err != "" && err == "":
213 fmt.Printf("%s: expected %q; got no error\n", t.name, t.err)
214 case t.err == "" && err != "":
219 fmt.Printf("%s: expected no error; got %q\n", t.name, err)
220 case t.err != "" && err != "":
221 if !strings.Contains(err, t.err) {
226 fmt.Printf("%s: expected %q; got %q\n", t.name, t.err, err)
232 // At this point we know we don't error on the values we're testing
233 for _, t := range float64Tests {
235 if !alike(x, t.out) {
240 fmt.Printf("%v/%v: expected %g error; got %g\n", t.f, t.g, t.out, x)