github.com/yanyiwu/go@v0.0.0-20150106053140-03d6637dbb7f/test/zerodivide.go (about)

     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  // Test that zero division causes a panic.
     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  	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
    37  
    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()
    41  
    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
    45  )
    46  
    47  // Fool gccgo into thinking that these variables can change.
    48  func NotCalled() {
    49  	i++
    50  	j++
    51  	k++
    52  	i8++
    53  	j8++
    54  	k8++
    55  	i16++
    56  	j16++
    57  	k16++
    58  	i32++
    59  	j32++
    60  	k32++
    61  	i64++
    62  	j64++
    63  	k64++
    64  
    65  	u++
    66  	v++
    67  	w++
    68  	u8++
    69  	v8++
    70  	w8++
    71  	u16++
    72  	v16++
    73  	w16++
    74  	u32++
    75  	v32++
    76  	w32++
    77  	u64++
    78  	v64++
    79  	w64++
    80  	up++
    81  	vp++
    82  	wp++
    83  
    84  	f += 1
    85  	g += 1
    86  	h += 1
    87  	f32 += 1
    88  	g32 += 1
    89  	h32 += 1
    90  	f64 += 1
    91  	g64 += 1
    92  	h64 += 1
    93  
    94  	c += 1 + 1i
    95  	d += 1 + 1i
    96  	e += 1 + 1i
    97  	c64 += 1 + 1i
    98  	d64 += 1 + 1i
    99  	e64 += 1 + 1i
   100  	c128 += 1 + 1i
   101  	d128 += 1 + 1i
   102  	e128 += 1 + 1i
   103  }
   104  
   105  var tmp interface{}
   106  
   107  // We could assign to _ but the compiler optimizes it too easily.
   108  func use(v interface{}) {
   109  	tmp = v
   110  }
   111  
   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"},
   120  
   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"},
   126  
   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"},
   133  
   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"},
   140  
   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) }, ""},
   145  
   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) }, ""},
   152  
   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) }, ""},
   157  
   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) }, ""},
   161  }
   162  
   163  func error_(fn func()) (error string) {
   164  	defer func() {
   165  		if e := recover(); e != nil {
   166  			error = e.(runtime.Error).Error()
   167  		}
   168  	}()
   169  	fn()
   170  	return ""
   171  }
   172  
   173  type FloatTest struct {
   174  	f, g float64
   175  	out  float64
   176  }
   177  
   178  var float64Tests = []FloatTest{
   179  	FloatTest{0, 0, nan},
   180  	FloatTest{nan, 0, nan},
   181  	FloatTest{inf, 0, inf},
   182  	FloatTest{negInf, 0, negInf},
   183  }
   184  
   185  func alike(a, b float64) bool {
   186  	switch {
   187  	case math.IsNaN(a) && math.IsNaN(b):
   188  		return true
   189  	case a == b:
   190  		return math.Signbit(a) == math.Signbit(b)
   191  	}
   192  	return false
   193  }
   194  
   195  func main() {
   196  	bad := false
   197  	for _, t := range errorTests {
   198  		if t.err != "" {
   199  			continue
   200  		}
   201  		err := error_(t.fn)
   202  		switch {
   203  		case t.err == "" && err == "":
   204  			// fine
   205  		case t.err != "" && err == "":
   206  			if !bad {
   207  				bad = true
   208  				fmt.Printf("BUG\n")
   209  			}
   210  			fmt.Printf("%s: expected %q; got no error\n", t.name, t.err)
   211  		case t.err == "" && err != "":
   212  			if !bad {
   213  				bad = true
   214  				fmt.Printf("BUG\n")
   215  			}
   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 {
   219  				if !bad {
   220  					bad = true
   221  					fmt.Printf("BUG\n")
   222  				}
   223  				fmt.Printf("%s: expected %q; got %q\n", t.name, t.err, err)
   224  				continue
   225  			}
   226  		}
   227  	}
   228  
   229  	// At this point we know we don't error on the values we're testing
   230  	for _, t := range float64Tests {
   231  		x := t.f / t.g
   232  		if !alike(x, t.out) {
   233  			if !bad {
   234  				bad = true
   235  				fmt.Printf("BUG\n")
   236  			}
   237  			fmt.Printf("%v/%v: expected %g error; got %g\n", t.f, t.g, t.out, x)
   238  		}
   239  	}
   240  	if bad {
   241  		panic("zerodivide")
   242  	}
   243  }