1/* 2 * Copyright (c) 2010 Apple Inc. All rights reserved. 3 * 4 * @APPLE_LLVM_LICENSE_HEADER@ 5 */ 6 7// 8// nestedBlock.m 9// testObjects 10// 11// Created by Blaine Garst on 6/24/08. 12// Copyright 2008 __MyCompanyName__. All rights reserved. 13// 14 15// test non-GC -retain and -release 16 17// TEST_CONFIG MEM=mrc 18// TEST_CFLAGS -framework Foundation 19 20#import <stdio.h> 21#import <Block.h> 22#import <Foundation/Foundation.h> 23#import "test.h" 24 25int Retained = 0; 26 27void (^savedBlock)(void); 28void (^savedBlock2)(void); 29 30void saveit(void (^block)(void)) { 31 savedBlock = Block_copy(block); 32} 33void callit() { 34 savedBlock(); 35} 36void releaseit() { 37 Block_release(savedBlock); 38 savedBlock = nil; 39} 40void saveit2(void (^block)(void)) { 41 savedBlock2 = Block_copy(block); 42} 43void callit2() { 44 savedBlock2(); 45} 46void releaseit2() { 47 Block_release(savedBlock2); 48 savedBlock2 = nil; 49} 50 51@interface TestObject : NSObject 52@end 53 54@implementation TestObject 55- (id)retain { 56 ++Retained; 57 [super retain]; 58 return self; 59} 60- (oneway void)release { 61 --Retained; 62 [super retain]; 63} 64 65 66@end 67id global; 68 69void test(id param) { 70 saveit(^{ 71 saveit2( 72 ^{ 73 global = param; 74 }); 75 }); 76} 77 78 79int main() { 80 TestObject *to = [[TestObject alloc] init]; 81 82 test(to); 83 if (Retained == 0) { 84 fail("didn't update Retained"); 85 } 86 callit(); 87 callit2(); 88 89 succeed(__FILE__); 90} 91