@@@ BROKEN wip
[mLib] / test / t / tvec-test.c
CommitLineData
b64eb60f
MW
1/* -*-c-*-
2 *
3 * Test the test-vector framework
4 *
5 * (c) 2023 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 "tvec.h"
31
32/*----- Register definitions ----------------------------------------------*/
33
b64eb60f
MW
34static const struct tvec_iassoc ienum_assocs[] = {
35 { "less", -1 },
36 { "equal", 0 },
37 { "greater", +1 },
38 { 0 }
39};
40
41static const struct tvec_uassoc uenum_assocs[] = {
42 { "apple", 0 },
43 { "banana", 1 },
44 { "clementine", 2 },
45 { 0 }
46};
47
e63124bc
MW
48static const struct tvec_fassoc fenum_assocs[] = {
49 { "e", 2.718281828459045 },
50 { "pi", 3.141592653589793 },
51 { "tau", 6.283185307179586 },
52 { 0 }
53};
54
b64eb60f
MW
55static const struct tvec_passoc penum_assocs[] = {
56 { "alice", &uenum_assocs[0] },
57 { "bob", &uenum_assocs[1] },
58 { "carol", &uenum_assocs[2] },
59 { 0 }
60};
61
e63124bc 62#if __STDC_VERSION__x >= 199901
b64eb60f
MW
63# define DSGINIT(x) x
64#else
65# define DSGINIT(x)
66#endif
67
e63124bc
MW
68static const struct tvec_floatinfo fenum_fltinfo =
69 { TVFF_ABSDELTA, -10, +10, 1e-3 };
70
71#define DEFENUM(tag, ty, slot) \
72 static const struct tvec_##slot##enuminfo slot##enum_info = \
3efcfd2d 73 { slot##enum_NAME, slot##enum_assocs slot##enum_ARGS };
e63124bc
MW
74#define ienum_NAME "order"
75#define ienum_ARGS , &tvrange_i16
76#define uenum_NAME "fruit"
77#define uenum_ARGS , &tvrange_u16
78#define fenum_NAME "const"
79#define fenum_ARGS , &fenum_fltinfo
80#define penum_NAME "actor"
81#define penum_ARGS
82TVEC_MISCSLOTS(DEFENUM)
83#undef DEFENUM
b64eb60f
MW
84
85static const struct tvec_flag attr_flags[] = {
86 { "black-fg", 0x07, 0x00 },
87 { "blue-fg", 0x07, 0x01 },
88 { "red-fg", 0x07, 0x02 },
89 { "magenta-fg", 0x07, 0x03 },
90 { "green-fg", 0x07, 0x04 },
91 { "cyan-fg", 0x07, 0x05 },
92 { "yellow-fg", 0x07, 0x06 },
93 { "white-fg", 0x07, 0x07 },
94
95 { "black-bg", 0x38, 0x00 },
96 { "blue-bg", 0x38, 0x08 },
97 { "red-bg", 0x38, 0x10 },
98 { "magenta-bg", 0x38, 0x18 },
99 { "green-bg", 0x38, 0x20 },
100 { "cyan-bg", 0x38, 0x28 },
101 { "yellow-bg", 0x38, 0x30 },
102 { "white-bg", 0x38, 0x38 },
103
104 { "normal", 0xc0, 0x00 },
105 { "bright", 0x40, 0x40 },
106 { "flash", 0x80, 0x80 },
107
108 { 0 }
109};
110
111static const struct tvec_flaginfo attr_info =
112 { "attr", attr_flags, &tvrange_u16 };
113
e63124bc
MW
114static const struct tvec_floatinfo fltish_info =
115 { TVFF_RELDELTA, -1.0, +1.0, 1e-6 };
116
b64eb60f
MW
117static const struct tvec_urange range_32 = { 0, 31 };
118
119#define TYPEREGS(_) \
120 _(int, RI, int, p, &tvrange_i16) \
121 _(uint, RU, uint, p, &tvrange_u16) \
e63124bc
MW
122 _(float, RFP, float, p, 0) \
123 _(fltish, RFISH, float, p, &fltish_info) \
124 _(char, RCH, char, p, 0) \
3efcfd2d
MW
125 _(ienum, RIE, ienum, p, &ienum_info) \
126 _(uenum, RUE, uenum, p, &uenum_info) \
127 _(fenum, RFE, fenum, p, &fenum_info) \
128 _(penum, RPE, penum, p, &penum_info) \
b64eb60f
MW
129 _(flags, RF, flags, p, &attr_info) \
130 _(string, RSTR, string, p, &range_32) \
131 _(bytes, RBY, bytes, p, &tvrange_byte) \
132 _(buffer, RBUF, buffer, p, &tvrange_u16)
133
e63124bc
MW
134enum {
135 /* Output registers, one for each register type. */
136#define DEFREG(name, i, ty, argslot, argval) i,
137 TYPEREGS(DEFREG)
138#undef DEFREG
139 NSER,
140
141 /* Standard outputs. */
142 RRC = NSER, /* return code from deserialize */
143
144 /* Additional diagnostic outputs. */
145 RSER, /* serialized data */
146
147 NROUT,
148
149 /* Some additional inputs. */
150 RSAB = NROUT, /* which register to sabotage */
151
152 NREG,
153
154 /* Single register for copy tests. */
155 RV = 0
156};
157
b64eb60f
MW
158/*----- Serialization test ------------------------------------------------*/
159
160struct test_context {
161 struct tvec_state *tv;
162};
163
e63124bc
MW
164static int capture_setup(struct tvec_state *tv,
165 const struct tvec_env *env, void *pctx, void *ctx)
166 { struct test_context *tctx = ctx; tctx->tv = tv; return (0); }
b64eb60f 167
e63124bc
MW
168static void capture_run(struct tvec_state *tv, tvec_testfn *fn, void *ctx)
169{
b64eb60f 170 if (!(tv->in[RRC].f&TVRF_LIVE)) {
e63124bc
MW
171 tv->in[RRC].f |= TVRF_LIVE; tv->in[RRC].v.i = 0;
172 tv->out[RRC].f |= TVRF_LIVE;
b64eb60f 173 }
e63124bc 174 fn(tv->in, tv->out, ctx); tvec_check(tv, 0);
b64eb60f 175}
e63124bc
MW
176static const struct tvec_env capture_testenv =
177 { sizeof(struct test_context), capture_setup, 0, 0, capture_run, 0, 0 };
b64eb60f
MW
178
179static void test_serialization
180 (const struct tvec_reg *in, struct tvec_reg *out, void *ctx)
181{
182 struct test_context *tctx = ctx;
183 struct tvec_state *tv = tctx->tv;
184 const struct tvec_regdef *rd;
185 union tvec_regval *rv;
e63124bc 186 dbuf b = DBUF_INIT;
b64eb60f 187
e63124bc
MW
188 if (tvec_serialize(tv->in, DBUF_BUF(&b), tv->test->regs,
189 NSER, sizeof(struct tvec_reg)))
190 { out[NSER].v.i = -1; goto end; }
191 tvec_allocbytes(&out[RSER].v, DBLEN(&b));
192 memcpy(out[RSER].v.bytes.p, DBBASE(&b), DBLEN(&b));
b64eb60f 193 out[RSER].f |= TVRF_LIVE;
e63124bc 194 buf_flip(DBUF_BUF(&b));
b64eb60f 195
e63124bc
MW
196 if (tvec_deserialize(tv->out, DBUF_BUF(&b), tv->test->regs,
197 NSER, sizeof(struct tvec_reg)))
198 { out[RRC].v.i = -2; goto end; }
199 if (BLEFT(&b._b))
200 { out[RRC].v.i = -3; goto end; }
b64eb60f 201
3efcfd2d
MW
202 if ((in[RSAB].f&TVRF_LIVE) && in[RSAB].v.i >= 0) {
203 rd = &tv->test->regs[in[RSAB].v.i]; rv = &out[in[RSAB].v.i].v;
204 if (rd->ty == &tvty_int || rd->ty == &tvty_ienum)
205 rv->i ^= 1;
206 else if (rd->ty == &tvty_uint ||
207 rd->ty == &tvty_flags || rd->ty == &tvty_uenum)
208 rv->u ^= 1;
209 else if (rd->ty == &tvty_float || rd->ty == &tvty_fenum) {
210 if (rv->f == rv->f) rv->f = -rv->f;
211 else rv->f = 1.0;
212 } else if (rd->ty == &tvty_penum)
213 rv->p = rv->p
214 ? 0
215 : (/*unconst*/ void *)
216 ((const struct tvec_penuminfo *)rd->arg.p)->av[0].p;
217 else if (rd->ty == &tvty_string)
218 { if (rv->str.sz) rv->str.p[0] ^= 1; }
219 else if (rd->ty == &tvty_bytes)
220 { if (rv->bytes.sz) rv->bytes.p[0] ^= 1; }
b64eb60f
MW
221 }
222
223 out[RRC].v.i = 0;
e63124bc
MW
224end:
225 dbuf_destroy(&b);
b64eb60f
MW
226}
227
3efcfd2d
MW
228static const struct tvec_iassoc type_assocs[] = {
229 { "none", -1 },
230#define DEFASSOC(name, i, ty, argslot, argval) { #name, i },
231 TYPEREGS(DEFASSOC)
232#undef DEFASSOC
233 { 0, 0 }
234};
235static const struct tvec_ienuminfo type_enum = { "regty", type_assocs, 0 };
236
e63124bc 237static DSGINIT(const) struct tvec_regdef test_regs[] = {
b64eb60f
MW
238#define DEFREG(name, i, ty, argslot, argval) \
239 { #name, i, &tvty_##ty, TVRF_OPT, \
240 DSGINIT({ .argslot = argval }) },
241 TYPEREGS(DEFREG)
242#undef DEFREG
243 { "rc", RRC, &tvty_int, TVRF_OPT, { &tvrange_int } },
244 { "serialized", RSER, &tvty_bytes, TVRF_OPT },
3efcfd2d 245 { "sabotage", RSAB, &tvty_ienum, TVRF_OPT, { &type_enum } },
b64eb60f
MW
246
247 { 0 }
248};
249
250/*----- Single-type copy tests --------------------------------------------*/
251
252static void test_copy_simple
253 (const struct tvec_reg *in, struct tvec_reg *out, void *ctx)
254 { out->v = in->v; }
255
256static void test_copy_string
257 (const struct tvec_reg *in, struct tvec_reg *out, void *ctx)
258{
259 tvec_allocstring(&out->v, in->v.str.sz);
260 memcpy(out->v.str.p, in->v.str.p, in->v.str.sz);
261}
262
263static void test_copy_bytes
264 (const struct tvec_reg *in, struct tvec_reg *out, void *ctx)
265{
266 tvec_allocstring(&out->v, in->v.str.sz);
267 memcpy(out->v.str.p, in->v.str.p, in->v.str.sz);
268}
269
270#define test_copy_int test_copy_simple
271#define test_copy_uint test_copy_simple
272#define test_copy_ienum test_copy_simple
273#define test_copy_uenum test_copy_simple
e63124bc 274#define test_copy_fenum test_copy_simple
b64eb60f 275#define test_copy_penum test_copy_simple
e63124bc 276#define test_copy_char test_copy_simple
b64eb60f 277#define test_copy_flags test_copy_simple
e63124bc
MW
278#define test_copy_float test_copy_simple
279#define test_copy_fltish test_copy_simple
b64eb60f
MW
280#define test_copy_buffer test_copy_bytes
281
282#define SINGLEREG(name, i, ty, argslot, argval) \
283 DSGINIT(const) struct tvec_regdef name##_regs[] = { \
284 { #name, RV, &tvty_##ty, 0, DSGINIT({ .argslot = argval }) }, \
285 { 0 } \
286 };
287TYPEREGS(SINGLEREG)
288#undef SINGLEREG
289
e63124bc
MW
290struct singlectx {
291 unsigned f;
292#define SF_SHOW 1u
293};
294
295static int single_setup(struct tvec_state *tv, const struct tvec_env *env,
296 void *pctx, void *ctx)
297 { struct singlectx *s = ctx; s->f = 0; return (0); }
298
299static int single_set(struct tvec_state *tv, const char *name,
300 const struct tvec_env *env, void *ctx)
301{
302 struct singlectx *s = ctx;
303 union tvec_regval rv;
304 static const struct tvec_regdef rd =
3efcfd2d 305 { "@show", -1, &tvty_ienum, 0, { &tvenum_bool } };
e63124bc
MW
306
307 if (STRCMP(name, ==, "@show")) {
3efcfd2d 308 if (tvty_ienum.parse(&rv, &rd, tv)) return (-1);
e63124bc
MW
309 if (s) {
310 if (rv.i) s->f |= SF_SHOW;
311 else s->f &= ~SF_SHOW;
312 }
313 return (1);
314 } else
315 return (0);
316}
317
318static void single_run(struct tvec_state *tv, tvec_testfn *fn, void *ctx)
319{
320 struct singlectx *s = ctx;
321 unsigned f = s->f;
322
323 fn(tv->in, tv->out, 0);
324 if (tvec_checkregs(tv)) { tvec_fail(tv, 0); f |= SF_SHOW; }
325 if (f&SF_SHOW) tvec_mismatch(tv, TVMF_IN | TVMF_OUT);
326}
327
328static void single_after(struct tvec_state *tv, void *ctx)
329 { struct singlectx *s = ctx; s->f = 0; }
330
331static const struct tvec_env single_testenv =
332 { sizeof(struct singlectx),
333 single_setup,
334 single_set,
335 0,
336 single_run,
337 single_after,
338 0 };
339
b64eb60f
MW
340/*----- Front end ---------------------------------------------------------*/
341
342static const struct tvec_test tests[] = {
e63124bc 343 { "types", test_regs, &capture_testenv, test_serialization },
b64eb60f
MW
344
345#define DEFCOPY(name, i, ty, argslot, argval) \
e63124bc 346 { #name, name##_regs, &single_testenv, test_copy_##name },
b64eb60f
MW
347 TYPEREGS(DEFCOPY)
348#undef DEFCOPY
349
350 { 0 }
351};
352
c5e0e403 353static const struct tvec_config testconfig = {
b64eb60f
MW
354 tests,
355 NROUT, NREG, sizeof(struct tvec_reg)
356};
357
358int main(int argc, char *argv[])
359{
e63124bc 360#if __STDC_VERSION__x < 199901
b64eb60f 361# define POKE(name, i, ty, argslot, argval) \
e63124bc 362 test_regs[i].arg.argslot = argval; \
b64eb60f
MW
363 name##_regs->arg.argslot = argval;
364 TYPEREGS(POKE)
365# undef POKE
366#endif
c5e0e403 367 return (tvec_main(argc, argv, &testconfig, 0));
b64eb60f
MW
368}
369
370/*----- That's all, folks -------------------------------------------------*/