Index: src/OFExceptions.m ================================================================== --- src/OFExceptions.m +++ src/OFExceptions.m @@ -33,11 +33,11 @@ } - init: (id)obj withSize: (size_t)size { - fprintf(stderr, "ERROR: Could not allocate %zd byte for object %s!\n ", + fprintf(stderr, "ERROR: Could not allocate %zd byte for object %s!\n", size, [obj name]); return [super init]; } @end Index: tests/OFObject/OFObject.m ================================================================== --- tests/OFObject/OFObject.m +++ tests/OFObject/OFObject.m @@ -13,10 +13,23 @@ #import #import #import "OFObject.h" #import "OFExceptions.h" + +#define CATCH_EXCEPTION(code, exception) \ + caught = false; \ + @try { \ + code; \ + } @catch (exception *e) { \ + caught = true; \ + puts("CAUGHT! Resuming..."); \ + } \ + if (!caught) { \ + puts("NOT CAUGHT!"); \ + return 1; \ + } int main() { OFObject *obj = [OFObject new]; @@ -24,21 +37,11 @@ void *p, *q, *r; /* Test freeing memory not allocated by obj */ puts("Freeing memory not allocated by object (should throw an " "exception)..."); - caught = false; - @try { - [obj freeMem: (void*)123]; - } @catch (OFMemNotPartOfObjException *e) { - caught = true; - puts("CAUGHT! Resuming..."); - } - if (!caught) { - puts("NOT CAUGHT!"); - return 1; - } + CATCH_EXCEPTION([obj freeMem: (void*)123], OFMemNotPartOfObjException) /* Test allocating memory */ puts("Allocating memory through object..."); p = [obj getMem: 4096]; puts("Allocated 4096 bytes."); @@ -48,21 +51,11 @@ [obj freeMem: p]; puts("Free'd."); /* It shouldn't be recognized as part of our obj anymore */ puts("Trying to free it again (should throw an exception)..."); - caught = false; - @try { - [obj freeMem: p]; - } @catch (OFMemNotPartOfObjException *e) { - caught = true; - puts("CAUGHT! Resuming..."); - } - if (!caught) { - puts("NOT CAUGHT!"); - return 1; - } + CATCH_EXCEPTION([obj freeMem: p], OFMemNotPartOfObjException) /* Test multiple memory chunks */ puts("Allocating 3 chunks of memory..."); p = [obj getMem: 4096]; q = [obj getMem: 4096]; @@ -76,44 +69,17 @@ [obj freeMem: r]; puts("Freed them all."); /* Try to free again */ puts("Now trying to free them again..."); - caught = false; - @try { - [obj freeMem: p]; - } @catch (OFMemNotPartOfObjException *e) { - caught = true; - puts("CAUGHT! Resuming..."); - } - if (!caught) { - puts("NOT CAUGHT!"); - return 1; - } - caught = false; - @try { - [obj freeMem: q]; - } @catch (OFMemNotPartOfObjException *e) { - caught = true; - puts("CAUGHT! Resuming..."); - } - if (!caught) { - puts("NOT CAUGHT!"); - return 1; - } - caught = false; - @try { - [obj freeMem: r]; - } @catch (OFMemNotPartOfObjException *e) { - caught = true; - puts("CAUGHT! Resuming..."); - } - if (!caught) { - puts("NOT CAUGHT!"); - return 1; - } + CATCH_EXCEPTION([obj freeMem: p], OFMemNotPartOfObjException) + CATCH_EXCEPTION([obj freeMem: q], OFMemNotPartOfObjException) + CATCH_EXCEPTION([obj freeMem: r], OFMemNotPartOfObjException) puts("Got all 3!"); + puts("Trying to allocate more memory than possible..."); + CATCH_EXCEPTION(p = [obj getMem: 4294967295U], OFNoMemException) + /* TODO: Test if freeing object frees all memory */ return 0; }