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 #include "tvec-adhoc.h"
39 #include "tvec-types.h"
41 /*----- Main code ---------------------------------------------------------*/
43 static struct tvec_state tvstate;
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); \
55 #define MISSTEP do { \
56 tvec_claim(&tvstate, 0, __FILE__, __LINE__, \
57 "shouldn't have reached here"); \
61 int main(int argc, char *argv[])
66 tvec_parseargs(argc, argv, &tvstate, &argpos, &tvec_adhocconfig);
67 if (argpos < argc) die(2, "no input files expected");
69 TESTGROUP("before-after") {
70 MC_BEFORE(before0, STEP(0)) STEP(1);
71 MC_AFTER(after0, STEP(3)) STEP(2);
76 MC_WRAP(wrap0, STEP(0), STEP(2), MISSTEP)
78 MC_WRAP(wrap1, STEP(3), MISSTEP, STEP(5))
85 MC_AFTER(after1, STEP(1); break) STEP(0);
91 #define FORELSE(head) MC_LOOPELSE(forelse, for (head))
93 TESTGROUP("for-else") {
94 FORELSE (i = 0; i < 10; i++) {
102 TESTGROUP("for-else-break") {
103 FORELSE (i = 0; i < 10; i++) {
113 TESTGROUP("loop-between") {
114 MC_LOOPBETWEEN(x, i = 0, i < 5, i++) STEP(2*i);
119 TESTGROUP("loop-between-continue-break") {
120 MC_LOOPBETWEEN(x, i = 0, i < 5, i++) {
121 if (i == 1) { STEP(2); continue; }
129 #define WRAPELSE_TEST \
130 MC_TARGET(done_plain, { STEP(4); MC_GOELSE(elsie); }) \
131 MC_WRAP(outer_wrap, { STEP(0); }, \
134 MC_ALLOWELSE(elsie) \
135 MC_WRAP(inner_wrap, { STEP(1); }, \
136 { STEP(3); MC_GOTARGET(done_plain); }, \
139 TESTGROUP("wrap-else") {
140 WRAPELSE_TEST STEP(2);
148 #if __STDC_VERSION__ >= 199901 || defined(__cplusplus)
150 MC_DECL(decl0, int j = 1) STEP(j);
153 tvec_skipgroup(&tvstate, "`MC_DECL' not supported on C89");
157 #define FIZZBUZZ_DECLS(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++) \
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; \
173 { MC_GOTARGET(out); })
175 TESTGROUP("fizzbuzz") {
178 static const char *const ref[] = {
179 "19", "buzz", "fizz", "22", "23", "fizz", "buzz",
180 "26", "fizz", "28", "29", "fizzbuzz", "31", 0
184 FOR_FIZZBUZZ(fb, 19, 32)
186 if (TVEC_CLAIM(&tvstate, ref[i]))
187 { TVEC_CLAIMEQ_TEXTZ(&tvstate, fb, ref[i]); i++; }
188 TVEC_CLAIM(&tvstate, !ref[i]);
191 #undef FIZZBUZZ_DECLS
194 return (tvec_end(&tvstate));
197 /*----- That's all, folks -------------------------------------------------*/