Move unit test summary counters into global struct in dejagnu.h
[dejagnu.git] / dejagnu.h
blob03f24d02ffab889ea37a202a284c62501913820b
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 char buffer[512];
45 static inline void
46 pass (const char* fmt, ...)
48 va_list ap;
50 DG__status.pass++;
51 va_start (ap, fmt);
52 vsnprintf (buffer, sizeof (buffer), fmt, ap);
53 va_end (ap);
54 printf ("\tPASSED: %s\n", buffer);
57 static inline void
58 xpass (const char* fmt, ...)
60 va_list ap;
62 DG__status.xpass++;
63 va_start (ap, fmt);
64 vsnprintf (buffer, sizeof (buffer), fmt, ap);
65 va_end (ap);
66 printf ("\tXPASSED: %s\n", buffer);
69 static inline void
70 fail (const char* fmt, ...)
72 va_list ap;
74 DG__status.fail++;
75 va_start (ap, fmt);
76 vsnprintf (buffer, sizeof (buffer), fmt, ap);
77 va_end (ap);
78 printf ("\tFAILED: %s\n", buffer);
81 static inline void
82 xfail (const char* fmt, ...)
84 va_list ap;
86 DG__status.xfail++;
87 va_start (ap, fmt);
88 vsnprintf (buffer, sizeof (buffer), fmt, ap);
89 va_end (ap);
90 printf ("\tXFAILED: %s\n", buffer);
93 static inline void
94 untested (const char* fmt, ...)
96 va_list ap;
98 DG__status.untested++;
99 va_start (ap, fmt);
100 vsnprintf (buffer, sizeof (buffer), fmt, ap);
101 va_end (ap);
102 printf ("\tUNTESTED: %s\n", buffer);
105 static inline void
106 unresolved (const char* fmt, ...)
108 va_list ap;
110 DG__status.unresolved++;
111 va_start (ap, fmt);
112 vsnprintf (buffer, sizeof (buffer), fmt, ap);
113 va_end (ap);
114 printf ("\tUNRESOLVED: %s\n", buffer);
117 static inline void
118 unsupported (const char* fmt, ...)
120 va_list ap;
122 DG__status.unsupported++;
123 va_start (ap, fmt);
124 vsnprintf (buffer, sizeof (buffer), fmt, ap);
125 va_end (ap);
126 printf ("\tUNSUPPORTED: %s\n", buffer);
129 static inline void
130 note (const char* fmt, ...)
132 va_list ap;
134 va_start (ap, fmt);
135 vsnprintf (buffer, sizeof (buffer), fmt, ap);
136 va_end (ap);
137 printf ("\tNOTE: %s\n", buffer);
140 static inline void
141 totals (void)
143 printf ("\nTotals:\n");
144 printf ("\t#passed:\t\t%d\n", DG__status.pass);
145 printf ("\t#real failed:\t\t%d\n", DG__status.fail);
146 if (DG__status.xfail)
147 printf ("\t#expected failures:\t\t%d\n", DG__status.xfail);
148 if (DG__status.xpass)
149 printf ("\t#unexpected passes:\t\t%d\n", DG__status.xpass);
150 if (DG__status.untested)
151 printf ("\t#untested:\t\t%d\n", DG__status.untested);
152 if (DG__status.unresolved)
153 printf ("\t#unresolved:\t\t%d\n", DG__status.unresolved);
154 if (DG__status.unsupported)
155 printf ("\t#unsupported:\t\t%d\n", DG__status.unsupported);
156 printf ("\tEND: done\n");
159 #ifdef __cplusplus
161 #include <iostream>
162 #include <iomanip>
163 #include <fstream>
164 #include <string>
166 const char *outstate_list[] = {
167 "FAILED: ", "PASSED: ",
168 "UNTESTED: ", "UNRESOLVED: ", "UNSUPPORTED: ",
169 "XFAILED: ", "XPASSED: "
172 const char ** outstate = outstate_list;
174 enum teststate { FAILED, PASSED,
175 UNTESTED, UNRESOLVED, UNSUPPORTED,
176 XFAILED, XPASSED } laststate;
178 class TestState {
179 private:
180 teststate laststate;
181 std::string lastmsg;
182 public:
183 TestState (void)
185 DG__status.pass = 0;
186 DG__status.fail = 0;
187 DG__status.untested = 0;
188 DG__status.xpass = 0;
189 DG__status.xfail = 0;
190 DG__status.unresolved = 0;
191 DG__status.unsupported = 0;
194 ~TestState (void) { totals(); }
196 void testrun (bool b, std::string s)
198 if (b)
199 pass (s);
200 else
201 fail (s);
204 void pass (std::string s)
206 DG__status.pass++;
207 laststate = PASSED;
208 lastmsg = s;
209 std::cout << "\t" << outstate[PASSED] << s << std::endl;
212 void xpass (std::string s)
214 DG__status.xpass++;
215 laststate = PASSED;
216 lastmsg = s;
217 std::cout << "\t" << outstate[XPASSED] << s << std::endl;
220 void fail (std::string s)
222 DG__status.fail++;
223 laststate = FAILED;
224 lastmsg = s;
225 std::cout << "\t" << outstate[FAILED] << s << std::endl;
228 void xfail (std::string s)
230 DG__status.xfail++;
231 laststate = XFAILED;
232 lastmsg = s;
233 std::cout << "\t" << outstate[XFAILED] << s << std::endl;
236 void untested (std::string s)
238 DG__status.untested++;
239 laststate = UNTESTED;
240 lastmsg = s;
241 std::cout << "\t" << outstate[UNTESTED] << s << std::endl;
244 void unresolved (std::string s)
246 DG__status.unresolved++;
247 laststate = UNRESOLVED;
248 lastmsg = s;
249 std::cout << "\t" << outstate[UNRESOLVED] << s << std::endl;
252 void unsupported (std::string s)
254 DG__status.unsupported++;
255 laststate = UNSUPPORTED;
256 lastmsg = s;
257 std::cout << "\t" << outstate[UNSUPPORTED] << s << std::endl;
260 void note (std::string s)
262 std::cout << "\t" << "NOTE: " << s << std::endl;
265 void totals (void)
267 std::cout << std::endl << "Totals:" << std::endl;
269 std::cout << "\t#passed:\t\t"
270 << DG__status.pass << std::endl;
271 std::cout << "\t#real failed:\t\t"
272 << DG__status.fail << std::endl;
274 if (DG__status.xfail)
275 std::cout << "\t#expected failures:\t\t"
276 << DG__status.xfail << std::endl;
277 if (DG__status.xpass)
278 std::cout << "\t#unexpected passes:\t\t"
279 << DG__status.xpass << std::endl;
280 if (DG__status.untested)
281 std::cout << "\t#untested:\t\t"
282 << DG__status.untested << std::endl;
283 if (DG__status.unresolved)
284 std::cout << "\t#unresolved:\t\t"
285 << DG__status.unresolved << std::endl;
286 if (DG__status.unsupported)
287 std::cout << "\t#unsupported:\t\t"
288 << DG__status.unsupported << std::endl;
290 std::cout << "\tEND: done" << std::endl;
293 // This is so this class can be printed in an ostream.
294 friend std::ostream & operator << (std::ostream &os, TestState& t)
296 return os << "\t" << outstate[t.laststate] << t.lastmsg ;
299 int GetState (void) { return laststate; }
300 std::string GetMsg (void) { return lastmsg; }
303 #endif /* __cplusplus */
304 #endif /* _DEJAGNU_H_ */