Skip to content

Commit

Permalink
Added additional exception tests.
Browse files Browse the repository at this point in the history
Adds ARC and ObjC++ variants of ExceptionTest.
  • Loading branch information
triplef committed May 2, 2020
1 parent 3b88797 commit 7f460e4
Show file tree
Hide file tree
Showing 4 changed files with 301 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ set(TESTS
ConstantString.m
Category.m
ExceptionTest.m
ExceptionTest_arc.m
FastARC.m
Forward.m
ManyManySelectors.m
Expand Down Expand Up @@ -57,6 +58,13 @@ set(TESTS
setSuperclass.m
)

if (ENABLE_OBJCXX)
list(APPEND TESTS
ExceptionTestObjCXX.mm
ExceptionTestObjCXX_arc.mm
)
endif()

if (WIN32)
else ()
# Don't run the tests that are specific to Itanium-style exceptions on
Expand Down
97 changes: 97 additions & 0 deletions Test/ExceptionTestObjCXX.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#include "Test.h"

BOOL finallyEntered = NO;
BOOL cleanupRun = NO;
BOOL idRethrown = NO;
BOOL catchallRethrown = NO;
BOOL testCaught = NO;
BOOL wrongMatch = NO;

@interface NSString : Test @end
void runCleanup(void *x)
{
assert(cleanupRun == NO);
cleanupRun = YES;
}

int throwException(void)
{
@throw [Test new];
}

int finally(void)
{
__attribute__((cleanup(runCleanup)))
int x;
(void)x;
@try { throwException(); }
@finally { finallyEntered = YES; }
return 0;
}
int rethrow_id(void)
{
@try { finally(); }
@catch(id x)
{
assert(object_getClass(x) == [Test class]);
idRethrown = YES;
@throw;
}
return 0;
}
int rethrow_test(void)
{
@try { rethrow_id(); }
@catch (Test *t)
{
testCaught = YES;
@throw;
}
@catch (id x)
{
assert(0 && "should not be reached!");
}
@catch (...)
{
assert(0 && "should not be reached!");
}
}
int rethrow_catchall(void)
{
@try { rethrow_test(); }
@catch(...)
{
assert(testCaught);
catchallRethrown = YES;
@throw;
}
return 0;
}
int not_matched_catch(void)
{
@try { rethrow_catchall(); }
@catch(NSString *s)
{
wrongMatch = YES;
}
return 0;
}

int main(void)
{
@try
{
rethrow_catchall();
}
@catch (id x)
{
assert(finallyEntered == YES);
assert(cleanupRun == YES);
assert(idRethrown == YES);
assert(catchallRethrown == YES);
assert(wrongMatch == NO);
assert(object_getClass(x) == [Test class]);
[x dealloc];
}
return 0;
}
96 changes: 96 additions & 0 deletions Test/ExceptionTestObjCXX_arc.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#include "Test.h"

BOOL finallyEntered = NO;
BOOL cleanupRun = NO;
BOOL idRethrown = NO;
BOOL catchallRethrown = NO;
BOOL testCaught = NO;
BOOL wrongMatch = NO;

@interface NSString : Test @end
void runCleanup(void *x)
{
assert(cleanupRun == NO);
cleanupRun = YES;
}

int throwException(void)
{
@throw [Test new];
}

int finally(void)
{
__attribute__((cleanup(runCleanup)))
int x;
(void)x;
@try { throwException(); }
@finally { finallyEntered = YES; }
return 0;
}
int rethrow_id(void)
{
@try { finally(); }
@catch(id x)
{
assert(object_getClass(x) == [Test class]);
idRethrown = YES;
@throw;
}
return 0;
}
int rethrow_test(void)
{
@try { rethrow_id(); }
@catch (Test *t)
{
testCaught = YES;
@throw;
}
@catch (id x)
{
assert(0 && "should not be reached!");
}
@catch (...)
{
assert(0 && "should not be reached!");
}
}
int rethrow_catchall(void)
{
@try { rethrow_test(); }
@catch(...)
{
assert(testCaught);
catchallRethrown = YES;
@throw;
}
return 0;
}
int not_matched_catch(void)
{
@try { rethrow_catchall(); }
@catch(NSString *s)
{
wrongMatch = YES;
}
return 0;
}

int main(void)
{
@try
{
rethrow_catchall();
}
@catch (id x)
{
assert(finallyEntered == YES);
assert(cleanupRun == YES);
assert(idRethrown == YES);
assert(catchallRethrown == YES);
assert(wrongMatch == NO);
assert(object_getClass(x) == [Test class]);
}
return 0;
}
100 changes: 100 additions & 0 deletions Test/ExceptionTest_arc.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#include "Test.h"

#if __cplusplus
#error This is not an ObjC++ test!
#endif

BOOL finallyEntered = NO;
BOOL cleanupRun = NO;
BOOL idRethrown = NO;
BOOL catchallRethrown = NO;
BOOL testCaught = NO;
BOOL wrongMatch = NO;

@interface NSString : Test @end
void runCleanup(void *x)
{
assert(cleanupRun == NO);
cleanupRun = YES;
}

int throw(void)
{
@throw [Test new];
}

int finally(void)
{
__attribute__((cleanup(runCleanup)))
int x;
(void)x;
@try { throw(); }
@finally { finallyEntered = YES; }
return 0;
}
int rethrow_id(void)
{
@try { finally(); }
@catch(id x)
{
assert(object_getClass(x) == [Test class]);
idRethrown = YES;
@throw;
}
return 0;
}
int rethrow_test(void)
{
@try { rethrow_id(); }
@catch (Test *t)
{
testCaught = YES;
@throw;
}
@catch (id x)
{
assert(0 && "should not be reached!");
}
@catch (...)
{
assert(0 && "should not be reached!");
}
}
int rethrow_catchall(void)
{
@try { rethrow_test(); }
@catch(...)
{
assert(testCaught);
catchallRethrown = YES;
@throw;
}
return 0;
}
int not_matched_catch(void)
{
@try { rethrow_catchall(); }
@catch(NSString *s)
{
wrongMatch = YES;
}
return 0;
}

int main(void)
{
@try
{
rethrow_catchall();
}
@catch (id x)
{
assert(finallyEntered == YES);
assert(cleanupRun == YES);
assert(idRethrown == YES);
assert(catchallRethrown == YES);
assert(wrongMatch == NO);
assert(object_getClass(x) == [Test class]);
}
return 0;
}

0 comments on commit 7f460e4

Please sign in to comment.