File: tests.c

package info (click to toggle)
datatype99 1.6.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 460 kB
  • sloc: ansic: 1,071; sh: 43; makefile: 6
file content (341 lines) | stat: -rw-r--r-- 7,398 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#include <datatype99.h>

#include <assert.h>
#include <stdbool.h>
#include <stddef.h>

// clang-format off
datatype(
    EmptyDatatype,
    (EmptyA),
    (EmptyB)
);

datatype(
    ComplexDatatype,
    (A),
    (B, int),
    (C, const char *, int),
    (D, char, unsigned, long long, int *)
);

record(
    MyTinyRecord,
    (int, x)
);

record(
    MyRecord,
    (int, x),
    (long long, d),
    (const char *, str)
);

record(MyEmptyRecord);
// clang-format on

#define FAIL assert(false)

static void test_match_complex(ComplexDatatype expr) {
    match(expr) {
        of(A) {
            assert(ATag == expr.tag);

            return;
        }
        of(B, x) {
            assert(BTag == expr.tag);

            assert(x == &expr.data.B._0);

            return;
        }
        of(C, str, x) {
            assert(CTag == expr.tag);

            assert(str == &expr.data.C._0);
            assert(x == &expr.data.C._1);

            return;
        }
        of(D, c, x, y, ptr) {
            assert(DTag == expr.tag);

            assert(c == &expr.data.D._0);
            assert(x == &expr.data.D._1);
            assert(y == &expr.data.D._2);
            assert(ptr == &expr.data.D._3);

            return;
        }
    }

    FAIL;
}

static void test_match_empty(EmptyDatatype expr) {
    match(expr) {
        of(A) {
            assert(EmptyATag == expr.tag);
            return;
        }
        of(B) {
            assert(EmptyBTag == expr.tag);
            return;
        }
    }

    FAIL;
}

int main(void) {
    const char *const hello = "hello";

    const ComplexDatatype a = A(), b = B(42), c = C(hello, 12), d = D('~', 0, 5274, NULL);
    const EmptyDatatype empty_a = EmptyA(), empty_b = EmptyB();

    // Test the contents of the values.
    {
        assert(ATag == a.tag);

        assert(BTag == b.tag);
        assert(42 == b.data.B._0);

        assert(CTag == c.tag);
        assert(hello == c.data.C._0);
        assert(12 == c.data.C._1);

        assert(DTag == d.tag);
        assert('~' == d.data.D._0);
        assert(0 == d.data.D._1);
        assert(5274 == d.data.D._2);
        assert(NULL == d.data.D._3);

        assert(EmptyATag == empty_a.tag);
        assert('\0' == empty_a.data.dummy);

        assert(EmptyBTag == empty_b.tag);
        assert('\0' == empty_b.data.dummy);
    }

    // <datatype-name>Tag
    {
        ComplexDatatypeTag tag;

        tag = ATag;
        tag = BTag;
        tag = CTag;
        tag = DTag;

        (void)tag;
    }

    // <datatype-name>Variants
    {
        ComplexDatatypeVariants data = {.dummy = 0};

        data.B._0 = (int)123;

        data.C._0 = (const char *)"abc";
        data.C._1 = (int)9;

        data.D._0 = (char)'A';
        data.D._1 = (unsigned)2924;
        data.D._2 = (long long)-1811;
        data.D._3 = (int *)(int[]){123};

        (void)data;
    }

    // <variant-name>SumT
    {
        const ASumT a_indirect = a;
        const BSumT b_indirect = b;
        const CSumT c_indirect = c;
        const DSumT d_indirect = d;

        (void)a_indirect;
        (void)b_indirect;
        (void)c_indirect;
        (void)d_indirect;
    }

    // <variant-name>_I
    {
        B_0 b_0 = (int)123;
        (void)b_0;

        C_0 c_0 = (const char *)"baba";
        C_1 c_1 = (int)-91;
        (void)c_0;
        (void)c_1;

        D_0 d_0 = (char)'(';
        D_1 d_1 = (unsigned)12322;
        D_2 d_2 = (long long)-7;
        D_3 d_3 = (int *)(int[]){42};
        (void)d_0;
        (void)d_1;
        (void)d_2;
        (void)d_3;
    }

    // MATCHES
    {
        assert(MATCHES(a, A));
        assert(MATCHES(b, B));
        assert(MATCHES(c, C));
        assert(MATCHES(d, D));

        assert(!MATCHES(a, C));
        assert(!MATCHES(b, D));
        assert(!MATCHES(c, B));
        assert(!MATCHES(d, A));

        // Pass an rvalue to `matches`.
        assert(MATCHES(A(), A));
    }

    // Test a single `otherwise` branch.
    {
        match(a) {
            otherwise goto end_single_otherwise;
        }
        FAIL;
    end_single_otherwise:;
    }

    // Test `match`.
    {
        test_match_complex(a);
        test_match_complex(b);
        test_match_complex(c);
        test_match_complex(d);

        test_match_empty(empty_a);
        test_match_empty(empty_b);
    }

    // Test the reserved identifier `_`.
    {
        const ComplexDatatype expr = C("abc", 124);

        match(expr) {
            of(A) {}
            of(B, _) {}
            of(C, _, x) {
                (void)x;
            }
            of(D, c, _, _, ptr) {
                (void)c;
                (void)ptr;
            }
        }
    }

    // Test a nested `match`.
    {
        match(a) {
            of(A) {
                match(b) {
                    of(B) goto end_nested_match;
                    otherwise FAIL;
                }
            }
            otherwise FAIL;
        }
        FAIL;
    end_nested_match:;
    }

    // Test two `match` in the same lexical scope.
    {
#define TEST_MATCH                                                                                 \
    match(a) {                                                                                     \
        of(A);                                                                                     \
        of(B, _);                                                                                  \
        of(C, _, _);                                                                               \
        of(D, _, _, _, _);                                                                         \
    }

        TEST_MATCH;
        TEST_MATCH;

#undef TEST_MATCH
    }

    // The same identifiers from different branches shall not clash with each other.
    {
        const ComplexDatatype expr = A();

        match(expr) {
            of(A) {}
            of(B, same1) {
                (void)same1;
            }
            of(C, same1, same2) {
                (void)same1;
                (void)same2;
            }
            of(D, same1, same2, _, _) {
                (void)same1;
                (void)same2;
            }
        }
    }

    // ifLet
    {
        const ComplexDatatype expr = C("abc", 918);
        ifLet(expr, C, str, x) {
            assert(str == &expr.data.C._0);
            assert(x == &expr.data.C._1);
            goto end_if_let;
        }
        FAIL;
    end_if_let:;

        ifLet(expr, B, _) FAIL;
    }

    // Test two `ifLet` in the same lexical scope.
    {
        ifLet(b, B, _);
        ifLet(b, B, _);
    }

    // Make sure that `match` and `ifLet` result in a single C statement.
    {
        const ComplexDatatype expr = B(42);
        if (true)
            match(expr) {
                otherwise {}
            }

        if (true)
            ifLet(expr, B, _) {}
    }

    // Test record types.
    {
        MyTinyRecord tiny = (MyTinyRecord){.x = 123};
        struct MyTinyRecord tiny2 = tiny;
        (void)tiny;
        (void)tiny2;

        MyRecord r = (MyRecord){.x = 123, .d = 15, .str = "hello world"};
        struct MyRecord r2 = r;
        (void)r;
        (void)r2;

        MyEmptyRecord empty_r = {.dummy = '\0'};
        struct MyEmptyRecord empty_r2 = empty_r;
        (void)empty_r;
        (void)empty_r2;
    }

    UnitT dummy = unit_v;
    (void)dummy;

    return 0;
}