Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ARC and ObjC++ variants of ExceptionTest #138

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 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
FastRefCount.m
Forward.m
Expand Down Expand Up @@ -58,6 +59,15 @@ set(TESTS
setSuperclass.m
)

if (ENABLE_OBJCXX)
list(APPEND TESTS
ExceptionTestObjCXX.mm
ExceptionTestObjCXX_arc.mm
NestedExceptionsObjCXX.mm
NestedExceptionsObjCXX_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;
}
33 changes: 33 additions & 0 deletions Test/NestedExceptionsObjCXX.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include "Test.h"


id a;
int throwException(void)
{
@throw a;
}


int main(void)
{
id e1 = @"e1";
id e2 = @"e2";
@try
{
a = e1;
throwException();
}
@catch (id x)
{
assert(x == e1);
@try {
a = e2;
@throw a;
}
@catch (id y)
{
assert(y == e2);
}
}
return 0;
}
33 changes: 33 additions & 0 deletions Test/NestedExceptionsObjCXX_arc.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include "Test.h"


id a;
int throwException(void)
{
@throw a;
}


int main(void)
{
id e1 = @"e1";
id e2 = @"e2";
@try
{
a = e1;
throwException();
}
@catch (id x)
{
assert(x == e1);
@try {
a = e2;
@throw a;
}
@catch (id y)
{
assert(y == e2);
}
}
return 0;
}