|
0
|
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 Cygnus eCos Public License |
|
|
12 // Version 1.0 (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://sourceware.cygnus.com/ecos |
|
|
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 Cygnus Operating System, released |
|
|
22 // September 30, 1998. |
|
|
23 // |
|
|
24 // The Initial Developer of the Original Code is Cygnus. Portions created |
|
2
|
25 // by Cygnus are Copyright (C) 1998,1999 Cygnus Solutions. All Rights Reserved. |
|
0
|
26 // ------------------------------------------- |
|
|
27 // |
|
|
28 //####COPYRIGHTEND#### |
|
|
29 //================================================================= |
|
|
30 //#####DESCRIPTIONBEGIN#### |
|
|
31 // |
|
2
|
32 // Author(s): jlarmour |
|
|
33 // Contributors: jlarmour |
|
0
|
34 // Date: 1998/8/31 |
|
|
35 // Description: Contains testcode for C library realloc() function |
|
|
36 // |
|
|
37 // |
|
|
38 //####DESCRIPTIONEND#### |
|
|
39 |
|
|
40 // Declarations for test system: |
|
|
41 // |
|
|
42 // TESTCASE_TYPE=CYG_TEST_MODULE |
|
|
43 |
|
|
44 |
|
|
45 // INCLUDES |
|
|
46 |
|
2
|
47 #include <pkgconf/system.h> // Overall system configuration |
|
|
48 #include <pkgconf/libc.h> // config header for C library so we can know |
|
|
49 // size of malloc pool |
|
|
50 #ifdef CYGPKG_KERNEL |
|
|
51 # include <pkgconf/kernel.h> // CYGSEM_KERNEL_MEMORY_COALESCE |
|
|
52 #endif |
|
0
|
53 #include <stdlib.h> |
|
|
54 #include <cyg/infra/testcase.h> |
|
|
55 #include <sys/cstartup.h> // C library initialisation |
|
|
56 |
|
|
57 |
|
|
58 // FUNCTIONS |
|
|
59 |
|
|
60 externC void |
|
|
61 cyg_package_start( void ) |
|
|
62 { |
|
|
63 #ifdef CYGPKG_LIBC |
|
|
64 cyg_iso_c_start(); |
|
|
65 #else |
|
|
66 (void)main(0, NULL); |
|
|
67 #endif |
|
|
68 } // cyg_package_start() |
|
|
69 |
|
|
70 |
|
2
|
71 #if defined(CYGPKG_LIBC) && defined(CYGPKG_LIBC_MALLOC) && \ |
|
|
72 defined(CYGSEM_KERNEL_MEMORY_COALESCE) |
|
0
|
73 |
|
|
74 static const char alphabet[]="abcdefghijklmnopqrstuvwxyz{-}[]#';:@~!$^&*()" |
|
|
75 "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; |
|
|
76 |
|
|
77 static int |
|
|
78 compare_with_alphabet( char *buf, int size, int offset ) |
|
|
79 { |
|
|
80 int i, buf_offset; |
|
|
81 |
|
|
82 for (i=offset, buf_offset=0; |
|
|
83 buf_offset < size; |
|
|
84 buf_offset++,i++ ) { |
|
|
85 |
|
|
86 if ( i==sizeof(alphabet)-1 ) |
|
|
87 i=0; |
|
|
88 |
|
|
89 if ( buf[buf_offset] != alphabet[i] ) { |
|
|
90 CYG_TEST_FAIL( "buffer has not retained correct data!"); |
|
|
91 return 0; // fail |
|
|
92 } // if |
|
|
93 } // for |
|
|
94 |
|
|
95 return 1; // success |
|
|
96 } // compare_with_alphabet() |
|
|
97 |
|
|
98 static int |
|
|
99 fill_with_alphabet( char *buf, int size, int offset ) |
|
|
100 { |
|
|
101 int i, buf_offset; |
|
|
102 |
|
|
103 for (i=offset, buf_offset=0; |
|
|
104 buf_offset < size; |
|
|
105 buf_offset++,i++ ) { |
|
|
106 |
|
|
107 if ( i==sizeof(alphabet)-1 ) |
|
|
108 i=0; |
|
|
109 |
|
|
110 buf[buf_offset] = alphabet[i]; |
|
|
111 |
|
|
112 } // for |
|
|
113 |
|
|
114 return compare_with_alphabet( buf, size, offset); // be sure |
|
|
115 } // fill_with_alphabet() |
|
|
116 |
|
2
|
117 #endif // if defined(CYGPKG_LIBC) && defined(CYGPKG_LIBC_MALLOC) && |
|
|
118 // defined(CYGSEM_KERNEL_MEMORY_COALESCE) |
|
|
119 |
|
0
|
120 |
|
|
121 int |
|
|
122 main( int argc, char *argv[] ) |
|
|
123 { |
|
2
|
124 #if defined(CYGPKG_LIBC) && defined(CYGPKG_LIBC_MALLOC) && \ |
|
|
125 defined(CYGSEM_KERNEL_MEMORY_COALESCE) |
|
|
126 |
|
0
|
127 char *str; |
|
|
128 int size = CYGNUM_LIBC_MALLOC_MEMPOOL_SIZE/2; |
|
|
129 #endif |
|
|
130 |
|
|
131 CYG_TEST_INIT(); |
|
|
132 |
|
|
133 CYG_TEST_INFO("Starting tests from testcase " __FILE__ " for C library " |
|
|
134 "realloc() function"); |
|
|
135 |
|
2
|
136 #if defined(CYGPKG_LIBC) && defined(CYGPKG_LIBC_MALLOC) && \ |
|
|
137 defined(CYGSEM_KERNEL_MEMORY_COALESCE) |
|
0
|
138 |
|
|
139 str = (char *)realloc( NULL, size ); |
|
|
140 CYG_TEST_PASS_FAIL( str != NULL, "realloc doing only allocation"); |
|
|
141 CYG_TEST_PASS_FAIL( fill_with_alphabet( str, size, 0 ), |
|
|
142 "allocation usability"); |
|
|
143 |
|
|
144 str = (char *)realloc( str, 0 ); |
|
|
145 CYG_TEST_PASS_FAIL( str == NULL, "realloc doing implicit free" ); |
|
|
146 |
|
|
147 str = (char *)realloc( NULL, size/2 ); |
|
|
148 CYG_TEST_PASS_FAIL( str != NULL, "realloc doing allocation to half size"); |
|
|
149 CYG_TEST_PASS_FAIL( fill_with_alphabet( str, size/2, 5 ), |
|
|
150 "half allocation usability"); |
|
|
151 |
|
|
152 str = (char *)realloc( str, size ); |
|
|
153 CYG_TEST_PASS_FAIL( str != NULL, |
|
|
154 "reallocing allocation back to normal size"); |
|
|
155 CYG_TEST_PASS_FAIL( compare_with_alphabet(str, size/2, 5), |
|
|
156 "after realloc to normal size, old contents kept" ); |
|
|
157 CYG_TEST_PASS_FAIL( fill_with_alphabet( str, size, 3 ), |
|
|
158 "reallocation normal size usability"); |
|
|
159 |
|
|
160 str = (char *)realloc( str, size/4 ); |
|
|
161 CYG_TEST_PASS_FAIL( str != NULL, "reallocing allocation to quarter size"); |
|
|
162 CYG_TEST_PASS_FAIL( compare_with_alphabet(str, size/4, 3), |
|
|
163 "after realloc to quarter size, old contents kept" ); |
|
|
164 CYG_TEST_PASS_FAIL( fill_with_alphabet( str, size/4, 1 ), |
|
|
165 "reallocation quarter size usability"); |
|
|
166 |
|
|
167 CYG_TEST_PASS_FAIL( realloc( str, size*100 ) == NULL, |
|
|
168 "reallocing allocation that is too large" ); |
|
|
169 CYG_TEST_PASS_FAIL( compare_with_alphabet( str, size/4, 1 ), |
|
|
170 "Checking old contents maintained despite failure" ); |
|
|
171 |
|
|
172 str = (char *)realloc( str, 0 ); |
|
|
173 CYG_TEST_PASS_FAIL( str == NULL, "realloc doing implicit free again" ); |
|
|
174 |
|
|
175 |
|
|
176 #else |
|
2
|
177 CYG_TEST_NA("Testing is not applicable to this configuration"); |
|
|
178 #endif // if defined(CYGPKG_LIBC) && defined(CYGPKG_LIBC_MALLOC) && |
|
|
179 // defined(CYGSEM_KERNEL_MEMORY_COALESCE) |
|
|
180 |
|
0
|
181 |
|
|
182 CYG_TEST_FINISH("Finished tests from testcase " __FILE__ " for C library " |
|
|
183 "realloc() function"); |
|
|
184 } // main() |
|
|
185 |
|
|
186 // EOF realloc.c |