| 1 | //
 | 
|---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2023 University of Waterloo
 | 
|---|
| 3 | //
 | 
|---|
| 4 | // The contents of this file are covered under the licence agreement in the
 | 
|---|
| 5 | // file "LICENCE" distributed with Cforall.
 | 
|---|
| 6 | //
 | 
|---|
| 7 | // array-raii.hfa -- combined test implementation for both -c and -cfa versions
 | 
|---|
| 8 | //
 | 
|---|
| 9 | // Author           : Mike Brooks
 | 
|---|
| 10 | // Created On       : Fri Sep 22 15:00:00 2023
 | 
|---|
| 11 | // Last Modified By :
 | 
|---|
| 12 | // Last Modified On :
 | 
|---|
| 13 | // Update Count     :
 | 
|---|
| 14 | //
 | 
|---|
| 15 | 
 | 
|---|
| 16 | 
 | 
|---|
| 17 | #include <raii.hfa>
 | 
|---|
| 18 | 
 | 
|---|
| 19 | // To keep C- vs CFA-array compatibility simple. these tests
 | 
|---|
| 20 | // - use only single-place subscripting, like `m[1][2]`, while deferring 
 | 
|---|
| 21 | //   equivalence of m[1][2] vs m[1,2] to array-md-sbscr-cases
 | 
|---|
| 22 | // - declare arrays via test-specific ADECL macros
 | 
|---|
| 23 | 
 | 
|---|
| 24 | 
 | 
|---|
| 25 | 
 | 
|---|
| 26 | volatile bool checkme = false;
 | 
|---|
| 27 | char junkref[] = "junk";
 | 
|---|
| 28 | extern "C" {
 | 
|---|
| 29 |     char* strcpy(char* destination, const char* source);
 | 
|---|
| 30 | }
 | 
|---|
| 31 | 
 | 
|---|
| 32 | // Array of built-in type does not get zeroed
 | 
|---|
| 33 | //   (Surprised?)
 | 
|---|
| 34 | //   Because it has nothing to do with arrays.
 | 
|---|
| 35 | //   Built-in types have no-op ctors.
 | 
|---|
| 36 | //   (Still surprised?  Me too.)
 | 
|---|
| 37 | // If pigs flew and built-ins zeroed themselves, then
 | 
|---|
| 38 | //   Array of built-in type gets zeroed
 | 
|---|
| 39 | //   Array of user-defined type with autogen ctors gets wrapped builtins zeroed (intended purpose of case2 when originally written)
 | 
|---|
| 40 | //   Expected outcome becomes "all zero" twice
 | 
|---|
| 41 | // As is
 | 
|---|
| 42 | //   case1 pretty much just shows the summary statement above and
 | 
|---|
| 43 | //   verifying the case2-wrapper behaviour is just silly, so
 | 
|---|
| 44 | //   the quality of respecting autogens is checked (for real this time) under test_custom
 | 
|---|
| 45 | void test_builtins() {
 | 
|---|
| 46 |     void writeJunkOnStack(int depth) {
 | 
|---|
| 47 |         if (depth == 0) return;
 | 
|---|
| 48 |         char junk[5];
 | 
|---|
| 49 |         strcpy(junk, junkref);
 | 
|---|
| 50 |         writeJunkOnStack(depth-1);
 | 
|---|
| 51 |         if (checkme) printf("%s\n", junk);
 | 
|---|
| 52 |     }
 | 
|---|
| 53 |     void checkzero(float f0, float f1, float f2, float f3, float f4) {
 | 
|---|
| 54 |         if (f0 == 0.f && f1 == 0.f && f2 == 0.f && f3 == 0.f && f4 == 0.f) {
 | 
|---|
| 55 |             printf("all zero\n");
 | 
|---|
| 56 |         } else {
 | 
|---|
| 57 |             printf("some nonzero\n");
 | 
|---|
| 58 |           //printf("%f %f %f %f %f\n", f0, f1, f2, f3, f4);
 | 
|---|
| 59 |         }
 | 
|---|
| 60 |     }
 | 
|---|
| 61 |   #pragma GCC diagnostic push
 | 
|---|
| 62 |   #pragma GCC diagnostic ignored "-Wuninitialized"
 | 
|---|
| 63 |   #pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
 | 
|---|
| 64 |     void case1() {
 | 
|---|
| 65 |         ADECL1(x, float, 5)
 | 
|---|
| 66 |         checkzero(x[0], x[1], x[2], x[3], x[4]);
 | 
|---|
| 67 |     }
 | 
|---|
| 68 |     struct ffloat { float f; };
 | 
|---|
| 69 |     void case2() {
 | 
|---|
| 70 |         ADECL1(x, ffloat, 5)
 | 
|---|
| 71 |         checkzero(x[0].f, x[1].f, x[2].f, x[3].f, x[4].f);
 | 
|---|
| 72 |     }
 | 
|---|
| 73 |   #pragma GCC diagnostic pop
 | 
|---|
| 74 | 
 | 
|---|
| 75 |     writeJunkOnStack(5);
 | 
|---|
| 76 |     case1();
 | 
|---|
| 77 |     printf("silly: ");
 | 
|---|
| 78 |     writeJunkOnStack(5);
 | 
|---|
| 79 |     case2();
 | 
|---|
| 80 | }
 | 
|---|
| 81 | 
 | 
|---|
| 82 | // Array of type with custom raii sees cdtors called
 | 
|---|
| 83 | // Array of user-defined type with autogen cdtors gets wrapped cdtors called
 | 
|---|
| 84 | void test_custom() {
 | 
|---|
| 85 |     int nctorcalls;
 | 
|---|
| 86 |     struct thing { int mem; };
 | 
|---|
| 87 |     void ?{}( thing & this ) {
 | 
|---|
| 88 |         (this.mem){ nctorcalls++ };
 | 
|---|
| 89 |         printf("ctor %d\n", this.mem);
 | 
|---|
| 90 |     }
 | 
|---|
| 91 |     void ^?{}( thing & this ) {
 | 
|---|
| 92 |         printf("dtor %d\n", this.mem);
 | 
|---|
| 93 |     }
 | 
|---|
| 94 |     struct wrapper1 { thing inner; };
 | 
|---|
| 95 |     forall( T ) struct wrapper2 { T inner; };
 | 
|---|
| 96 |     printf(" [1]\n");
 | 
|---|
| 97 |     {
 | 
|---|
| 98 |         nctorcalls = 0;
 | 
|---|
| 99 |         ADECL1(a, thing, 5)
 | 
|---|
| 100 |         for(i; 5) printf("func %d\n", a[i].mem);
 | 
|---|
| 101 |     }
 | 
|---|
| 102 |     printf(" [2]\n");
 | 
|---|
| 103 |     {
 | 
|---|
| 104 |         // White-box note: For the CFA array, the array cdtor is satisfying
 | 
|---|
| 105 |         // its own assertion, in a two-level recursive setup.
 | 
|---|
| 106 |         nctorcalls = 0;
 | 
|---|
| 107 |         ADECL2(a, thing, 2, 3)
 | 
|---|
| 108 |         for(i; 2) for(j; 3) printf("func %d at (%d, %d)\n", a[i][j].mem, i, j);
 | 
|---|
| 109 |     }
 | 
|---|
| 110 |     printf(" [3]\n");
 | 
|---|
| 111 |     {
 | 
|---|
| 112 |         nctorcalls = 0;
 | 
|---|
| 113 |         ADECL1(a, wrapper1, 5)
 | 
|---|
| 114 |         for(i; 5) printf("func %d\n", a[i].inner.mem);
 | 
|---|
| 115 |     }
 | 
|---|
| 116 |     printf(" [4]\n");
 | 
|---|
| 117 |     {
 | 
|---|
| 118 |         nctorcalls = 0;
 | 
|---|
| 119 |         ADECL1(a, wrapper2(thing), 5)
 | 
|---|
| 120 |         for(i; 5) printf("func %d\n", a[i].inner.mem);
 | 
|---|
| 121 |     }
 | 
|---|
| 122 | }
 | 
|---|
| 123 | 
 | 
|---|
| 124 | struct thing { int mem; };
 | 
|---|
| 125 | void ?{}( thing & this, int i ) {
 | 
|---|
| 126 |     (this.mem){ i };
 | 
|---|
| 127 |     printf("ctor %d\n", this.mem);
 | 
|---|
| 128 | }
 | 
|---|
| 129 | void ?{}( thing & this ) {
 | 
|---|
| 130 |     (this){ 999 };
 | 
|---|
| 131 | }
 | 
|---|
| 132 | void ^?{}( thing & this ) {
 | 
|---|
| 133 |     printf("dtor %d\n", this.mem);
 | 
|---|
| 134 | }
 | 
|---|
| 135 | 
 | 
|---|
| 136 | // Array of uninits sees explicit ctor calls (only), and implied dtor calls
 | 
|---|
| 137 | void test_uninit() {
 | 
|---|
| 138 |     printf(" [1]\n");
 | 
|---|
| 139 |     {
 | 
|---|
| 140 |         ADECL1(a, uninit(thing), 5)
 | 
|---|
| 141 |         printf("before ctors\n");
 | 
|---|
| 142 |         for(i; 5) {
 | 
|---|
| 143 |             if (i == 1)
 | 
|---|
| 144 |                 emplace(a[i]);
 | 
|---|
| 145 |             else if (i == 2)
 | 
|---|
| 146 |                 emplace(a[i], 888);
 | 
|---|
| 147 |             else
 | 
|---|
| 148 |                 (a[i]){i};
 | 
|---|
| 149 |         }
 | 
|---|
| 150 |         for(i; 5) printf("func %d\n", a[i].mem);
 | 
|---|
| 151 |     }
 | 
|---|
| 152 |     printf(" [2]\n");
 | 
|---|
| 153 |     {
 | 
|---|
| 154 |         ADECL2(a, uninit(thing), 2, 3)
 | 
|---|
| 155 |         printf("before ctors\n");
 | 
|---|
| 156 |         for(i; 2) for(j; 3) {
 | 
|---|
| 157 |             if (i == 1 && j == 1)
 | 
|---|
| 158 |                 emplace(a[i][j]);
 | 
|---|
| 159 |             else if (i == 1 && j == 2)
 | 
|---|
| 160 |                 emplace(a[i][j], 888);
 | 
|---|
| 161 |             else
 | 
|---|
| 162 |                 (a[i][j]){100 + 10 * i + j};
 | 
|---|
| 163 |         }
 | 
|---|
| 164 |         for(i; 2) for(j; 3) {
 | 
|---|
| 165 |             printf("func %d at (%d, %d)\n", a[i][j].mem, i, j);
 | 
|---|
| 166 |         }
 | 
|---|
| 167 |     }
 | 
|---|
| 168 | }
 | 
|---|
| 169 | 
 | 
|---|
| 170 | void test_extras();
 | 
|---|
| 171 | 
 | 
|---|
| 172 | int main() {
 | 
|---|
| 173 |     printf("=== builtins\n");
 | 
|---|
| 174 |     test_builtins();
 | 
|---|
| 175 | 
 | 
|---|
| 176 |     printf("=== custom\n");
 | 
|---|
| 177 |     test_custom();
 | 
|---|
| 178 | 
 | 
|---|
| 179 |     printf("=== uninit ( uNoCtor[] )\n");
 | 
|---|
| 180 |     test_uninit();
 | 
|---|
| 181 | 
 | 
|---|
| 182 |     test_extras();
 | 
|---|
| 183 | }
 | 
|---|