1/* DejaGnu unit testing header.
2   Copyright (C) 2000-2022 Free Software Foundation, Inc.
3
4This file is part of DejaGnu.
5
6DejaGnu is free software; you can redistribute it and/or modify it
7under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 3 of the License, or
9(at your option) any later version.
10
11DejaGnu is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with DejaGnu; if not, write to the Free Software Foundation,
18Inc., 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
43static int passed;
44static int failed;
45static int untest;
46static int unresolve;
47static int xfailed;
48#ifdef __cplusplus
49static int xpassed;
50#endif
51
52static char buffer[512];
53
54#ifdef _DEJAGNU_WAIT_
55void
56dg_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
69static inline void
70pass (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
84static inline void
85xpass (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
99static inline void
100fail (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
114static inline void
115xfail (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
129static inline void
130untested (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
144static inline void
145unresolved (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
159static inline void
160note (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
173static inline void
174totals (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
195const char *outstate_list[] = {
196  "FAILED: ", "PASSED: ", "UNTESTED: ", "UNRESOLVED: ", "XFAILED: ", "XPASSED: "
197};
198
199const char ** outstate = outstate_list;
200
201enum teststate { FAILED, PASSED, UNTESTED, UNRESOLVED, XFAILED, XPASSED} laststate;
202
203class TestState {
204 private:
205  teststate laststate;
206  std::string lastmsg;
207 public:
208  TestState (void)
209    {
210      passed = 0;
211      failed = 0;
212      untest = 0;
213      xpassed = 0;
214      xfailed = 0;
215      unresolve = 0;
216    }
217
218  ~TestState (void) { totals(); }
219
220  void testrun (bool b, std::string s)
221    {
222      if (b)
223	pass (s);
224      else
225	fail (s);
226    }
227
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
236    void pass (const char *c)
237      {
238	std::string s = c;
239	pass (s);
240      }
241
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
250    void xpass (const char *c)
251      {
252	std::string s = c;
253	xpass (s);
254      }
255
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
264    void fail (const char *c)
265      {
266	std::string s = c;
267	fail (s);
268      }
269
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
278    void xfail (const char *c)
279      {
280	std::string s = c;
281	xfail (s);
282      }
283
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
292    void untested (const char *c)
293      {
294	std::string s = c;
295	untested (s);
296      }
297
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
306    void unresolved (const char *c)
307      {
308	std::string s = c;
309	unresolved (s);
310      }
311
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
333    int GetState (void) { return laststate; }
334    std::string GetMsg (void) { return lastmsg; }
335};
336
337#endif /* __cplusplus */
338#endif /* _DEJAGNU_H_ */
339