chiark / gitweb /
@@@ wip@g
[mLib] / utils / t / control-test.c
1 /* -*-c-*-
2  *
3  * Test the control-flow metaprogramming macros
4  *
5  * (c) 2022 Straylight/Edgeware
6  */
7
8 /*----- Licensing notice --------------------------------------------------*
9  *
10  * This file is part of the mLib utilities library.
11  *
12  * mLib is free software: you can redistribute it and/or modify it under
13  * the terms of the GNU Library General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or (at
15  * your option) any later version.
16  *
17  * mLib is distributed in the hope that it will be useful, but WITHOUT
18  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
20  * License for more details.
21  *
22  * You should have received a copy of the GNU Library General Public
23  * License along with mLib.  If not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
25  * USA.
26  */
27
28 /*----- Header files ------------------------------------------------------*/
29
30 #include <stdio.h>
31 #include <stdlib.h>
32
33 #include "control.h"
34
35 /*----- Main code ---------------------------------------------------------*/
36
37 static int step = 0;
38 static int rc = 0;
39
40 #define STEP(s) check_step(s, __FILE__ ": " STR(__LINE__))
41 #define MISSTEP STEP(-1)
42 static void check_step(int s, const char *where)
43 {
44   if (step != s) {
45     fprintf(stderr, "misstep at %s: expected %d but found %d\n",
46             where, step, s);
47     rc = 2;
48   }
49   step++;
50 }
51
52 #define LASTSTEP(s) laststep(s, __FILE__ ": " STR(__LINE__))
53 static void laststep(int s, const char *where)
54   { check_step(s, where); step = 0; }
55
56 #define FOR_FIZZBUZZ(var, base, limit)                                  \
57         FIRSTBRANCH(fizzbuzz0) GOBRANCH(fizzbuzz2);                     \
58         MIDBRANCH(fizzbuzz1) ;                                          \
59         LASTBRANCH(fizzbuzz2)                                           \
60         DECL(fizzbuzz3, int _i = base COMMA _limit = limit)             \
61           for (; _i < _limit; _i++)                                     \
62             DECL(fizzbuzz4, char _buf[24])                              \
63             DECL(fizzbuzz5, const char *var)                            \
64             WRAP(fizzbuzz6, {                                           \
65               switch (_i%15) {                                          \
66                 case 0: var = "fizzbuzz"; break;                        \
67                 case 3: case 6: case 9: case 12: var = "fizz"; break;   \
68                 case 5: case 10: var = "buzz"; break;                   \
69                 default: sprintf(_buf, "%d", _i); var = _buf; break;    \
70               }                                                         \
71             },                                                          \
72             { ; },                                                      \
73             { GOBRANCH(fizzbuzz1); })
74
75 int main(void)
76 {
77   BEFORE(before0, { STEP(0); }) STEP(1);
78   AFTER(after0, { STEP(3); }) STEP(2);
79   LASTSTEP(4);
80
81   WRAP(wrap0, { STEP(0); }, { STEP(2); }, { MISSTEP; }) STEP(1);
82   WRAP(wrap1, { STEP(3); }, { MISSTEP; }, { STEP(5); }) { STEP(4); break; }
83   LASTSTEP(6);
84
85   STEP(0);
86   DECL(decl0, int i = 1) STEP(i);
87   LASTSTEP(2);
88
89   FOR_FIZZBUZZ(fb, 19, 27) printf("%s\n", fb);
90
91   return (rc);
92 }
93
94 /*----- That's all, folks -------------------------------------------------*/