@@@ fltfmt 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 tvec_adhoc(&tvstate);
69
70 TESTGROUP("before-after") {
71 MC_BEFORE(before0, STEP(0)) STEP(1);
72 MC_AFTER(after0, STEP(3)) STEP(2);
73 STEP(4);
74 }
75
76 TESTGROUP("wrap") {
77 MC_WRAP(wrap0, STEP(0), STEP(2), MISSTEP)
78 STEP(1);
79 MC_WRAP(wrap1, STEP(3), MISSTEP, STEP(5))
80 { STEP(4); break; }
81 STEP(6);
82 }
83
84 TESTGROUP("loop") {
85 for (;;) {
86 MC_AFTER(after1, STEP(1); break) STEP(0);
87 MISSTEP; break;
88 }
89 STEP(2);
90 }
91
92 #define FORELSE(head) MC_LOOPELSE(forelse, for (head))
93
94 TESTGROUP("for-else") {
95 FORELSE (i = 0; i < 10; i++) {
96 STEP(i);
97 if (i == 7) break;
98 } else
99 MISSTEP;
100 STEP(8);
101 }
102
103 TESTGROUP("for-else-break") {
104 FORELSE (i = 0; i < 10; i++) {
105 STEP(i);
106 if (i == 12) break;
107 } else
108 STEP(10);
109 STEP(11);
110 }
111
112 #undef FORELSE
113
114 TESTGROUP("loop-between") {
115 MC_LOOPBETWEEN(x, i = 0, i < 5, i++) STEP(2*i);
116 else STEP(2*i - 1);
117 STEP(9);
118 }
119
120 TESTGROUP("loop-between-continue-break") {
121 MC_LOOPBETWEEN(x, i = 0, i < 5, i++) {
122 if (i == 1) { STEP(2); continue; }
123 STEP(2*i);
124 if (i == 3) break;
125 } else
126 STEP(2*i - 1);
127 STEP(7);
128 }
129
130 #define WRAPELSE_TEST \
131 MC_TARGET(done_plain, { STEP(4); MC_GOELSE(elsie); }) \
132 MC_WRAP(outer_wrap, { STEP(0); }, \
133 { STEP(6); }, \
134 { MISSTEP; }) \
135 MC_ALLOWELSE(elsie) \
136 MC_WRAP(inner_wrap, { STEP(1); }, \
137 { STEP(3); MC_GOTARGET(done_plain); }, \
138 { MISSTEP; })
139
140 TESTGROUP("wrap-else") {
141 WRAPELSE_TEST STEP(2);
142 else STEP(5);
143 STEP(7);
144 }
145
146 #undef WRAPELSE_TEST
147
148 TESTGROUP("decl") {
149 #if __STDC_VERSION__ >= 199901 || defined(__cplusplus)
150 STEP(0);
151 MC_DECL(decl0, int j = 1) STEP(j);
152 STEP(2);
153 #else
154 tvec_skipgroup(&tvstate, "`MC_DECL' not supported on C89");
155 #endif
156 }
157
158 #define FIZZBUZZ_DECLS(var) \
159 int _i, _limit; \
160 char _buf[24]; \
161 const char *var
162 #define FOR_FIZZBUZZ(var, base, limit) \
163 MC_TARGET(out, { ; }) \
164 MC_BEFORE(bounds, { _i = base; _limit = limit; }) \
165 for (; _i < _limit; _i++) \
166 MC_WRAP(wrap, \
167 { switch (_i%15) { \
168 case 0: var = "fizzbuzz"; break; \
169 case 3: case 6: case 9: case 12: var = "fizz"; break; \
170 case 5: case 10: var = "buzz"; break; \
171 default: sprintf(_buf, "%d", _i); var = _buf; break; \
172 } }, \
173 { ; }, \
174 { MC_GOTARGET(out); })
175
176 TESTGROUP("fizzbuzz") {
177 FIZZBUZZ_DECLS(fb);
178 unsigned i;
179 static const char *const ref[] = {
180 "19", "buzz", "fizz", "22", "23", "fizz", "buzz",
181 "26", "fizz", "28", "29", "fizzbuzz", "31", 0
182 };
183
184 i = 0;
185 FOR_FIZZBUZZ(fb, 19, 32)
186 TEST
187 if (TVEC_CLAIM(&tvstate, ref[i]))
188 { TVEC_CLAIMEQ_TEXTZ(&tvstate, fb, ref[i]); i++; }
189 TVEC_CLAIM(&tvstate, !ref[i]);
190 }
191
192 #undef FIZZBUZZ_DECLS
193 #undef FOR_FIZZBUZZ
194
195 return (tvec_end(&tvstate));
196 }
197
198 /*----- That's all, folks -------------------------------------------------*/