Move C++ internals to prefixed names in dejagnu.h
[dejagnu.git] / dejagnu.h
blob2541464ab7866e043eab2df99f11ac6227fd845f
1 /* DejaGnu unit testing header.
2 Copyright (C) 2000-2016, 2022 Free Software Foundation, Inc.
4 This file is part of DejaGnu.
6 DejaGnu is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 DejaGnu is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with DejaGnu; if not, write to the Free Software Foundation,
18 Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
20 #ifndef __DEJAGNU_H__
21 #define __DEJAGNU_H__
23 #include <stdio.h>
24 #include <stdarg.h>
25 #include <string.h>
27 /* If you have problems with DejaGnu dropping failed, untested, or
28 * unresolved messages generated by a unit testcase, then see the section
29 * "Priority of Expect Patterns" in *note (dejagnu)Writing a test case. or
30 * use the DejaGnu built-in unit testing support in your testsuite, which
31 * has been improved to resolve this issue in DejaGnu 1.6.3. */
33 static struct {
34 int pass;
35 int fail;
36 int xpass;
37 int xfail;
38 int untested;
39 int unresolved;
40 int unsupported;
41 } DG__status = { 0 };
43 static inline void
44 pass (const char* fmt, ...)
46 va_list ap;
48 DG__status.pass++;
50 flockfile (stdout);
51 fputs ("\tPASSED: ", stdout);
52 va_start (ap, fmt); vfprintf (stdout, fmt, ap); va_end (ap);
53 fputc ('\n', stdout);
54 funlockfile (stdout);
57 static inline void
58 xpass (const char* fmt, ...)
60 va_list ap;
62 DG__status.xpass++;
64 flockfile (stdout);
65 fputs ("\tXPASSED: ", stdout);
66 va_start (ap, fmt); vfprintf (stdout, fmt, ap); va_end (ap);
67 fputc ('\n', stdout);
68 funlockfile (stdout);
71 static inline void
72 fail (const char* fmt, ...)
74 va_list ap;
76 DG__status.fail++;
78 flockfile (stdout);
79 fputs ("\tFAILED: ", stdout);
80 va_start (ap, fmt); vfprintf (stdout, fmt, ap); va_end (ap);
81 fputc ('\n', stdout);
82 funlockfile (stdout);
85 static inline void
86 xfail (const char* fmt, ...)
88 va_list ap;
90 DG__status.xfail++;
92 flockfile (stdout);
93 fputs ("\tXFAILED: ", stdout);
94 va_start (ap, fmt); vfprintf (stdout, fmt, ap); va_end (ap);
95 fputc ('\n', stdout);
96 funlockfile (stdout);
99 static inline void
100 untested (const char* fmt, ...)
102 va_list ap;
104 DG__status.untested++;
106 flockfile (stdout);
107 fputs ("\tUNTESTED: ", stdout);
108 va_start (ap, fmt); vfprintf (stdout, fmt, ap); va_end (ap);
109 fputc ('\n', stdout);
110 funlockfile (stdout);
113 static inline void
114 unresolved (const char* fmt, ...)
116 va_list ap;
118 DG__status.unresolved++;
120 flockfile (stdout);
121 fputs ("\tUNRESOLVED: ", stdout);
122 va_start (ap, fmt); vfprintf (stdout, fmt, ap); va_end (ap);
123 fputc ('\n', stdout);
124 funlockfile (stdout);
127 static inline void
128 unsupported (const char* fmt, ...)
130 va_list ap;
132 DG__status.unsupported++;
134 flockfile (stdout);
135 fputs ("\tUNSUPPORTED: ", stdout);
136 va_start (ap, fmt); vfprintf (stdout, fmt, ap); va_end (ap);
137 fputc ('\n', stdout);
138 funlockfile (stdout);
141 static inline void
142 note (const char* fmt, ...)
144 va_list ap;
146 flockfile (stdout);
147 fputs ("\tNOTE: ", stdout);
148 va_start (ap, fmt); vfprintf (stdout, fmt, ap); va_end (ap);
149 fputc ('\n', stdout);
150 funlockfile (stdout);
153 static inline void
154 totals (void)
156 printf ("\nTotals:\n");
157 printf ("\t#passed:\t\t%d\n", DG__status.pass);
158 printf ("\t#real failed:\t\t%d\n", DG__status.fail);
159 if (DG__status.xfail)
160 printf ("\t#expected failures:\t\t%d\n", DG__status.xfail);
161 if (DG__status.xpass)
162 printf ("\t#unexpected passes:\t\t%d\n", DG__status.xpass);
163 if (DG__status.untested)
164 printf ("\t#untested:\t\t%d\n", DG__status.untested);
165 if (DG__status.unresolved)
166 printf ("\t#unresolved:\t\t%d\n", DG__status.unresolved);
167 if (DG__status.unsupported)
168 printf ("\t#unsupported:\t\t%d\n", DG__status.unsupported);
169 printf ("\tEND: done\n");
172 #ifdef __cplusplus
174 #include <iostream>
175 #include <iomanip>
176 #include <fstream>
177 #include <string>
179 const char * DG__outstate_list[] = {
180 "\tFAILED: ", "\tPASSED: ",
181 "\tUNTESTED: ", "\tUNRESOLVED: ", "\tUNSUPPORTED: ",
182 "\tXFAILED: ", "\tXPASSED: "
185 enum DG_teststate { FAILED, PASSED,
186 UNTESTED, UNRESOLVED, UNSUPPORTED,
187 XFAILED, XPASSED };
189 class TestState {
190 private:
191 DG_teststate laststate;
192 std::string lastmsg;
193 public:
194 TestState (void)
196 DG__status.pass = 0;
197 DG__status.fail = 0;
198 DG__status.xpass = 0;
199 DG__status.xfail = 0;
200 DG__status.untested = 0;
201 DG__status.unresolved = 0;
202 DG__status.unsupported = 0;
205 ~TestState (void) { totals(); }
207 void testrun (bool b, std::string s)
209 if (b)
210 pass (s);
211 else
212 fail (s);
215 void pass (std::string s)
217 DG__status.pass++;
218 laststate = PASSED;
219 lastmsg = s;
220 std::cout << DG__outstate_list[PASSED] << s << std::endl;
223 void xpass (std::string s)
225 DG__status.xpass++;
226 laststate = PASSED;
227 lastmsg = s;
228 std::cout << DG__outstate_list[XPASSED] << s << std::endl;
231 void fail (std::string s)
233 DG__status.fail++;
234 laststate = FAILED;
235 lastmsg = s;
236 std::cout << DG__outstate_list[FAILED] << s << std::endl;
239 void xfail (std::string s)
241 DG__status.xfail++;
242 laststate = XFAILED;
243 lastmsg = s;
244 std::cout << DG__outstate_list[XFAILED] << s << std::endl;
247 void untested (std::string s)
249 DG__status.untested++;
250 laststate = UNTESTED;
251 lastmsg = s;
252 std::cout << DG__outstate_list[UNTESTED] << s << std::endl;
255 void unresolved (std::string s)
257 DG__status.unresolved++;
258 laststate = UNRESOLVED;
259 lastmsg = s;
260 std::cout << DG__outstate_list[UNRESOLVED] << s << std::endl;
263 void unsupported (std::string s)
265 DG__status.unsupported++;
266 laststate = UNSUPPORTED;
267 lastmsg = s;
268 std::cout << DG__outstate_list[UNSUPPORTED] << s << std::endl;
271 void note (std::string s)
273 std::cout << "\t" << "NOTE: " << s << std::endl;
276 void totals (void)
278 std::cout << std::endl << "Totals:" << std::endl;
280 std::cout << "\t#passed:\t\t"
281 << DG__status.pass << std::endl;
282 std::cout << "\t#real failed:\t\t"
283 << DG__status.fail << std::endl;
285 if (DG__status.xfail)
286 std::cout << "\t#expected failures:\t\t"
287 << DG__status.xfail << std::endl;
288 if (DG__status.xpass)
289 std::cout << "\t#unexpected passes:\t\t"
290 << DG__status.xpass << std::endl;
291 if (DG__status.untested)
292 std::cout << "\t#untested:\t\t"
293 << DG__status.untested << std::endl;
294 if (DG__status.unresolved)
295 std::cout << "\t#unresolved:\t\t"
296 << DG__status.unresolved << std::endl;
297 if (DG__status.unsupported)
298 std::cout << "\t#unsupported:\t\t"
299 << DG__status.unsupported << std::endl;
301 std::cout << "\tEND: done" << std::endl;
304 // This is so this class can be printed in an ostream.
305 friend std::ostream & operator << (std::ostream &os, TestState& t)
307 return os << DG__outstate_list[t.laststate] << t.lastmsg ;
310 int GetState (void) { return laststate; }
311 std::string GetMsg (void) { return lastmsg; }
314 #endif /* __cplusplus */
315 #endif /* _DEJAGNU_H_ */