OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [host/] [infra/] [testsuite/] [cyginfra/] [tassert3.cxx] - Blame information for rev 790

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 786 skrzyp
//==========================================================================
2
//
3
//      tassert3.cxx
4
//
5
//      Assertion test case                                                                
6
//
7
//==========================================================================
8
// ####ECOSHOSTGPLCOPYRIGHTBEGIN####                                        
9
// -------------------------------------------                              
10
// This file is part of the eCos host tools.                                
11
// Copyright (C) 1998, 1999, 2000 Free Software Foundation, Inc.            
12
//
13
// This program is free software; you can redistribute it and/or modify     
14
// it under the terms of the GNU General Public License as published by     
15
// the Free Software Foundation; either version 2 or (at your option) any   
16
// later version.                                                           
17
//
18
// This program is distributed in the hope that it will be useful, but      
19
// WITHOUT ANY WARRANTY; without even the implied warranty of               
20
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU        
21
// General Public License for more details.                                 
22
//
23
// You should have received a copy of the GNU General Public License        
24
// along with this program; if not, write to the                            
25
// Free Software Foundation, Inc., 51 Franklin Street,                      
26
// Fifth Floor, Boston, MA  02110-1301, USA.                                
27
// -------------------------------------------                              
28
// ####ECOSHOSTGPLCOPYRIGHTEND####                                          
29
//==========================================================================
30
//#####DESCRIPTIONBEGIN####                                             
31
//
32
// Author(s):           bartv
33
// Contributors:        bartv
34
// Date:                1998-12-21
35
// Purpose:
36
// Description:         Check the host extensions.
37
//
38
//####DESCRIPTIONEND####
39
//==========================================================================
40
 
41
// -------------------------------------------------------------------------
42
// The host-side assertion support has three extensions to the generic
43
// assertion support:
44
//
45
// 1) cyg_assert_install_failure_handler()
46
//    Provide an alternative handler that gets invoked when an
47
//    assertion fails.
48
//
49
// 2) cyg_assert_install_failure_callback()
50
//    Provide an additional callback that should get invoked when an
51
//    assertion triggers to provide additional information.
52
//
53
// 3) cyg_assert_failure_invoke_callbacks()
54
//    Used by (1) to call the functions installed via (2).
55
//
56
// The tests make use of setjmp()/longjmp() buffer to avoid having to
57
// do lots of output parsing in the DejaGnu driver.
58
 
59
#define CYG_DECLARE_HOST_ASSERTION_SUPPORT
60
#define CYGDBG_USE_ASSERTS
61
#define CYGDBG_INFRA_DEBUG_PRECONDITIONS
62
#define CYGDBG_INFRA_DEBUG_POSTCONDITIONS
63
#define CYGDBG_INFRA_DEBUG_LOOP_INVARIANTS
64
#define CYGDBG_INFRA_DEBUG_INVARIANTS
65
 
66
#include <cyg/infra/testcase.h>
67
#include <cyg/infra/cyg_ass.h>
68
#include <cstdlib>
69
#include <csetjmp>
70
#include <cstring>
71
 
72
// Forward declarations of some integers to allow the line number to be
73
// checked.
74
extern cyg_uint32 main_start, main_end;
75
 
76
// This is used to "recover" from an assertion failure
77
static jmp_buf setjmp_buffer;
78
 
79
// A constant string useful for comparisons.
80
static const char message[] = "fail";
81
 
82
// Some state information to make sure that the world is in the expected
83
// state
84
bool expecting_failure1         = false;
85
bool expecting_failure2         = false;
86
bool expecting_failure3         = false;
87
 
88
// -------------------------------------------------------------------------
89
// The first assertion handler. This is used to check that the arguments
90
// make sense.
91
 
92
extern "C"
93
bool
94
failure_handler1(const char* fn, const char* file, cyg_uint32 line, const char* msg)
95
{
96
    if (!expecting_failure1) {
97
        CYG_TEST_FAIL("assertion failure handler1 invoked unexpectedly");
98
    }
99
    expecting_failure1 = false;
100
    // The function should be 0 or contain main
101
    if (0 != fn) {
102
        if (0 == strstr(fn, "main")) {
103
            CYG_TEST_FAIL("invalid function name passed to assertion failure handler");
104
        }
105
    }
106
    // The file name should always be valid and contain tassert3.cxx.
107
    // It may contain path information as well.
108
    if ( (0 == file) || (0 == strstr(file, "tassert3.cxx"))) {
109
        CYG_TEST_FAIL("invalid file name passed to assertion failure handler");
110
    }
111
    // The line number can be validated against some globals.
112
    if ((line <= main_start) || (line >= main_end)) {
113
        CYG_TEST_FAIL("invalid line number passed to assertion failure handler");
114
    }
115
    // The message passed is known.
116
    if (0 != strcmp(msg, message)) {
117
        CYG_TEST_FAIL("invalid message passed to assertion failure handler");
118
    }
119
    CYG_TEST_PASS("application-specific assertion failure handler");
120
 
121
    // Everything OK, back to main()
122
    longjmp(setjmp_buffer, 1);
123
    CYG_TEST_FAIL_FINISH("longjmp() is not functional");
124
    return false;
125
}
126
 
127
// -------------------------------------------------------------------------
128
// A second assertion handler. This is used to make sure that assertion
129
// failure handlers can be overwritten.
130
 
131
extern "C"
132
bool
133
failure_handler2(const char* fn, const char* file, cyg_uint32 line, const char* msg)
134
{
135
    if (!expecting_failure2) {
136
        CYG_TEST_FAIL("assertion failure handler2 invoked incorrectly");
137
    }
138
    expecting_failure2 = false;
139
    CYG_TEST_PASS("assertion failure handlers can be replaced");
140
    longjmp(setjmp_buffer, 1);
141
    CYG_TEST_FAIL_FINISH("longjmp() is not functional");
142
    return false;
143
}
144
// -------------------------------------------------------------------------
145
// The third assertion handler. This time a couple of output callbacks are
146
// installed and the main failure handler has to invoke the output callbacks.
147
// A number of statics are used to make sure everything works ok.
148
 
149
static const char callback1_title[] = "callback1_header";
150
static const char callback2_title[] = "callback2_header";
151
static const char callback2_data[]  = "callback2 data\n";
152
static bool seen_callback1_title    = false;
153
static bool seen_callback2_title    = false;
154
static bool callback1_done          = false;
155
static bool callback2_done          = false;
156
static int  number_of_headers       = 0;
157
static bool callback1_invoked       = false;
158
static bool callback2_invoked       = false;
159
static int  number_of_lines_seen    = 0;
160
const  int  callback2_lines         = 16;
161
static int  callback2_lines_seen    = 0;
162
static int  number_of_trailers      = 0;
163
 
164
extern "C"
165
void
166
callback1(void (*outputfn)(const char*))
167
{
168
    if (callback1_invoked) {
169
        CYG_TEST_FAIL("callback1 invoked multiple times");
170
    }
171
    callback1_invoked = true;
172
 
173
    // This callback does nothing.
174
}
175
 
176
extern "C"
177
void
178
callback2(void (*outputfn)(const char*))
179
{
180
    if (callback2_invoked) {
181
        CYG_TEST_FAIL("callback2 invoked multiple times");
182
    }
183
    callback2_invoked = true;
184
    for (int i = 0; i < callback2_lines; i++) {
185
        (*outputfn)(callback2_data);
186
    }
187
}
188
 
189
// handle_callback_header() should be invoked at least twice,
190
// once with callback1_header and once with callback2_header
191
extern "C"
192
void
193
handle_callback_header(const char* name)
194
{
195
    number_of_headers++;
196
    if (0 == strcmp(name, callback1_title)) {
197
        if (seen_callback1_title) {
198
            CYG_TEST_FAIL("callback1 title seen multiple times");
199
        } else {
200
            seen_callback1_title = true;
201
        }
202
    }
203
    if (0 == strcmp(name, callback2_title)) {
204
        if (seen_callback2_title) {
205
            CYG_TEST_FAIL("callback2 title seen multiple times");
206
        } else {
207
            seen_callback2_title = true;
208
        }
209
    }
210
}
211
 
212
// The body output function should be invoked zero times for
213
// callback1 and a fixed number of times for callback2.
214
extern "C"
215
void
216
handle_callback_body(const char* data)
217
{
218
    number_of_lines_seen++;
219
 
220
    if (seen_callback1_title && !callback1_done) {
221
        CYG_TEST_FAIL("callback1 should not perform any output");
222
    }
223
    if (seen_callback2_title && !callback2_done) {
224
        callback2_lines_seen++;
225
        if (0 != strcmp(data, callback2_data)) {
226
            CYG_TEST_FAIL("callback2 has generated incorrect data");
227
        }
228
    }
229
}
230
 
231
// The trailer output function should be invoked at least twice, once
232
// for each callback.
233
extern "C"
234
void
235
handle_callback_trailer(void)
236
{
237
    if (0 == number_of_headers) {
238
        CYG_TEST_FAIL("callback trailer seen before header");
239
    }
240
    number_of_trailers++;
241
    if (seen_callback1_title && !callback1_done) {
242
        callback1_done = true;
243
    }
244
    if (seen_callback2_title && !callback2_done) {
245
        callback2_done = true;
246
    }
247
}
248
 
249
// This is the failure handler. It causes the various callbacks to run, then
250
// checks the resulting values of the statics.
251
extern "C"
252
bool
253
failure_handler3(const char* fn, const char* file, cyg_uint32 line, const char* msg)
254
{
255
    if (!expecting_failure3) {
256
        CYG_TEST_FAIL("assertion failure handler3 invoked incorrectly");
257
    }
258
    expecting_failure3 = false;
259
 
260
    cyg_assert_failure_invoke_callbacks(
261
        &handle_callback_header,
262
        &handle_callback_body,
263
        &handle_callback_trailer);
264
 
265
    bool all_ok = true;
266
    if (!seen_callback1_title) {
267
        CYG_TEST_FAIL("callback1's title not seen");
268
        all_ok = false;
269
    }
270
    if (!seen_callback2_title) {
271
        CYG_TEST_FAIL("callback2's title not seen");
272
        all_ok = false;
273
    }
274
    if (number_of_headers != number_of_trailers) {
275
        CYG_TEST_FAIL("headers and trailers do not match up");
276
        all_ok = false;
277
    }
278
    if (!callback1_done) {
279
        CYG_TEST_FAIL("callback1 did not finish");
280
        all_ok = false;
281
    }
282
    if (!callback2_done) {
283
        CYG_TEST_FAIL("callback2 did not finish");
284
        all_ok = false;
285
    }
286
    if (number_of_lines_seen < callback2_lines) {
287
        CYG_TEST_FAIL("total number of output lines is less than expected");
288
        all_ok = false;
289
    }
290
    if (callback2_lines_seen != callback2_lines) {
291
        CYG_TEST_FAIL("callback2 generated the wrong number of lines");
292
        all_ok = false;
293
    }
294
 
295
    if (all_ok) {
296
        CYG_TEST_PASS("assertion callbacks");
297
    }
298
 
299
    longjmp(setjmp_buffer, 1);
300
    CYG_TEST_FAIL_FINISH("longjmp() is not functional");
301
    return false;
302
}
303
 
304
// ----------------------------------------------------------------------------
305
// main(). Perform the various assertion tests in order.
306
 
307
cyg_uint32 main_start = (cyg_uint32) __LINE__;
308
int
309
main(int argc, char** argv)
310
{
311
    expecting_failure1 = true;
312
    // First check, a very basic assertion invocation.
313
    if (setjmp(setjmp_buffer) == 0) {
314
        cyg_assert_install_failure_handler(&failure_handler1);
315
        CYG_FAIL(message);
316
        CYG_TEST_FAIL("assertion did not trigger");
317
    }
318
    expecting_failure1 = false;
319
    expecting_failure2 = true;
320
 
321
    // Now try installing a different assertion handler.
322
    if (setjmp(setjmp_buffer) == 0) {
323
        cyg_assert_install_failure_handler(&failure_handler2);
324
        CYG_FAIL(message);
325
        CYG_TEST_FAIL("assertion did not trigger");
326
    }
327
    expecting_failure2 = false;
328
    expecting_failure3 = true;
329
 
330
    if (setjmp(setjmp_buffer) == 0) {
331
        cyg_assert_install_failure_callback(callback1_title, &callback1);
332
        cyg_assert_install_failure_callback(callback2_title, &callback2);
333
        cyg_assert_install_failure_handler(&failure_handler3);
334
        CYG_FAIL(message);
335
        CYG_TEST_FAIL("assertion did not trigger");
336
    }
337
    expecting_failure3 = false;
338
 
339
    return 0;
340
}
341
cyg_uint32 main_end = (cyg_uint32) __LINE__;
342
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.