[cfbc56ec] | 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 | void case1() {
|
---|
| 62 | ADECL1(x, float, 5)
|
---|
| 63 | checkzero(x[0], x[1], x[2], x[3], x[4]);
|
---|
| 64 | }
|
---|
| 65 | struct ffloat { float f; };
|
---|
| 66 | void case2() {
|
---|
| 67 | ADECL1(x, ffloat, 5)
|
---|
| 68 | checkzero(x[0].f, x[1].f, x[2].f, x[3].f, x[4].f);
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | writeJunkOnStack(5);
|
---|
| 72 | case1();
|
---|
| 73 | printf("silly: ");
|
---|
| 74 | writeJunkOnStack(5);
|
---|
| 75 | case2();
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | // Array of type with custom raii sees cdtors called
|
---|
| 79 | // Array of user-defined type with autogen cdtors gets wrapped cdtors called
|
---|
| 80 | void test_custom() {
|
---|
| 81 | int nctorcalls;
|
---|
| 82 | struct thing { int mem; };
|
---|
| 83 | void ?{}( thing & this ) {
|
---|
| 84 | (this.mem){ nctorcalls++ };
|
---|
| 85 | printf("ctor %d\n", this.mem);
|
---|
| 86 | }
|
---|
| 87 | void ^?{}( thing & this ) {
|
---|
| 88 | printf("dtor %d\n", this.mem);
|
---|
| 89 | }
|
---|
| 90 | struct wrapper1 { thing inner; };
|
---|
| 91 | forall( T ) struct wrapper2 { T inner; };
|
---|
| 92 | printf(" [1]\n");
|
---|
| 93 | {
|
---|
| 94 | nctorcalls = 0;
|
---|
| 95 | ADECL1(a, thing, 5)
|
---|
| 96 | for(i; 5) printf("func %d\n", a[i].mem);
|
---|
| 97 | }
|
---|
| 98 | printf(" [2]\n");
|
---|
| 99 | {
|
---|
| 100 | // White-box note: For the CFA array, the array cdtor is satisfying
|
---|
| 101 | // its own assertion, in a two-level recursive setup.
|
---|
| 102 | nctorcalls = 0;
|
---|
| 103 | ADECL2(a, thing, 2, 3)
|
---|
| 104 | for(i; 2) for(j; 3) printf("func %d at (%d, %d)\n", a[i][j].mem, i, j);
|
---|
| 105 | }
|
---|
| 106 | printf(" [3]\n");
|
---|
| 107 | {
|
---|
| 108 | nctorcalls = 0;
|
---|
| 109 | ADECL1(a, wrapper1, 5)
|
---|
| 110 | for(i; 5) printf("func %d\n", a[i].inner.mem);
|
---|
| 111 | }
|
---|
| 112 | printf(" [4]\n");
|
---|
| 113 | {
|
---|
| 114 | nctorcalls = 0;
|
---|
| 115 | ADECL1(a, wrapper2(thing), 5)
|
---|
| 116 | for(i; 5) printf("func %d\n", a[i].inner.mem);
|
---|
| 117 | }
|
---|
| 118 | }
|
---|
| 119 |
|
---|
[1665ee5] | 120 | struct thing { int mem; };
|
---|
| 121 | void ?{}( thing & this, int i ) {
|
---|
| 122 | (this.mem){ i };
|
---|
| 123 | printf("ctor %d\n", this.mem);
|
---|
| 124 | }
|
---|
| 125 | void ?{}( thing & this ) {
|
---|
| 126 | (this){ 999 };
|
---|
| 127 | }
|
---|
| 128 | void ^?{}( thing & this ) {
|
---|
| 129 | printf("dtor %d\n", this.mem);
|
---|
| 130 | }
|
---|
| 131 |
|
---|
[cfbc56ec] | 132 | // Array of uninits sees explicit ctor calls (only), and implied dtor calls
|
---|
| 133 | void test_uninit() {
|
---|
| 134 | printf(" [1]\n");
|
---|
| 135 | {
|
---|
| 136 | ADECL1(a, uninit(thing), 5)
|
---|
| 137 | printf("before ctors\n");
|
---|
| 138 | for(i; 5) {
|
---|
| 139 | if (i == 1)
|
---|
| 140 | emplace(a[i]);
|
---|
| 141 | else if (i == 2)
|
---|
| 142 | emplace(a[i], 888);
|
---|
| 143 | else
|
---|
| 144 | (a[i]){i};
|
---|
| 145 | }
|
---|
| 146 | for(i; 5) printf("func %d\n", a[i].mem);
|
---|
| 147 | }
|
---|
| 148 | printf(" [2]\n");
|
---|
| 149 | {
|
---|
| 150 | ADECL2(a, uninit(thing), 2, 3)
|
---|
| 151 | printf("before ctors\n");
|
---|
| 152 | for(i; 2) for(j; 3) {
|
---|
| 153 | if (i == 1 && j == 1)
|
---|
| 154 | emplace(a[i][j]);
|
---|
| 155 | else if (i == 1 && j == 2)
|
---|
| 156 | emplace(a[i][j], 888);
|
---|
| 157 | else
|
---|
| 158 | (a[i][j]){100 + 10 * i + j};
|
---|
| 159 | }
|
---|
| 160 | for(i; 2) for(j; 3) {
|
---|
| 161 | printf("func %d at (%d, %d)\n", a[i][j].mem, i, j);
|
---|
| 162 | }
|
---|
| 163 | }
|
---|
| 164 | }
|
---|
| 165 |
|
---|
[1665ee5] | 166 | void test_extras();
|
---|
| 167 |
|
---|
[cfbc56ec] | 168 | int main() {
|
---|
| 169 | printf("=== builtins\n");
|
---|
| 170 | test_builtins();
|
---|
| 171 |
|
---|
| 172 | printf("=== custom\n");
|
---|
| 173 | test_custom();
|
---|
| 174 |
|
---|
[1665ee5] | 175 | printf("=== uninit ( uNoCtor[] )\n");
|
---|
[cfbc56ec] | 176 | test_uninit();
|
---|
[1665ee5] | 177 |
|
---|
| 178 | test_extras();
|
---|
[cfbc56ec] | 179 | }
|
---|