]> git.saurik.com Git - apple/objc4.git/blob - test/unwind.m
objc4-437.tar.gz
[apple/objc4.git] / test / unwind.m
1 #include "test.h"
2 #include <objc/objc-exception.h>
3 #include <Foundation/Foundation.h>
4
5 #if !defined(__OBJC2__)
6
7 int main()
8 {
9 succeed(__FILE__);
10 }
11
12 #else
13
14 static int state;
15
16 @interface Foo : NSObject @end
17
18 @interface Foo (Unimplemented)
19 +(void)method;
20 @end
21
22
23 @implementation Foo
24
25 -(void)check { state++; }
26 +(void)check { testassert(!"caught class object, not instance"); }
27
28 static id exc;
29
30 static void handler(id unused __unused, void *ctx __unused)
31 {
32 testassert(state == 3); state++;
33 }
34
35 +(BOOL) resolveClassMethod:(SEL)__unused name
36 {
37 testassert(state == 1); state++;
38 objc_addExceptionHandler(&handler, 0);
39 testassert(state == 2); state++;
40 exc = [Foo new];
41 @throw exc;
42 }
43
44
45 @end
46
47 int main()
48 {
49 int i;
50
51 // unwind exception and alt handler through objc_msgSend()
52
53 NSAutoreleasePool *pool = [NSAutoreleasePool new];
54
55 state = 0;
56 for (i = 0; i < 100000; i++) {
57 @try {
58 testassert(state == 0); state++;
59 [Foo method];
60 testassert(0);
61 } @catch (NSException *e) {
62 testassert(0);
63 } @catch (Foo *e) {
64 testassert(e == exc);
65 testassert(state == 4); state++;
66 testassert(state == 5); [e check]; // state++
67 [e release];
68 } @catch (id e) {
69 testassert(0);
70 } @catch (...) {
71 testassert(0);
72 } @finally {
73 testassert(state == 6); state++;
74 }
75 testassert(state == 7); state = 0;
76 }
77
78 [pool drain];
79
80 succeed(__FILE__);
81 }
82
83 #endif