Some more tests (minor)
[gnash.git] / testsuite / dejagnu.h
blobafdf61d6c0516e9af575f540a5300931f055c46f
1 /* DejaGnu unit testing header.
2 Copyright (C) 2000, 2001, 2002, 2004, 2006 Free Software
3 Foundation, Inc.
5 This file is part of DejaGnu.
7 DejaGnu is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 DejaGnu is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with DejaGnu; if not, write to the Free Software Foundation,
19 Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
21 #ifndef __DEJAGNU_H__
22 #define __DEJAGNU_H__
24 #include <stdio.h>
25 #include <stdarg.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: */
31 /* #define _DEJAGNU_WAIT_ */
33 #ifdef _DEJAGNU_WAIT_
34 #include <sys/time.h>
35 #include <sys/types.h>
36 #include <unistd.h>
37 #endif
39 static int passed;
40 static int failed;
41 static int untest;
42 static int unresolve;
43 static int xfailed;
44 static int xpassed;
46 static char buffer[512];
48 void
49 wait (void)
51 #ifdef _DEJAGNU_WAIT_
52 fd_set rfds;
53 struct timeval tv;
55 FD_ZERO (&rfds);
56 tv.tv_sec = 0;
57 tv.tv_usec = 1;
59 select (0, &rfds, NULL, NULL, &tv);
60 #endif
63 inline void
64 pass (const char* fmt, ...)
66 va_list ap;
68 passed++;
69 va_start (ap, fmt);
70 vsnprintf (buffer, sizeof (buffer), fmt, ap);
71 va_end (ap);
72 printf ("\tPASSED: %s\n", buffer);
73 wait ();
76 inline void
77 xpass (const char* fmt, ...)
79 va_list ap;
81 passed++;
82 va_start (ap, fmt);
83 vsnprintf (buffer, sizeof (buffer), fmt, ap);
84 va_end (ap);
85 printf ("\tXPASSED: %s\n", buffer);
86 wait ();
89 inline void
90 fail (const char* fmt, ...)
92 va_list ap;
94 failed++;
95 va_start (ap, fmt);
96 vsnprintf (buffer, sizeof (buffer), fmt, ap);
97 va_end (ap);
98 printf ("\tFAILED: %s\n", buffer);
99 wait ();
102 inline void
103 xfail (const char* fmt, ...)
105 va_list ap;
107 failed++;
108 va_start (ap, fmt);
109 vsnprintf (buffer, sizeof (buffer), fmt, ap);
110 va_end (ap);
111 printf ("\tXFAILED: %s\n", buffer);
112 wait ();
115 inline void
116 untested (const char* fmt, ...)
118 va_list ap;
120 untest++;
121 va_start (ap, fmt);
122 vsnprintf (buffer, sizeof (buffer), fmt, ap);
123 va_end (ap);
124 printf ("\tUNTESTED: %s\n", buffer);
125 wait ();
128 inline void
129 unresolved (const char* fmt, ...)
131 va_list ap;
133 unresolve++;
134 va_start (ap, fmt);
135 vsnprintf (buffer, sizeof (buffer), fmt, ap);
136 va_end (ap);
137 printf ("\tUNRESOLVED: %s\n", buffer);
138 wait ();
141 inline void
142 note (const char* fmt, ...)
144 va_list ap;
146 va_start (ap, fmt);
147 vsnprintf (buffer, sizeof (buffer), fmt, ap);
148 va_end (ap);
149 printf ("\tNOTE: %s\n", buffer);
150 wait ();
153 inline void
154 totals (void)
156 printf ("\nTotals:\n");
157 printf ("\t#passed:\t\t%d\n", passed);
158 printf ("\t#real failed:\t\t%d\n", failed);
159 if (xfailed)
160 printf ("\t#expected failures:\t\t%d\n", xfailed);
161 if (untest)
162 printf ("\t#untested:\t\t%d\n", untest);
163 if (unresolve)
164 printf ("\t#unresolved:\t\t%d\n", unresolve);
167 #ifdef __cplusplus
169 #include <iostream>
170 #include <iomanip>
171 #include <fstream>
172 #include <string>
174 const char *outstate_list[] = {
175 "FAILED: ", "PASSED: ", "UNTESTED: ", "UNRESOLVED: ", "XFAILED: ", "XPASSED: "
178 const char ** outstate = outstate_list;
180 enum teststate { FAILED, PASSED, UNTESTED, UNRESOLVED, XFAILED, XPASSED} laststate;
182 class TestState {
183 private:
184 teststate laststate;
185 std::string lastmsg;
186 public:
187 TestState (void)
189 passed = 0;
190 failed = 0;
191 untest = 0;
192 xpassed = 0;
193 xfailed = 0;
194 unresolve = 0;
197 ~TestState (void) { totals(); }
199 void testrun (bool b, std::string s)
201 if (b)
202 pass (s);
203 else
204 fail (s);
207 void pass (std::string s)
209 passed++;
210 laststate = PASSED;
211 lastmsg = s;
212 std::cout << "\t" << outstate[PASSED] << s << std::endl;
215 void pass (const char *c)
217 std::string s = c;
218 pass (s);
221 void xpass (std::string s)
223 xpassed++;
224 laststate = PASSED;
225 lastmsg = s;
226 std::cout << "\t" << outstate[XPASSED] << s << std::endl;
229 void xpass (const char *c)
231 std::string s = c;
232 xpass (s);
235 void fail (std::string s)
237 failed++;
238 laststate = FAILED;
239 lastmsg = s;
240 std::cout << "\t" << outstate[FAILED] << s << std::endl;
243 void fail (const char *c)
245 std::string s = c;
246 fail (s);
249 void xfail (std::string s)
251 xfailed++;
252 laststate = XFAILED;
253 lastmsg = s;
254 std::cout << "\t" << outstate[XFAILED] << s << std::endl;
257 void xfail (const char *c)
259 std::string s = c;
260 xfail (s);
263 void untested (std::string s)
265 untest++;
266 laststate = UNTESTED;
267 lastmsg = s;
268 std::cout << "\t" << outstate[UNTESTED] << s << std::endl;
271 void untested (const char *c)
273 std::string s = c;
274 untested (s);
277 void unresolved (std::string s)
279 unresolve++;
280 laststate = UNRESOLVED;
281 lastmsg = s;
282 std::cout << "\t" << outstate[UNRESOLVED] << s << std::endl;
285 void unresolved (const char *c)
287 std::string s = c;
288 unresolved (s);
291 void totals (void)
293 std::cout << "\t#passed:\t\t" << passed << std::endl;
294 std::cout << "\t#real failed:\t\t" << failed << std::endl;
295 if (xfailed)
296 std::cout << "\t#expected failures:\t\t" << xfailed << std::endl;
297 if (xpassed)
298 std::cout << "\t#unexpected passes:\t\t" << xpassed << std::endl;
299 if (untest)
300 std::cout << "\t#untested:\t\t" << untest << std::endl;
301 if (unresolve)
302 std::cout << "\t#unresolved:\t\t" << unresolve << std::endl;
305 // This is so this class can be printed in an ostream.
306 friend std::ostream & operator << (std::ostream &os, TestState& t)
308 return os << "\t" << outstate[t.laststate] << t.lastmsg ;
311 int GetState (void) { return laststate; }
312 std::string GetMsg (void) { return lastmsg; }
315 #endif /* __cplusplus */
316 #endif /* _DEJAGNU_H_ */