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