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

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [rtos/] [ecos-2.0/] [packages/] [services/] [memalloc/] [common/] [v2_0/] [tests/] [realloc.c] - Blame information for rev 174

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 27 unneback
//=================================================================
2
//
3
//        realloc.c
4
//
5
//        Testcase for C library realloc()
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
// Description:   Contains testcode for C library realloc() function
47
//
48
//
49
//####DESCRIPTIONEND####
50
 
51
 
52
// INCLUDES
53
 
54
#include <pkgconf/system.h>             // Overall system configuration
55
#include <pkgconf/memalloc.h>           // config header
56
#ifdef CYGPKG_ISOINFRA
57
# include <pkgconf/isoinfra.h>
58
# include <stdlib.h>
59
#endif
60
#include <cyg/infra/testcase.h>
61
 
62
#if !defined(CYGPKG_ISOINFRA)
63
# define NA_MSG "Requires isoinfra package"
64
#elif !CYGINT_ISO_MAIN_STARTUP
65
# define NA_MSG "Requires main() to be called"
66
#elif !CYGINT_ISO_MALLOC
67
# define NA_MSG "Requires malloc"
68
#elif !CYGINT_ISO_MALLINFO
69
# define NA_MSG "Requires mallinfo"
70
#endif
71
 
72
#ifdef NA_MSG
73
void
74
cyg_start(void)
75
{
76
    CYG_TEST_INIT();
77
    CYG_TEST_NA( NA_MSG );
78
    CYG_TEST_FINISH("Done");
79
}
80
#else
81
 
82
 
83
// FUNCTIONS
84
 
85
static const char alphabet[]="abcdefghijklmnopqrstuvwxyz{-}[]#';:@~!$^&*()"
86
                             "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
87
 
88
extern int
89
cyg_memalloc_maxalloc( void );
90
 
91
static int
92
compare_with_alphabet( char *buf, int size, int offset )
93
{
94
    int i, buf_offset;
95
 
96
    for (i=offset, buf_offset=0;
97
         buf_offset < size;
98
         buf_offset++,i++ ) {
99
 
100
        if ( i==sizeof(alphabet)-1 )
101
            i=0;
102
 
103
        if ( buf[buf_offset] != alphabet[i] ) {
104
            CYG_TEST_FAIL( "buffer has not retained correct data!");
105
            return 0; // fail
106
        } // if
107
    } // for
108
 
109
    return 1; // success
110
} // compare_with_alphabet()
111
 
112
static int
113
fill_with_alphabet( char *buf, int size, int offset )
114
{
115
    int i, buf_offset;
116
 
117
    for (i=offset, buf_offset=0;
118
         buf_offset < size;
119
         buf_offset++,i++ ) {
120
 
121
        if ( i==sizeof(alphabet)-1 )
122
            i=0;
123
 
124
        buf[buf_offset] = alphabet[i];
125
 
126
    } // for
127
 
128
    return compare_with_alphabet( buf, size, offset); // be sure
129
} // fill_with_alphabet()
130
 
131
 
132
int
133
main( int argc, char *argv[] )
134
{
135
    char *str;
136
    int size;
137
    int poolmax;
138
 
139
    CYG_TEST_INIT();
140
 
141
    CYG_TEST_INFO("Starting tests from testcase " __FILE__ " for C library "
142
                  "realloc() function");
143
 
144
    poolmax = mallinfo().maxfree;
145
 
146
    if ( poolmax <= 0 ) {
147
        CYG_TEST_FAIL_FINISH( "Can't determine allocation size to use" );
148
    }
149
 
150
    size = poolmax/2;
151
 
152
    str = (char *)realloc( NULL, size );
153
    CYG_TEST_PASS_FAIL( str != NULL, "realloc doing only allocation");
154
    CYG_TEST_PASS_FAIL( fill_with_alphabet( str, size, 0 ),
155
                        "allocation usability");
156
 
157
    str = (char *)realloc( str, 0 );
158
    CYG_TEST_PASS_FAIL( str == NULL, "realloc doing implicit free" );
159
 
160
    str = (char *)realloc( NULL, size/2 );
161
    CYG_TEST_PASS_FAIL( str != NULL, "realloc doing allocation to half size");
162
    CYG_TEST_PASS_FAIL( fill_with_alphabet( str, size/2, 5 ),
163
                        "half allocation usability");
164
 
165
    str = (char *)realloc( str, size );
166
    CYG_TEST_PASS_FAIL( str != NULL,
167
                        "reallocing allocation back to normal size");
168
    CYG_TEST_PASS_FAIL( compare_with_alphabet(str, size/2, 5),
169
                        "after realloc to normal size, old contents kept" );
170
    CYG_TEST_PASS_FAIL( fill_with_alphabet( str, size, 3 ),
171
                        "reallocation normal size usability");
172
 
173
    str = (char *)realloc( str, size/4 );
174
    CYG_TEST_PASS_FAIL( str != NULL, "reallocing allocation to quarter size");
175
    CYG_TEST_PASS_FAIL( compare_with_alphabet(str, size/4, 3),
176
                        "after realloc to quarter size, old contents kept" );
177
    CYG_TEST_PASS_FAIL( fill_with_alphabet( str, size/4, 1 ),
178
                        "reallocation quarter size usability");
179
 
180
    CYG_TEST_PASS_FAIL( realloc( str, size*4 ) == NULL,
181
                        "reallocing allocation that is too large" );
182
    CYG_TEST_PASS_FAIL( compare_with_alphabet( str, size/4, 1 ),
183
                        "Checking old contents maintained despite failure" );
184
 
185
    str = (char *)realloc( str, 0 );
186
    CYG_TEST_PASS_FAIL( str == NULL, "realloc doing implicit free again" );
187
 
188
    CYG_TEST_FINISH("Finished tests from testcase " __FILE__ " for C library "
189
                    "realloc() function");
190
 
191
    return 0;
192
} // main()
193
 
194
#endif // ifndef NA_MSG
195
 
196
// EOF realloc.c

powered by: WebSVN 2.1.0

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