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 ------------------------------------------------------*/
35 /*----- Main code ---------------------------------------------------------*/
40 #define STEP(s) check_step(s, __FILE__ ": " STR(__LINE__))
41 #define MISSTEP STEP(-1)
42 static void check_step(int s, const char *where)
45 fprintf(stderr, "misstep at %s: expected %d but found %d\n",
52 #define LASTSTEP(s) laststep(s, __FILE__ ": " STR(__LINE__))
53 static void laststep(int s, const char *where)
54 { check_step(s, where); step = 0; }
56 #define FOR_FIZZBUZZ(var, base, limit) \
57 FIRSTBRANCH(fizzbuzz0) GOBRANCH(fizzbuzz2); \
58 MIDBRANCH(fizzbuzz1) ; \
59 LASTBRANCH(fizzbuzz2) \
60 DECL(fizzbuzz3, int _i = base COMMA _limit = limit) \
61 for (; _i < _limit; _i++) \
62 DECL(fizzbuzz4, char _buf[24]) \
63 DECL(fizzbuzz5, const char *var) \
66 case 0: var = "fizzbuzz"; break; \
67 case 3: case 6: case 9: case 12: var = "fizz"; break; \
68 case 5: case 10: var = "buzz"; break; \
69 default: sprintf(_buf, "%d", _i); var = _buf; break; \
73 { GOBRANCH(fizzbuzz1); })
77 BEFORE(before0, { STEP(0); }) STEP(1);
78 AFTER(after0, { STEP(3); }) STEP(2);
81 WRAP(wrap0, { STEP(0); }, { STEP(2); }, { MISSTEP; }) STEP(1);
82 WRAP(wrap1, { STEP(3); }, { MISSTEP; }, { STEP(5); }) { STEP(4); break; }
86 DECL(decl0, int i = 1) STEP(i);
89 FOR_FIZZBUZZ(fb, 19, 27) printf("%s\n", fb);
94 /*----- That's all, folks -------------------------------------------------*/