Changeset 8f2f185


Ignore:
Timestamp:
Mar 28, 2022, 10:43:21 PM (2 years ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
ADT, ast-experimental, enum, master, pthread-emulation, qualifiedEnum
Children:
65781a8, dfd3410, edf247b
Parents:
72ba508
Message:

update and add designation tests

File:
1 edited

Legend:

Unmodified
Added
Removed
  • tests/designations.cfa

    r72ba508 r8f2f185  
    1010// Created On       : Thu Jun 29 15:26:36 2017
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Thu Jul 27 11:46:35 2017
    13 // Update Count     : 3
     12// Last Modified On : Mon Mar 28 22:41:55 2022
     13// Update Count     : 15
    1414//
    1515
     
    1818// is used for the designation syntax
    1919#ifdef __cforall
    20 #define DES :
     20#define _ :
     21#define AT @
    2122#else
    22 int printf(const char *, ...);
    23 #define DES =
     23int printf( const char *, ...);
     24#define _ =
     25#define AT
    2426#endif
    2527
    2628const int indentAmt = 2;
    27 void indent(int level) {
    28         for (int i = 0; i < level; ++i) {
    29                 printf(" ");
     29void indent( int level ) {
     30        for ( int i = 0; i < level; ++i ) {
     31                printf( " " );
    3032        }
    3133}
     
    3638        int * ptr;
    3739};
    38 void printA(struct A a, int level) {
    39         indent(level);
    40         printf("(A){ %d %d %p }\n", a.x, a.y, a.ptr);
     40void printA( struct A a, int level ) {
     41        indent( level );
     42        printf( "(A){ %d %d %p }\n", a.x, a.y, a.ptr );
    4143}
    4244
     
    4547        struct A a0, a1;
    4648};
    47 void printB(struct B b, int level) {
    48         indent(level);
    49         printf("(B){\n");
    50         printA(b.a0, level+indentAmt);
    51         printA(b.a1, level+indentAmt);
    52         indent(level);
    53         printf("}\n");
     49void printB( struct B b, int level ) {
     50        indent( level );
     51        printf( "(B){\n" );
     52        printA( b.a0, level+indentAmt );
     53        printA( b.a1, level+indentAmt );
     54        indent( level );
     55        printf( "}\n" );
    5456}
    5557
     
    5961        struct B b;
    6062};
    61 void printC(struct C c, int level) {
    62         indent(level);
    63         printf("(C){\n");
    64         indent(level+indentAmt);
    65         printf("(int[]{ %d %d %d }\n", c.arr[0], c.arr[1], c.arr[2]);
    66         printB(c.b, level+indentAmt);
    67         indent(level);
    68         printf("}\n");
     63void printC( struct C c, int level ) {
     64        indent( level );
     65        printf( "(C){\n" );
     66        indent( level+indentAmt );
     67        printf( "(int[]{ %d %d %d }\n", c.arr[0], c.arr[1], c.arr[2]);
     68        printB( c.b, level+indentAmt );
     69        indent( level );
     70        printf( "}\n" );
    6971}
    7072
     
    7577        };
    7678};
    77 void printD(struct D d, int level) {
    78         indent(level);
    79         printf("(D){ %d }\n", d.x);
     79void printD( struct D d, int level ) {
     80        indent( level);
     81        printf( "(D ){ %d }\n", d.x );
    8082}
    8183
     
    99101    } m;
    100102};
    101 struct Fred s1 @= { .m.j : 3 };
    102 struct Fred s2 @= { .i : { [2] : 2 } };
     103struct Fred s1 AT= { .m.j _ 3 };
     104struct Fred s2 AT= { .i _ { [2] _ 2 } };
    103105
    104106int main() {
    105107        // simple designation case - starting from beginning of structure, leaves ptr default-initialized (zero)
    106108        struct A y0 = {
    107                 .x DES 2,
    108                 .y DES 3
     109                .x _ 2,
     110                .y _ 3
    109111        };
    110112
     
    117119        // use designation to move to member y, leaving x default-initialized (zero)
    118120        struct A y2 = {
    119                 .y DES 3,
     121                .y _ 3,
    120122                0
    121123        };
     
    127129#endif
    128130
    129         printf("=====A=====\n");
    130         printA(y0, 0);
    131         printA(y1, 0);
    132         printA(y2, 0);
    133         printf("=====A=====\n\n");
     131        printf( "=====A=====\n" );
     132        printA( y0, 0 );
     133        printA( y1, 0 );
     134        printA( y2, 0 );
     135        printf( "=====A=====\n\n" );
    134136
    135137        // initialize only first element (z0.a.x), leaving everything else default-initialized (zero), no nested curly-braces
     
    140142                { 3 }, // z1.a0
    141143                { 4 }, // z1.a1
    142                 .a0 DES { 5 }, // z1.a0
     144                .a0 _ { 5 }, // z1.a0
    143145                { 6 }, // z1.a1
    144                 .a0.y DES 2, // z1.a0.y
     146                .a0.y _ 2, // z1.a0.y
    145147                0, // z1.a0.ptr
    146148        };
     
    170172        };
    171173
    172         printf("=====B=====\n");
    173         printB(z0, 0);
    174         printB(z1, 0);
    175         printB(z2, 0);
    176         printB(z3, 0);
    177         printB(z5, 0);
    178         printB(z6, 0);
    179         printf("=====B=====\n\n");
     174        printf( "=====B=====\n" );
     175        printB( z0, 0 );
     176        printB( z1, 0 );
     177        printB( z2, 0 );
     178        printB( z3, 0 );
     179        printB( z5, 0 );
     180        printB( z6, 0 );
     181        printf( "=====B=====\n\n" );
    180182
    181183        // TODO: what about extra things in a nested init? are empty structs skipped??
     
    188190        };
    189191
    190         printf("=====C=====\n");
    191         printC(c1, 0);
    192         printf("=====C=====\n\n");
     192        printf( "=====C=====\n" );
     193        printC( c1, 0 );
     194        printf( "=====C=====\n\n" );
    193195
    194196#if ERROR
     
    213215#endif
    214216        // array designation
    215         int i[2] = { [1] : 3 };
     217        int i[2] = { [1] _ 3 };
    216218        // allowed to have 'too many' initialized lists - essentially they are ignored.
    217219        int i1 = { 3 };
     
    219221        // doesn't work yet.
    220222        // designate unnamed object's members
    221         // struct D d = { .x DES 3 };
     223        // struct D d = { .x _ 3 };
    222224#if ERROR
    223         struct D d1 = { .y DES 3 };
     225        struct D d1 = { .y _ 3 };
    224226#endif
    225227
     
    241243        // move cursor to e4.b.a0.x and initialize until e3.b.a1.ptr inclusive
    242244        union E e3 = {
    243                 .b.a0.x DES 2, 3, 0, 5, 6, 0
    244         };
    245 
    246         printf("=====E=====\n");
    247         printA(e0.a, 0);
    248         printA(e1.a, 0);
    249         printA(e2.a, 0);
    250         printB(e3.b, 0);
    251         printf("=====E=====\n\n");
     245                .b.a0.x _ 2, 3, 0, 5, 6, 0
     246        };
     247
     248        printf( "=====E=====\n" );
     249        printA( e0.a, 0 );
     250        printA( e1.a, 0 );
     251        printA( e2.a, 0 );
     252        printB( e3.b, 0 );
     253        printf( "=====E=====\n\n" );
    252254
    253255        // special case of initialization: char[] can be initialized with a string literal
    254256        const char * str0 = "hello";
    255257        char str1[] = "hello";
    256         const char c1[] = "abc";
    257         const char c2[] = { 'a', 'b', 'c' };
    258         const char c3[][2] = { { 'a', 'b' }, { 'c', 'd'}, { 'c', 'd'} };
     258        const char c2[] = "abc";
     259        const char c3[] = { 'a', 'b', 'c' };
     260        const char c4[][2] = { { 'a', 'b' }, { 'c', 'd'}, { 'c', 'd'} };
     261
     262        // more cases
     263
     264//      int widths[] = { [3 ... 9] _ 1, [10 ... 99] _ 2, [100] _ 3 };
     265//      int widths[] = { [3 ~ 9] _ 1, [10 ~ 99] _ 2, [100] _ 3 };
     266        struct point { int x, y; };
     267        struct point p = { .y _ 5, .x _ 7 };
     268        union foo { int i; double d; };
     269        union foo f = { .d _ 4 };
     270        int v1, v2, v4;
     271        int w[6] = { [1] _ v1, v2, [4] _ v4 };
     272        int whitespace[256] = { [' '] _ 1, ['\t'] _ 1, ['\v'] _ 1, ['\f'] _ 1, ['\n'] _ 1, ['\r'] _ 1 };
     273        struct point ptarray[10] = { [2].y _ 34, [2].x _ 35, [0].x _ 36 };
    259274}
    260275
Note: See TracChangeset for help on using the changeset viewer.