Add tests for C unit test library
[dejagnu.git] / dejagnu.h
blob2b22b76c27ecea06e01d0c909fde2dc9c55f772f
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 xpassed++;
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 xfailed++;
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 (xpassed)
161 printf ("\t#unexpected passes:\t\t%d\n", xpassed);
162 if (untest)
163 printf ("\t#untested:\t\t%d\n", untest);
164 if (unresolve)
165 printf ("\t#unresolved:\t\t%d\n", unresolve);
166 printf ("\tEND: done\n");
169 #ifdef __cplusplus
171 #include <iostream>
172 #include <iomanip>
173 #include <fstream>
174 #include <string>
176 const char *outstate_list[] = {
177 "FAILED: ", "PASSED: ", "UNTESTED: ", "UNRESOLVED: ", "XFAILED: ", "XPASSED: "
180 const char ** outstate = outstate_list;
182 enum teststate { FAILED, PASSED, UNTESTED, UNRESOLVED, XFAILED, XPASSED} laststate;
184 class TestState {
185 private:
186 teststate laststate;
187 std::string lastmsg;
188 public:
189 TestState (void)
191 passed = 0;
192 failed = 0;
193 untest = 0;
194 xpassed = 0;
195 xfailed = 0;
196 unresolve = 0;
199 ~TestState (void) { totals(); }
201 void testrun (bool b, std::string s)
203 if (b)
204 pass (s);
205 else
206 fail (s);
209 void pass (std::string s)
211 passed++;
212 laststate = PASSED;
213 lastmsg = s;
214 std::cout << "\t" << outstate[PASSED] << s << std::endl;
217 void xpass (std::string s)
219 xpassed++;
220 laststate = PASSED;
221 lastmsg = s;
222 std::cout << "\t" << outstate[XPASSED] << s << std::endl;
225 void fail (std::string s)
227 failed++;
228 laststate = FAILED;
229 lastmsg = s;
230 std::cout << "\t" << outstate[FAILED] << s << std::endl;
233 void xfail (std::string s)
235 xfailed++;
236 laststate = XFAILED;
237 lastmsg = s;
238 std::cout << "\t" << outstate[XFAILED] << s << std::endl;
241 void untested (std::string s)
243 untest++;
244 laststate = UNTESTED;
245 lastmsg = s;
246 std::cout << "\t" << outstate[UNTESTED] << s << std::endl;
249 void unresolved (std::string s)
251 unresolve++;
252 laststate = UNRESOLVED;
253 lastmsg = s;
254 std::cout << "\t" << outstate[UNRESOLVED] << s << std::endl;
257 void totals (void)
259 std::cout << "\t#passed:\t\t" << passed << std::endl;
260 std::cout << "\t#real failed:\t\t" << failed << std::endl;
261 if (xfailed)
262 std::cout << "\t#expected failures:\t\t" << xfailed << std::endl;
263 if (xpassed)
264 std::cout << "\t#unexpected passes:\t\t" << xpassed << std::endl;
265 if (untest)
266 std::cout << "\t#untested:\t\t" << untest << std::endl;
267 if (unresolve)
268 std::cout << "\t#unresolved:\t\t" << unresolve << std::endl;
269 std::cout << "\tEND: done" << std::endl;
272 // This is so this class can be printed in an ostream.
273 friend std::ostream & operator << (std::ostream &os, TestState& t)
275 return os << "\t" << outstate[t.laststate] << t.lastmsg ;
278 int GetState (void) { return laststate; }
279 std::string GetMsg (void) { return lastmsg; }
282 #endif /* __cplusplus */
283 #endif /* _DEJAGNU_H_ */