comparison packages/services/memalloc/common/current/tests/malloc3.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 // 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 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 malloc(), calloc() and
38 // free() functions
39 //
40 //
41 //####DESCRIPTIONEND####
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 // FUNCTIONS
74
75 extern int
76 cyg_memalloc_maxalloc( void );
77
78 static int
79 fill_with_data( char *buf, int size )
80 {
81 int i;
82
83 for (i=0; i < size; i++)
84 buf[i] = 'f';
85
86 for (i=0; i < size; i++)
87 if (buf[i] != 'f') {
88 CYG_TEST_FAIL( "data written to buffer does not compare "
89 "correctly! #1" );
90 return 0;
91 } // if
92
93
94 for (i=0; i < size; i++)
95 buf[i] = 'z';
96
97 for (i=0; i < size; i++)
98 if (buf[i] != 'z') {
99 CYG_TEST_FAIL( "data written to buffer does not compare "
100 "correctly! #2" );
101 return 0;
102 } // if
103
104 return 1;
105 } // fill_with_data()
106
107 int
108 main( int argc, char *argv[] )
109 {
110 char *str;
111 int size;
112 int poolmax;
113
114 CYG_TEST_INIT();
115
116 CYG_TEST_INFO("Starting tests from testcase " __FILE__ " for C library "
117 "malloc() and free() functions");
118 CYG_TEST_INFO("This checks allocation and freeing of large regions");
119
120 poolmax = mallinfo().maxfree;
121
122 if ( poolmax <= 0 ) {
123 CYG_TEST_FAIL_FINISH( "Can't determine allocation size to use" );
124 }
125
126 size = poolmax/2;
127
128 // Don't allocate all the memory at once - leave room for any structures
129 // used to manage the memory
130 str = (char *)malloc( size );
131 CYG_TEST_PASS_FAIL( str != NULL, "allocation 1");
132 CYG_TEST_PASS_FAIL( fill_with_data( str, size ), "allocation 1 usability");
133 free( str );
134
135
136 str = (char *)malloc( size );
137 CYG_TEST_PASS_FAIL( str != NULL, "allocation 2");
138 CYG_TEST_PASS_FAIL( fill_with_data( str, size ), "allocation 2 usability");
139 free( str );
140
141 str = (char *)malloc( size );
142 CYG_TEST_PASS_FAIL( str != NULL, "allocation 3");
143 CYG_TEST_PASS_FAIL( fill_with_data( str, size ), "allocation 3 usability");
144 free( str );
145
146 str = (char *)malloc( size );
147 CYG_TEST_PASS_FAIL( str != NULL, "allocation 4");
148 CYG_TEST_PASS_FAIL( fill_with_data( str, size ), "allocation 4 usability");
149 free( str );
150
151 str = (char *)malloc( size );
152 CYG_TEST_PASS_FAIL( str != NULL, "allocation 5");
153 CYG_TEST_PASS_FAIL( fill_with_data( str, size ), "allocation 5 usability");
154 free( str );
155
156 str = (char *)malloc( size );
157 CYG_TEST_PASS_FAIL( str != NULL, "allocation 6");
158 CYG_TEST_PASS_FAIL( fill_with_data( str, size ), "allocation 6 usability");
159 free( str );
160
161 str = (char *)malloc( size );
162 CYG_TEST_PASS_FAIL( str != NULL, "allocation 7");
163 CYG_TEST_PASS_FAIL( fill_with_data( str, size ), "allocation 7 usability");
164 free( str );
165
166 str = (char *)malloc( size );
167 CYG_TEST_PASS_FAIL( str != NULL, "allocation 8");
168 CYG_TEST_PASS_FAIL( fill_with_data( str, size ), "allocation 8 usability");
169 free( str );
170
171 str = (char *)malloc( size );
172 CYG_TEST_PASS_FAIL( str != NULL, "allocation 9");
173 CYG_TEST_PASS_FAIL( fill_with_data( str, size ), "allocation 9 usability");
174 free( str );
175
176 str = (char *)malloc( size );
177 CYG_TEST_PASS_FAIL( str != NULL, "allocation 10");
178 CYG_TEST_PASS_FAIL( fill_with_data( str, size ),"allocation 10 usability");
179 free( str );
180
181 CYG_TEST_FINISH("Finished tests from testcase " __FILE__ " for C library "
182 "malloc() and free() functions");
183 } // main()
184
185 #endif // ifndef NA_MSG
186
187 // EOF malloc3.c