OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [gcc/] [testsuite/] [go.test/] [test/] [recover3.go] - Blame information for rev 700

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 700 jeremybenn
// $G $D/$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
        "runtime"
11
        "strings"
12
)
13
 
14
var didbug bool
15
 
16
func bug() {
17
        if didbug {
18
                return
19
        }
20
        println("BUG")
21
        didbug = true
22
}
23
 
24
func check(name string, f func(), err string) {
25
        defer func() {
26
                v := recover()
27
                if v == nil {
28
                        bug()
29
                        println(name, "did not panic")
30
                        return
31
                }
32
                runt, ok := v.(runtime.Error)
33
                if !ok {
34
                        bug()
35
                        println(name, "panicked but not with runtime.Error")
36
                        return
37
                }
38
                s := runt.Error()
39
                if strings.Index(s, err) < 0 {
40
                        bug()
41
                        println(name, "panicked with", s, "not", err)
42
                        return
43
                }
44
        }()
45
 
46
        f()
47
}
48
 
49
func main() {
50
        var x int
51
        var x64 int64
52
        var p *[10]int
53
        var q *[10000]int
54
        var i int
55
 
56
        check("int-div-zero", func() { println(1 / x) }, "integer divide by zero")
57
        check("int64-div-zero", func() { println(1 / x64) }, "integer divide by zero")
58
 
59
        check("nil-deref", func() { println(p[0]) }, "nil pointer dereference")
60
        check("nil-deref-1", func() { println(p[1]) }, "nil pointer dereference")
61
        check("nil-deref-big", func() { println(q[5000]) }, "nil pointer dereference")
62
 
63
        i = 99999
64
        var sl []int
65
        check("array-bounds", func() { println(p[i]) }, "index out of range")
66
        check("slice-bounds", func() { println(sl[i]) }, "index out of range")
67
 
68
        var inter interface{}
69
        inter = 1
70
        check("type-concrete", func() { println(inter.(string)) }, "int, not string")
71
        check("type-interface", func() { println(inter.(m)) }, "missing method m")
72
}
73
 
74
type m interface {
75
        m()
76
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.