chiark / gitweb /
@@@ more mess
[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 #include <string.h>
33
34 #include "control.h"
35 #include "report.h"
36
37 #include "tvec.h"
38 #include "tvec-adhoc.h"
39 #include "tvec-types.h"
40
41 /*----- Main code ---------------------------------------------------------*/
42
43 static struct tvec_state tvstate;
44 static int step;
45
46 #define TESTGROUP(name)                                                 \
47         TVEC_TESTGROUP_TAG(grp, &tvstate, name)                         \
48         MC_BEFORE(init, { step = 0; })
49 #define TEST TVEC_TEST_TAG(test, &tvstate)
50 #define STEP(s) do {                                                    \
51           tvec_claim(&tvstate, s == step, __FILE__, __LINE__,           \
52                      "found %d /= expected %d", s, step);               \
53           step++;                                                       \
54         } while (0)
55 #define MISSTEP do {                                                    \
56           tvec_claim(&tvstate, 0, __FILE__, __LINE__,                   \
57                      "shouldn't have reached here");                    \
58           step++;                                                       \
59         } while (0)
60
61 int main(int argc, char *argv[])
62 {
63   int argpos;
64   int i;
65
66   tvec_parseargs(argc, argv, &tvstate, &argpos, &tvec_adhocconfig);
67   if (argpos < argc) die(2, "no input files expected");
68
69   TESTGROUP("before-after") {
70     MC_BEFORE(before0, STEP(0)) STEP(1);
71     MC_AFTER(after0, STEP(3)) STEP(2);
72     STEP(4);
73   }
74
75   TESTGROUP("wrap") {
76     MC_WRAP(wrap0, STEP(0), STEP(2), MISSTEP)
77       STEP(1);
78     MC_WRAP(wrap1, STEP(3), MISSTEP, STEP(5))
79       { STEP(4); break; }
80     STEP(6);
81   }
82
83   TESTGROUP("loop") {
84     for (;;) {
85       MC_AFTER(after1, STEP(1); break) STEP(0);
86       MISSTEP; break;
87     }
88     STEP(2);
89   }
90
91 #define FORELSE(head) MC_LOOPELSE(forelse, for (head))
92
93   TESTGROUP("for-else") {
94     FORELSE (i = 0; i < 10; i++) {
95       STEP(i);
96       if (i == 7) break;
97     } else
98       MISSTEP;
99     STEP(8);
100   }
101
102   TESTGROUP("for-else-break") {
103     FORELSE (i = 0; i < 10; i++) {
104       STEP(i);
105       if (i == 12) break;
106     } else
107       STEP(10);
108     STEP(11);
109   }
110
111 #undef FORELSE
112
113   TESTGROUP("loop-between") {
114     MC_LOOPBETWEEN(x, i = 0, i < 5, i++) STEP(2*i);
115     else STEP(2*i - 1);
116     STEP(9);
117   }
118
119   TESTGROUP("loop-between-continue-break") {
120     MC_LOOPBETWEEN(x, i = 0, i < 5, i++) {
121       if (i == 1) { STEP(2); continue; }
122       STEP(2*i);
123       if (i == 3) break;
124     } else
125       STEP(2*i - 1);
126     STEP(7);
127   }
128
129 #define WRAPELSE_TEST                                                   \
130         MC_TARGET(done_plain, { STEP(4); MC_GOELSE(elsie); })           \
131         MC_WRAP(outer_wrap, { STEP(0); },                               \
132                             { STEP(6); },                               \
133                             { MISSTEP; })                               \
134         MC_ALLOWELSE(elsie)                                             \
135         MC_WRAP(inner_wrap, { STEP(1); },                               \
136                             { STEP(3); MC_GOTARGET(done_plain); },      \
137                             { MISSTEP; })
138
139   TESTGROUP("wrap-else") {
140     WRAPELSE_TEST STEP(2);
141     else STEP(5);
142     STEP(7);
143   }
144
145 #undef WRAPELSE_TEST
146
147   TESTGROUP("decl") {
148 #if __STDC_VERSION__ >= 199901 || defined(__cplusplus)
149     STEP(0);
150     MC_DECL(decl0, int j = 1) STEP(j);
151     STEP(2);
152 #else
153     tvec_skipgroup(&tvstate, "`MC_DECL' not supported on C89");
154 #endif
155   }
156
157 #define FIZZBUZZ_DECLS(var)                                             \
158         int _i, _limit;                                                 \
159         char _buf[24];                                                  \
160         const char *var
161 #define FOR_FIZZBUZZ(var, base, limit)                                  \
162         MC_TARGET(out, { ; })                                           \
163         MC_BEFORE(bounds, { _i = base; _limit = limit; })               \
164         for (; _i < _limit; _i++)                                       \
165           MC_WRAP(wrap,                                                 \
166             { switch (_i%15) {                                          \
167                 case 0: var = "fizzbuzz"; break;                        \
168                 case 3: case 6: case 9: case 12: var = "fizz"; break;   \
169                 case 5: case 10: var = "buzz"; break;                   \
170                 default: sprintf(_buf, "%d", _i); var = _buf; break;    \
171               } },                                                      \
172             { ; },                                                      \
173             { MC_GOTARGET(out); })
174
175   TESTGROUP("fizzbuzz") {
176     FIZZBUZZ_DECLS(fb);
177     unsigned i;
178     static const char *const ref[] = {
179       "19", "buzz", "fizz", "22", "23", "fizz", "buzz",
180       "26", "fizz", "28", "29", "fizzbuzz", "31", 0
181     };
182
183     i = 0;
184     FOR_FIZZBUZZ(fb, 19, 32)
185       TEST
186         if (TVEC_CLAIM(&tvstate, ref[i]))
187           { TVEC_CLAIMEQ_TEXTZ(&tvstate, fb, ref[i]); i++; }
188     TVEC_CLAIM(&tvstate, !ref[i]);
189   }
190
191 #undef FIZZBUZZ_DECLS
192 #undef FOR_FIZZBUZZ
193
194   return (tvec_end(&tvstate));
195 }
196
197 /*----- That's all, folks -------------------------------------------------*/