chiark / gitweb /
Initial sketch.
[finally] / finally-test.c
1 /* -*-c-*-
2  *
3  * Test program for using `finally.h'
4  *
5  * (c) 2023 Mark Wooding
6  */
7
8 /*----- Licensing notice --------------------------------------------------*
9  *
10  * This file is part of the `Finally' package.
11  *
12  * Finally is free software: you can redistribute it and/or modify it
13  * under the terms of the GNU Library General Public License as published
14  * by the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * Finally 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 Finally.  If not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
25  * USA.
26  */
27
28 /* Attention!
29  *
30  * This file is also compiled as C++, so we must be careful to keep it in the
31  * common subset of C and C++.  Anything language-specific needs to be split
32  * off into its own separate source file.
33  */
34
35 /*----- Header files ------------------------------------------------------*/
36
37 #include <stdio.h>
38
39 #include "finally.h"
40 #include "finally-test.h"
41
42 /*----- Main code ---------------------------------------------------------*/
43
44 static int test_softball(void)
45 {
46   FINALLY({ STEP(1); });
47
48   STEP(0);
49   return (2);
50 }
51
52 static int test_ordering(void)
53 {
54   FINALLY({ STEP(2); });
55   FINALLY({ STEP(1); });
56
57   STEP(0);
58
59   return (3);
60 }
61
62 static int test_capture(void)
63 {
64   int n = -1; FINALLY({ STEP(n); });
65
66   STEP(0);
67   n = 1; return (2);
68 }
69
70 static int test_internal_block(void)
71 {
72   int i;
73   FINALLY({ STEP(10); });
74
75   for (i = 0; i < 5; i++) {
76     FINALLY({ STEP(2*i + 1); });
77     STEP(2*i);
78   }
79   return (11);
80 }
81
82 static int test_local_xfer(void)
83 {
84   int i, j;
85
86   STEP(0);
87
88   { FINALLY({ STEP(2); }); STEP(1); }
89
90   do {
91     FINALLY({ STEP(4); });
92     STEP(3);
93     if (secretly_true) break;
94     MISSTEP;
95   } while (0);
96
97   for (i = 0; i < 5; i++) {
98     FINALLY({
99       if (i == 3) STEP(46);
100       else STEP(12*i + 16);
101     });
102     STEP(12*i + 5);
103     for (j = 0; j < 5; j++) {
104       FINALLY({ STEP(12*i + 2*j + 7); });
105       if (i == 3 && j == 1) { STEP(44); goto escape; }
106       else if (j != 3) STEP(12*i + 2*j + 6);
107       else { FINALLY({ STEP(12*i + 2*j + 6); }); continue; }
108     }
109   }
110 escape:
111   STEP(47);
112
113   return (48);
114 }
115
116 #if defined(HAVE_FEXCEPTIONS)
117 void try_catch_filling(unsigned f)
118 {
119   int outstep = f&TCF_THROW ? 12 : 5; FINALLY({ STEP(outstep); });
120
121   if (f&TCF_THROW) STEP(10);
122   else STEP(2);
123
124   try_catch_inner(f);
125   STEP(4);
126 }
127
128 static int test_try_catch(void)
129 {
130   STEP(0);
131   try_catch_outer(0);
132   STEP(8);
133   try_catch_outer(TCF_THROW);
134   return (15);
135 }
136 #endif
137
138 int main(void)
139 {
140   init_test();
141
142 #define RUNTEST(name)                                                   \
143         do { begin_test(#name); STEP(test_##name()); end_test(); } while (0)
144 #define SKIPTEST(name, excuse) skip_test(#name, excuse)
145
146   RUNTEST(softball);
147   RUNTEST(ordering);
148   RUNTEST(local_xfer);
149   RUNTEST(capture);
150   RUNTEST(internal_block);
151 #if defined(HAVE_FEXCEPTIONS)
152   RUNTEST(try_catch);
153 #else
154   SKIPTEST(try_catch, "no C++ compiler or no exception-handling support");
155 #endif
156
157 #undef RUNTEST
158 #undef SKIPTEST
159
160   return (test_report());
161 }
162
163 /*----- That's all, folks -------------------------------------------------*/