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");
70 TESTGROUP("before-after") {
71 MC_BEFORE(before0
, STEP(0)) STEP(1);
72 MC_AFTER(after0
, STEP(3)) STEP(2);
77 MC_WRAP(wrap0
, STEP(0), STEP(2), MISSTEP
)
79 MC_WRAP(wrap1
, STEP(3), MISSTEP
, STEP(5))
86 MC_AFTER(after1
, STEP(1); break) STEP(0);
92 #define FORELSE(head) MC_LOOPELSE(forelse, for (head))
94 TESTGROUP("for-else") {
95 FORELSE (i
= 0; i
< 10; i
++) {
103 TESTGROUP("for-else-break") {
104 FORELSE (i
= 0; i
< 10; i
++) {
114 TESTGROUP("loop-between") {
115 MC_LOOPBETWEEN(x
, i
= 0, i
< 5, i
++) STEP(2*i
);
120 TESTGROUP("loop-between-continue-break") {
121 MC_LOOPBETWEEN(x
, i
= 0, i
< 5, i
++) {
122 if (i
== 1) { STEP(2); continue; }
130 #define WRAPELSE_TEST \
131 MC_TARGET(done_plain, { STEP(4); MC_GOELSE(elsie); }) \
132 MC_WRAP(outer_wrap, { STEP(0); }, \
135 MC_ALLOWELSE(elsie) \
136 MC_WRAP(inner_wrap, { STEP(1); }, \
137 { STEP(3); MC_GOTARGET(done_plain); }, \
140 TESTGROUP("wrap-else") {
141 WRAPELSE_TEST
STEP(2);
149 #if __STDC_VERSION__ >= 199901 || defined(__cplusplus)
151 MC_DECL(decl0
, int j
= 1) STEP(j
);
154 tvec_skipgroup(&tvstate
, "`MC_DECL' not supported on C89");
158 #define FIZZBUZZ_DECLS(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++) \
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; \
174 { MC_GOTARGET(out); })
176 TESTGROUP("fizzbuzz") {
179 static const char *const ref
[] = {
180 "19", "buzz", "fizz", "22", "23", "fizz", "buzz",
181 "26", "fizz", "28", "29", "fizzbuzz", "31", 0
185 FOR_FIZZBUZZ(fb
, 19, 32)
187 if (TVEC_CLAIM(&tvstate
, ref
[i
]))
188 { TVEC_CLAIMEQ_TEXTZ(&tvstate
, fb
, ref
[i
]); i
++; }
189 TVEC_CLAIM(&tvstate
, !ref
[i
]);
192 #undef FIZZBUZZ_DECLS
195 return (tvec_end(&tvstate
));
198 /*----- That's all, folks -------------------------------------------------*/