Revise generation of "END" messages in dejagnu.h
[dejagnu.git] / dejagnu.h
blob920ff691939521013bf9cee008aa14bf3ce7d665
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 <stdlib.h>
26 #include <string.h>
28 /* If you have problems with DejaGnu dropping failed, untested, or
29 * unresolved messages generated by a unit testcase, then see the section
30 * "Priority of Expect Patterns" in *note (dejagnu)Writing a test case. or
31 * use the DejaGnu built-in unit testing support in your testsuite, which
32 * has been improved to resolve this issue in DejaGnu 1.6.3. */
34 static struct {
35 int pass;
36 int fail;
37 int xpass;
38 int xfail;
39 int untested;
40 int unresolved;
41 int unsupported;
42 /**/
43 int endmsg_registered;
44 int TestState_count; /* number of live TestState objects in C++ */
45 } DG__status = { 0 };
47 static inline void
48 DG__endmsg (void)
49 { puts ("\tEND: done"); }
51 static inline void
52 DG__init (void)
54 if (DG__status.endmsg_registered) return;
56 if (atexit (DG__endmsg) == 0)
57 DG__status.endmsg_registered = 1;
60 static inline void
61 pass (const char* fmt, ...)
63 va_list ap;
65 DG__status.pass++;
66 DG__init ();
68 flockfile (stdout);
69 fputs ("\tPASSED: ", stdout);
70 va_start (ap, fmt); vfprintf (stdout, fmt, ap); va_end (ap);
71 fputc ('\n', stdout);
72 funlockfile (stdout);
75 static inline void
76 xpass (const char* fmt, ...)
78 va_list ap;
80 DG__status.xpass++;
81 DG__init ();
83 flockfile (stdout);
84 fputs ("\tXPASSED: ", stdout);
85 va_start (ap, fmt); vfprintf (stdout, fmt, ap); va_end (ap);
86 fputc ('\n', stdout);
87 funlockfile (stdout);
90 static inline void
91 fail (const char* fmt, ...)
93 va_list ap;
95 DG__status.fail++;
96 DG__init ();
98 flockfile (stdout);
99 fputs ("\tFAILED: ", stdout);
100 va_start (ap, fmt); vfprintf (stdout, fmt, ap); va_end (ap);
101 fputc ('\n', stdout);
102 funlockfile (stdout);
105 static inline void
106 xfail (const char* fmt, ...)
108 va_list ap;
110 DG__status.xfail++;
111 DG__init ();
113 flockfile (stdout);
114 fputs ("\tXFAILED: ", stdout);
115 va_start (ap, fmt); vfprintf (stdout, fmt, ap); va_end (ap);
116 fputc ('\n', stdout);
117 funlockfile (stdout);
120 static inline void
121 untested (const char* fmt, ...)
123 va_list ap;
125 DG__status.untested++;
126 DG__init ();
128 flockfile (stdout);
129 fputs ("\tUNTESTED: ", stdout);
130 va_start (ap, fmt); vfprintf (stdout, fmt, ap); va_end (ap);
131 fputc ('\n', stdout);
132 funlockfile (stdout);
135 static inline void
136 unresolved (const char* fmt, ...)
138 va_list ap;
140 DG__status.unresolved++;
141 DG__init ();
143 flockfile (stdout);
144 fputs ("\tUNRESOLVED: ", stdout);
145 va_start (ap, fmt); vfprintf (stdout, fmt, ap); va_end (ap);
146 fputc ('\n', stdout);
147 funlockfile (stdout);
150 static inline void
151 unsupported (const char* fmt, ...)
153 va_list ap;
155 DG__status.unsupported++;
156 DG__init ();
158 flockfile (stdout);
159 fputs ("\tUNSUPPORTED: ", stdout);
160 va_start (ap, fmt); vfprintf (stdout, fmt, ap); va_end (ap);
161 fputc ('\n', stdout);
162 funlockfile (stdout);
165 static inline void
166 note (const char* fmt, ...)
168 va_list ap;
170 DG__init ();
172 flockfile (stdout);
173 fputs ("\tNOTE: ", stdout);
174 va_start (ap, fmt); vfprintf (stdout, fmt, ap); va_end (ap);
175 fputc ('\n', stdout);
176 funlockfile (stdout);
179 static inline void
180 totals (void)
182 printf ("\nTotals:\n");
183 printf ("\t#passed:\t\t%d\n", DG__status.pass);
184 printf ("\t#real failed:\t\t%d\n", DG__status.fail);
185 if (DG__status.xfail)
186 printf ("\t#expected failures:\t\t%d\n", DG__status.xfail);
187 if (DG__status.xpass)
188 printf ("\t#unexpected passes:\t\t%d\n", DG__status.xpass);
189 if (DG__status.untested)
190 printf ("\t#untested:\t\t%d\n", DG__status.untested);
191 if (DG__status.unresolved)
192 printf ("\t#unresolved:\t\t%d\n", DG__status.unresolved);
193 if (DG__status.unsupported)
194 printf ("\t#unsupported:\t\t%d\n", DG__status.unsupported);
197 #ifdef __cplusplus
199 #include <iostream>
200 #include <iomanip>
201 #include <fstream>
202 #include <string>
204 const char * DG__outstate_list[] = {
205 "\tFAILED: ", "\tPASSED: ",
206 "\tUNTESTED: ", "\tUNRESOLVED: ", "\tUNSUPPORTED: ",
207 "\tXFAILED: ", "\tXPASSED: "
210 enum DG_teststate { FAILED, PASSED,
211 UNTESTED, UNRESOLVED, UNSUPPORTED,
212 XFAILED, XPASSED };
214 class TestState {
215 private:
216 DG_teststate laststate;
217 std::string lastmsg;
218 public:
219 TestState (void)
221 DG__status.pass = 0;
222 DG__status.fail = 0;
223 DG__status.xpass = 0;
224 DG__status.xfail = 0;
225 DG__status.untested = 0;
226 DG__status.unresolved = 0;
227 DG__status.unsupported = 0;
229 /* C++ object destruction will substitute for atexit(). */
230 DG__status.endmsg_registered = 1;
231 DG__status.TestState_count++;
234 ~TestState (void)
236 DG__status.TestState_count--;
238 if (DG__status.TestState_count > 0) return;
240 /* The last TestState object is being destroyed. */
241 totals ();
242 std::cout << "\tEND: done" << std::endl;
245 void testrun (bool b, std::string s)
247 if (b)
248 pass (s);
249 else
250 fail (s);
253 void pass (std::string s)
255 DG__status.pass++;
256 laststate = PASSED;
257 lastmsg = s;
258 std::cout << DG__outstate_list[PASSED] << s << std::endl;
261 void xpass (std::string s)
263 DG__status.xpass++;
264 laststate = PASSED;
265 lastmsg = s;
266 std::cout << DG__outstate_list[XPASSED] << s << std::endl;
269 void fail (std::string s)
271 DG__status.fail++;
272 laststate = FAILED;
273 lastmsg = s;
274 std::cout << DG__outstate_list[FAILED] << s << std::endl;
277 void xfail (std::string s)
279 DG__status.xfail++;
280 laststate = XFAILED;
281 lastmsg = s;
282 std::cout << DG__outstate_list[XFAILED] << s << std::endl;
285 void untested (std::string s)
287 DG__status.untested++;
288 laststate = UNTESTED;
289 lastmsg = s;
290 std::cout << DG__outstate_list[UNTESTED] << s << std::endl;
293 void unresolved (std::string s)
295 DG__status.unresolved++;
296 laststate = UNRESOLVED;
297 lastmsg = s;
298 std::cout << DG__outstate_list[UNRESOLVED] << s << std::endl;
301 void unsupported (std::string s)
303 DG__status.unsupported++;
304 laststate = UNSUPPORTED;
305 lastmsg = s;
306 std::cout << DG__outstate_list[UNSUPPORTED] << s << std::endl;
309 void note (std::string s)
311 std::cout << "\t" << "NOTE: " << s << std::endl;
314 void totals (void)
316 std::cout << std::endl << "Totals:" << std::endl;
318 std::cout << "\t#passed:\t\t"
319 << DG__status.pass << std::endl;
320 std::cout << "\t#real failed:\t\t"
321 << DG__status.fail << std::endl;
323 if (DG__status.xfail)
324 std::cout << "\t#expected failures:\t\t"
325 << DG__status.xfail << std::endl;
326 if (DG__status.xpass)
327 std::cout << "\t#unexpected passes:\t\t"
328 << DG__status.xpass << std::endl;
329 if (DG__status.untested)
330 std::cout << "\t#untested:\t\t"
331 << DG__status.untested << std::endl;
332 if (DG__status.unresolved)
333 std::cout << "\t#unresolved:\t\t"
334 << DG__status.unresolved << std::endl;
335 if (DG__status.unsupported)
336 std::cout << "\t#unsupported:\t\t"
337 << DG__status.unsupported << std::endl;
340 // This is so this class can be printed in an ostream.
341 friend std::ostream & operator << (std::ostream &os, TestState& t)
343 return os << DG__outstate_list[t.laststate] << t.lastmsg ;
346 int GetState (void) { return laststate; }
347 std::string GetMsg (void) { return lastmsg; }
350 #endif /* __cplusplus */
351 #endif /* _DEJAGNU_H_ */