1 /* DejaGnu unit testing header.
2    Copyright (C) 2000-2016 Free Software Foundation, Inc.
3 
4 This file is part of DejaGnu.
5 
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.
10 
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.
15 
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.  */
19 
20 /* Imported from 1.6.2 with modifications
21    * to avoid and unused symbol in C compilations
22    * avoid wait () clashing with system-provided routines
23    * provide a deterministic last line of output after the totals.  */
24 
25 #ifndef __DEJAGNU_H__
26 #define __DEJAGNU_H__
27 
28 #include <stdio.h>
29 #include <stdarg.h>
30 #include <string.h>
31 
32 /* If you have problems with DejaGnu dropping failed, untested, or
33  * unresolved messages generated by a unit testcase, then:  */
34 
35 /* #define _DEJAGNU_WAIT_  */
36 
37 #ifdef _DEJAGNU_WAIT_
38 #include <sys/time.h>
39 #include <sys/types.h>
40 #include <unistd.h>
41 #endif
42 
43 static int passed;
44 static int failed;
45 static int untest;
46 static int unresolve;
47 static int xfailed;
48 #ifdef __cplusplus
49 static int xpassed;
50 #endif
51 
52 static char buffer[512];
53 
54 #ifdef _DEJAGNU_WAIT_
55 void
dg_wait(void)56 dg_wait (void)
57 {
58   fd_set rfds;
59   struct timeval tv;
60 
61   FD_ZERO (&rfds);
62   tv.tv_sec = 0;
63   tv.tv_usec = 1;
64 
65   select (0, &rfds, NULL, NULL, &tv);
66 }
67 #endif
68 
69 static inline void
pass(const char * fmt,...)70 pass (const char* fmt, ...)
71 {
72   va_list ap;
73 
74   passed++;
75   va_start (ap, fmt);
76   vsnprintf (buffer, sizeof (buffer), fmt, ap);
77   va_end (ap);
78   printf ("\tPASSED: %s\n", buffer);
79 #ifdef _DEJAGNU_WAIT_
80   dg_wait ();
81 #endif
82 }
83 
84 static inline void
xpass(const char * fmt,...)85 xpass (const char* fmt, ...)
86 {
87   va_list ap;
88 
89   passed++;
90   va_start (ap, fmt);
91   vsnprintf (buffer, sizeof (buffer), fmt, ap);
92   va_end (ap);
93   printf ("\tXPASSED: %s\n", buffer);
94 #ifdef _DEJAGNU_WAIT_
95   dg_wait ();
96 #endif
97 }
98 
99 static inline void
fail(const char * fmt,...)100 fail (const char* fmt, ...)
101 {
102   va_list ap;
103 
104   failed++;
105   va_start (ap, fmt);
106   vsnprintf (buffer, sizeof (buffer), fmt, ap);
107   va_end (ap);
108   printf ("\tFAILED: %s\n", buffer);
109 #ifdef _DEJAGNU_WAIT_
110   dg_wait ();
111 #endif
112 }
113 
114 static inline void
xfail(const char * fmt,...)115 xfail (const char* fmt, ...)
116 {
117   va_list ap;
118 
119   failed++;
120   va_start (ap, fmt);
121   vsnprintf (buffer, sizeof (buffer), fmt, ap);
122   va_end (ap);
123   printf ("\tXFAILED: %s\n", buffer);
124 #ifdef _DEJAGNU_WAIT_
125   dg_wait ();
126 #endif
127 }
128 
129 static inline void
untested(const char * fmt,...)130 untested (const char* fmt, ...)
131 {
132   va_list ap;
133 
134   untest++;
135   va_start (ap, fmt);
136   vsnprintf (buffer, sizeof (buffer), fmt, ap);
137   va_end (ap);
138   printf ("\tUNTESTED: %s\n", buffer);
139 #ifdef _DEJAGNU_WAIT_
140   dg_wait ();
141 #endif
142 }
143 
144 static inline void
unresolved(const char * fmt,...)145 unresolved (const char* fmt, ...)
146 {
147   va_list ap;
148 
149   unresolve++;
150   va_start (ap, fmt);
151   vsnprintf (buffer, sizeof (buffer), fmt, ap);
152   va_end (ap);
153   printf ("\tUNRESOLVED: %s\n", buffer);
154 #ifdef _DEJAGNU_WAIT_
155   dg_wait ();
156 #endif
157 }
158 
159 static inline void
note(const char * fmt,...)160 note (const char* fmt, ...)
161 {
162   va_list ap;
163 
164   va_start (ap, fmt);
165   vsnprintf (buffer, sizeof (buffer), fmt, ap);
166   va_end (ap);
167   printf ("\tNOTE: %s\n", buffer);
168 #ifdef _DEJAGNU_WAIT_
169   dg_wait ();
170 #endif
171 }
172 
173 static inline void
totals(void)174 totals (void)
175 {
176   printf ("\nTotals:\n");
177   printf ("\t#passed:\t\t%d\n", passed);
178   printf ("\t#real failed:\t\t%d\n", failed);
179   if (xfailed)
180     printf ("\t#expected failures:\t\t%d\n", xfailed);
181   if (untest)
182     printf ("\t#untested:\t\t%d\n", untest);
183   if (unresolve)
184     printf ("\t#unresolved:\t\t%d\n", unresolve);
185   printf ("\njit-dg-harness-complete\n");
186 }
187 
188 #ifdef __cplusplus
189 
190 #include <iostream>
191 #include <iomanip>
192 #include <fstream>
193 #include <string>
194 
195 const char *outstate_list[] = {
196   "FAILED: ", "PASSED: ", "UNTESTED: ", "UNRESOLVED: ", "XFAILED: ", "XPASSED: "
197 };
198 
199 const char ** outstate = outstate_list;
200 
201 enum teststate { FAILED, PASSED, UNTESTED, UNRESOLVED, XFAILED, XPASSED} laststate;
202 
203 class TestState {
204  private:
205   teststate laststate;
206   std::string lastmsg;
207  public:
TestState(void)208   TestState (void)
209     {
210       passed = 0;
211       failed = 0;
212       untest = 0;
213       xpassed = 0;
214       xfailed = 0;
215       unresolve = 0;
216     }
217 
~TestState(void)218   ~TestState (void) { totals(); }
219 
testrun(bool b,std::string s)220   void testrun (bool b, std::string s)
221     {
222       if (b)
223 	pass (s);
224       else
225 	fail (s);
226     }
227 
pass(std::string s)228     void pass (std::string s)
229       {
230 	passed++;
231 	laststate = PASSED;
232 	lastmsg = s;
233 	std::cout << "\t" << outstate[PASSED] << s << std::endl;
234       }
235 
pass(const char * c)236     void pass (const char *c)
237       {
238 	std::string s = c;
239 	pass (s);
240       }
241 
xpass(std::string s)242     void xpass (std::string s)
243       {
244 	xpassed++;
245 	laststate = PASSED;
246 	lastmsg = s;
247 	std::cout << "\t" << outstate[XPASSED] << s << std::endl;
248       }
249 
xpass(const char * c)250     void xpass (const char *c)
251       {
252 	std::string s = c;
253 	xpass (s);
254       }
255 
fail(std::string s)256     void fail (std::string s)
257       {
258 	failed++;
259 	laststate = FAILED;
260 	lastmsg = s;
261 	std::cout << "\t" << outstate[FAILED] << s << std::endl;
262       }
263 
fail(const char * c)264     void fail (const char *c)
265       {
266 	std::string s = c;
267 	fail (s);
268       }
269 
xfail(std::string s)270     void xfail (std::string s)
271       {
272 	xfailed++;
273 	laststate = XFAILED;
274 	lastmsg = s;
275 	std::cout << "\t" << outstate[XFAILED] << s << std::endl;
276       }
277 
xfail(const char * c)278     void xfail (const char *c)
279       {
280 	std::string s = c;
281 	xfail (s);
282       }
283 
untested(std::string s)284     void untested (std::string s)
285       {
286 	untest++;
287 	laststate = UNTESTED;
288 	lastmsg = s;
289 	std::cout << "\t" << outstate[UNTESTED] << s << std::endl;
290       }
291 
untested(const char * c)292     void untested (const char *c)
293       {
294 	std::string s = c;
295 	untested (s);
296       }
297 
unresolved(std::string s)298     void unresolved (std::string s)
299       {
300 	unresolve++;
301 	laststate = UNRESOLVED;
302 	lastmsg = s;
303 	std::cout << "\t" << outstate[UNRESOLVED] << s << std::endl;
304       }
305 
unresolved(const char * c)306     void unresolved (const char *c)
307       {
308 	std::string s = c;
309 	unresolved (s);
310       }
311 
totals(void)312     void totals (void)
313       {
314 	std::cout << "\t#passed:\t\t" << passed << std::endl;
315 	std::cout << "\t#real failed:\t\t" << failed << std::endl;
316 	if (xfailed)
317 	  std::cout << "\t#expected failures:\t\t" << xfailed << std::endl;
318 	if (xpassed)
319 	  std::cout << "\t#unexpected passes:\t\t" << xpassed << std::endl;
320 	if (untest)
321 	  std::cout << "\t#untested:\t\t" << untest << std::endl;
322 	if (unresolve)
323 	  std::cout << "\t#unresolved:\t\t" << unresolve << std::endl;
324         std::cout << "\njit-dg-harness-complete" << std::endl;
325       }
326 
327     // This is so this class can be printed in an ostream.
328     friend std::ostream & operator << (std::ostream &os, TestState& t)
329       {
330 	return os << "\t" << outstate[t.laststate] << t.lastmsg ;
331       }
332 
GetState(void)333     int GetState (void) { return laststate; }
GetMsg(void)334     std::string GetMsg (void) { return lastmsg; }
335 };
336 
337 #endif /* __cplusplus */
338 #endif /* _DEJAGNU_H_ */
339