xref: /aosp_15_r20/external/compiler-rt/test/BlocksRuntime/byrefcopycopy.c (revision 7c3d14c8b49c529e04be81a3ce6f5cc23712e4c6)
1*7c3d14c8STreehugger Robot //
2*7c3d14c8STreehugger Robot //                     The LLVM Compiler Infrastructure
3*7c3d14c8STreehugger Robot //
4*7c3d14c8STreehugger Robot // This file is distributed under the University of Illinois Open Source
5*7c3d14c8STreehugger Robot // License. See LICENSE.TXT for details.
6*7c3d14c8STreehugger Robot 
7*7c3d14c8STreehugger Robot // CONFIG rdar://6255170
8*7c3d14c8STreehugger Robot 
9*7c3d14c8STreehugger Robot #include <stdio.h>
10*7c3d14c8STreehugger Robot #include <stdbool.h>
11*7c3d14c8STreehugger Robot #include <stdlib.h>
12*7c3d14c8STreehugger Robot #include <Block.h>
13*7c3d14c8STreehugger Robot #include <Block_private.h>
14*7c3d14c8STreehugger Robot #include <assert.h>
15*7c3d14c8STreehugger Robot 
16*7c3d14c8STreehugger Robot 
17*7c3d14c8STreehugger Robot int
main(int argc,char * argv[])18*7c3d14c8STreehugger Robot main(int argc, char *argv[])
19*7c3d14c8STreehugger Robot {
20*7c3d14c8STreehugger Robot     __block int var = 0;
21*7c3d14c8STreehugger Robot     int shouldbe = 0;
22*7c3d14c8STreehugger Robot     void (^b)(void) = ^{ var++; /*printf("var is at %p with value %d\n", &var, var);*/ };
23*7c3d14c8STreehugger Robot     __typeof(b) _b;
24*7c3d14c8STreehugger Robot     //printf("before copy...\n");
25*7c3d14c8STreehugger Robot     b(); ++shouldbe;
26*7c3d14c8STreehugger Robot     size_t i;
27*7c3d14c8STreehugger Robot 
28*7c3d14c8STreehugger Robot     for (i = 0; i < 10; i++) {
29*7c3d14c8STreehugger Robot             _b = Block_copy(b); // make a new copy each time
30*7c3d14c8STreehugger Robot             assert(_b);
31*7c3d14c8STreehugger Robot             ++shouldbe;
32*7c3d14c8STreehugger Robot             _b();               // should still update the stack
33*7c3d14c8STreehugger Robot             Block_release(_b);
34*7c3d14c8STreehugger Robot     }
35*7c3d14c8STreehugger Robot 
36*7c3d14c8STreehugger Robot     //printf("after...\n");
37*7c3d14c8STreehugger Robot     b(); ++shouldbe;
38*7c3d14c8STreehugger Robot 
39*7c3d14c8STreehugger Robot     if (var != shouldbe) {
40*7c3d14c8STreehugger Robot         printf("Hmm, var is %d but should be %d\n", var, shouldbe);
41*7c3d14c8STreehugger Robot         return 1;
42*7c3d14c8STreehugger Robot     }
43*7c3d14c8STreehugger Robot     printf("%s: Success!!\n", argv[0]);
44*7c3d14c8STreehugger Robot 
45*7c3d14c8STreehugger Robot     return 0;
46*7c3d14c8STreehugger Robot }
47