comparison packages/language/c/libc/current/tests/stdlib/malloc3.c @ 0:3111d98ba7b3 ecos-v1_1-release

Initial commit of eCos version 1.1
author jlarmour
date Tue, 11 May 1999 11:16:07 +0000
parents
children 443894e2e912
comparison
equal deleted inserted replaced
-1:000000000000 0:3111d98ba7b3
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
25 // by Cygnus are Copyright (C) 1998 Cygnus Solutions. All Rights Reserved.
26 // -------------------------------------------
27 //
28 //####COPYRIGHTEND####
29 //=================================================================
30 //#####DESCRIPTIONBEGIN####
31 //
32 // Author(s): jlarmour@cygnus.co.uk
33 // Contributors: jlarmour@cygnus.co.uk
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
48 #include <pkgconf/libc.h> // config header for C library so we can know size
49 // of malloc pool
50 #include <stdlib.h>
51 #include <cyg/infra/testcase.h>
52 #include <sys/cstartup.h> // C library initialisation
53
54
55 // FUNCTIONS
56
57 externC void
58 cyg_package_start( void )
59 {
60 #ifdef CYGPKG_LIBC
61 cyg_iso_c_start();
62 #else
63 (void)main(0, NULL);
64 #endif
65 } // cyg_package_start()
66
67
68 #if defined(CYGPKG_LIBC) && defined(CYGPKG_LIBC_MALLOC)
69 static int
70 fill_with_data( char *buf, int size )
71 {
72 int i;
73
74 for (i=0; i < size; i++)
75 buf[i] = 'f';
76
77 for (i=0; i < size; i++)
78 if (buf[i] != 'f') {
79 CYG_TEST_FAIL( "data written to buffer does not compare "
80 "correctly! #1" );
81 return 0;
82 } // if
83
84
85 for (i=0; i < size; i++)
86 buf[i] = 'z';
87
88 for (i=0; i < size; i++)
89 if (buf[i] != 'z') {
90 CYG_TEST_FAIL( "data written to buffer does not compare "
91 "correctly! #2" );
92 return 0;
93 } // if
94
95 return 1;
96 } // fill_with_data()
97
98 #endif // if defined(CYGPKG_LIBC) && defined(CYGPKG_LIBC_MALLOC)
99
100 int
101 main( int argc, char *argv[] )
102 {
103 #if defined(CYGPKG_LIBC) && defined(CYGPKG_LIBC_MALLOC)
104 char *str;
105 int size = CYGNUM_LIBC_MALLOC_MEMPOOL_SIZE/2;
106 #endif
107
108 CYG_TEST_INIT();
109
110 CYG_TEST_INFO("Starting tests from testcase " __FILE__ " for C library "
111 "malloc() and free() functions");
112 CYG_TEST_INFO("This checks allocation and freeing of large regions");
113
114 #if defined(CYGPKG_LIBC) && defined(CYGPKG_LIBC_MALLOC)
115
116 // Don't allocate all the memory at once - leave room for any structures
117 // used to manage the memory
118 str = (char *)malloc( size );
119 CYG_TEST_PASS_FAIL( str != NULL, "allocation 1");
120 CYG_TEST_PASS_FAIL( fill_with_data( str, size ), "allocation 1 usability");
121 free( str );
122
123
124 str = (char *)malloc( size );
125 CYG_TEST_PASS_FAIL( str != NULL, "allocation 2");
126 CYG_TEST_PASS_FAIL( fill_with_data( str, size ), "allocation 2 usability");
127 free( str );
128
129 str = (char *)malloc( size );
130 CYG_TEST_PASS_FAIL( str != NULL, "allocation 3");
131 CYG_TEST_PASS_FAIL( fill_with_data( str, size ), "allocation 3 usability");
132 free( str );
133
134 str = (char *)malloc( size );
135 CYG_TEST_PASS_FAIL( str != NULL, "allocation 4");
136 CYG_TEST_PASS_FAIL( fill_with_data( str, size ), "allocation 4 usability");
137 free( str );
138
139 str = (char *)malloc( size );
140 CYG_TEST_PASS_FAIL( str != NULL, "allocation 5");
141 CYG_TEST_PASS_FAIL( fill_with_data( str, size ), "allocation 5 usability");
142 free( str );
143
144 str = (char *)malloc( size );
145 CYG_TEST_PASS_FAIL( str != NULL, "allocation 6");
146 CYG_TEST_PASS_FAIL( fill_with_data( str, size ), "allocation 6 usability");
147 free( str );
148
149 str = (char *)malloc( size );
150 CYG_TEST_PASS_FAIL( str != NULL, "allocation 7");
151 CYG_TEST_PASS_FAIL( fill_with_data( str, size ), "allocation 7 usability");
152 free( str );
153
154 str = (char *)malloc( size );
155 CYG_TEST_PASS_FAIL( str != NULL, "allocation 8");
156 CYG_TEST_PASS_FAIL( fill_with_data( str, size ), "allocation 8 usability");
157 free( str );
158
159 str = (char *)malloc( size );
160 CYG_TEST_PASS_FAIL( str != NULL, "allocation 9");
161 CYG_TEST_PASS_FAIL( fill_with_data( str, size ), "allocation 9 usability");
162 free( str );
163
164 str = (char *)malloc( size );
165 CYG_TEST_PASS_FAIL( str != NULL, "allocation 10");
166 CYG_TEST_PASS_FAIL( fill_with_data( str, size ),"allocation 10 usability");
167 free( str );
168
169
170 #else
171 CYG_TEST_PASS("Testing is not applicable to this configuration");
172 #endif // if defined(CYGPKG_LIBC) && defined(CYGPKG_LIBC_MALLOC)
173
174 CYG_TEST_FINISH("Finished tests from testcase " __FILE__ " for C library "
175 "malloc() and free() functions");
176 } // main()
177
178 // EOF malloc3.c