chiark / gitweb /
@@@ tty commentary
[mLib] / utils / t / exc-test.c
1 #include <stdio.h>
2
3 #include "exc.h"
4 #include "macros.h"
5 #include "report.h"
6 #include "tvec.h"
7 #include "tvec-adhoc.h"
8 #include "tvec-types.h"
9
10 static struct tvec_state tvstate;
11 static int step;
12
13 #define TESTGROUP(name)                                                 \
14         TVEC_TESTGROUP_TAG(grp, &tvstate, name)                         \
15         MC_BEFORE(init, { step = 0; })
16 #define TEST TVEC_TEST_TAG(test, &tvstate)
17 #define STEP(s) do {                                                    \
18           tvec_claim(&tvstate, s == step, __FILE__, __LINE__,           \
19                      "found %d /= expected %d", s, step);               \
20           step++;                                                       \
21         } while (0)
22 #define MISSTEP do {                                                    \
23           tvec_claim(&tvstate, 0, __FILE__, __LINE__,                   \
24                      "shouldn't have reached here");                    \
25           step++;                                                       \
26         } while (0)
27
28 static void func(void)
29 {
30   TRY { STEP(2); THROW(EXC_FAIL, "excession"); MISSTEP; }
31   CATCH switch (exc_type) {
32     case EXC_FAIL: TVEC_CLAIMEQ_TEXTZ(&tvstate, exc_s, "excession"); break;
33     default: MISSTEP; break;
34   } END_TRY;
35
36   TRY { STEP(3); THROW(EXC_ERRNO, 53); }
37   CATCH switch (exc_type) {
38     case EXC_ERRNO: RETHROW;
39     default: MISSTEP; break;
40   } END_TRY;
41   MISSTEP;
42 }
43
44 int main(int argc, char *argv[])
45 {
46   int argpos;
47
48   tvec_parseargs(argc, argv, &tvstate, &argpos, &tvec_adhocconfig);
49   if (argpos < argc) die(2, "no input files expected");
50
51   TESTGROUP("flow") {
52     STEP(0);
53     TRY { STEP(1); func(); MISSTEP; }
54     CATCH switch (exc_type) {
55       case EXC_ERRNO: STEP(4); TVEC_CLAIMEQ_INT(&tvstate, exc_i, 53); break;
56       default: MISSTEP;
57     } END_TRY;
58     STEP(5);
59   }
60
61   TESTGROUP("final")
62     TVEC_CLAIMEQ_PTR(&tvstate, __exc_list, 0);
63
64   return (tvec_end(&tvstate));
65 }