chiark / gitweb /
@@@ tvec doc wip
[mLib] / utils / t / control-test.c
CommitLineData
e6591bec
MW
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>
b64eb60f 32#include <string.h>
e6591bec
MW
33
34#include "control.h"
b64eb60f
MW
35#include "report.h"
36#include "tvec.h"
e6591bec
MW
37
38/*----- Main code ---------------------------------------------------------*/
39
b64eb60f
MW
40static struct tvec_state tvstate;
41static int step;
42
43#define TESTGROUP(name) \
3efcfd2d 44 TVEC_TESTGROUP_TAG(grp, &tvstate, name) \
b64eb60f 45 MC_BEFORE(init, { step = 0; })
3efcfd2d 46#define TEST TVEC_TEST_TAG(test, &tvstate)
b64eb60f
MW
47#define STEP(s) do { \
48 tvec_claim(&tvstate, s == step, __FILE__, __LINE__, \
49 "found %d /= expected %d", s, step); \
50 step++; \
51 } while (0)
52#define MISSTEP do { \
53 tvec_claim(&tvstate, 0, __FILE__, __LINE__, \
54 "shouldn't have reached here"); \
55 step++; \
56 } while (0)
57
58int main(int argc, char *argv[])
e6591bec 59{
b64eb60f
MW
60 struct tvec_test test;
61 int argpos;
62 int i;
63
c5e0e403 64 tvec_parseargs(argc, argv, &tvstate, &argpos, &tvec_adhocconfig);
b64eb60f
MW
65 if (argpos < argc) die(2, "no input files expected");
66 tvec_adhoc(&tvstate, &test);
67
68 TESTGROUP("before-after") {
69 MC_BEFORE(before0, STEP(0)) STEP(1);
70 MC_AFTER(after0, STEP(3)) STEP(2);
71 STEP(4);
e6591bec 72 }
e6591bec 73
b64eb60f
MW
74 TESTGROUP("wrap") {
75 MC_WRAP(wrap0, STEP(0), STEP(2), MISSTEP)
76 STEP(1);
77 MC_WRAP(wrap1, STEP(3), MISSTEP, STEP(5))
78 { STEP(4); break; }
79 STEP(6);
80 }
e6591bec 81
b64eb60f
MW
82 TESTGROUP("loop") {
83 for (;;) {
84 MC_AFTER(after1, STEP(1); break) STEP(0);
85 MISSTEP; break;
86 }
87 STEP(2);
88 }
2edecb46 89
b64eb60f
MW
90#define FORELSE(head) MC_LOOPELSE(forelse, for (head))
91
92 TESTGROUP("for-else") {
93 FORELSE (i = 0; i < 10; i++) {
94 STEP(i);
95 if (i == 7) break;
96 } else
97 MISSTEP;
98 STEP(8);
99 }
100
101 TESTGROUP("for-else-break") {
102 FORELSE (i = 0; i < 10; i++) {
103 STEP(i);
104 if (i == 12) break;
105 } else
106 STEP(10);
107 STEP(11);
108 }
2edecb46 109
b64eb60f 110#undef FORELSE
e6591bec 111
b64eb60f
MW
112 TESTGROUP("loop-between") {
113 MC_LOOPBETWEEN(x, i = 0, i < 5, i++) STEP(2*i);
114 else STEP(2*i - 1);
115 STEP(9);
116 }
117
118 TESTGROUP("loop-between-continue-break") {
119 MC_LOOPBETWEEN(x, i = 0, i < 5, i++) {
120 if (i == 1) { STEP(2); continue; }
121 STEP(2*i);
122 if (i == 3) break;
123 } else
124 STEP(2*i - 1);
125 STEP(7);
126 }
e6591bec 127
b64eb60f
MW
128#define WRAPELSE_TEST \
129 MC_TARGET(done_plain, { STEP(4); MC_GOELSE(elsie); }) \
130 MC_WRAP(outer_wrap, { STEP(0); }, \
131 { STEP(6); }, \
132 { MISSTEP; }) \
133 MC_ALLOWELSE(elsie) \
134 MC_WRAP(inner_wrap, { STEP(1); }, \
135 { STEP(3); MC_GOTARGET(done_plain); }, \
136 { MISSTEP; })
137
138 TESTGROUP("wrap-else") {
139 WRAPELSE_TEST STEP(2);
140 else STEP(5);
141 STEP(7);
55d45d31 142 }
2edecb46 143
b64eb60f
MW
144#undef WRAPELSE_TEST
145
146 TESTGROUP("decl") {
2edecb46 147#if __STDC_VERSION__ >= 199901 || defined(__cplusplus)
b64eb60f
MW
148 STEP(0);
149 MC_DECL(decl0, int j = 1) STEP(j);
150 STEP(2);
151#else
152 tvec_skipgroup(&tvstate, "`MC_DECL' not supported on C89");
2edecb46 153#endif
b64eb60f
MW
154 }
155
156#define FIZZBUZZ_DECLS(var) \
157 int _i, _limit; \
158 char _buf[24]; \
159 const char *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++) \
164 MC_WRAP(wrap, \
165 { switch (_i%15) { \
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; \
170 } }, \
171 { ; }, \
172 { MC_GOTARGET(out); })
173
174 TESTGROUP("fizzbuzz") {
175 FIZZBUZZ_DECLS(fb);
176 unsigned i;
177 static const char *const ref[] = {
178 "19", "buzz", "fizz", "22", "23", "fizz", "buzz",
179 "26", "fizz", "28", "29", "fizzbuzz", "31", 0
180 };
181
182 i = 0;
183 FOR_FIZZBUZZ(fb, 19, 32)
184 TEST
185 if (TVEC_CLAIM(&tvstate, ref[i]))
c81c35df 186 { TVEC_CLAIMEQ_TEXTZ(&tvstate, fb, ref[i]); i++; }
b64eb60f
MW
187 TVEC_CLAIM(&tvstate, !ref[i]);
188 }
e6591bec 189
b64eb60f
MW
190#undef FIZZBUZZ_DECLS
191#undef FOR_FIZZBUZZ
e6591bec 192
b64eb60f 193 return (tvec_end(&tvstate));
e6591bec
MW
194}
195
196/*----- That's all, folks -------------------------------------------------*/