aboutsummaryrefslogtreecommitdiff
path: root/dejagnu.h
blob: 908fb3828475814a5e41f80474b11d0e3feb893b (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
/*
 *   Copyright (C) 2000, 2001, 2002 Free Software Foundation, Inc.
 *
 * This program 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 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#ifndef __DEJAGNU_H__
#define __DEJAGNU_H__

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

/* If you have problems with dejagnu dropping failed, untested, or
 * unresolved messages generated by a unit testcase,
 */

/* #define _DEJAGNU_WAIT_
 */

#ifdef _DEJAGNU_WAIT_
#      include <sys/time.h>
#      include <sys/types.h>
#      include <unistd.h>
#endif

#define _BUFFER_SIZE_ 512

static int passed;
static int failed;
static int untest;
static int unresolve;

static char buffer[ _BUFFER_SIZE_ ];

#ifdef _DEJAGNU_WAIT_
void
wait(void) {
       fd_set rfds;
       struct timeval tv;

       FD_ZERO(&rfds);
       tv.tv_sec = 0;
       tv.tv_usec = 1;

       select(0, &rfds, NULL, NULL, &tv);
}
#endif

inline void
pass (const char* fmt, ... ) {
	va_list ap;
	
    passed++;
	va_start( ap, fmt );
	vsnprintf( buffer, _BUFFER_SIZE_, fmt, ap );
	va_end( ap );
    printf ("\tPASSED: %s\n", buffer );
#ifdef _DEJAGNU_WAIT_
       wait();
#endif
}

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

    failed++;
	va_start( ap, fmt );
	vsnprintf( buffer, _BUFFER_SIZE_, fmt, ap );
	va_end( ap );
    printf ("\tFAILED: %s\n", buffer );
#ifdef _DEJAGNU_WAIT_
       wait();
#endif
}

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

    untest++;
	va_start( ap, fmt );
	vsnprintf( buffer, _BUFFER_SIZE_, fmt, ap );
	va_end( ap );
    printf ("\tUNTESTED: %s\n", buffer );
#ifdef _DEJAGNU_WAIT_
       wait();
#endif
}

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

    unresolve++;
	va_start( ap, fmt );
	vsnprintf( buffer, _BUFFER_SIZE_, fmt, ap );
	va_end( ap );
    printf ("\tUNRESOLVED: %s\n", buffer );
#ifdef _DEJAGNU_WAIT_
       wait();
#endif
}

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

	va_start( ap, fmt );
	vsnprintf( buffer, _BUFFER_SIZE_, fmt, ap );
	va_end( ap );
    printf ("\tNOTE: %s\n", buffer );
#ifdef _DEJAGNU_WAIT_
       wait();
#endif
}

inline void
totals (void) {
    printf ("\nTotals:\n");
    printf ("\t#passed:\t\t%d\n", passed);
    printf ("\t#failed:\t\t%d\n", failed);
    if (untest)
        printf ("\t#untested:\t\t%d\n", untest);
    if (unresolve)
        printf ("\t#unresolved:\t\t%d\n", unresolved);
}

#ifdef __cplusplus


#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#if 0
#ifdef __STDC___
#include <sstream>
#else
#include <strstream>
#endif
#endif

const char *outstate_list[] = {
    "FAILED: ",
    "PASSED: ",
    "UNTESTED: ",
    "UNRESOLVED: "
};

const char ** outstate = outstate_list;

#if 0
extern ios& __iomanip_testout (ios&, int);
inline smanip<int> testout (int n) {
    return smanip<int> (__iomanip_testout, n);
}
ios & __iomanip_testout (ios& i, int x) {
    return i;
}

template<class T>
class OMANIP {
 private:
    T i;
    ostream &(*f)(ostream&, T);
 public:
    OMANIP(ostream& (*ff)(ostream&, T), T ii) : f(ff), i(ii) {
    }
    friend ostream operator<<(ostream& us, OMANIP& m) {
      return m.f(os,m.i);
    }
};

ostream&
freakout(ostream& os, int x) {
    return os << "FREAKOUT" ;
//    return x << "TESTOUT " << x ;
}

OMANIP<int> testout(int i) {
    return OMANIP<int>(&freakout,i);
}
#endif

enum teststate {FAILED, PASSED,UNTESTED,UNRESOLVED} laststate;

class TestState {
 private:
    teststate laststate;
    std::string lastmsg;
 public:
    TestState(void) {
        passed = 0;
        failed = 0;
        untest = 0;
        unresolve = 0;
    }
    ~TestState(void) {
        totals();
    };

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

    void pass (std::string s) {
        passed++;
        laststate = PASSED;
        lastmsg = s;
        std::cout << "\t" << outstate[PASSED] << s << std::endl;
    }
    void pass (const char *c) {
        std::string s = c;
        pass (s);
    }

    void fail (std::string s) {
        failed++;
        laststate = FAILED;
        lastmsg = s;
        std::cout << "\t" << outstate[FAILED] << s << std::endl;
    }
    void fail (const char *c) {
        std::string s = c;
        fail (s);
    }

    void untested (std::string s) {
        untest++;
        laststate = UNTESTED;
        lastmsg = s;
        std::cout << "\t" << outstate[UNTESTED] << s << std::endl;
    }
    void untested (const char *c) {
        std::string s = c;
        untested (s);
    }

    void unresolved (std::string s) {
        unresolve++;
        laststate = UNRESOLVED;
        lastmsg = s;
        std::cout << "\t" << outstate[UNRESOLVED] << s << std::endl;
    }
    void unresolved (const char *c) {
        std::string s = c;
        unresolved (s);
    }

    void totals (void) {
        std::cout << "\t#passed:\t\t" << passed << std::endl;
        std::cout << "\t#failed:\t\t" << failed << std::endl;
        if (untest)
            std::cout << "\t#untested:\t\t" << untest << std::endl;
        if (unresolve)
            std::cout << "\t#unresolved:\t\t" << unresolve << std::endl;
    }

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

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

#endif		// __cplusplus
#endif          // _DEJAGNU_H_