comparison packages/services/memalloc/common/current/tests/realloc.c @ 115:6ed91473a1cd ecos-sw-2000-08-21

Merge from eCos master repository on 2000-08-21-22:40:54-BST
author jlarmour
date Fri, 25 Aug 2000 17:32:38 +0000
parents
children 518f42066aba
comparison
equal deleted inserted replaced
114:5ad2b71d525e 115:6ed91473a1cd
1 //=================================================================
2 //
3 // realloc.c
4 //
5 // Testcase for C library realloc()
6 //
7 //=================================================================
8 //####COPYRIGHTBEGIN####
9 //
10 // -------------------------------------------
11 // The contents of this file are subject to the Red Hat eCos Public License
12 // Version 1.1 (the "License"); you may not use this file except in
13 // compliance with the License. You may obtain a copy of the License at
14 // http://www.redhat.com/
15 //
16 // Software distributed under the License is distributed on an "AS IS"
17 // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
18 // License for the specific language governing rights and limitations under
19 // the License.
20 //
21 // The Original Code is eCos - Embedded Configurable Operating System,
22 // released September 30, 1998.
23 //
24 // The Initial Developer of the Original Code is Red Hat.
25 // Portions created by Red Hat are
26 // Copyright (C) 1998, 1999, 2000 Red Hat, Inc.
27 // All Rights Reserved.
28 // -------------------------------------------
29 //
30 //####COPYRIGHTEND####
31 //=================================================================
32 //#####DESCRIPTIONBEGIN####
33 //
34 // Author(s): jlarmour
35 // Contributors:
36 // Date: 2000-04-30
37 // Description: Contains testcode for C library realloc() function
38 //
39 //
40 //####DESCRIPTIONEND####
41
42
43 // INCLUDES
44
45 #include <pkgconf/system.h> // Overall system configuration
46 #include <pkgconf/memalloc.h> // config header
47 #ifdef CYGPKG_ISOINFRA
48 # include <pkgconf/isoinfra.h>
49 # include <stdlib.h>
50 #endif
51 #include <cyg/infra/testcase.h>
52
53 #if !defined(CYGPKG_ISOINFRA)
54 # define NA_MSG "Requires isoinfra package"
55 #elif !CYGINT_ISO_MAIN_STARTUP
56 # define NA_MSG "Requires main() to be called"
57 #elif !CYGINT_ISO_MALLOC
58 # define NA_MSG "Requires malloc"
59 #elif !CYGINT_ISO_MALLINFO
60 # define NA_MSG "Requires mallinfo"
61 #endif
62
63 #ifdef NA_MSG
64 void
65 cyg_start()
66 {
67 CYG_TEST_INIT();
68 CYG_TEST_NA( NA_MSG );
69 CYG_TEST_FINISH("Done");
70 }
71 #else
72
73
74 // FUNCTIONS
75
76 static const char alphabet[]="abcdefghijklmnopqrstuvwxyz{-}[]#';:@~!$^&*()"
77 "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
78
79 extern int
80 cyg_memalloc_maxalloc( void );
81
82 static int
83 compare_with_alphabet( char *buf, int size, int offset )
84 {
85 int i, buf_offset;
86
87 for (i=offset, buf_offset=0;
88 buf_offset < size;
89 buf_offset++,i++ ) {
90
91 if ( i==sizeof(alphabet)-1 )
92 i=0;
93
94 if ( buf[buf_offset] != alphabet[i] ) {
95 CYG_TEST_FAIL( "buffer has not retained correct data!");
96 return 0; // fail
97 } // if
98 } // for
99
100 return 1; // success
101 } // compare_with_alphabet()
102
103 static int
104 fill_with_alphabet( char *buf, int size, int offset )
105 {
106 int i, buf_offset;
107
108 for (i=offset, buf_offset=0;
109 buf_offset < size;
110 buf_offset++,i++ ) {
111
112 if ( i==sizeof(alphabet)-1 )
113 i=0;
114
115 buf[buf_offset] = alphabet[i];
116
117 } // for
118
119 return compare_with_alphabet( buf, size, offset); // be sure
120 } // fill_with_alphabet()
121
122
123 int
124 main( int argc, char *argv[] )
125 {
126 char *str;
127 int size;
128 int poolmax;
129
130 CYG_TEST_INIT();
131
132 CYG_TEST_INFO("Starting tests from testcase " __FILE__ " for C library "
133 "realloc() function");
134
135 poolmax = mallinfo().maxfree;
136
137 if ( poolmax <= 0 ) {
138 CYG_TEST_FAIL_FINISH( "Can't determine allocation size to use" );
139 }
140
141 size = poolmax/2;
142
143 str = (char *)realloc( NULL, size );
144 CYG_TEST_PASS_FAIL( str != NULL, "realloc doing only allocation");
145 CYG_TEST_PASS_FAIL( fill_with_alphabet( str, size, 0 ),
146 "allocation usability");
147
148 str = (char *)realloc( str, 0 );
149 CYG_TEST_PASS_FAIL( str == NULL, "realloc doing implicit free" );
150
151 str = (char *)realloc( NULL, size/2 );
152 CYG_TEST_PASS_FAIL( str != NULL, "realloc doing allocation to half size");
153 CYG_TEST_PASS_FAIL( fill_with_alphabet( str, size/2, 5 ),
154 "half allocation usability");
155
156 str = (char *)realloc( str, size );
157 CYG_TEST_PASS_FAIL( str != NULL,
158 "reallocing allocation back to normal size");
159 CYG_TEST_PASS_FAIL( compare_with_alphabet(str, size/2, 5),
160 "after realloc to normal size, old contents kept" );
161 CYG_TEST_PASS_FAIL( fill_with_alphabet( str, size, 3 ),
162 "reallocation normal size usability");
163
164 str = (char *)realloc( str, size/4 );
165 CYG_TEST_PASS_FAIL( str != NULL, "reallocing allocation to quarter size");
166 CYG_TEST_PASS_FAIL( compare_with_alphabet(str, size/4, 3),
167 "after realloc to quarter size, old contents kept" );
168 CYG_TEST_PASS_FAIL( fill_with_alphabet( str, size/4, 1 ),
169 "reallocation quarter size usability");
170
171 CYG_TEST_PASS_FAIL( realloc( str, size*100 ) == NULL,
172 "reallocing allocation that is too large" );
173 CYG_TEST_PASS_FAIL( compare_with_alphabet( str, size/4, 1 ),
174 "Checking old contents maintained despite failure" );
175
176 str = (char *)realloc( str, 0 );
177 CYG_TEST_PASS_FAIL( str == NULL, "realloc doing implicit free again" );
178
179 CYG_TEST_FINISH("Finished tests from testcase " __FILE__ " for C library "
180 "realloc() function");
181 } // main()
182
183 #endif // ifndef NA_MSG
184
185 // EOF realloc.c