Commit | Line | Data |
---|---|---|
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 | ||
31d0247c | 30 | #include "tv.h" |
b64eb60f MW |
31 | #include "tvec.h" |
32 | ||
31d0247c MW |
33 | #include <sys/select.h> |
34 | #include <sys/time.h> | |
35 | ||
b64eb60f MW |
36 | /*----- Register definitions ----------------------------------------------*/ |
37 | ||
b64eb60f MW |
38 | static const struct tvec_iassoc ienum_assocs[] = { |
39 | { "less", -1 }, | |
40 | { "equal", 0 }, | |
41 | { "greater", +1 }, | |
42 | { 0 } | |
43 | }; | |
44 | ||
45 | static const struct tvec_uassoc uenum_assocs[] = { | |
46 | { "apple", 0 }, | |
47 | { "banana", 1 }, | |
48 | { "clementine", 2 }, | |
49 | { 0 } | |
50 | }; | |
51 | ||
e63124bc MW |
52 | static const struct tvec_fassoc fenum_assocs[] = { |
53 | { "e", 2.718281828459045 }, | |
54 | { "pi", 3.141592653589793 }, | |
55 | { "tau", 6.283185307179586 }, | |
56 | { 0 } | |
57 | }; | |
58 | ||
b64eb60f MW |
59 | static const struct tvec_passoc penum_assocs[] = { |
60 | { "alice", &uenum_assocs[0] }, | |
61 | { "bob", &uenum_assocs[1] }, | |
62 | { "carol", &uenum_assocs[2] }, | |
63 | { 0 } | |
64 | }; | |
65 | ||
e63124bc | 66 | #if __STDC_VERSION__x >= 199901 |
b64eb60f MW |
67 | # define DSGINIT(x) x |
68 | #else | |
69 | # define DSGINIT(x) | |
70 | #endif | |
71 | ||
e63124bc MW |
72 | static const struct tvec_floatinfo fenum_fltinfo = |
73 | { TVFF_ABSDELTA, -10, +10, 1e-3 }; | |
74 | ||
75 | #define DEFENUM(tag, ty, slot) \ | |
76 | static const struct tvec_##slot##enuminfo slot##enum_info = \ | |
3efcfd2d | 77 | { slot##enum_NAME, slot##enum_assocs slot##enum_ARGS }; |
e63124bc MW |
78 | #define ienum_NAME "order" |
79 | #define ienum_ARGS , &tvrange_i16 | |
80 | #define uenum_NAME "fruit" | |
81 | #define uenum_ARGS , &tvrange_u16 | |
82 | #define fenum_NAME "const" | |
83 | #define fenum_ARGS , &fenum_fltinfo | |
84 | #define penum_NAME "actor" | |
85 | #define penum_ARGS | |
86 | TVEC_MISCSLOTS(DEFENUM) | |
87 | #undef DEFENUM | |
b64eb60f MW |
88 | |
89 | static const struct tvec_flag attr_flags[] = { | |
90 | { "black-fg", 0x07, 0x00 }, | |
91 | { "blue-fg", 0x07, 0x01 }, | |
92 | { "red-fg", 0x07, 0x02 }, | |
93 | { "magenta-fg", 0x07, 0x03 }, | |
94 | { "green-fg", 0x07, 0x04 }, | |
95 | { "cyan-fg", 0x07, 0x05 }, | |
96 | { "yellow-fg", 0x07, 0x06 }, | |
97 | { "white-fg", 0x07, 0x07 }, | |
98 | ||
99 | { "black-bg", 0x38, 0x00 }, | |
100 | { "blue-bg", 0x38, 0x08 }, | |
101 | { "red-bg", 0x38, 0x10 }, | |
102 | { "magenta-bg", 0x38, 0x18 }, | |
103 | { "green-bg", 0x38, 0x20 }, | |
104 | { "cyan-bg", 0x38, 0x28 }, | |
105 | { "yellow-bg", 0x38, 0x30 }, | |
106 | { "white-bg", 0x38, 0x38 }, | |
107 | ||
108 | { "normal", 0xc0, 0x00 }, | |
109 | { "bright", 0x40, 0x40 }, | |
110 | { "flash", 0x80, 0x80 }, | |
111 | ||
112 | { 0 } | |
113 | }; | |
114 | ||
115 | static const struct tvec_flaginfo attr_info = | |
116 | { "attr", attr_flags, &tvrange_u16 }; | |
117 | ||
e63124bc MW |
118 | static const struct tvec_floatinfo fltish_info = |
119 | { TVFF_RELDELTA, -1.0, +1.0, 1e-6 }; | |
120 | ||
b64eb60f MW |
121 | static const struct tvec_urange range_32 = { 0, 31 }; |
122 | ||
123 | #define TYPEREGS(_) \ | |
124 | _(int, RI, int, p, &tvrange_i16) \ | |
125 | _(uint, RU, uint, p, &tvrange_u16) \ | |
e63124bc MW |
126 | _(float, RFP, float, p, 0) \ |
127 | _(fltish, RFISH, float, p, &fltish_info) \ | |
128 | _(char, RCH, char, p, 0) \ | |
3efcfd2d MW |
129 | _(ienum, RIE, ienum, p, &ienum_info) \ |
130 | _(uenum, RUE, uenum, p, &uenum_info) \ | |
131 | _(fenum, RFE, fenum, p, &fenum_info) \ | |
132 | _(penum, RPE, penum, p, &penum_info) \ | |
b64eb60f | 133 | _(flags, RF, flags, p, &attr_info) \ |
c81c35df | 134 | _(text, RTXT, text, p, &range_32) \ |
b64eb60f | 135 | _(bytes, RBY, bytes, p, &tvrange_byte) \ |
67b5031e | 136 | _(buffer, RBUF, buffer, p, 0) |
b64eb60f | 137 | |
e63124bc MW |
138 | enum { |
139 | /* Output registers, one for each register type. */ | |
140 | #define DEFREG(name, i, ty, argslot, argval) i, | |
141 | TYPEREGS(DEFREG) | |
142 | #undef DEFREG | |
db2bf411 | 143 | NTY, |
e63124bc | 144 | |
db2bf411 MW |
145 | /* Outputs. */ |
146 | RRC = NTY, /* return code from deserialize */ | |
147 | RSEROUT, /* serialized output */ | |
e63124bc MW |
148 | |
149 | NROUT, | |
150 | ||
db2bf411 MW |
151 | /* Alternative outputs. */ |
152 | RVOUT = 0, /* output/copy value */ | |
153 | RLEFT, /* size data remaining in input */ | |
e63124bc | 154 | |
db2bf411 MW |
155 | /* Additional inputs. */ |
156 | RSAB = NROUT, /* which register to sabotage */ | |
157 | RV, /* input value */ | |
158 | RSER, /* serialized input */ | |
e63124bc | 159 | |
db2bf411 | 160 | NREG |
e63124bc MW |
161 | }; |
162 | ||
db2bf411 | 163 | /*----- Common execution environment --------------------------------------*/ |
b64eb60f MW |
164 | |
165 | struct test_context { | |
166 | struct tvec_state *tv; | |
db2bf411 MW |
167 | unsigned f; |
168 | #define SF_SHOW 1u | |
b64eb60f MW |
169 | }; |
170 | ||
c91413e6 | 171 | static void common_setup(struct tvec_state *tv, |
e63124bc | 172 | const struct tvec_env *env, void *pctx, void *ctx) |
db2bf411 MW |
173 | { |
174 | struct test_context *tctx = ctx; | |
175 | ||
176 | tctx->tv = tv; | |
177 | tctx->f = 0; | |
db2bf411 MW |
178 | } |
179 | ||
814e42ff MW |
180 | static int common_setvar(struct tvec_state *tv, const char *var, |
181 | const union tvec_regval *rv, void *ctx) | |
db2bf411 MW |
182 | { |
183 | struct test_context *tctx = ctx; | |
814e42ff MW |
184 | |
185 | if (STRCMP(var, ==, "@show")) { | |
186 | if (rv->i) tctx->f |= SF_SHOW; | |
187 | } else assert(!"unknown var"); | |
188 | return (0); | |
189 | } | |
190 | ||
191 | static const struct tvec_vardef show_var = | |
192 | { sizeof(struct tvec_reg), common_setvar, | |
193 | { "@show", -1, &tvty_ienum, 0, { &tvenum_bool } } }; | |
194 | ||
195 | static const struct tvec_vardef *common_findvar | |
196 | (struct tvec_state *tv, const char *var, void **ctx_out, void *ctx) | |
197 | { | |
198 | if (STRCMP(var, ==, "@show")) { *ctx_out = ctx; return (&show_var); } | |
199 | return (0); | |
db2bf411 MW |
200 | } |
201 | ||
202 | static void common_run(struct tvec_state *tv, tvec_testfn *fn, void *ctx) | |
203 | { | |
204 | struct test_context *tctx = ctx; | |
205 | unsigned f = tctx->f; | |
206 | ||
207 | fn(tv->in, tv->out, tctx); | |
208 | if (tvec_checkregs(tv)) { tvec_fail(tv, 0); f |= SF_SHOW; } | |
209 | if (f&SF_SHOW) tvec_mismatch(tv, TVMF_IN | TVMF_OUT); | |
210 | } | |
211 | ||
212 | static void common_after(struct tvec_state *tv, void *ctx) | |
213 | { struct test_context *tctx = ctx; tctx->f = 0; } | |
b64eb60f | 214 | |
db2bf411 MW |
215 | static const struct tvec_env common_testenv = { |
216 | sizeof(struct test_context), | |
814e42ff | 217 | common_setup, common_findvar, |
db2bf411 MW |
218 | 0, common_run, common_after, |
219 | 0 | |
220 | }; | |
221 | ||
222 | /*----- Single-type copy tests --------------------------------------------*/ | |
223 | ||
224 | static void test_copy_simple | |
225 | (const struct tvec_reg *in, struct tvec_reg *out, void *ctx) | |
226 | { out->v = in->v; } | |
227 | ||
c81c35df | 228 | static void test_copy_text |
db2bf411 MW |
229 | (const struct tvec_reg *in, struct tvec_reg *out, void *ctx) |
230 | { | |
c81c35df MW |
231 | tvec_alloctext(&out->v, in->v.text.sz); |
232 | memcpy(out->v.text.p, in->v.text.p, in->v.text.sz); | |
db2bf411 MW |
233 | } |
234 | ||
235 | static void test_copy_bytes | |
236 | (const struct tvec_reg *in, struct tvec_reg *out, void *ctx) | |
237 | { | |
c81c35df MW |
238 | tvec_allocbytes(&out->v, in->v.bytes.sz); |
239 | memcpy(out->v.bytes.p, in->v.bytes.p, in->v.bytes.sz); | |
db2bf411 MW |
240 | } |
241 | ||
242 | #define test_copy_int test_copy_simple | |
243 | #define test_copy_uint test_copy_simple | |
244 | #define test_copy_ienum test_copy_simple | |
245 | #define test_copy_uenum test_copy_simple | |
246 | #define test_copy_fenum test_copy_simple | |
247 | #define test_copy_penum test_copy_simple | |
248 | #define test_copy_char test_copy_simple | |
249 | #define test_copy_flags test_copy_simple | |
250 | #define test_copy_float test_copy_simple | |
251 | #define test_copy_fltish test_copy_simple | |
252 | #define test_copy_buffer test_copy_bytes | |
253 | ||
254 | #define COPYREG(name, i, ty, argslot, argval) \ | |
255 | static DSGINIT(const) struct tvec_regdef name##_copyregs[] = { \ | |
256 | { #name, RVOUT, &tvty_##ty, 0, DSGINIT({ .argslot = argval }) }, \ | |
257 | { 0 } \ | |
258 | }; | |
259 | TYPEREGS(COPYREG) | |
260 | #undef COPYREG | |
261 | ||
262 | /*----- Single-type serialization tests -----------------------------------*/ | |
263 | ||
264 | static void setup_regdef(struct tvec_regdef *rd, unsigned i, | |
265 | struct tvec_state *tv) | |
266 | { | |
267 | const struct tvec_regdef *r; | |
268 | ||
269 | for (r = tv->test->regs; r->name; r++) if (r->i == i) goto found; | |
270 | tvec_error(tv, "internel: register definition not found"); exit(2); | |
271 | found: | |
272 | rd[0] = *r; rd[1].name = 0; | |
273 | } | |
274 | ||
275 | static void test_single_serialize | |
276 | (const struct tvec_reg *in, struct tvec_reg *out, void *ctx) | |
277 | { | |
278 | struct test_context *tctx = ctx; | |
279 | struct tvec_state *tv = tctx->tv; | |
280 | struct tvec_regdef rd[2]; | |
281 | dbuf b = DBUF_INIT; | |
282 | int rc; | |
283 | ||
284 | setup_regdef(rd, RV, tv); | |
285 | rc = tvec_serialize(tv->in, DBUF_BUF(&b), rd, NREG, | |
286 | sizeof(struct tvec_reg)); | |
287 | out[RRC].v.i = rc; | |
288 | if (rc) | |
289 | out[RSEROUT].f &= ~TVRF_LIVE; | |
290 | else { | |
291 | tvec_allocbytes(&out[RSEROUT].v, BLEN(DBUF_BUF(&b))); | |
292 | memcpy(out[RSEROUT].v.bytes.p, BBASE(DBUF_BUF(&b)), BLEN(DBUF_BUF(&b))); | |
293 | } | |
294 | } | |
295 | ||
296 | static void test_single_deserialize | |
297 | (const struct tvec_reg *in, struct tvec_reg *out, void *ctx) | |
298 | { | |
299 | struct test_context *tctx = ctx; | |
300 | struct tvec_state *tv = tctx->tv; | |
301 | struct tvec_regdef rd[2]; | |
302 | buf b; | |
303 | int rc; | |
304 | ||
305 | setup_regdef(rd, RV, tv); | |
306 | buf_init(&b, in[RSER].v.bytes.p, in[RSER].v.bytes.sz); | |
307 | rc = tvec_deserialize(tv->out, &b, rd, NREG, sizeof(struct tvec_reg)); | |
308 | out[RRC].v.i = rc; | |
309 | if (rc) out[RVOUT].f &= ~TVRF_LIVE; | |
310 | } | |
311 | ||
312 | #define SERREG(name, i, ty, argslot, argval) \ | |
313 | static DSGINIT(const) struct tvec_regdef name##_serregs[] = { \ | |
314 | { #name, RV, &tvty_##ty, 0, DSGINIT({ .argslot = argval }) }, \ | |
315 | { "buf", RSEROUT, &tvty_bytes }, \ | |
316 | { "rc", RRC, &tvty_int, TVRF_OPT, { &tvrange_int } }, \ | |
317 | TVEC_ENDREGS \ | |
318 | }; \ | |
319 | static DSGINIT(const) struct tvec_regdef name##_deserregs[] = { \ | |
320 | { "buf", RSER, &tvty_bytes }, \ | |
321 | { #name, RVOUT, &tvty_##ty, 0, DSGINIT({ .argslot = argval }) }, \ | |
322 | { "left", RLEFT, &tvty_uint, TVRF_OPT, { &tvrange_size } }, \ | |
323 | { "rc", RRC, &tvty_int, TVRF_OPT, { &tvrange_int } }, \ | |
324 | TVEC_ENDREGS \ | |
325 | }; | |
326 | TYPEREGS(SERREG) | |
327 | #undef SERREG | |
328 | ||
c91413e6 | 329 | static void before_single_serialize(struct tvec_state *tv, void *ctx) |
e63124bc | 330 | { |
b64eb60f | 331 | if (!(tv->in[RRC].f&TVRF_LIVE)) { |
db2bf411 | 332 | tv->in[RRC].v.i = 0; tv->in[RRC].f |= TVRF_LIVE; |
e63124bc | 333 | tv->out[RRC].f |= TVRF_LIVE; |
b64eb60f | 334 | } |
b64eb60f MW |
335 | } |
336 | ||
c91413e6 | 337 | static void before_single_deserialize(struct tvec_state *tv, void *ctx) |
db2bf411 MW |
338 | { |
339 | if (!(tv->in[RRC].f&TVRF_LIVE)) { | |
340 | tv->in[RRC].v.i = 0; tv->in[RRC].f |= TVRF_LIVE; | |
341 | tv->out[RRC].f |= TVRF_LIVE; | |
342 | } | |
343 | if (!(tv->in[RLEFT].f&TVRF_LIVE)) { | |
344 | tv->in[RLEFT].v.u = 0; tv->in[RLEFT].f |= TVRF_LIVE; | |
345 | tv->out[RLEFT].f |= TVRF_LIVE; | |
346 | } | |
db2bf411 MW |
347 | } |
348 | ||
349 | static const struct tvec_env single_serialize_testenv = { | |
350 | sizeof(struct test_context), | |
814e42ff | 351 | common_setup, common_findvar, |
db2bf411 MW |
352 | before_single_serialize, common_run, common_after, |
353 | 0 | |
354 | }, single_deserialize_testenv = { | |
355 | sizeof(struct test_context), | |
814e42ff | 356 | common_setup, common_findvar, |
db2bf411 MW |
357 | before_single_deserialize, common_run, common_after, |
358 | 0 | |
359 | }; | |
360 | ||
361 | /*----- Multi-type serialization test -------------------------------------*/ | |
362 | ||
363 | static void test_multi_serialize | |
b64eb60f MW |
364 | (const struct tvec_reg *in, struct tvec_reg *out, void *ctx) |
365 | { | |
366 | struct test_context *tctx = ctx; | |
367 | struct tvec_state *tv = tctx->tv; | |
368 | const struct tvec_regdef *rd; | |
369 | union tvec_regval *rv; | |
e63124bc | 370 | dbuf b = DBUF_INIT; |
b64eb60f | 371 | |
e63124bc | 372 | if (tvec_serialize(tv->in, DBUF_BUF(&b), tv->test->regs, |
db2bf411 MW |
373 | NTY, sizeof(struct tvec_reg))) |
374 | { out[RRC].v.i = -1; goto end; } | |
375 | tvec_allocbytes(&out[RSEROUT].v, DBLEN(&b)); | |
376 | memcpy(out[RSEROUT].v.bytes.p, DBBASE(&b), DBLEN(&b)); | |
377 | out[RSEROUT].f |= TVRF_LIVE; | |
e63124bc | 378 | buf_flip(DBUF_BUF(&b)); |
b64eb60f | 379 | |
e63124bc | 380 | if (tvec_deserialize(tv->out, DBUF_BUF(&b), tv->test->regs, |
db2bf411 | 381 | NTY, sizeof(struct tvec_reg))) |
e63124bc MW |
382 | { out[RRC].v.i = -2; goto end; } |
383 | if (BLEFT(&b._b)) | |
384 | { out[RRC].v.i = -3; goto end; } | |
b64eb60f | 385 | |
3efcfd2d MW |
386 | if ((in[RSAB].f&TVRF_LIVE) && in[RSAB].v.i >= 0) { |
387 | rd = &tv->test->regs[in[RSAB].v.i]; rv = &out[in[RSAB].v.i].v; | |
388 | if (rd->ty == &tvty_int || rd->ty == &tvty_ienum) | |
389 | rv->i ^= 1; | |
390 | else if (rd->ty == &tvty_uint || | |
391 | rd->ty == &tvty_flags || rd->ty == &tvty_uenum) | |
392 | rv->u ^= 1; | |
393 | else if (rd->ty == &tvty_float || rd->ty == &tvty_fenum) { | |
394 | if (rv->f == rv->f) rv->f = -rv->f; | |
395 | else rv->f = 1.0; | |
396 | } else if (rd->ty == &tvty_penum) | |
397 | rv->p = rv->p | |
398 | ? 0 | |
399 | : (/*unconst*/ void *) | |
400 | ((const struct tvec_penuminfo *)rd->arg.p)->av[0].p; | |
c81c35df MW |
401 | else if (rd->ty == &tvty_text) |
402 | { if (rv->text.sz) rv->text.p[0] ^= 1; } | |
3efcfd2d MW |
403 | else if (rd->ty == &tvty_bytes) |
404 | { if (rv->bytes.sz) rv->bytes.p[0] ^= 1; } | |
b64eb60f MW |
405 | } |
406 | ||
407 | out[RRC].v.i = 0; | |
e63124bc MW |
408 | end: |
409 | dbuf_destroy(&b); | |
b64eb60f MW |
410 | } |
411 | ||
db2bf411 | 412 | static const struct tvec_iassoc reg_assocs[] = { |
3efcfd2d MW |
413 | { "none", -1 }, |
414 | #define DEFASSOC(name, i, ty, argslot, argval) { #name, i }, | |
415 | TYPEREGS(DEFASSOC) | |
416 | #undef DEFASSOC | |
417 | { 0, 0 } | |
418 | }; | |
db2bf411 | 419 | static const struct tvec_ienuminfo reg_enum = { "reg", reg_assocs, 0 }; |
3efcfd2d | 420 | |
db2bf411 | 421 | static DSGINIT(const) struct tvec_regdef multi_serialize_regs[] = { |
b64eb60f MW |
422 | #define DEFREG(name, i, ty, argslot, argval) \ |
423 | { #name, i, &tvty_##ty, TVRF_OPT, \ | |
424 | DSGINIT({ .argslot = argval }) }, | |
425 | TYPEREGS(DEFREG) | |
426 | #undef DEFREG | |
db2bf411 | 427 | |
b64eb60f | 428 | { "rc", RRC, &tvty_int, TVRF_OPT, { &tvrange_int } }, |
db2bf411 MW |
429 | { "serialized", RSEROUT, &tvty_bytes, TVRF_OPT }, |
430 | { "sabotage", RSAB, &tvty_ienum, TVRF_OPT, { ®_enum } }, | |
b64eb60f | 431 | |
db2bf411 | 432 | TVEC_ENDREGS |
b64eb60f MW |
433 | }; |
434 | ||
c91413e6 | 435 | static void before_multi_serialize(struct tvec_state *tv, void *ctx) |
b64eb60f | 436 | { |
db2bf411 MW |
437 | if (!(tv->in[RRC].f&TVRF_LIVE)) { |
438 | tv->in[RRC].v.i = 0; tv->in[RRC].f |= TVRF_LIVE; | |
439 | tv->out[RRC].f |= TVRF_LIVE; | |
440 | } | |
b64eb60f MW |
441 | } |
442 | ||
db2bf411 MW |
443 | static const struct tvec_env multi_serialize_testenv = { |
444 | sizeof(struct test_context), | |
814e42ff | 445 | common_setup, common_findvar, |
db2bf411 MW |
446 | before_multi_serialize, common_run, common_after, |
447 | 0 | |
e63124bc MW |
448 | }; |
449 | ||
c91413e6 MW |
450 | /*----- Crash test --------------------------------------------------------*/ |
451 | ||
31d0247c MW |
452 | static void test_crash(const struct tvec_reg *in, struct tvec_reg *out, |
453 | void *ctx) | |
c91413e6 MW |
454 | { |
455 | out[RVOUT].v.u = in[RV].v.u; | |
456 | if (in[RSAB].v.i) abort(); | |
457 | } | |
458 | ||
459 | static const struct tvec_remotefork crash_testenv = | |
460 | { TVEC_REMOTEFORK(0, 0) }; | |
461 | ||
462 | static const struct tvec_regdef crash_regs[] = { | |
463 | { "crash", RSAB, &tvty_ienum, 0, { &tvenum_bool } }, | |
464 | { "x", RV, &tvty_uint, 0, { &tvrange_uint } }, | |
465 | { "z", RVOUT, &tvty_uint, 0, { &tvrange_uint } }, | |
466 | TVEC_ENDREGS | |
467 | }; | |
468 | ||
31d0247c MW |
469 | /*----- Sleep test --------------------------------------------------------*/ |
470 | ||
471 | static void test_sleep(const struct tvec_reg *in, struct tvec_reg *out, | |
472 | void *ctx) | |
473 | { | |
474 | struct timeval now, when, tv; | |
475 | int rc; | |
476 | ||
477 | rc = gettimeofday(&now, 0); assert(!rc); | |
478 | tv.tv_sec = in[RV].v.f; tv.tv_usec = 1e6*(in[RV].v.f - tv.tv_sec); | |
479 | ||
480 | TV_ADD(&when, &now, &tv); | |
481 | for (;;) { | |
482 | rc = select(0, 0, 0, 0, &tv); assert(!rc); | |
483 | rc = gettimeofday(&now, 0); assert(!rc); | |
484 | if (TV_CMP(&now, >=, &when)) break; | |
485 | TV_SUB(&tv, &when, &now); | |
486 | } | |
487 | out[RVOUT].v.f = in[RV].v.f; | |
488 | } | |
489 | ||
490 | static const struct tvec_timeoutenv sleep_subenv = | |
491 | { TVEC_TIMEOUTINIT(ITIMER_REAL, 0.25) }; | |
492 | static const struct tvec_remotefork sleep_testenv = | |
493 | { TVEC_REMOTEFORK(&sleep_subenv._env, 0) }; | |
494 | ||
495 | static const struct tvec_regdef sleep_regs[] = { | |
496 | { "time", RV, &tvty_float, 0, { &tvflt_nonneg } }, | |
497 | { "z", RVOUT, &tvty_float, 0, { &tvflt_nonneg } }, | |
498 | TVEC_ENDREGS | |
499 | }; | |
500 | ||
b64eb60f MW |
501 | /*----- Front end ---------------------------------------------------------*/ |
502 | ||
503 | static const struct tvec_test tests[] = { | |
db2bf411 MW |
504 | { "multi", multi_serialize_regs, &multi_serialize_testenv, |
505 | test_multi_serialize }, | |
506 | ||
507 | #define DEFSINGLE(name, i, ty, argslot, argval) \ | |
508 | { "copy-" #name, name##_copyregs, &common_testenv, test_copy_##name }, \ | |
509 | { "serialize-" #name, name##_serregs, &single_serialize_testenv, \ | |
510 | test_single_serialize }, \ | |
511 | { "deserialize-" #name, name##_deserregs, &single_deserialize_testenv, \ | |
512 | test_single_deserialize }, | |
513 | TYPEREGS(DEFSINGLE) | |
514 | #undef DEFSINGLE | |
515 | ||
31d0247c MW |
516 | { "crash", crash_regs, &crash_testenv._env, test_crash }, |
517 | { "sleep", sleep_regs, &sleep_testenv._env, test_sleep }, | |
c91413e6 | 518 | |
db2bf411 | 519 | TVEC_ENDTESTS |
b64eb60f MW |
520 | }; |
521 | ||
c5e0e403 | 522 | static const struct tvec_config testconfig = { |
b64eb60f MW |
523 | tests, |
524 | NROUT, NREG, sizeof(struct tvec_reg) | |
525 | }; | |
526 | ||
527 | int main(int argc, char *argv[]) | |
528 | { | |
e63124bc | 529 | #if __STDC_VERSION__x < 199901 |
b64eb60f | 530 | # define POKE(name, i, ty, argslot, argval) \ |
db2bf411 MW |
531 | multi_serialize_regs[i].arg.argslot = argval; \ |
532 | name##_copyregs->arg.argslot = argval; \ | |
533 | name##_serregs->arg.argslot = argval; \ | |
534 | name##_deserregs->arg.argslot = argval; | |
b64eb60f MW |
535 | TYPEREGS(POKE) |
536 | # undef POKE | |
537 | #endif | |
c5e0e403 | 538 | return (tvec_main(argc, argv, &testconfig, 0)); |
b64eb60f MW |
539 | } |
540 | ||
541 | /*----- That's all, folks -------------------------------------------------*/ |