xref: /llvm-project/compiler-rt/test/BlocksRuntime/byrefstruct.c (revision 2946cd701067404b99c39fb29dc9c74bd7193eb3)
1 //
2 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
3 // See https://llvm.org/LICENSE.txt for license information.
4 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5 
6 //  -*- mode:C; c-basic-offset:4; tab-width:4; intent-tabs-mode:nil;  -*-
7 // CONFIG
8 
9 #import <stdio.h>
10 #import <stdlib.h>
11 #import <string.h>
12 
13 typedef struct {
14     unsigned long ps[30];
15     int qs[30];
16 } BobTheStruct;
17 
main(int argc,const char * argv[])18 int main (int argc, const char * argv[]) {
19     __block BobTheStruct fiddly;
20     BobTheStruct copy;
21 
22     void (^incrementFiddly)() = ^{
23         int i;
24         for(i=0; i<30; i++) {
25             fiddly.ps[i]++;
26             fiddly.qs[i]++;
27         }
28     };
29 
30     memset(&fiddly, 0xA5, sizeof(fiddly));
31     memset(&copy, 0x2A, sizeof(copy));
32 
33     int i;
34     for(i=0; i<30; i++) {
35         fiddly.ps[i] = i * i * i;
36         fiddly.qs[i] = -i * i * i;
37     }
38 
39     copy = fiddly;
40     incrementFiddly();
41 
42     if ( &copy == &fiddly ) {
43         printf("%s: struct wasn't copied.", argv[0]);
44         exit(1);
45     }
46     for(i=0; i<30; i++) {
47         //printf("[%d]: fiddly.ps: %lu, copy.ps: %lu, fiddly.qs: %d, copy.qs: %d\n", i, fiddly.ps[i], copy.ps[i], fiddly.qs[i], copy.qs[i]);
48         if ( (fiddly.ps[i] != copy.ps[i] + 1) || (fiddly.qs[i] != copy.qs[i] + 1) ) {
49             printf("%s: struct contents were not incremented.", argv[0]);
50             exit(1);
51         }
52     }
53 
54     printf("%s: success\n", argv[0]);
55     return 0;
56 }
57