Improve DejaGnu internal library tests
[dejagnu.git] / dejagnu.h
blobbd0dcc3780d1081abc51ccf8bbcea23f3b82a6c8
1 /* DejaGnu unit testing header.
2 Copyright (C) 2000-2016 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: */
30 /* #define _DEJAGNU_WAIT_ */
32 #ifdef _DEJAGNU_WAIT_
33 #include <sys/time.h>
34 #include <sys/types.h>
35 #include <unistd.h>
36 #endif
38 static int passed;
39 static int failed;
40 static int untest;
41 static int unresolve;
42 static int xfailed;
43 static int xpassed;
45 static char buffer[512];
47 void
48 wait (void)
50 #ifdef _DEJAGNU_WAIT_
51 fd_set rfds;
52 struct timeval tv;
54 FD_ZERO (&rfds);
55 tv.tv_sec = 0;
56 tv.tv_usec = 1;
58 select (0, &rfds, NULL, NULL, &tv);
59 #endif
62 static inline void
63 pass (const char* fmt, ...)
65 va_list ap;
67 passed++;
68 va_start (ap, fmt);
69 vsnprintf (buffer, sizeof (buffer), fmt, ap);
70 va_end (ap);
71 printf ("\tPASSED: %s\n", buffer);
72 wait ();
75 static inline void
76 xpass (const char* fmt, ...)
78 va_list ap;
80 passed++;
81 va_start (ap, fmt);
82 vsnprintf (buffer, sizeof (buffer), fmt, ap);
83 va_end (ap);
84 printf ("\tXPASSED: %s\n", buffer);
85 wait ();
88 static inline void
89 fail (const char* fmt, ...)
91 va_list ap;
93 failed++;
94 va_start (ap, fmt);
95 vsnprintf (buffer, sizeof (buffer), fmt, ap);
96 va_end (ap);
97 printf ("\tFAILED: %s\n", buffer);
98 wait ();
101 static inline void
102 xfail (const char* fmt, ...)
104 va_list ap;
106 failed++;
107 va_start (ap, fmt);
108 vsnprintf (buffer, sizeof (buffer), fmt, ap);
109 va_end (ap);
110 printf ("\tXFAILED: %s\n", buffer);
111 wait ();
114 static inline void
115 untested (const char* fmt, ...)
117 va_list ap;
119 untest++;
120 va_start (ap, fmt);
121 vsnprintf (buffer, sizeof (buffer), fmt, ap);
122 va_end (ap);
123 printf ("\tUNTESTED: %s\n", buffer);
124 wait ();
127 static inline void
128 unresolved (const char* fmt, ...)
130 va_list ap;
132 unresolve++;
133 va_start (ap, fmt);
134 vsnprintf (buffer, sizeof (buffer), fmt, ap);
135 va_end (ap);
136 printf ("\tUNRESOLVED: %s\n", buffer);
137 wait ();
140 static inline void
141 note (const char* fmt, ...)
143 va_list ap;
145 va_start (ap, fmt);
146 vsnprintf (buffer, sizeof (buffer), fmt, ap);
147 va_end (ap);
148 printf ("\tNOTE: %s\n", buffer);
149 wait ();
152 static inline void
153 totals (void)
155 printf ("\nTotals:\n");
156 printf ("\t#passed:\t\t%d\n", passed);
157 printf ("\t#real failed:\t\t%d\n", failed);
158 if (xfailed)
159 printf ("\t#expected failures:\t\t%d\n", xfailed);
160 if (untest)
161 printf ("\t#untested:\t\t%d\n", untest);
162 if (unresolve)
163 printf ("\t#unresolved:\t\t%d\n", unresolve);
166 #ifdef __cplusplus
168 #include <iostream>
169 #include <iomanip>
170 #include <fstream>
171 #include <string>
173 const char *outstate_list[] = {
174 "FAILED: ", "PASSED: ", "UNTESTED: ", "UNRESOLVED: ", "XFAILED: ", "XPASSED: "
177 const char ** outstate = outstate_list;
179 enum teststate { FAILED, PASSED, UNTESTED, UNRESOLVED, XFAILED, XPASSED} laststate;
181 class TestState {
182 private:
183 teststate laststate;
184 std::string lastmsg;
185 public:
186 TestState (void)
188 passed = 0;
189 failed = 0;
190 untest = 0;
191 xpassed = 0;
192 xfailed = 0;
193 unresolve = 0;
196 ~TestState (void) { totals(); }
198 void testrun (bool b, std::string s)
200 if (b)
201 pass (s);
202 else
203 fail (s);
206 void pass (std::string s)
208 passed++;
209 laststate = PASSED;
210 lastmsg = s;
211 std::cout << "\t" << outstate[PASSED] << s << std::endl;
214 void xpass (std::string s)
216 xpassed++;
217 laststate = PASSED;
218 lastmsg = s;
219 std::cout << "\t" << outstate[XPASSED] << s << std::endl;
222 void fail (std::string s)
224 failed++;
225 laststate = FAILED;
226 lastmsg = s;
227 std::cout << "\t" << outstate[FAILED] << s << std::endl;
230 void xfail (std::string s)
232 xfailed++;
233 laststate = XFAILED;
234 lastmsg = s;
235 std::cout << "\t" << outstate[XFAILED] << s << std::endl;
238 void untested (std::string s)
240 untest++;
241 laststate = UNTESTED;
242 lastmsg = s;
243 std::cout << "\t" << outstate[UNTESTED] << s << std::endl;
246 void unresolved (std::string s)
248 unresolve++;
249 laststate = UNRESOLVED;
250 lastmsg = s;
251 std::cout << "\t" << outstate[UNRESOLVED] << s << std::endl;
254 void totals (void)
256 std::cout << "\t#passed:\t\t" << passed << std::endl;
257 std::cout << "\t#real failed:\t\t" << failed << std::endl;
258 if (xfailed)
259 std::cout << "\t#expected failures:\t\t" << xfailed << std::endl;
260 if (xpassed)
261 std::cout << "\t#unexpected passes:\t\t" << xpassed << std::endl;
262 if (untest)
263 std::cout << "\t#untested:\t\t" << untest << std::endl;
264 if (unresolve)
265 std::cout << "\t#unresolved:\t\t" << unresolve << std::endl;
268 // This is so this class can be printed in an ostream.
269 friend std::ostream & operator << (std::ostream &os, TestState& t)
271 return os << "\t" << outstate[t.laststate] << t.lastmsg ;
274 int GetState (void) { return laststate; }
275 std::string GetMsg (void) { return lastmsg; }
278 #endif /* __cplusplus */
279 #endif /* _DEJAGNU_H_ */