3 * Test the control-flow metaprogramming macros
5 * (c) 2022 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the mLib utilities library.
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.
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.
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,
28 /*----- Header files ------------------------------------------------------*/
38 /*----- Main code ---------------------------------------------------------*/
40 static struct tvec_state tvstate;
43 #define TESTGROUP(name) \
44 TVEC_TESTGROUP_TAG(grp, &tvstate, name) \
45 MC_BEFORE(init, { step = 0; })
46 #define TEST TVEC_TEST_TAG(test, &tvstate)
47 #define STEP(s) do { \
48 tvec_claim(&tvstate, s == step, __FILE__, __LINE__, \
49 "found %d /= expected %d", s, step); \
52 #define MISSTEP do { \
53 tvec_claim(&tvstate, 0, __FILE__, __LINE__, \
54 "shouldn't have reached here"); \
58 int main(int argc, char *argv[])
60 struct tvec_test test;
64 tvec_parseargs(argc, argv, &tvstate, &argpos, &tvec_adhocconfig);
65 if (argpos < argc) die(2, "no input files expected");
66 tvec_adhoc(&tvstate, &test);
68 TESTGROUP("before-after") {
69 MC_BEFORE(before0, STEP(0)) STEP(1);
70 MC_AFTER(after0, STEP(3)) STEP(2);
75 MC_WRAP(wrap0, STEP(0), STEP(2), MISSTEP)
77 MC_WRAP(wrap1, STEP(3), MISSTEP, STEP(5))
84 MC_AFTER(after1, STEP(1); break) STEP(0);
90 #define FORELSE(head) MC_LOOPELSE(forelse, for (head))
92 TESTGROUP("for-else") {
93 FORELSE (i = 0; i < 10; i++) {
101 TESTGROUP("for-else-break") {
102 FORELSE (i = 0; i < 10; i++) {
112 TESTGROUP("loop-between") {
113 MC_LOOPBETWEEN(x, i = 0, i < 5, i++) STEP(2*i);
118 TESTGROUP("loop-between-continue-break") {
119 MC_LOOPBETWEEN(x, i = 0, i < 5, i++) {
120 if (i == 1) { STEP(2); continue; }
128 #define WRAPELSE_TEST \
129 MC_TARGET(done_plain, { STEP(4); MC_GOELSE(elsie); }) \
130 MC_WRAP(outer_wrap, { STEP(0); }, \
133 MC_ALLOWELSE(elsie) \
134 MC_WRAP(inner_wrap, { STEP(1); }, \
135 { STEP(3); MC_GOTARGET(done_plain); }, \
138 TESTGROUP("wrap-else") {
139 WRAPELSE_TEST STEP(2);
147 #if __STDC_VERSION__ >= 199901 || defined(__cplusplus)
149 MC_DECL(decl0, int j = 1) STEP(j);
152 tvec_skipgroup(&tvstate, "`MC_DECL' not supported on C89");
156 #define FIZZBUZZ_DECLS(var) \
160 #define FOR_FIZZBUZZ(var, base, limit) \
161 MC_TARGET(out, { ; }) \
162 MC_BEFORE(bounds, { _i = base; _limit = limit; }) \
163 for (; _i < _limit; _i++) \
166 case 0: var = "fizzbuzz"; break; \
167 case 3: case 6: case 9: case 12: var = "fizz"; break; \
168 case 5: case 10: var = "buzz"; break; \
169 default: sprintf(_buf, "%d", _i); var = _buf; break; \
172 { MC_GOTARGET(out); })
174 TESTGROUP("fizzbuzz") {
177 static const char *const ref[] = {
178 "19", "buzz", "fizz", "22", "23", "fizz", "buzz",
179 "26", "fizz", "28", "29", "fizzbuzz", "31", 0
183 FOR_FIZZBUZZ(fb, 19, 32)
185 if (TVEC_CLAIM(&tvstate, ref[i]))
186 { TVEC_CLAIMEQ_STRZ(&tvstate, fb, ref[i]); i++; }
187 TVEC_CLAIM(&tvstate, !ref[i]);
190 #undef FIZZBUZZ_DECLS
193 return (tvec_end(&tvstate));
196 /*----- That's all, folks -------------------------------------------------*/