]> Cypherpunks.ru repositories - gostls13.git/blob - test/zerodivide.go
Add test for division by zero.
[gostls13.git] / test / zerodivide.go
1 // $G $F.go && $L $F.$A && ./$A.out
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         "strings"
13 )
14
15 type Error interface {
16         String() string
17 }
18
19 type ErrorTest struct {
20         name    string
21         fn      func()
22         err     string
23 }
24
25 var (
26         i, j, k int = 0, 0, 1
27         i8, j8, k8 int8 = 0, 0, 1
28         i16, j16, k16 int16 = 0, 0, 1
29         i32, j32, k32 int32 = 0, 0, 1
30         i64, j64, k64 int64 = 0, 0, 1
31
32         u, v, w uint = 0, 0, 1
33         u8, v8, w8 uint8 = 0, 0, 1
34         u16, v16, w16 uint16 = 0, 0, 1
35         u32, v32, w32 uint32 = 0, 0, 1
36         u64, v64, w64 uint64 = 0, 0, 1
37         up, vp, wp uintptr = 0, 0, 1
38
39         f, g, h float = 0, 0, 1
40         f32, g32, h32 float32 = 0, 0, 1
41         f64, g64, h64, inf, negInf, nan float64 = 0, 0, 1, math.Inf(1), math.Inf(-1), math.NaN()
42
43         c, d, e complex = 0+0i, 0+0i, 1+1i
44         c64, d64, e64 complex64 = 0+0i, 0+0i, 1+1i
45         c128, d128, e128 complex128 = 0+0i, 0+0i, 1+1i
46 )
47
48 var tmp interface{}
49
50 // We could assign to _ but the compiler optimizes it too easily.
51 func use(v interface{}) {
52         tmp = v
53 }
54
55 // Verify error/no error for all types.
56 var errorTests = []ErrorTest{
57         // All integer divide by zero should error.
58         ErrorTest{ "int 0/0", func() { use(i/j) }, "divide", },
59 // TODO commented out: fails in 8g.
60 //      ErrorTest{ "int8 0/0", func() { use(i8/j8) }, "divide", },
61         ErrorTest{ "int16 0/0", func() { use(i16/j16) }, "divide", },
62         ErrorTest{ "int32 0/0", func() { use(i32/j32) }, "divide", },
63         ErrorTest{ "int64 0/0", func() { use(i64/j64) }, "divide", },
64
65         ErrorTest{ "int 1/0", func() { use(k/j) }, "divide", },
66 // TODO commented out: fails in 8g.
67 //      ErrorTest{ "int8 1/0", func() { use(k8/j8) }, "divide", },
68         ErrorTest{ "int16 1/0", func() { use(k16/j16) }, "divide", },
69         ErrorTest{ "int32 1/0", func() { use(k32/j32) }, "divide", },
70         ErrorTest{ "int64 1/0", func() { use(k64/j64) }, "divide", },
71
72         ErrorTest{ "uint 0/0", func() { use(u/v) }, "divide", },
73 // TODO commented out: fails in 8g.
74 //      ErrorTest{ "uint8 0/0", func() { use(u8/v8) }, "divide", },
75         ErrorTest{ "uint16 0/0", func() { use(u16/v16) }, "divide", },
76         ErrorTest{ "uint32 0/0", func() { use(u32/v32) }, "divide", },
77         ErrorTest{ "uint64 0/0", func() { use(u64/v64) }, "divide", },
78         ErrorTest{ "uintptr 0/0", func() { use(up/vp) }, "divide", },
79
80         ErrorTest{ "uint 1/0", func() { use(w/v) }, "divide", },
81 // TODO commented out: fails in 8g.
82 //      ErrorTest{ "uint8 1/0", func() { use(w8/v8) }, "divide", },
83         ErrorTest{ "uint16 1/0", func() { use(w16/v16) }, "divide", },
84         ErrorTest{ "uint32 1/0", func() { use(w32/v32) }, "divide", },
85         ErrorTest{ "uint64 1/0", func() { use(w64/v64) }, "divide", },
86         ErrorTest{ "uintptr 1/0", func() { use(wp/vp) }, "divide", },
87
88         // All floating divide by zero should not error.
89         ErrorTest{ "float 0/0", func() { use(f/g) }, "", },
90         ErrorTest{ "float32 0/0", func() { use(f32/g32) }, "", },
91         ErrorTest{ "float64 0/0", func() { use(f64/g64) }, "", },
92
93         ErrorTest{ "float 1/0", func() { use(h/g) }, "", },
94         ErrorTest{ "float32 1/0", func() { use(h32/g32) }, "", },
95         ErrorTest{ "float64 1/0", func() { use(h64/g64) }, "", },
96         ErrorTest{ "float64 inf/0", func() { use(inf/g64) }, "", },
97         ErrorTest{ "float64 -inf/0", func() { use(negInf/g64) }, "", },
98         ErrorTest{ "float64 nan/0", func() { use(nan/g64) }, "", },
99
100         // All complex divide by zero should not error.
101         ErrorTest{ "complex 0/0", func() { use(c/d) }, "", },
102         ErrorTest{ "complex64 0/0", func() { use(c64/d64) }, "", },
103         ErrorTest{ "complex128 0/0", func() { use(c128/d128) }, "", },
104
105         ErrorTest{ "complex 1/0", func() { use(e/d) }, "", },
106         ErrorTest{ "complex64 1/0", func() { use(e64/d64) }, "", },
107         ErrorTest{ "complex128 1/0", func() { use(e128/d128) }, "", },
108 }
109
110 func error(fn func()) (error string) {
111         defer func() {
112                 if e := recover(); e != nil {
113                         error = e.(Error).String()
114                 }
115         }()
116         fn()
117         return ""
118 }
119
120 type FloatTest struct{
121         name    string
122         f, g    float64
123         out     float64
124 }
125
126 var floatTests = []FloatTest{
127         FloatTest{"float64 0/0", 0, 0, nan },
128         FloatTest{"float64 nan/0", nan, 0, nan },
129         FloatTest{"float64 inf/0", inf, 0, inf },
130         FloatTest{"float64 -inf/0", negInf, 0, negInf },
131 }
132
133 func alike(a, b float64) bool {
134         switch {
135         case math.IsNaN(a) && math.IsNaN(b):
136                 return true
137         case a == b:
138                 return math.Signbit(a) == math.Signbit(b)
139         }
140         return false
141 }
142
143 func main() {
144         for _, t := range errorTests {
145                 err := error(t.fn)
146                 switch {
147                 case t.err == "" && err == "":
148                         // fine
149                 case t.err != "" && err == "":
150                         fmt.Printf("%s: expected %q; got no error\n", t.name, t.err)
151                 case t.err == "" && err != "":
152                         fmt.Printf("%s: expected no error; got %q\n", t.name, err)
153                 case t.err != "" && err != "":
154                         if strings.Index(err, t.err) < 0 {
155                                 fmt.Printf("%s: expected %q; got %q\n", t.name, t.err, err)
156                                 continue
157                         }
158                 }
159         }
160
161         // At this point we know we don't error on the values we're testing
162         for _, t := range floatTests {
163                 x := t.f/t.g
164                 if !alike(x, t.out) {
165                         fmt.Printf("%s: expected %g error; got %g\n", t.name, t.out, x)
166                 }
167         }
168 }