Add global TestState object in dejagnu.h
[dejagnu.git] / dejagnu.h
blob282b072ed91b045e35e27d715902562e0cd828cc
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.TestState_count++;
223 if (DG__status.TestState_count > 1)
224 return; /* Do not clear the counters if additional TestState
225 objects are constructed. */
227 DG__status.pass = 0;
228 DG__status.fail = 0;
229 DG__status.xpass = 0;
230 DG__status.xfail = 0;
231 DG__status.untested = 0;
232 DG__status.unresolved = 0;
233 DG__status.unsupported = 0;
235 /* C++ object destruction will substitute for atexit(). */
236 DG__status.endmsg_registered = 1;
239 ~TestState (void)
241 DG__status.TestState_count--;
243 if (DG__status.TestState_count > 0) return;
245 /* The last TestState object is being destroyed. */
246 totals ();
247 std::cout << "\tEND: done" << std::endl;
250 void testrun (bool b, std::string s)
252 if (b)
253 pass (s);
254 else
255 fail (s);
258 void pass (std::string s)
260 DG__status.pass++;
261 laststate = PASSED;
262 lastmsg = s;
263 std::cout << DG__outstate_list[PASSED] << s << std::endl;
266 void xpass (std::string s)
268 DG__status.xpass++;
269 laststate = PASSED;
270 lastmsg = s;
271 std::cout << DG__outstate_list[XPASSED] << s << std::endl;
274 void fail (std::string s)
276 DG__status.fail++;
277 laststate = FAILED;
278 lastmsg = s;
279 std::cout << DG__outstate_list[FAILED] << s << std::endl;
282 void xfail (std::string s)
284 DG__status.xfail++;
285 laststate = XFAILED;
286 lastmsg = s;
287 std::cout << DG__outstate_list[XFAILED] << s << std::endl;
290 void untested (std::string s)
292 DG__status.untested++;
293 laststate = UNTESTED;
294 lastmsg = s;
295 std::cout << DG__outstate_list[UNTESTED] << s << std::endl;
298 void unresolved (std::string s)
300 DG__status.unresolved++;
301 laststate = UNRESOLVED;
302 lastmsg = s;
303 std::cout << DG__outstate_list[UNRESOLVED] << s << std::endl;
306 void unsupported (std::string s)
308 DG__status.unsupported++;
309 laststate = UNSUPPORTED;
310 lastmsg = s;
311 std::cout << DG__outstate_list[UNSUPPORTED] << s << std::endl;
314 void note (std::string s)
316 std::cout << "\t" << "NOTE: " << s << std::endl;
319 void totals (void)
321 std::cout << std::endl << "Totals:" << std::endl;
323 std::cout << "\t#passed:\t\t"
324 << DG__status.pass << std::endl;
325 std::cout << "\t#real failed:\t\t"
326 << DG__status.fail << std::endl;
328 if (DG__status.xfail)
329 std::cout << "\t#expected failures:\t\t"
330 << DG__status.xfail << std::endl;
331 if (DG__status.xpass)
332 std::cout << "\t#unexpected passes:\t\t"
333 << DG__status.xpass << std::endl;
334 if (DG__status.untested)
335 std::cout << "\t#untested:\t\t"
336 << DG__status.untested << std::endl;
337 if (DG__status.unresolved)
338 std::cout << "\t#unresolved:\t\t"
339 << DG__status.unresolved << std::endl;
340 if (DG__status.unsupported)
341 std::cout << "\t#unsupported:\t\t"
342 << DG__status.unsupported << std::endl;
345 // This is so this class can be printed in an ostream.
346 friend std::ostream & operator << (std::ostream &os, TestState& t)
348 return os << DG__outstate_list[t.laststate] << t.lastmsg ;
351 int GetState (void) { return laststate; }
352 std::string GetMsg (void) { return lastmsg; }
355 TestState DG;
357 #endif /* __cplusplus */
358 #endif /* _DEJAGNU_H_ */