Index: configure.ac ================================================================== --- configure.ac +++ configure.ac @@ -203,20 +203,20 @@ case "$host" in *-*-mingw*) AC_PATH_PROG(WINE, wine) if test x"$WINE" != "x"; then - AC_SUBST(TESTS, "tests") + AC_SUBST(TESTS, "tests_new tests") AC_SUBST(TEST_LAUNCHER, "$WINE") fi ;; esac else - AC_SUBST(TESTS, "tests") + AC_SUBST(TESTS, "tests_new tests") fi BUILDSYS_TOUCH_DEPS AC_SUBST(PACKAGE, objfw) AC_CONFIG_FILES([buildsys.mk extra.mk objfw-config]) AC_CONFIG_HEADERS(config.h) AC_OUTPUT DELETED tests/OFObject/Makefile Index: tests/OFObject/Makefile ================================================================== --- tests/OFObject/Makefile +++ tests/OFObject/Makefile @@ -1,25 +0,0 @@ -PROG_NOINST = ofobject${PROG_SUFFIX} -SRCS = OFObject.m - -include ../../buildsys.mk -include ../../extra.mk - -CPPFLAGS += -I../../src -I../.. -LIBS := -L../../src -lobjfw ${LIBS} - -.PHONY: run - -all: run -run: ${PROG_NOINST} - rm -f libobjfw.so.0 libobjfw.so.0.1 libobjfw.dll libobjfw.dylib - ln -s ../../src/libobjfw.so libobjfw.so.0 - ln -s ../../src/libobjfw.so libobjfw.so.0.1 - if test -f ../../src/libobjfw.dll; then \ - ln ../../src/libobjfw.dll libobjfw.dll; \ - fi - ln -s ../../src/libobjfw.dylib libobjfw.dylib - LD_LIBRARY_PATH=.$${LD_LIBRARY_PATH+:}$$LD_LIBRARY_PATH \ - DYLD_LIBRARY_PATH=.$${DYLD_LIBRARY_PATH+:}$$DYLD_LIBRARY_PATH \ - ${TEST_LAUNCHER} ./${PROG_NOINST}; EXIT=$$?; \ - rm -f libobjfw.so.0 libobjfw.so.0.1 libobjfw.dll libobjfw.dylib; \ - exit $$EXIT DELETED tests/OFObject/OFObject.m Index: tests/OFObject/OFObject.m ================================================================== --- tests/OFObject/OFObject.m +++ tests/OFObject/OFObject.m @@ -1,105 +0,0 @@ -/* - * Copyright (c) 2008 - 2009 - * Jonathan Schleifer - * - * All rights reserved. - * - * This file is part of libobjfw. It may be distributed under the terms of the - * Q Public License 1.0, which can be found in the file LICENSE included in - * the packaging of this file. - */ - -#include "config.h" - -#include -#include -#include - -#import "OFObject.h" -#import "OFExceptions.h" - -#define CATCH_EXCEPTION(code, exception) \ - @try { \ - code; \ - \ - puts("NOT CAUGHT!"); \ - return 1; \ - } @catch (exception *e) { \ - puts("CAUGHT! Error string was:"); \ - puts([[e string] cString]); \ - puts("Resuming..."); \ - } - -int -main() -{ - OFObject *obj = [[OFObject alloc] init]; - void *p, *q, *r; - - /* Test freeing memory not allocated by obj */ - puts("Freeing memory not allocated by object (should throw an " - "exception)..."); - CATCH_EXCEPTION([obj freeMemory: NULL], - OFMemoryNotPartOfObjectException) - - /* Test allocating memory */ - puts("Allocating memory through object..."); - p = [obj allocMemoryWithSize: 4096]; - puts("Allocated 4096 bytes."); - - /* Test freeing the just allocated memory */ - puts("Freeing just allocated memory..."); - [obj freeMemory: 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)..."); - CATCH_EXCEPTION([obj freeMemory: p], OFMemoryNotPartOfObjectException) - - /* Test multiple memory chunks */ - puts("Allocating 3 chunks of memory..."); - p = [obj allocMemoryWithSize: 4096]; - q = [obj allocMemoryWithSize: 4096]; - r = [obj allocMemoryWithSize: 4096]; - puts("Allocated 3 * 4096 bytes."); - - /* Free them */ - puts("Now freeing them..."); - [obj freeMemory: p]; - [obj freeMemory: q]; - [obj freeMemory: r]; - puts("Freed them all."); - - /* Try to free again */ - puts("Now trying to free them again..."); - CATCH_EXCEPTION([obj freeMemory: p], OFMemoryNotPartOfObjectException) - CATCH_EXCEPTION([obj freeMemory: q], OFMemoryNotPartOfObjectException) - CATCH_EXCEPTION([obj freeMemory: r], OFMemoryNotPartOfObjectException) - puts("Got all 3!"); - - puts("Trying to allocate more memory than possible..."); - CATCH_EXCEPTION(p = [obj allocMemoryWithSize: SIZE_MAX], - OFOutOfMemoryException) - - puts("Allocating 1 byte..."); - p = [obj allocMemoryWithSize: 1]; - - puts("Trying to resize that 1 byte to more than possible..."); - CATCH_EXCEPTION(p = [obj resizeMemory: p - toSize: SIZE_MAX], - OFOutOfMemoryException) - - puts("Trying to resize NULL to 1024 bytes..."); - p = [obj resizeMemory: NULL - toSize: 1024]; - [obj freeMemory: p]; - - puts("Trying to resize memory that is not part of object..."); - CATCH_EXCEPTION(p = [obj resizeMemory: (void*)1 - toSize: 1024], - OFMemoryNotPartOfObjectException) - - /* TODO: Test if freeing object frees all memory */ - - return 0; -} ADDED tests_new/Makefile Index: tests_new/Makefile ================================================================== --- tests_new/Makefile +++ tests_new/Makefile @@ -0,0 +1,26 @@ +PROG_NOINST = tests${PROG_SUFFIX} +SRCS = main.m \ + object.m + +include ../buildsys.mk +include ../extra.mk + +CPPFLAGS += -I../src -I.. -DSTDOUT +LIBS := -L../src -lobjfw ${LIBS} + +.PHONY: run + +all: run +run: ${PROG_NOINST} + rm -f libobjfw.so.0 libobjfw.so.0.1 libobjfw.dll libobjfw.dylib + ln -s ../src/libobjfw.so libobjfw.so.0 + ln -s ../src/libobjfw.so libobjfw.so.0.1 + if test -f ../src/libobjfw.dll; then \ + ln ../src/libobjfw.dll libobjfw.dll; \ + fi + ln -s ../src/libobjfw.dylib libobjfw.dylib + LD_LIBRARY_PATH=.$${LD_LIBRARY_PATH+:}$$LD_LIBRARY_PATH \ + DYLD_LIBRARY_PATH=.$${DYLD_LIBRARY_PATH+:}$$DYLD_LIBRARY_PATH \ + ${TEST_LAUNCHER} ./${PROG_NOINST}; EXIT=$$?; \ + rm -f libobjfw.so.0 libobjfw.so.0.1 libobjfw.dll libobjfw.dylib; \ + exit $$EXIT ADDED tests_new/main.h Index: tests_new/main.h ================================================================== --- tests_new/main.h +++ tests_new/main.h @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2008 - 2009 + * Jonathan Schleifer + * + * All rights reserved. + * + * This file is part of libobjfw. It may be distributed under the terms of the + * Q Public License 1.0, which can be found in the file LICENSE included in + * the packaging of this file. + */ + +#import "OFString.h" + +extern void testing(OFString*, OFString*); +extern void success(OFString*, OFString*); +extern void failed(OFString*, OFString*); + +#define TEST(test, cond) \ + { \ + testing(module, test); \ + if (cond) \ + success(module, test); \ + else \ + failed(module, test); \ + } +#define EXPECT_EXCEPTION(test, exception, code) \ + { \ + BOOL caught = NO; \ + \ + @try { \ + code; \ + } @catch (exception *e) { \ + caught = YES; \ + [e dealloc]; \ + } \ + \ + TEST(test, caught) \ + } ADDED tests_new/main.m Index: tests_new/main.m ================================================================== --- tests_new/main.m +++ tests_new/main.m @@ -0,0 +1,88 @@ +/* + * Copyright (c) 2008 - 2009 + * Jonathan Schleifer + * + * All rights reserved. + * + * This file is part of libobjfw. It may be distributed under the terms of the + * Q Public License 1.0, which can be found in the file LICENSE included in + * the packaging of this file. + */ + +#include "config.h" + +#ifdef STDOUT +#include +#endif +#include + +#import +#import + +extern void object_tests(); + +static void +output(OFString *str, int color) +{ +#ifdef STDOUT + switch (color) { + case 0: + fputs("\r\033[K\033[1;33m", stdout); + break; + case 1: + fputs("\r\033[K\033[1;32m", stdout); + break; + case 2: + fputs("\r\033[K\033[1;31m", stdout); + break; + } + + fputs([str cString], stdout); + + fputs("\033[m", stdout); +#else +#error No output method! +#endif +} + +void +testing(OFString *module, OFString *test) +{ + OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; + OFString *str = [OFString stringWithFormat: @"[%s] %s: testing...", + [module cString], + [test cString]]; + output(str, 0); + [pool release]; +} + +void +success(OFString *module, OFString *test) +{ + OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; + OFString *str = [OFString stringWithFormat: @"[%s] %s: ok\n", + [module cString], + [test cString]]; + output(str, 1); + [pool release]; +} + +void +failed(OFString *module, OFString *test) +{ + OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; + OFString *str = [OFString stringWithFormat: @"[%s] %s: failed\n", + [module cString], + [test cString]]; + output(str, 2); + [pool release]; + exit(1); +} + +int +main() +{ + object_tests(); + + return 0; +} ADDED tests_new/object.m Index: tests_new/object.m ================================================================== --- tests_new/object.m +++ tests_new/object.m @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2008 - 2009 + * Jonathan Schleifer + * + * All rights reserved. + * + * This file is part of libobjfw. It may be distributed under the terms of the + * Q Public License 1.0, which can be found in the file LICENSE included in + * the packaging of this file. + */ + +#include "config.h" + +#import "OFAutoreleasePool.h" +#import "OFExceptions.h" + +#import "main.h" + +static OFString *module = @"OFObject"; + +void +object_tests() +{ + OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; + OFObject *obj = [[[OFObject alloc] init] autorelease]; + void *p, *q, *r; + + EXPECT_EXCEPTION(@"Detect freeing of memory not allocated by object", + OFMemoryNotPartOfObjectException, [obj freeMemory: NULL]) + + TEST(@"Allocating 4096 bytes", + (p = [obj allocMemoryWithSize: 4096]) != NULL) + + TEST(@"Freeing memory", [obj freeMemory: p]) + + EXPECT_EXCEPTION(@"Detect freeing of memory twice", + OFMemoryNotPartOfObjectException, [obj freeMemory: p]) + + TEST(@"Allocating and freeing 4096 bytes 3 times", + (p = [obj allocMemoryWithSize: 4096]) != NULL && + (q = [obj allocMemoryWithSize: 4096]) != NULL && + (r = [obj allocMemoryWithSize: 4096]) != NULL && + [obj freeMemory: p] && [obj freeMemory: q] && [obj freeMemory: r]) + + EXPECT_EXCEPTION(@"Detect out of memory on alloc", + OFOutOfMemoryException, [obj allocMemoryWithSize: SIZE_MAX]) + + EXPECT_EXCEPTION(@"Detect out of memory on resize", + OFOutOfMemoryException, + { + p = [obj allocMemoryWithSize: 1]; + [obj resizeMemory: p + toSize: SIZE_MAX]; + }) + [obj freeMemory: p]; + + TEST(@"Allocate when trying to resize NULL", + (p = [obj resizeMemory: NULL + toSize: 1024]) != NULL) + [obj freeMemory: p]; + + EXPECT_EXCEPTION(@"Detect resizing of memory not allocated by object", + OFMemoryNotPartOfObjectException, [obj resizeMemory: (void*)1 + toSize: 1024]) + + [pool release]; +}