Index: configure.ac ================================================================== --- configure.ac +++ configure.ac @@ -68,10 +68,11 @@ int (^foo)(int bar); foo = ^(int bar) { return 0; } ], [ AC_DEFINE(OF_HAVE_BLOCKS, 1, [Compiler support for blocks]) AC_SUBST(BLOCKS_FLAGS, "-fblocks") + AC_SUBST(OFBLOCKTESTS_M, "OFBlockTests.m") AC_MSG_RESULT(yes) ], [ AC_MSG_RESULT(no) OBJCFLAGS="$old_OBJCFLAGS" ]) Index: extra.mk.in ================================================================== --- extra.mk.in +++ extra.mk.in @@ -6,10 +6,11 @@ ASPRINTF_M = @ASPRINTF_M@ ATOMIC_H = @ATOMIC_H@ BIN_PREFIX = @BIN_PREFIX@ MACH_ALIAS_LIST = @MACH_ALIAS_LIST@ +OFBLOCKTESTS_M = @OFBLOCKTESTS_M@ OBJC_PROPERTIES_M = @OBJC_PROPERTIES_M@ OBJC_SYNC_M = @OBJC_SYNC_M@ OFPLUGIN_M = @OFPLUGIN_M@ OFPLUGINTESTS_M = @OFPLUGINTESTS_M@ OFTHREAD_M = @OFTHREAD_M@ Index: tests/Makefile ================================================================== --- tests/Makefile +++ tests/Makefile @@ -2,10 +2,11 @@ SUBDIRS = ${TESTPLUGIN} PROG_NOINST = tests${PROG_SUFFIX} SRCS = OFArrayTests.m \ + ${OFBLOCKTESTS_M} \ OFDataArrayTests.m \ OFDictionaryTests.m \ OFFileTests.m \ OFListTests.m \ OFMD5HashTests.m \ ADDED tests/OFBlockTests.m Index: tests/OFBlockTests.m ================================================================== --- tests/OFBlockTests.m +++ tests/OFBlockTests.m @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2008 - 2010 + * Jonathan Schleifer + * + * All rights reserved. + * + * This file is part of ObjFW. 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 "OFString.h" +#import "OFBlock.h" +#import "OFAutoreleasePool.h" +#import "OFExceptions.h" + +#if defined(OF_OBJFW_RUNTIME) +# include +#elif defined(OF_GNU_RUNTIME) +# include +#endif +#if defined(OF_GNU_RUNTIME) || defined(OF_OBJFW_RUNTIME) +# define objc_getClass objc_get_class +#endif + +#import "TestsAppDelegate.h" + +static OFString *module = @"OFBlock"; + +extern void *_NSConcreteStackBlock; +extern void *_NSConcreteGlobalBlock; +extern void *_NSConcreteMallocBlock; + +@implementation TestsAppDelegate (OFBlockTests) +- (void)blockTests +{ + OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; + __block int x; + void (^s)() = ^ { x = 0; }; + void (^g)() = ^ {}; + void (^m)(); + + TEST(@"Class of stack block", + (Class)&_NSConcreteStackBlock == objc_getClass("OFStackBlock") && + [s isKindOfClass: [OFBlock class]]) + + TEST(@"Class of global block", + (Class)&_NSConcreteGlobalBlock == objc_getClass("OFGlobalBlock") && + [g isKindOfClass: [OFBlock class]]) + + TEST(@"Class of a malloc block", + (Class)&_NSConcreteMallocBlock == objc_getClass("OFMallocBlock")) + + TEST(@"Copying a stack block", + (m = [s copy]) && [m class] == objc_getClass("OFMallocBlock") && + [m isKindOfClass: [OFBlock class]]) + + TEST(@"Copying a global block", (id)g == [g copy]) + + TEST(@"Copying a malloc block", + (id)m == [m copy] && [m retainCount] == 2) + + TEST(@"Autorelease a stack block", R([s autorelease])) + + TEST(@"Autorelease a global block", R([g autorelease])) + + TEST(@"Autorelease a malloc block", R([m autorelease])) + + [pool drain]; +} +@end Index: tests/TestsAppDelegate.h ================================================================== --- tests/TestsAppDelegate.h +++ tests/TestsAppDelegate.h @@ -69,10 +69,14 @@ @end @interface TestsAppDelegate (OFArrayTests) - (void)arrayTests; @end + +@interface TestsAppDelegate (OFBlockTests) +- (void)blockTests; +@end @interface TestsAppDelegate (OFDataArrayTests) - (void)dataArrayTests; @end Index: tests/TestsAppDelegate.m ================================================================== --- tests/TestsAppDelegate.m +++ tests/TestsAppDelegate.m @@ -75,10 +75,13 @@ } - (void)applicationDidFinishLaunching { [self objectTests]; +#ifdef OF_HAVE_BLOCKS + [self blockTests]; +#endif [self stringTests]; [self fileTests]; [self MD5HashTests]; [self SHA1HashTests]; [self dataArrayTests];