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