~ubuntu-branches/ubuntu/warty/dejagnu/warty

« back to all changes in this revision

Viewing changes to dejagnu.h

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Jacobowitz
  • Date: 2004-02-09 15:07:58 UTC
  • Revision ID: james.westby@ubuntu.com-20040209150758-oaj7r5zrop60v8sb
Tags: upstream-1.4.4
ImportĀ upstreamĀ versionĀ 1.4.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *   Copyright (C) 2000, 2001, 2002 Free Software Foundation, Inc.
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or modify
 
5
 * it under the terms of the GNU General Public License as published by
 
6
 * the Free Software Foundation; either version 2 of the License, or
 
7
 * (at your option) any later version.
 
8
 *
 
9
 * This program is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
 * GNU General Public License for more details.
 
13
 
 
14
 * You should have received a copy of the GNU General Public License
 
15
 * along with this program; if not, write to the Free Software
 
16
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
17
 */
 
18
 
 
19
#ifndef __DEJAGNU_H__
 
20
#define __DEJAGNU_H__
 
21
 
 
22
#include <stdio.h>
 
23
#include <stdarg.h>
 
24
#include <string.h>
 
25
 
 
26
/* If you have problems with dejagnu dropping failed, untested, or
 
27
 * unresolved messages generated by a unit testcase,
 
28
 */
 
29
 
 
30
/* #define _DEJAGNU_WAIT_
 
31
 */
 
32
 
 
33
#ifdef _DEJAGNU_WAIT_
 
34
#      include <sys/time.h>
 
35
#      include <sys/types.h>
 
36
#      include <unistd.h>
 
37
#endif
 
38
 
 
39
#define _BUFFER_SIZE_ 512
 
40
 
 
41
static int passed;
 
42
static int failed;
 
43
static int untest;
 
44
static int unresolve;
 
45
 
 
46
static char buffer[ _BUFFER_SIZE_ ];
 
47
 
 
48
#ifdef _DEJAGNU_WAIT_
 
49
void
 
50
wait(void) {
 
51
       fd_set rfds;
 
52
       struct timeval tv;
 
53
 
 
54
       FD_ZERO(&rfds);
 
55
       tv.tv_sec = 0;
 
56
       tv.tv_usec = 1;
 
57
 
 
58
       select(0, &rfds, NULL, NULL, &tv);
 
59
}
 
60
#endif
 
61
 
 
62
inline void
 
63
pass (const char* fmt, ... ) {
 
64
        va_list ap;
 
65
        
 
66
    passed++;
 
67
        va_start( ap, fmt );
 
68
        vsnprintf( buffer, _BUFFER_SIZE_, fmt, ap );
 
69
        va_end( ap );
 
70
    printf ("\tPASSED: %s\n", buffer );
 
71
#ifdef _DEJAGNU_WAIT_
 
72
       wait();
 
73
#endif
 
74
}
 
75
 
 
76
inline void
 
77
fail (const char* fmt, ... ) {
 
78
        va_list ap;
 
79
 
 
80
    failed++;
 
81
        va_start( ap, fmt );
 
82
        vsnprintf( buffer, _BUFFER_SIZE_, fmt, ap );
 
83
        va_end( ap );
 
84
    printf ("\tFAILED: %s\n", buffer );
 
85
#ifdef _DEJAGNU_WAIT_
 
86
       wait();
 
87
#endif
 
88
}
 
89
 
 
90
inline void
 
91
untested (const char* fmt, ... ) {
 
92
        va_list ap;
 
93
 
 
94
    untest++;
 
95
        va_start( ap, fmt );
 
96
        vsnprintf( buffer, _BUFFER_SIZE_, fmt, ap );
 
97
        va_end( ap );
 
98
    printf ("\tUNTESTED: %s\n", buffer );
 
99
#ifdef _DEJAGNU_WAIT_
 
100
       wait();
 
101
#endif
 
102
}
 
103
 
 
104
inline void
 
105
unresolved (const char* fmt, ... ) {
 
106
        va_list ap;
 
107
 
 
108
    unresolve++;
 
109
        va_start( ap, fmt );
 
110
        vsnprintf( buffer, _BUFFER_SIZE_, fmt, ap );
 
111
        va_end( ap );
 
112
    printf ("\tUNRESOLVED: %s\n", buffer );
 
113
#ifdef _DEJAGNU_WAIT_
 
114
       wait();
 
115
#endif
 
116
}
 
117
 
 
118
inline void
 
119
note (const char* fmt, ... ) {
 
120
        va_list ap;
 
121
 
 
122
        va_start( ap, fmt );
 
123
        vsnprintf( buffer, _BUFFER_SIZE_, fmt, ap );
 
124
        va_end( ap );
 
125
    printf ("\tNOTE: %s\n", buffer );
 
126
#ifdef _DEJAGNU_WAIT_
 
127
       wait();
 
128
#endif
 
129
}
 
130
 
 
131
inline void
 
132
totals (void) {
 
133
    printf ("\nTotals:\n");
 
134
    printf ("\t#passed:\t\t%d\n", passed);
 
135
    printf ("\t#failed:\t\t%d\n", failed);
 
136
    if (untest)
 
137
        printf ("\t#untested:\t\t%d\n", untest);
 
138
    if (unresolve)
 
139
        printf ("\t#unresolved:\t\t%d\n", unresolved);
 
140
}
 
141
 
 
142
#ifdef __cplusplus
 
143
 
 
144
 
 
145
#include <iostream>
 
146
#include <iomanip>
 
147
#include <fstream>
 
148
#include <string>
 
149
#if 0
 
150
#ifdef __STDC___
 
151
#include <sstream>
 
152
#else
 
153
#include <strstream>
 
154
#endif
 
155
#endif
 
156
 
 
157
const char *outstate_list[] = {
 
158
    "FAILED: ",
 
159
    "PASSED: ",
 
160
    "UNTESTED: ",
 
161
    "UNRESOLVED: "
 
162
};
 
163
 
 
164
const char ** outstate = outstate_list;
 
165
 
 
166
#if 0
 
167
extern ios& __iomanip_testout (ios&, int);
 
168
inline smanip<int> testout (int n) {
 
169
    return smanip<int> (__iomanip_testout, n);
 
170
}
 
171
ios & __iomanip_testout (ios& i, int x) {
 
172
    return i;
 
173
}
 
174
 
 
175
template<class T>
 
176
class OMANIP {
 
177
 private:
 
178
    T i;
 
179
    ostream &(*f)(ostream&, T);
 
180
 public:
 
181
    OMANIP(ostream& (*ff)(ostream&, T), T ii) : f(ff), i(ii) {
 
182
    }
 
183
    friend ostream operator<<(ostream& us, OMANIP& m) {
 
184
      return m.f(os,m.i);
 
185
    }
 
186
};
 
187
 
 
188
ostream&
 
189
freakout(ostream& os, int x) {
 
190
    return os << "FREAKOUT" ;
 
191
//    return x << "TESTOUT " << x ;
 
192
}
 
193
 
 
194
OMANIP<int> testout(int i) {
 
195
    return OMANIP<int>(&freakout,i);
 
196
}
 
197
#endif
 
198
 
 
199
enum teststate {FAILED, PASSED,UNTESTED,UNRESOLVED} laststate;
 
200
 
 
201
class TestState {
 
202
 private:
 
203
    teststate laststate;
 
204
    std::string lastmsg;
 
205
 public:
 
206
    TestState(void) {
 
207
        passed = 0;
 
208
        failed = 0;
 
209
        untest = 0;
 
210
        unresolve = 0;
 
211
    }
 
212
    ~TestState(void) {
 
213
        totals();
 
214
    };
 
215
 
 
216
    void testrun (bool b, std::string s) {
 
217
        if (b)
 
218
            pass (s);
 
219
        else
 
220
            fail (s);
 
221
    }
 
222
 
 
223
    void pass (std::string s) {
 
224
        passed++;
 
225
        laststate = PASSED;
 
226
        lastmsg = s;
 
227
        std::cout << "\t" << outstate[PASSED] << s << std::endl;
 
228
    }
 
229
    void pass (const char *c) {
 
230
        std::string s = c;
 
231
        pass (s);
 
232
    }
 
233
 
 
234
    void fail (std::string s) {
 
235
        failed++;
 
236
        laststate = FAILED;
 
237
        lastmsg = s;
 
238
        std::cout << "\t" << outstate[FAILED] << s << std::endl;
 
239
    }
 
240
    void fail (const char *c) {
 
241
        std::string s = c;
 
242
        fail (s);
 
243
    }
 
244
 
 
245
    void untested (std::string s) {
 
246
        untest++;
 
247
        laststate = UNTESTED;
 
248
        lastmsg = s;
 
249
        std::cout << "\t" << outstate[UNTESTED] << s << std::endl;
 
250
    }
 
251
    void untested (const char *c) {
 
252
        std::string s = c;
 
253
        untested (s);
 
254
    }
 
255
 
 
256
    void unresolved (std::string s) {
 
257
        unresolve++;
 
258
        laststate = UNRESOLVED;
 
259
        lastmsg = s;
 
260
        std::cout << "\t" << outstate[UNRESOLVED] << s << std::endl;
 
261
    }
 
262
    void unresolved (const char *c) {
 
263
        std::string s = c;
 
264
        unresolved (s);
 
265
    }
 
266
 
 
267
    void totals (void) {
 
268
        std::cout << "\t#passed:\t\t" << passed << std::endl;
 
269
        std::cout << "\t#failed:\t\t" << failed << std::endl;
 
270
        if (untest)
 
271
            std::cout << "\t#untested:\t\t" << untest << std::endl;
 
272
        if (unresolve)
 
273
            std::cout << "\t#unresolved:\t\t" << unresolve << std::endl;
 
274
    }
 
275
 
 
276
    // This is so this class can be printed in an ostream.
 
277
    friend std::ostream & operator << (std::ostream &os, TestState& t) {
 
278
        return os << "\t" << outstate[t.laststate] << t.lastmsg ;
 
279
    }
 
280
 
 
281
    int GetState(void) {
 
282
        return laststate;
 
283
    }
 
284
    std::string GetMsg(void) {
 
285
        return lastmsg;
 
286
    }
 
287
};
 
288
 
 
289
#endif          // __cplusplus
 
290
#endif          // _DEJAGNU_H_
 
291
 
 
292