Skip to content

Commit 3a8b7f0

Browse files
committed
Added additional exception tests.
Adds ARC and ObjC++ variants of ExceptionTest.
1 parent 7c79dfc commit 3a8b7f0

File tree

4 files changed

+301
-0
lines changed

4 files changed

+301
-0
lines changed

Test/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ set(TESTS
2626
ConstantString.m
2727
Category.m
2828
ExceptionTest.m
29+
ExceptionTest_arc.m
2930
Forward.m
3031
ManyManySelectors.m
3132
NestedExceptions.m
@@ -55,6 +56,13 @@ set(TESTS
5556
setSuperclass.m
5657
)
5758

59+
if (ENABLE_OBJCXX)
60+
list(APPEND TESTS
61+
ExceptionTestObjCXX.mm
62+
ExceptionTestObjCXX_arc.mm
63+
)
64+
endif()
65+
5866
if (WIN32)
5967
else ()
6068
# Don't run the tests that are specific to Itanium-style exceptions on

Test/ExceptionTestObjCXX.mm

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#include "Test.h"
2+
3+
BOOL finallyEntered = NO;
4+
BOOL cleanupRun = NO;
5+
BOOL idRethrown = NO;
6+
BOOL catchallRethrown = NO;
7+
BOOL testCaught = NO;
8+
BOOL wrongMatch = NO;
9+
10+
@interface NSString : Test @end
11+
void runCleanup(void *x)
12+
{
13+
assert(cleanupRun == NO);
14+
cleanupRun = YES;
15+
}
16+
17+
int throwException(void)
18+
{
19+
@throw [Test new];
20+
}
21+
22+
int finally(void)
23+
{
24+
__attribute__((cleanup(runCleanup)))
25+
int x;
26+
(void)x;
27+
@try { throwException(); }
28+
@finally { finallyEntered = YES; }
29+
return 0;
30+
}
31+
int rethrow_id(void)
32+
{
33+
@try { finally(); }
34+
@catch(id x)
35+
{
36+
assert(object_getClass(x) == [Test class]);
37+
idRethrown = YES;
38+
@throw;
39+
}
40+
return 0;
41+
}
42+
int rethrow_test(void)
43+
{
44+
@try { rethrow_id(); }
45+
@catch (Test *t)
46+
{
47+
testCaught = YES;
48+
@throw;
49+
}
50+
@catch (id x)
51+
{
52+
assert(0 && "should not be reached!");
53+
}
54+
@catch (...)
55+
{
56+
assert(0 && "should not be reached!");
57+
}
58+
}
59+
int rethrow_catchall(void)
60+
{
61+
@try { rethrow_test(); }
62+
@catch(...)
63+
{
64+
assert(testCaught);
65+
catchallRethrown = YES;
66+
@throw;
67+
}
68+
return 0;
69+
}
70+
int not_matched_catch(void)
71+
{
72+
@try { rethrow_catchall(); }
73+
@catch(NSString *s)
74+
{
75+
wrongMatch = YES;
76+
}
77+
return 0;
78+
}
79+
80+
int main(void)
81+
{
82+
@try
83+
{
84+
rethrow_catchall();
85+
}
86+
@catch (id x)
87+
{
88+
assert(finallyEntered == YES);
89+
assert(cleanupRun == YES);
90+
assert(idRethrown == YES);
91+
assert(catchallRethrown == YES);
92+
assert(wrongMatch == NO);
93+
assert(object_getClass(x) == [Test class]);
94+
[x dealloc];
95+
}
96+
return 0;
97+
}

Test/ExceptionTestObjCXX_arc.mm

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
#include "Test.h"
2+
3+
BOOL finallyEntered = NO;
4+
BOOL cleanupRun = NO;
5+
BOOL idRethrown = NO;
6+
BOOL catchallRethrown = NO;
7+
BOOL testCaught = NO;
8+
BOOL wrongMatch = NO;
9+
10+
@interface NSString : Test @end
11+
void runCleanup(void *x)
12+
{
13+
assert(cleanupRun == NO);
14+
cleanupRun = YES;
15+
}
16+
17+
int throwException(void)
18+
{
19+
@throw [Test new];
20+
}
21+
22+
int finally(void)
23+
{
24+
__attribute__((cleanup(runCleanup)))
25+
int x;
26+
(void)x;
27+
@try { throwException(); }
28+
@finally { finallyEntered = YES; }
29+
return 0;
30+
}
31+
int rethrow_id(void)
32+
{
33+
@try { finally(); }
34+
@catch(id x)
35+
{
36+
assert(object_getClass(x) == [Test class]);
37+
idRethrown = YES;
38+
@throw;
39+
}
40+
return 0;
41+
}
42+
int rethrow_test(void)
43+
{
44+
@try { rethrow_id(); }
45+
@catch (Test *t)
46+
{
47+
testCaught = YES;
48+
@throw;
49+
}
50+
@catch (id x)
51+
{
52+
assert(0 && "should not be reached!");
53+
}
54+
@catch (...)
55+
{
56+
assert(0 && "should not be reached!");
57+
}
58+
}
59+
int rethrow_catchall(void)
60+
{
61+
@try { rethrow_test(); }
62+
@catch(...)
63+
{
64+
assert(testCaught);
65+
catchallRethrown = YES;
66+
@throw;
67+
}
68+
return 0;
69+
}
70+
int not_matched_catch(void)
71+
{
72+
@try { rethrow_catchall(); }
73+
@catch(NSString *s)
74+
{
75+
wrongMatch = YES;
76+
}
77+
return 0;
78+
}
79+
80+
int main(void)
81+
{
82+
@try
83+
{
84+
rethrow_catchall();
85+
}
86+
@catch (id x)
87+
{
88+
assert(finallyEntered == YES);
89+
assert(cleanupRun == YES);
90+
assert(idRethrown == YES);
91+
assert(catchallRethrown == YES);
92+
assert(wrongMatch == NO);
93+
assert(object_getClass(x) == [Test class]);
94+
}
95+
return 0;
96+
}

Test/ExceptionTest_arc.m

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
#include "Test.h"
2+
3+
#if __cplusplus
4+
#error This is not an ObjC++ test!
5+
#endif
6+
7+
BOOL finallyEntered = NO;
8+
BOOL cleanupRun = NO;
9+
BOOL idRethrown = NO;
10+
BOOL catchallRethrown = NO;
11+
BOOL testCaught = NO;
12+
BOOL wrongMatch = NO;
13+
14+
@interface NSString : Test @end
15+
void runCleanup(void *x)
16+
{
17+
assert(cleanupRun == NO);
18+
cleanupRun = YES;
19+
}
20+
21+
int throw(void)
22+
{
23+
@throw [Test new];
24+
}
25+
26+
int finally(void)
27+
{
28+
__attribute__((cleanup(runCleanup)))
29+
int x;
30+
(void)x;
31+
@try { throw(); }
32+
@finally { finallyEntered = YES; }
33+
return 0;
34+
}
35+
int rethrow_id(void)
36+
{
37+
@try { finally(); }
38+
@catch(id x)
39+
{
40+
assert(object_getClass(x) == [Test class]);
41+
idRethrown = YES;
42+
@throw;
43+
}
44+
return 0;
45+
}
46+
int rethrow_test(void)
47+
{
48+
@try { rethrow_id(); }
49+
@catch (Test *t)
50+
{
51+
testCaught = YES;
52+
@throw;
53+
}
54+
@catch (id x)
55+
{
56+
assert(0 && "should not be reached!");
57+
}
58+
@catch (...)
59+
{
60+
assert(0 && "should not be reached!");
61+
}
62+
}
63+
int rethrow_catchall(void)
64+
{
65+
@try { rethrow_test(); }
66+
@catch(...)
67+
{
68+
assert(testCaught);
69+
catchallRethrown = YES;
70+
@throw;
71+
}
72+
return 0;
73+
}
74+
int not_matched_catch(void)
75+
{
76+
@try { rethrow_catchall(); }
77+
@catch(NSString *s)
78+
{
79+
wrongMatch = YES;
80+
}
81+
return 0;
82+
}
83+
84+
int main(void)
85+
{
86+
@try
87+
{
88+
rethrow_catchall();
89+
}
90+
@catch (id x)
91+
{
92+
assert(finallyEntered == YES);
93+
assert(cleanupRun == YES);
94+
assert(idRethrown == YES);
95+
assert(catchallRethrown == YES);
96+
assert(wrongMatch == NO);
97+
assert(object_getClass(x) == [Test class]);
98+
}
99+
return 0;
100+
}

0 commit comments

Comments
 (0)