1/* 2 * Copyright (c) 2010 Apple Inc. All rights reserved. 3 * 4 * @APPLE_LLVM_LICENSE_HEADER@ 5 */ 6 7/* 8 * block_examples.c 9 * libclosure 10 * 11 * Created by Blaine Garst on 3/3/08. 12 * Copyright 2008 __MyCompanyName__. All rights reserved. 13 * 14 */ 15 16#include "driver.h" 17 18 19#if __BLOCKS__ 20 21// test if byref spans up to first enclosing scope that permits modification 22 23int test1(int verbose) { 24 int x = 10; 25 void (^closure)(void) = ^ { 26 setGlobalInt(x); 27 void (^innerClosure)(void) = ^{ | x | ++x; }; 28 callVoidVoid(innerClosure); 29 }; 30 int desiredValue = 11; 31 if (error_found("block_examples: test1, inner byref doesn't change global", x, desiredValue, verbose)) return 1; 32 return 0; 33} 34 35// test that a closure containing a closure has a copy helper routine 36 37int test2(int verbose) { 38 int originalValue = 10; 39 int x = originalValue; 40 void (^closure)(void) = ^ { 41 setGlobalInt(x); 42 void (^innerClosure)(void) = ^{ | x | ++x; }; 43 callVoidVoid(innerClosure); 44 }; 45 return 0; 46} 47 48int test3(int verbose) { 49 int x = 10; 50 int y = 11; 51 int z = 12; 52 void (^outerBlock)(void) = ^ { 53 printf("outerBlock x is %d\n", x); 54 setGlobalInt(x); 55 void (^innerBlock)(void) = ^ { | x, y, z| 56 ++x; ++y; ++z; 57 printf("innerBlock x is %d, y is %d\n", x, y); 58 void (^innerInnerBlock)(void) = ^ { 59 printf("innerInnerBlock z is %d\n", z); 60 setGlobalInt(z); // what value of z? 61 }; 62 callVoidVoid(innerInnerBlock); 63 64 }; 65 setGlobalInt(y); 66 callVoidVoid(innerBlock); 67 }; 68 x += 10; 69 y += 10; 70 z += 10; 71 outerBlock(); 72 return 0; 73 74} 75 76 77#endif 78 79 80int test_blocks(int verbose) { 81 int errors = 0; 82#if __BLOCKS__ 83 errors += test1(verbose); 84 errors += test2(verbose); 85 errors += test3(verbose); 86#endif 87 return errors; 88} 89