]> Cypherpunks.ru repositories - gostls13.git/blob - test/zerodivide.go
test: use testlib (final 61)
[gostls13.git] / test / zerodivide.go
1 // run
2
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.
6
7 package main
8
9 import (
10         "fmt"
11         "math"
12         "runtime"
13         "strings"
14 )
15
16 type ErrorTest struct {
17         name string
18         fn   func()
19         err  string
20 }
21
22 var (
23         i, j, k       int   = 0, 0, 1
24         i8, j8, k8    int8  = 0, 0, 1
25         i16, j16, k16 int16 = 0, 0, 1
26         i32, j32, k32 int32 = 0, 0, 1
27         i64, j64, k64 int64 = 0, 0, 1
28
29         u, v, w       uint    = 0, 0, 1
30         u8, v8, w8    uint8   = 0, 0, 1
31         u16, v16, w16 uint16  = 0, 0, 1
32         u32, v32, w32 uint32  = 0, 0, 1
33         u64, v64, w64 uint64  = 0, 0, 1
34         up, vp, wp    uintptr = 0, 0, 1
35
36         f, g, h                         float64 = 0, 0, 1
37         f32, g32, h32                   float32 = 0, 0, 1
38         f64, g64, h64, inf, negInf, nan float64 = 0, 0, 1, math.Inf(1), math.Inf(-1), math.NaN()
39
40         c, d, e          complex128 = 0 + 0i, 0 + 0i, 1 + 1i
41         c64, d64, e64    complex64  = 0 + 0i, 0 + 0i, 1 + 1i
42         c128, d128, e128 complex128 = 0 + 0i, 0 + 0i, 1 + 1i
43 )
44
45 // Fool gccgo into thinking that these variables can change.
46 func NotCalled() {
47         i++
48         j++
49         k++
50         i8++
51         j8++
52         k8++
53         i16++
54         j16++
55         k16++
56         i32++
57         j32++
58         k32++
59         i64++
60         j64++
61         k64++
62
63         u++
64         v++
65         w++
66         u8++
67         v8++
68         w8++
69         u16++
70         v16++
71         w16++
72         u32++
73         v32++
74         w32++
75         u64++
76         v64++
77         w64++
78         up++
79         vp++
80         wp++
81
82         f += 1
83         g += 1
84         h += 1
85         f32 += 1
86         g32 += 1
87         h32 += 1
88         f64 += 1
89         g64 += 1
90         h64 += 1
91
92         c += 1 + 1i
93         d += 1 + 1i
94         e += 1 + 1i
95         c64 += 1 + 1i
96         d64 += 1 + 1i
97         e64 += 1 + 1i
98         c128 += 1 + 1i
99         d128 += 1 + 1i
100         e128 += 1 + 1i
101 }
102
103 var tmp interface{}
104
105 // We could assign to _ but the compiler optimizes it too easily.
106 func use(v interface{}) {
107         tmp = v
108 }
109
110 // Verify error/no error for all types.
111 var errorTests = []ErrorTest{
112         // All integer divide by zero should error.
113         ErrorTest{"int 0/0", func() { use(i / j) }, "divide"},
114         ErrorTest{"int8 0/0", func() { use(i8 / j8) }, "divide"},
115         ErrorTest{"int16 0/0", func() { use(i16 / j16) }, "divide"},
116         ErrorTest{"int32 0/0", func() { use(i32 / j32) }, "divide"},
117         ErrorTest{"int64 0/0", func() { use(i64 / j64) }, "divide"},
118
119         ErrorTest{"int 1/0", func() { use(k / j) }, "divide"},
120         ErrorTest{"int8 1/0", func() { use(k8 / j8) }, "divide"},
121         ErrorTest{"int16 1/0", func() { use(k16 / j16) }, "divide"},
122         ErrorTest{"int32 1/0", func() { use(k32 / j32) }, "divide"},
123         ErrorTest{"int64 1/0", func() { use(k64 / j64) }, "divide"},
124
125         ErrorTest{"uint 0/0", func() { use(u / v) }, "divide"},
126         ErrorTest{"uint8 0/0", func() { use(u8 / v8) }, "divide"},
127         ErrorTest{"uint16 0/0", func() { use(u16 / v16) }, "divide"},
128         ErrorTest{"uint32 0/0", func() { use(u32 / v32) }, "divide"},
129         ErrorTest{"uint64 0/0", func() { use(u64 / v64) }, "divide"},
130         ErrorTest{"uintptr 0/0", func() { use(up / vp) }, "divide"},
131
132         ErrorTest{"uint 1/0", func() { use(w / v) }, "divide"},
133         ErrorTest{"uint8 1/0", func() { use(w8 / v8) }, "divide"},
134         ErrorTest{"uint16 1/0", func() { use(w16 / v16) }, "divide"},
135         ErrorTest{"uint32 1/0", func() { use(w32 / v32) }, "divide"},
136         ErrorTest{"uint64 1/0", func() { use(w64 / v64) }, "divide"},
137         ErrorTest{"uintptr 1/0", func() { use(wp / vp) }, "divide"},
138
139         // All float64ing divide by zero should not error.
140         ErrorTest{"float64 0/0", func() { use(f / g) }, ""},
141         ErrorTest{"float32 0/0", func() { use(f32 / g32) }, ""},
142         ErrorTest{"float64 0/0", func() { use(f64 / g64) }, ""},
143
144         ErrorTest{"float64 1/0", func() { use(h / g) }, ""},
145         ErrorTest{"float32 1/0", func() { use(h32 / g32) }, ""},
146         ErrorTest{"float64 1/0", func() { use(h64 / g64) }, ""},
147         ErrorTest{"float64 inf/0", func() { use(inf / g64) }, ""},
148         ErrorTest{"float64 -inf/0", func() { use(negInf / g64) }, ""},
149         ErrorTest{"float64 nan/0", func() { use(nan / g64) }, ""},
150
151         // All complex divide by zero should not error.
152         ErrorTest{"complex 0/0", func() { use(c / d) }, ""},
153         ErrorTest{"complex64 0/0", func() { use(c64 / d64) }, ""},
154         ErrorTest{"complex128 0/0", func() { use(c128 / d128) }, ""},
155
156         ErrorTest{"complex 1/0", func() { use(e / d) }, ""},
157         ErrorTest{"complex64 1/0", func() { use(e64 / d64) }, ""},
158         ErrorTest{"complex128 1/0", func() { use(e128 / d128) }, ""},
159 }
160
161 func error_(fn func()) (error string) {
162         defer func() {
163                 if e := recover(); e != nil {
164                         error = e.(runtime.Error).Error()
165                 }
166         }()
167         fn()
168         return ""
169 }
170
171 type FloatTest struct {
172         f, g float64
173         out  float64
174 }
175
176 var float64Tests = []FloatTest{
177         FloatTest{0, 0, nan},
178         FloatTest{nan, 0, nan},
179         FloatTest{inf, 0, inf},
180         FloatTest{negInf, 0, negInf},
181 }
182
183 func alike(a, b float64) bool {
184         switch {
185         case math.IsNaN(a) && math.IsNaN(b):
186                 return true
187         case a == b:
188                 return math.Signbit(a) == math.Signbit(b)
189         }
190         return false
191 }
192
193 func main() {
194         bad := false
195         for _, t := range errorTests {
196                 if t.err != "" {
197                         continue
198                 }
199                 err := error_(t.fn)
200                 switch {
201                 case t.err == "" && err == "":
202                         // fine
203                 case t.err != "" && err == "":
204                         if !bad {
205                                 bad = true
206                                 fmt.Printf("BUG\n")
207                         }
208                         fmt.Printf("%s: expected %q; got no error\n", t.name, t.err)
209                 case t.err == "" && err != "":
210                         if !bad {
211                                 bad = true
212                                 fmt.Printf("BUG\n")
213                         }
214                         fmt.Printf("%s: expected no error; got %q\n", t.name, err)
215                 case t.err != "" && err != "":
216                         if strings.Index(err, t.err) < 0 {
217                                 if !bad {
218                                         bad = true
219                                         fmt.Printf("BUG\n")
220                                 }
221                                 fmt.Printf("%s: expected %q; got %q\n", t.name, t.err, err)
222                                 continue
223                         }
224                 }
225         }
226
227         // At this point we know we don't error on the values we're testing
228         for _, t := range float64Tests {
229                 x := t.f / t.g
230                 if !alike(x, t.out) {
231                         if !bad {
232                                 bad = true
233                                 fmt.Printf("BUG\n")
234                         }
235                         fmt.Printf("%v/%v: expected %g error; got %g\n", t.f, t.g, t.out, x)
236                 }
237         }
238 }