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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [language/] [c/] [libc/] [setjmp/] [current/] [tests/] [setjmp.c] - Blame information for rev 853

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

Line No. Rev Author Line
1 786 skrzyp
//===========================================================================
2
//
3
//      setjmp.c
4
//
5
//      Tests for ANSI standard setjmp() and longjmp() functions
6
//
7
//===========================================================================
8
// ####ECOSGPLCOPYRIGHTBEGIN####                                            
9
// -------------------------------------------                              
10
// This file is part of eCos, the Embedded Configurable Operating System.   
11
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
12
//
13
// eCos is free software; you can redistribute it and/or modify it under    
14
// the terms of the GNU General Public License as published by the Free     
15
// Software Foundation; either version 2 or (at your option) any later      
16
// version.                                                                 
17
//
18
// eCos is distributed in the hope that it will be useful, but WITHOUT      
19
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or    
20
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License    
21
// for more details.                                                        
22
//
23
// You should have received a copy of the GNU General Public License        
24
// along with eCos; if not, write to the Free Software Foundation, Inc.,    
25
// 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.            
26
//
27
// As a special exception, if other files instantiate templates or use      
28
// macros or inline functions from this file, or you compile this file      
29
// and link it with other works to produce a work based on this file,       
30
// this file does not by itself cause the resulting work to be covered by   
31
// the GNU General Public License. However the source code for this file    
32
// must still be made available in accordance with section (3) of the GNU   
33
// General Public License v2.                                               
34
//
35
// This exception does not invalidate any other reasons why a work based    
36
// on this file might be covered by the GNU General Public License.         
37
// -------------------------------------------                              
38
// ####ECOSGPLCOPYRIGHTEND####                                              
39
//===========================================================================
40
//#####DESCRIPTIONBEGIN####
41
//
42
// Author(s):    jlarmour
43
// Contributors: 
44
// Date:         2000-04-30
45
// Purpose:     
46
// Description: Contains test code for C library setjmp() and longjmp()
47
//              functions
48
//              The ANSI standard allows setjmp calls to be used ONLY in the
49
//              forms:
50
//                while (setjmp (jumpbuf))
51
//                while (setjmp (jumpbuf) < 42)
52
//                while (!setjmp (jumpbuf))
53
//                setjmp (jumpbuf);
54
//                result = setjmp(jumpbuf);
55
//
56
//              Or "while" can also be "if" or "switch" or the implicit
57
//              while of "for". 
58
// Usage:       
59
//
60
//####DESCRIPTIONEND####
61
//
62
//===========================================================================
63
 
64
// INCLUDES
65
 
66
#include <pkgconf/system.h>
67
#include <cyg/infra/testcase.h>    // Test infrastructure header
68
#include <setjmp.h>                // Header for what we're testing
69
 
70
// GLOBALS
71
 
72
static jmp_buf jbuf, jbuf2, jbuf3;
73
 
74
 
75
// FUNCTIONS
76
 
77
static int
78
test_fun( void )
79
{
80
    volatile int i=0; // add something to the stack to confuse things
81
 
82
    longjmp( jbuf, 5 );
83
    i+=5;
84
 
85
    return i;
86
} // test_fun();
87
 
88
 
89
#ifndef CYGPKG_LIBC_STARTUP
90
void cyg_user_start(void)
91
#else
92
int
93
main( int argc, char *argv[] )
94
#endif
95
{
96
    static int static_i=0;         // temporary variable
97
    int j=0;                       // ditto
98
    volatile int vol_k=0, vol_l=0; // ditto
99
 
100
    CYG_TEST_INIT();
101
 
102
    CYG_TEST_INFO("Starting tests from testcase " __FILE__ " for C library "
103
                  "setjmp()/longjmp() functions");
104
 
105
    // Test 1
106
    if ( setjmp(jbuf) == 0 )
107
    {
108
        static_i=1;
109
        longjmp(jbuf, 1);
110
        static_i=2;
111
    } // if
112
 
113
    CYG_TEST_PASS_FAIL( static_i==1, "Simple test 1" );
114
 
115
    // Test 2
116
    j=2;
117
 
118
    if ( !setjmp(jbuf) )
119
    {
120
        static_i=3;
121
        longjmp(jbuf, 1);
122
        static_i=4;
123
        j=3;
124
    } // if
125
 
126
    CYG_TEST_PASS_FAIL( (static_i==3) && (j==2), "Simple test 2" );
127
 
128
    // Test 3
129
 
130
    static_i=0;
131
    switch (setjmp(jbuf))
132
    {
133
    case 0:
134
        static_i++;
135
        if (static_i!=1)
136
            CYG_TEST_FAIL("Test of value passed to longjmp()");
137
        else
138
            longjmp(jbuf, 5);
139
        break;
140
    case 5:
141
        static_i++;
142
        CYG_TEST_PASS_FAIL( (static_i==2),"Test of value passed to longjmp()");
143
        break;
144
    default:
145
        CYG_TEST_FAIL( "Test of value passed to longjmp()");
146
        break;
147
    } // switch
148
 
149
    // Test 3
150
 
151
    static_i=0;
152
    switch (setjmp(jbuf))
153
    {
154
    case 0:
155
        static_i++;
156
        if (static_i!=1)
157
            CYG_TEST_FAIL("Test of value passed to longjmp() being 0");
158
        else
159
            longjmp(jbuf, 0);
160
        break;
161
    case 1:
162
        static_i++;
163
        CYG_TEST_PASS_FAIL( (static_i==2),
164
                            "Test of value passed to longjmp() being 0");
165
        break;
166
    default:
167
        CYG_TEST_FAIL( "Test of value passed to longjmp() being 0");
168
        break;
169
    } // switch
170
 
171
 
172
    // Test 4
173
 
174
    vol_k=0;
175
    static_i=0;
176
    setjmp( jbuf );
177
 
178
    static_i++;
179
    setjmp( jbuf2 );
180
 
181
    static_i++;
182
    setjmp( jbuf3 );
183
 
184
    if (vol_k==0)
185
    {
186
        vol_k++;
187
        longjmp( jbuf2, 1 );
188
    }
189
    else
190
    {
191
        CYG_TEST_PASS_FAIL( (vol_k==1) && (static_i==3), "Multiple setjmp's" );
192
    }
193
 
194
    // Test 5
195
 
196
    vol_k=3;
197
    if ( (j=setjmp(jbuf)) == 0 )
198
    {
199
        volatile int l; // put something extra on the stack to confuse things
200
 
201
        vol_k++;
202
        l=test_fun();
203
        l++;
204
        vol_k=l;
205
    } // if
206
 
207
    CYG_TEST_PASS_FAIL( (j==5) && (vol_k==4), "longjmp from a function" );
208
 
209
    // Test 6
210
 
211
    vol_k=0;
212
 
213
    vol_l=setjmp(jbuf);
214
 
215
    vol_k += 3;
216
 
217
    if (!vol_l)
218
        test_fun();
219
 
220
    vol_l=setjmp(jbuf);
221
 
222
    vol_k += 7;
223
 
224
    if (!vol_l)
225
        test_fun();
226
 
227
    CYG_TEST_PASS_FAIL ( (vol_k == 20), "Repeated longjmps from a function" );
228
 
229
//    CYG_TEST_NA("Testing is not applicable to this configuration");
230
 
231
    CYG_TEST_FINISH("Finished tests from testcase " __FILE__ " for C library "
232
                    "setjmp()/longjmp() functions");
233
 
234
} // main()
235
 
236
// EOF setjmp.c

powered by: WebSVN 2.1.0

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