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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-2.0/] [packages/] [language/] [c/] [libc/] [setjmp/] [v2_0/] [tests/] [setjmp.c] - Blame information for rev 174

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 27 unneback
//===========================================================================
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 Red Hat, 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 version.
16
//
17
// eCos is distributed in the hope that it will be useful, but WITHOUT ANY
18
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
19
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
20
// for more details.
21
//
22
// You should have received a copy of the GNU General Public License along
23
// with eCos; if not, write to the Free Software Foundation, Inc.,
24
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
25
//
26
// As a special exception, if other files instantiate templates or use macros
27
// or inline functions from this file, or you compile this file and link it
28
// with other works to produce a work based on this file, this file does not
29
// by itself cause the resulting work to be covered by the GNU General Public
30
// License. However the source code for this file must still be made available
31
// in accordance with section (3) of the GNU General Public License.
32
//
33
// This exception does not invalidate any other reasons why a work based on
34
// this file might be covered by the GNU General Public License.
35
//
36
// Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
37
// at http://sources.redhat.com/ecos/ecos-license/
38
// -------------------------------------------
39
//####ECOSGPLCOPYRIGHTEND####
40
//===========================================================================
41
//#####DESCRIPTIONBEGIN####
42
//
43
// Author(s):    jlarmour
44
// Contributors: 
45
// Date:         2000-04-30
46
// Purpose:     
47
// Description: Contains test code for C library setjmp() and longjmp()
48
//              functions
49
//              The ANSI standard allows setjmp calls to be used ONLY in the
50
//              forms:
51
//                while (setjmp (jumpbuf))
52
//                while (setjmp (jumpbuf) < 42)
53
//                while (!setjmp (jumpbuf))
54
//                setjmp (jumpbuf);
55
//                result = setjmp(jumpbuf);
56
//
57
//              Or "while" can also be "if" or "switch" or the implicit
58
//              while of "for". 
59
// Usage:       
60
//
61
//####DESCRIPTIONEND####
62
//
63
//===========================================================================
64
 
65
// INCLUDES
66
 
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
int
90
main( int argc, char *argv[] )
91
{
92
    static int static_i=0;         // temporary variable
93
    int j=0;                       // ditto
94
    volatile int vol_k=0, vol_l=0; // ditto
95
 
96
    CYG_TEST_INIT();
97
 
98
    CYG_TEST_INFO("Starting tests from testcase " __FILE__ " for C library "
99
                  "setjmp()/longjmp() functions");
100
 
101
    // Test 1
102
    if ( setjmp(jbuf) == 0 )
103
    {
104
        static_i=1;
105
        longjmp(jbuf, 1);
106
        static_i=2;
107
    } // if
108
 
109
    CYG_TEST_PASS_FAIL( static_i==1, "Simple test 1" );
110
 
111
    // Test 2
112
    j=2;
113
 
114
    if ( !setjmp(jbuf) )
115
    {
116
        static_i=3;
117
        longjmp(jbuf, 1);
118
        static_i=4;
119
        j=3;
120
    } // if
121
 
122
    CYG_TEST_PASS_FAIL( (static_i==3) && (j==2), "Simple test 2" );
123
 
124
    // Test 3
125
 
126
    static_i=0;
127
    switch (setjmp(jbuf))
128
    {
129
    case 0:
130
        static_i++;
131
        if (static_i!=1)
132
            CYG_TEST_FAIL("Test of value passed to longjmp()");
133
        else
134
            longjmp(jbuf, 5);
135
        break;
136
    case 5:
137
        static_i++;
138
        CYG_TEST_PASS_FAIL( (static_i==2),"Test of value passed to longjmp()");
139
        break;
140
    default:
141
        CYG_TEST_FAIL( "Test of value passed to longjmp()");
142
        break;
143
    } // switch
144
 
145
    // Test 3
146
 
147
    static_i=0;
148
    switch (setjmp(jbuf))
149
    {
150
    case 0:
151
        static_i++;
152
        if (static_i!=1)
153
            CYG_TEST_FAIL("Test of value passed to longjmp() being 0");
154
        else
155
            longjmp(jbuf, 0);
156
        break;
157
    case 1:
158
        static_i++;
159
        CYG_TEST_PASS_FAIL( (static_i==2),
160
                            "Test of value passed to longjmp() being 0");
161
        break;
162
    default:
163
        CYG_TEST_FAIL( "Test of value passed to longjmp() being 0");
164
        break;
165
    } // switch
166
 
167
 
168
    // Test 4
169
 
170
    vol_k=0;
171
    static_i=0;
172
    setjmp( jbuf );
173
 
174
    static_i++;
175
    setjmp( jbuf2 );
176
 
177
    static_i++;
178
    setjmp( jbuf3 );
179
 
180
    if (vol_k==0)
181
    {
182
        vol_k++;
183
        longjmp( jbuf2, 1 );
184
    }
185
    else
186
    {
187
        CYG_TEST_PASS_FAIL( (vol_k==1) && (static_i==3), "Multiple setjmp's" );
188
    }
189
 
190
    // Test 5
191
 
192
    vol_k=3;
193
    if ( (j=setjmp(jbuf)) == 0 )
194
    {
195
        volatile int l; // put something extra on the stack to confuse things
196
 
197
        vol_k++;
198
        l=test_fun();
199
        l++;
200
        vol_k=l;
201
    } // if
202
 
203
    CYG_TEST_PASS_FAIL( (j==5) && (vol_k==4), "longjmp from a function" );
204
 
205
    // Test 6
206
 
207
    vol_k=0;
208
 
209
    vol_l=setjmp(jbuf);
210
 
211
    vol_k += 3;
212
 
213
    if (!vol_l)
214
        test_fun();
215
 
216
    vol_l=setjmp(jbuf);
217
 
218
    vol_k += 7;
219
 
220
    if (!vol_l)
221
        test_fun();
222
 
223
    CYG_TEST_PASS_FAIL ( (vol_k == 20), "Repeated longjmps from a function" );
224
 
225
//    CYG_TEST_NA("Testing is not applicable to this configuration");
226
 
227
    CYG_TEST_FINISH("Finished tests from testcase " __FILE__ " for C library "
228
                    "setjmp()/longjmp() functions");
229
 
230
} // main()
231
 
232
// EOF setjmp.c

powered by: WebSVN 2.1.0

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