|
0
|
1 //================================================================= |
|
|
2 // |
|
|
3 // malloc3.c |
|
|
4 // |
|
|
5 // Testcase for C library malloc(), calloc() and free() |
|
|
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/6/3 |
|
|
35 // Description: Contains testcode for C library malloc(), calloc() and |
|
|
36 // free() functions |
|
|
37 // |
|
|
38 // |
|
|
39 //####DESCRIPTIONEND#### |
|
|
40 |
|
|
41 // Declarations for test system: |
|
|
42 // |
|
|
43 // TESTCASE_TYPE=CYG_TEST_MODULE |
|
|
44 |
|
|
45 |
|
|
46 // INCLUDES |
|
|
47 |
|
2
|
48 #include <pkgconf/system.h> // Overall system configuration |
|
|
49 #include <pkgconf/libc.h> // config header for C library so we can know |
|
|
50 // size of malloc pool |
|
|
51 #ifdef CYGPKG_KERNEL |
|
|
52 # include <pkgconf/kernel.h> // CYGSEM_KERNEL_MEMORY_COALESCE |
|
|
53 #endif |
|
0
|
54 #include <stdlib.h> |
|
|
55 #include <cyg/infra/testcase.h> |
|
|
56 #include <sys/cstartup.h> // C library initialisation |
|
|
57 |
|
|
58 |
|
|
59 // FUNCTIONS |
|
|
60 |
|
|
61 externC void |
|
|
62 cyg_package_start( void ) |
|
|
63 { |
|
|
64 #ifdef CYGPKG_LIBC |
|
|
65 cyg_iso_c_start(); |
|
|
66 #else |
|
|
67 (void)main(0, NULL); |
|
|
68 #endif |
|
|
69 } // cyg_package_start() |
|
|
70 |
|
|
71 |
|
2
|
72 #if defined(CYGPKG_LIBC) && defined(CYGPKG_LIBC_MALLOC) && \ |
|
|
73 defined(CYGSEM_KERNEL_MEMORY_COALESCE) |
|
0
|
74 static int |
|
|
75 fill_with_data( char *buf, int size ) |
|
|
76 { |
|
|
77 int i; |
|
|
78 |
|
|
79 for (i=0; i < size; i++) |
|
|
80 buf[i] = 'f'; |
|
|
81 |
|
|
82 for (i=0; i < size; i++) |
|
|
83 if (buf[i] != 'f') { |
|
|
84 CYG_TEST_FAIL( "data written to buffer does not compare " |
|
|
85 "correctly! #1" ); |
|
|
86 return 0; |
|
|
87 } // if |
|
|
88 |
|
|
89 |
|
|
90 for (i=0; i < size; i++) |
|
|
91 buf[i] = 'z'; |
|
|
92 |
|
|
93 for (i=0; i < size; i++) |
|
|
94 if (buf[i] != 'z') { |
|
|
95 CYG_TEST_FAIL( "data written to buffer does not compare " |
|
|
96 "correctly! #2" ); |
|
|
97 return 0; |
|
|
98 } // if |
|
|
99 |
|
|
100 return 1; |
|
|
101 } // fill_with_data() |
|
|
102 |
|
2
|
103 #endif // if defined(CYGPKG_LIBC) && defined(CYGPKG_LIBC_MALLOC) && |
|
|
104 // defined(CYGSEM_KERNEL_MEMORY_COALESCE) |
|
|
105 |
|
0
|
106 |
|
|
107 int |
|
|
108 main( int argc, char *argv[] ) |
|
|
109 { |
|
2
|
110 #if defined(CYGPKG_LIBC) && defined(CYGPKG_LIBC_MALLOC) && \ |
|
|
111 defined(CYGSEM_KERNEL_MEMORY_COALESCE) |
|
|
112 |
|
0
|
113 char *str; |
|
|
114 int size = CYGNUM_LIBC_MALLOC_MEMPOOL_SIZE/2; |
|
|
115 #endif |
|
|
116 |
|
|
117 CYG_TEST_INIT(); |
|
|
118 |
|
|
119 CYG_TEST_INFO("Starting tests from testcase " __FILE__ " for C library " |
|
|
120 "malloc() and free() functions"); |
|
|
121 CYG_TEST_INFO("This checks allocation and freeing of large regions"); |
|
|
122 |
|
2
|
123 #if defined(CYGPKG_LIBC) && defined(CYGPKG_LIBC_MALLOC) && \ |
|
|
124 defined(CYGSEM_KERNEL_MEMORY_COALESCE) |
|
0
|
125 |
|
|
126 // Don't allocate all the memory at once - leave room for any structures |
|
|
127 // used to manage the memory |
|
|
128 str = (char *)malloc( size ); |
|
|
129 CYG_TEST_PASS_FAIL( str != NULL, "allocation 1"); |
|
|
130 CYG_TEST_PASS_FAIL( fill_with_data( str, size ), "allocation 1 usability"); |
|
|
131 free( str ); |
|
|
132 |
|
|
133 |
|
|
134 str = (char *)malloc( size ); |
|
|
135 CYG_TEST_PASS_FAIL( str != NULL, "allocation 2"); |
|
|
136 CYG_TEST_PASS_FAIL( fill_with_data( str, size ), "allocation 2 usability"); |
|
|
137 free( str ); |
|
|
138 |
|
|
139 str = (char *)malloc( size ); |
|
|
140 CYG_TEST_PASS_FAIL( str != NULL, "allocation 3"); |
|
|
141 CYG_TEST_PASS_FAIL( fill_with_data( str, size ), "allocation 3 usability"); |
|
|
142 free( str ); |
|
|
143 |
|
|
144 str = (char *)malloc( size ); |
|
|
145 CYG_TEST_PASS_FAIL( str != NULL, "allocation 4"); |
|
|
146 CYG_TEST_PASS_FAIL( fill_with_data( str, size ), "allocation 4 usability"); |
|
|
147 free( str ); |
|
|
148 |
|
|
149 str = (char *)malloc( size ); |
|
|
150 CYG_TEST_PASS_FAIL( str != NULL, "allocation 5"); |
|
|
151 CYG_TEST_PASS_FAIL( fill_with_data( str, size ), "allocation 5 usability"); |
|
|
152 free( str ); |
|
|
153 |
|
|
154 str = (char *)malloc( size ); |
|
|
155 CYG_TEST_PASS_FAIL( str != NULL, "allocation 6"); |
|
|
156 CYG_TEST_PASS_FAIL( fill_with_data( str, size ), "allocation 6 usability"); |
|
|
157 free( str ); |
|
|
158 |
|
|
159 str = (char *)malloc( size ); |
|
|
160 CYG_TEST_PASS_FAIL( str != NULL, "allocation 7"); |
|
|
161 CYG_TEST_PASS_FAIL( fill_with_data( str, size ), "allocation 7 usability"); |
|
|
162 free( str ); |
|
|
163 |
|
|
164 str = (char *)malloc( size ); |
|
|
165 CYG_TEST_PASS_FAIL( str != NULL, "allocation 8"); |
|
|
166 CYG_TEST_PASS_FAIL( fill_with_data( str, size ), "allocation 8 usability"); |
|
|
167 free( str ); |
|
|
168 |
|
|
169 str = (char *)malloc( size ); |
|
|
170 CYG_TEST_PASS_FAIL( str != NULL, "allocation 9"); |
|
|
171 CYG_TEST_PASS_FAIL( fill_with_data( str, size ), "allocation 9 usability"); |
|
|
172 free( str ); |
|
|
173 |
|
|
174 str = (char *)malloc( size ); |
|
|
175 CYG_TEST_PASS_FAIL( str != NULL, "allocation 10"); |
|
|
176 CYG_TEST_PASS_FAIL( fill_with_data( str, size ),"allocation 10 usability"); |
|
|
177 free( str ); |
|
|
178 |
|
|
179 |
|
|
180 #else |
|
2
|
181 CYG_TEST_NA("Testing is not applicable to this configuration"); |
|
|
182 #endif // if defined(CYGPKG_LIBC) && defined(CYGPKG_LIBC_MALLOC) && |
|
|
183 // defined(CYGSEM_KERNEL_MEMORY_COALESCE) |
|
|
184 |
|
0
|
185 |
|
|
186 CYG_TEST_FINISH("Finished tests from testcase " __FILE__ " for C library " |
|
|
187 "malloc() and free() functions"); |
|
|
188 } // main() |
|
|
189 |
|
|
190 // EOF malloc3.c |