diff options
Diffstat (limited to 'test')
| -rw-r--r-- | test/golden.out | 12 | ||||
| -rw-r--r-- | test/recover.go | 34 | ||||
| -rw-r--r-- | test/recover2.go | 86 |
3 files changed, 107 insertions, 25 deletions
diff --git a/test/golden.out b/test/golden.out index 2bb6f110f..e0b6ad624 100644 --- a/test/golden.out +++ b/test/golden.out @@ -2,26 +2,22 @@ == ./ =========== ./cmp2.go -comparing uncomparable type []int -throw: interface compare +panic: runtime error: comparing uncomparable type []int panic PC=xxx =========== ./cmp3.go -comparing uncomparable type []int -throw: interface compare +panic: runtime error: comparing uncomparable type []int panic PC=xxx =========== ./cmp4.go -hash of unhashable type []int -throw: interface hash +panic: runtime error: hash of unhashable type []int panic PC=xxx =========== ./cmp5.go -hash of unhashable type []int -throw: interface hash +panic: runtime error: hash of unhashable type []int panic PC=xxx diff --git a/test/recover.go b/test/recover.go index 8b47b8247..ca6f07288 100644 --- a/test/recover.go +++ b/test/recover.go @@ -23,21 +23,21 @@ func main() { } func die() { - runtime.Breakpoint() // can't depend on panic + runtime.Breakpoint() // can't depend on panic } func mustRecover(x interface{}) { - mustNotRecover() // because it's not a defer call + mustNotRecover() // because it's not a defer call v := recover() if v == nil { println("missing recover") - die() // panic is useless here + die() // panic is useless here } if v != x { println("wrong value", v, x) die() } - + // the value should be gone now regardless v = recover() if v != nil { @@ -49,19 +49,19 @@ func mustRecover(x interface{}) { func mustNotRecover() { v := recover() if v != nil { - println("spurious recover") + println("spurious recover", v) die() } } func withoutRecover() { - mustNotRecover() // because it's a sub-call + mustNotRecover() // because it's a sub-call } func test1() { - defer mustNotRecover() // because mustRecover will squelch it - defer mustRecover(1) // because of panic below - defer withoutRecover() // should be no-op, leaving for mustRecover to find + defer mustNotRecover() // because mustRecover will squelch it + defer mustRecover(1) // because of panic below + defer withoutRecover() // should be no-op, leaving for mustRecover to find panic(1) } @@ -102,14 +102,14 @@ func test2() { // It does not see the panic when called from a call within a deferred call (too late) // nor does it see the panic when it *is* the deferred call (too early). defer mustRecover(2) - defer recover() // should be no-op + defer recover() // should be no-op panic(2) } func test3() { defer mustNotRecover() defer func() { - recover() // should squelch + recover() // should squelch }() panic(3) } @@ -118,7 +118,7 @@ func test4() { // Equivalent to test3 but using defer to make the call. defer mustNotRecover() defer func() { - defer recover() // should squelch + defer recover() // should squelch }() panic(4) } @@ -154,8 +154,8 @@ func test5() { println("wrong value", v, 5) die() } - - s := try(func() { }, "hi").(string) + + s := try(func() {}, "hi").(string) if s != "hi" { println("wrong value", s, "hi") die() @@ -166,8 +166,8 @@ func test5() { println("try1 wrong value", v, 5) die() } - - s = try1(func() { }, "hi").(string) + + s = try1(func() {}, "hi").(string) if s != "hi" { println("try1 wrong value", s, "hi") die() @@ -183,7 +183,7 @@ func big(mustRecover bool) { x[0] = 1 x[99999] = 1 _ = x - + v := recover() if mustRecover { if v == nil { diff --git a/test/recover2.go b/test/recover2.go new file mode 100644 index 000000000..96d591a15 --- /dev/null +++ b/test/recover2.go @@ -0,0 +1,86 @@ +// $G $D/$F.go && $L $F.$A && ./$A.out + +// Copyright 2010 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Test of recover for run-time errors. + +// TODO(rsc): +// integer divide by zero? +// null pointer accesses + +package main + +import ( + "os" + "strings" +) + +var x = make([]byte, 10) + +func main() { + test1() + test2() + test3() + test4() + test5() + test6() + test7() +} + +func mustRecover(s string) { + v := recover() + if v == nil { + panic("expected panic") + } + if e := v.(os.Error).String(); strings.Index(e, s) < 0 { + panic("want: " + s + "; have: " + e) + } +} + +func test1() { + defer mustRecover("index") + println(x[123]) +} + +func test2() { + defer mustRecover("slice") + println(x[5:15]) +} + +func test3() { + defer mustRecover("slice") + println(x[11:9]) +} + +func test4() { + defer mustRecover("interface") + var x interface{} = 1 + println(x.(float)) +} + +type T struct { + a, b int +} + +func test5() { + defer mustRecover("uncomparable") + var x T + var z interface{} = x + println(z != z) +} + +func test6() { + defer mustRecover("unhashable") + var x T + var z interface{} = x + m := make(map[interface{}]int) + m[z] = 1 +} + +func test7() { + defer mustRecover("complex divide by zero") + var x, y complex + println(x / y) +} |
