1/* 2 * Copyright (c) 2010 Apple Inc. All rights reserved. 3 * 4 * @APPLE_LLVM_LICENSE_HEADER@ 5 */ 6 7// 8// __blockObjectAssign.m 9// testObjects 10// 11// Created by Blaine Garst on 2/5/09. 12// Copyright 2009 Apple. All rights reserved. 13// 14 15// TEST_CONFIG 16// TEST_CFLAGS -framework Foundation 17 18// tests whether assigning to a __block id variable works in a reasonable way 19 20 21#import <Foundation/Foundation.h> 22#import <stdio.h> 23#import <Block.h> 24#import "test.h" 25 26 27@interface TestObject : NSObject { 28 int version; 29} 30- (void)hi; 31@end 32 33int AllocationCounter = 0; 34int DellocationCounter = 0; 35 36@implementation TestObject 37 38 39- (id)init { 40 version = AllocationCounter++; 41 return self; 42} 43 44- (void)hi { 45 //printf("hi from %p, #%d\n", self, version); 46} 47 48- (void)dealloc { 49 //printf("dealloc %p, #%d called\n", self, version); 50 ++DellocationCounter; 51 [super dealloc]; 52} 53 54 55@end 56 57void testFunction(bool doExecute, bool doCopy) { 58 __block id a = [[TestObject alloc] init]; 59 //printf("testing - will execute? %d\n", doExecute); 60 void (^changeA)(void) = ^{ 61 [a hi]; 62 [a release]; 63 a = [[TestObject alloc] init]; 64 [a hi]; 65 }; 66 if (doCopy) changeA = [changeA copy]; 67 if (doExecute) changeA(); 68 if (doCopy) [changeA release]; 69 [a release]; 70 //printf("done with explict releasing, implicit to follow\n"); 71} 72 73int main() { 74 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 75 testFunction(false, true); 76 testFunction(true, true); 77 testFunction(false, false); 78 testFunction(true, true); 79 [pool drain]; 80 if (! objc_collectingEnabled()) { 81 if (DellocationCounter != AllocationCounter) { 82 fail("only recovered %d of %d objects", DellocationCounter, AllocationCounter); 83 } 84 } 85 86 succeed(__FILE__); 87} 88