Source file
test/zerodivide.go
1
2
3
4
5
6
7
8
9 package main
10
11 import (
12 "fmt"
13 "math"
14 "runtime"
15 "strings"
16 )
17
18 type ErrorTest struct {
19 name string
20 fn func()
21 err string
22 }
23
24 var (
25 i, j, k int = 0, 0, 1
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
30
31 bb = []int16{2, 0}
32
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
39
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()
43
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
47 )
48
49
50 func NotCalled() {
51 i++
52 j++
53 k++
54 i8++
55 j8++
56 k8++
57 i16++
58 j16++
59 k16++
60 i32++
61 j32++
62 k32++
63 i64++
64 j64++
65 k64++
66
67 u++
68 v++
69 w++
70 u8++
71 v8++
72 w8++
73 u16++
74 v16++
75 w16++
76 u32++
77 v32++
78 w32++
79 u64++
80 v64++
81 w64++
82 up++
83 vp++
84 wp++
85
86 f += 1
87 g += 1
88 h += 1
89 f32 += 1
90 g32 += 1
91 h32 += 1
92 f64 += 1
93 g64 += 1
94 h64 += 1
95
96 c += 1 + 1i
97 d += 1 + 1i
98 e += 1 + 1i
99 c64 += 1 + 1i
100 d64 += 1 + 1i
101 e64 += 1 + 1i
102 c128 += 1 + 1i
103 d128 += 1 + 1i
104 e128 += 1 + 1i
105 }
106
107 var tmp interface{}
108
109
110 func use(v interface{}) {
111 tmp = v
112 }
113
114
115 var errorTests = []ErrorTest{
116
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"},
122
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"},
128
129
130
131 ErrorTest{"int16 _ = bb[0]/bb[1]", func() { _ = bb[0] / bb[1] }, "divide"},
132
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"},
139
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"},
146
147
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) }, ""},
151
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) }, ""},
158
159
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) }, ""},
163
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) }, ""},
167 }
168
169 func error_(fn func()) (error string) {
170 defer func() {
171 if e := recover(); e != nil {
172 error = e.(runtime.Error).Error()
173 }
174 }()
175 fn()
176 return ""
177 }
178
179 type FloatTest struct {
180 f, g float64
181 out float64
182 }
183
184 var float64Tests = []FloatTest{
185 FloatTest{0, 0, nan},
186 FloatTest{nan, 0, nan},
187 FloatTest{inf, 0, inf},
188 FloatTest{negInf, 0, negInf},
189 }
190
191 func alike(a, b float64) bool {
192 switch {
193 case math.IsNaN(a) && math.IsNaN(b):
194 return true
195 case a == b:
196 return math.Signbit(a) == math.Signbit(b)
197 }
198 return false
199 }
200
201 func main() {
202 bad := false
203 for _, t := range errorTests {
204 err := error_(t.fn)
205 switch {
206 case t.err == "" && err == "":
207
208 case t.err != "" && err == "":
209 if !bad {
210 bad = true
211 fmt.Printf("BUG\n")
212 }
213 fmt.Printf("%s: expected %q; got no error\n", t.name, t.err)
214 case t.err == "" && err != "":
215 if !bad {
216 bad = true
217 fmt.Printf("BUG\n")
218 }
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) {
222 if !bad {
223 bad = true
224 fmt.Printf("BUG\n")
225 }
226 fmt.Printf("%s: expected %q; got %q\n", t.name, t.err, err)
227 continue
228 }
229 }
230 }
231
232
233 for _, t := range float64Tests {
234 x := t.f / t.g
235 if !alike(x, t.out) {
236 if !bad {
237 bad = true
238 fmt.Printf("BUG\n")
239 }
240 fmt.Printf("%v/%v: expected %g error; got %g\n", t.f, t.g, t.out, x)
241 }
242 }
243 if bad {
244 panic("zerodivide")
245 }
246 }
247
View as plain text