~ubuntu-branches/ubuntu/oneiric/dejagnu/oneiric

« back to all changes in this revision

Viewing changes to dejagnu.h

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Jacobowitz
  • Date: 2006-12-11 09:06:59 UTC
  • mfrom: (2.1.6 edgy)
  • Revision ID: james.westby@ubuntu.com-20061211090659-w586kgi3giz84053
Tags: 1.4.4.cvs20060709-3
* Acknowledge previous NMUs.
* Fix permissions on /usr/share/dejagnu when building without fakeroot
  (Closes: #392589, #379809).

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