aboutsummaryrefslogtreecommitdiff
path: root/dejagnu.h
blob: fc6aaca1722ab3fb968cbd9cb317256ce8b0cfdd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/* DejaGnu unit testing header.
   Copyright (C) 2000-2016, 2022 Free Software Foundation, Inc.

This file is part of DejaGnu.

DejaGnu is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.

DejaGnu is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with DejaGnu; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */

#ifndef __DEJAGNU_H__
#define __DEJAGNU_H__

#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>

/* If you have problems with DejaGnu dropping failed, untested, or
 * unresolved messages generated by a unit testcase, then see the section
 * "Priority of Expect Patterns" in *note (dejagnu)Writing a test case. or
 * use the DejaGnu built-in unit testing support in your testsuite, which
 * has been improved to resolve this issue in DejaGnu 1.6.3.  */

static struct {
  int pass;
  int fail;
  int xpass;
  int xfail;
  int untested;
  int unresolved;
  int unsupported;
  /**/
  int endmsg_registered;
  int TestState_count;	/* number of live TestState objects in C++ */
} DG__status = { 0 };

static inline void
DG__endmsg (void)
{ puts ("\tEND: done"); }

static inline void
DG__init (void)
{
  if (DG__status.endmsg_registered) return;

  if (atexit (DG__endmsg) == 0)
    DG__status.endmsg_registered = 1;
}

static inline void
pass (const char* fmt, ...)
{
  va_list ap;

  DG__status.pass++;
  DG__init ();

  flockfile (stdout);
  fputs ("\tPASSED: ", stdout);
  va_start (ap, fmt); vfprintf (stdout, fmt, ap); va_end (ap);
  fputc ('\n', stdout);
  funlockfile (stdout);
}

static inline void
xpass (const char* fmt, ...)
{
  va_list ap;

  DG__status.xpass++;
  DG__init ();

  flockfile (stdout);
  fputs ("\tXPASSED: ", stdout);
  va_start (ap, fmt); vfprintf (stdout, fmt, ap); va_end (ap);
  fputc ('\n', stdout);
  funlockfile (stdout);
}

static inline void
fail (const char* fmt, ...)
{
  va_list ap;

  DG__status.fail++;
  DG__init ();

  flockfile (stdout);
  fputs ("\tFAILED: ", stdout);
  va_start (ap, fmt); vfprintf (stdout, fmt, ap); va_end (ap);
  fputc ('\n', stdout);
  funlockfile (stdout);
}

static inline void
xfail (const char* fmt, ...)
{
  va_list ap;

  DG__status.xfail++;
  DG__init ();

  flockfile (stdout);
  fputs ("\tXFAILED: ", stdout);
  va_start (ap, fmt); vfprintf (stdout, fmt, ap); va_end (ap);
  fputc ('\n', stdout);
  funlockfile (stdout);
}

static inline void
untested (const char* fmt, ...)
{
  va_list ap;

  DG__status.untested++;
  DG__init ();

  flockfile (stdout);
  fputs ("\tUNTESTED: ", stdout);
  va_start (ap, fmt); vfprintf (stdout, fmt, ap); va_end (ap);
  fputc ('\n', stdout);
  funlockfile (stdout);
}

static inline void
unresolved (const char* fmt, ...)
{
  va_list ap;

  DG__status.unresolved++;
  DG__init ();

  flockfile (stdout);
  fputs ("\tUNRESOLVED: ", stdout);
  va_start (ap, fmt); vfprintf (stdout, fmt, ap); va_end (ap);
  fputc ('\n', stdout);
  funlockfile (stdout);
}

static inline void
unsupported (const char* fmt, ...)
{
  va_list ap;

  DG__status.unsupported++;
  DG__init ();

  flockfile (stdout);
  fputs ("\tUNSUPPORTED: ", stdout);
  va_start (ap, fmt); vfprintf (stdout, fmt, ap); va_end (ap);
  fputc ('\n', stdout);
  funlockfile (stdout);
}

static inline void
note (const char* fmt, ...)
{
  va_list ap;

  DG__init ();

  flockfile (stdout);
  fputs ("\tNOTE: ", stdout);
  va_start (ap, fmt); vfprintf (stdout, fmt, ap); va_end (ap);
  fputc ('\n', stdout);
  funlockfile (stdout);
}

static inline void
DG_error (const char* fmt, ...)
{
  va_list ap;

  DG__init ();

  flockfile (stdout);
  fputs ("\tERROR: ", stdout);
  va_start (ap, fmt); vfprintf (stdout, fmt, ap); va_end (ap);
  fputc ('\n', stdout);
  funlockfile (stdout);
}

static inline void
DG_warning (const char* fmt, ...)
{
  va_list ap;

  DG__init ();

  flockfile (stdout);
  fputs ("\tWARNING: ", stdout);
  va_start (ap, fmt); vfprintf (stdout, fmt, ap); va_end (ap);
  fputc ('\n', stdout);
  funlockfile (stdout);
}

static inline void
totals (void)
{
  printf ("\nTotals:\n");
  printf ("\t#passed:\t\t%d\n", DG__status.pass);
  printf ("\t#failed:\t\t%d\n", DG__status.fail);
  if (DG__status.xfail)
    printf ("\t#expected failures:\t\t%d\n", DG__status.xfail);
  if (DG__status.xpass)
    printf ("\t#unexpected passes:\t\t%d\n", DG__status.xpass);
  if (DG__status.untested)
    printf ("\t#untested:\t\t%d\n", DG__status.untested);
  if (DG__status.unresolved)
    printf ("\t#unresolved:\t\t%d\n", DG__status.unresolved);
  if (DG__status.unsupported)
    printf ("\t#unsupported:\t\t%d\n", DG__status.unsupported);
}

#ifdef __cplusplus

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>

const char * DG__outstate_list[] = {
  "\tFAILED: ", "\tPASSED: ",
  "\tUNTESTED: ", "\tUNRESOLVED: ", "\tUNSUPPORTED: ",
  "\tXFAILED: ", "\tXPASSED: "
};

enum DG_teststate { FAILED, PASSED,
		    UNTESTED, UNRESOLVED, UNSUPPORTED,
		    XFAILED, XPASSED };

class TestState {
 private:
  DG_teststate laststate;
  std::string lastmsg;
 public:
  TestState (void)
    {
      DG__status.TestState_count++;

      if (DG__status.TestState_count > 1)
	return; /* Do not clear the counters if additional TestState
		   objects are constructed. */

      DG__status.pass = 0;
      DG__status.fail = 0;
      DG__status.xpass = 0;
      DG__status.xfail = 0;
      DG__status.untested = 0;
      DG__status.unresolved = 0;
      DG__status.unsupported = 0;

      /* C++ object destruction will substitute for atexit(). */
      DG__status.endmsg_registered = 1;
    }

  ~TestState (void)
    {
      DG__status.TestState_count--;

      if (DG__status.TestState_count > 0) return;

      /* The last TestState object is being destroyed. */
      totals ();
      std::cout << "\tEND: done" << std::endl;
    }

  void testrun (bool b, std::string s)
    {
      if (b)
	pass (s);
      else
	fail (s);
    }

    void pass (std::string s)
      {
	DG__status.pass++;
	laststate = PASSED;
	lastmsg = s;
	std::cout << DG__outstate_list[PASSED] << s << std::endl;
      }

    void xpass (std::string s)
      {
	DG__status.xpass++;
	laststate = PASSED;
	lastmsg = s;
	std::cout << DG__outstate_list[XPASSED] << s << std::endl;
      }

    void fail (std::string s)
      {
	DG__status.fail++;
	laststate = FAILED;
	lastmsg = s;
	std::cout << DG__outstate_list[FAILED] << s << std::endl;
      }

    void xfail (std::string s)
      {
	DG__status.xfail++;
	laststate = XFAILED;
	lastmsg = s;
	std::cout << DG__outstate_list[XFAILED] << s << std::endl;
      }

    void untested (std::string s)
      {
	DG__status.untested++;
	laststate = UNTESTED;
	lastmsg = s;
	std::cout << DG__outstate_list[UNTESTED] << s << std::endl;
      }

    void unresolved (std::string s)
      {
	DG__status.unresolved++;
	laststate = UNRESOLVED;
	lastmsg = s;
	std::cout << DG__outstate_list[UNRESOLVED] << s << std::endl;
      }

    void unsupported (std::string s)
      {
	DG__status.unsupported++;
	laststate = UNSUPPORTED;
	lastmsg = s;
	std::cout << DG__outstate_list[UNSUPPORTED] << s << std::endl;
      }

    void note (std::string s)
      {
	std::cout << "\t" << "NOTE: " << s << std::endl;
      }

    void error (std::string s)
      {
	std::cout << "\t" << "ERROR: " << s << std::endl;
      }

    void warning (std::string s)
      {
	std::cout << "\t" << "WARNING: " << s << std::endl;
      }

    void totals (void)
      {
	std::cout << std::endl << "Totals:" << std::endl;

	std::cout << "\t#passed:\t\t"
		  << DG__status.pass << std::endl;
	std::cout << "\t#failed:\t\t"
		  << DG__status.fail << std::endl;

	if (DG__status.xfail)
	  std::cout << "\t#expected failures:\t\t"
		    << DG__status.xfail << std::endl;
	if (DG__status.xpass)
	  std::cout << "\t#unexpected passes:\t\t"
		    << DG__status.xpass << std::endl;
	if (DG__status.untested)
	  std::cout << "\t#untested:\t\t"
		    << DG__status.untested << std::endl;
	if (DG__status.unresolved)
	  std::cout << "\t#unresolved:\t\t"
		    << DG__status.unresolved << std::endl;
	if (DG__status.unsupported)
	  std::cout << "\t#unsupported:\t\t"
		    << DG__status.unsupported << std::endl;
      }

    // This is so this class can be printed in an ostream.
    friend std::ostream & operator << (std::ostream &os, TestState& t)
      {
	return os << DG__outstate_list[t.laststate] << t.lastmsg ;
      }

    int GetState (void) { return laststate; }
    std::string GetMsg (void) { return lastmsg; }
};

TestState DG;

#endif /* __cplusplus */
#endif /* _DEJAGNU_H_ */