]> git.karo-electronics.de Git - karo-tx-redboot.git/blob - packages/services/memalloc/common/v2_0/tests/malloc3.c
Initial revision
[karo-tx-redboot.git] / packages / services / memalloc / common / v2_0 / tests / malloc3.c
1 //=================================================================
2 //
3 //        malloc3.c
4 //
5 //        Testcase for C library malloc(), calloc() and free()
6 //
7 //=================================================================
8 //####ECOSGPLCOPYRIGHTBEGIN####
9 // -------------------------------------------
10 // This file is part of eCos, the Embedded Configurable Operating System.
11 // Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
12 //
13 // eCos is free software; you can redistribute it and/or modify it under
14 // the terms of the GNU General Public License as published by the Free
15 // Software Foundation; either version 2 or (at your option) any later version.
16 //
17 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY
18 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
20 // for more details.
21 //
22 // You should have received a copy of the GNU General Public License along
23 // with eCos; if not, write to the Free Software Foundation, Inc.,
24 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
25 //
26 // As a special exception, if other files instantiate templates or use macros
27 // or inline functions from this file, or you compile this file and link it
28 // with other works to produce a work based on this file, this file does not
29 // by itself cause the resulting work to be covered by the GNU General Public
30 // License. However the source code for this file must still be made available
31 // in accordance with section (3) of the GNU General Public License.
32 //
33 // This exception does not invalidate any other reasons why a work based on
34 // this file might be covered by the GNU General Public License.
35 //
36 // Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
37 // at http://sources.redhat.com/ecos/ecos-license/
38 // -------------------------------------------
39 //####ECOSGPLCOPYRIGHTEND####
40 //=================================================================
41 //#####DESCRIPTIONBEGIN####
42 //
43 // Author(s):     jlarmour
44 // Contributors:  
45 // Date:          2000-04-30
46 // Description:   Contains testcode for C library malloc(), calloc() and
47 //                free() functions
48 //
49 //
50 //####DESCRIPTIONEND####
51
52 // INCLUDES
53
54 #include <pkgconf/system.h>    // Overall system configuration
55 #include <pkgconf/memalloc.h>  // config header
56 #ifdef CYGPKG_ISOINFRA
57 # include <pkgconf/isoinfra.h>
58 # include <stdlib.h>
59 #endif
60 #include <cyg/infra/testcase.h>
61
62 #if !defined(CYGPKG_ISOINFRA)
63 # define NA_MSG "Requires isoinfra package"
64 #elif !CYGINT_ISO_MAIN_STARTUP
65 # define NA_MSG "Requires main() to be called"
66 #elif !CYGINT_ISO_MALLOC
67 # define NA_MSG "Requires malloc"
68 #elif !CYGINT_ISO_MALLINFO
69 # define NA_MSG "Requires mallinfo"
70 #endif
71
72 #ifdef NA_MSG
73 void
74 cyg_start(void)
75 {
76     CYG_TEST_INIT();
77     CYG_TEST_NA( NA_MSG );
78     CYG_TEST_FINISH("Done");
79 }
80 #else
81
82 // FUNCTIONS
83
84 extern int
85 cyg_memalloc_maxalloc( void );
86
87 static int
88 fill_with_data( char *buf, int size )
89 {
90     int i;
91
92     for (i=0; i < size; i++)
93         buf[i] = 'f';
94
95     for (i=0; i < size; i++)
96         if (buf[i] != 'f') {
97             CYG_TEST_FAIL( "data written to buffer does not compare "
98                            "correctly! #1" );
99             return 0;
100         } // if
101
102
103     for (i=0; i < size; i++)
104         buf[i] = 'z';
105
106     for (i=0; i < size; i++)
107         if (buf[i] != 'z') {
108             CYG_TEST_FAIL( "data written to buffer does not compare "
109                            "correctly! #2" );
110             return 0;
111         } // if
112
113     return 1;
114 } // fill_with_data()
115
116 int
117 main( int argc, char *argv[] )
118 {
119     char *str;
120     int size;
121     int poolmax;
122
123     CYG_TEST_INIT();
124
125     CYG_TEST_INFO("Starting tests from testcase " __FILE__ " for C library "
126                   "malloc() and free() functions");
127     CYG_TEST_INFO("This checks allocation and freeing of large regions");
128
129     poolmax = mallinfo().maxfree;
130     
131     if ( poolmax <= 0 ) {
132         CYG_TEST_FAIL_FINISH( "Can't determine allocation size to use" );
133     }
134
135     size = poolmax/2;
136
137     // Don't allocate all the memory at once - leave room for any structures
138     // used to manage the memory
139     str = (char *)malloc( size );
140     CYG_TEST_PASS_FAIL( str != NULL, "allocation 1");
141     CYG_TEST_PASS_FAIL( fill_with_data( str, size ), "allocation 1 usability");
142     free( str );
143
144
145     str = (char *)malloc( size );
146     CYG_TEST_PASS_FAIL( str != NULL, "allocation 2");
147     CYG_TEST_PASS_FAIL( fill_with_data( str, size ), "allocation 2 usability");
148     free( str );
149
150     str = (char *)malloc( size );
151     CYG_TEST_PASS_FAIL( str != NULL, "allocation 3");
152     CYG_TEST_PASS_FAIL( fill_with_data( str, size ), "allocation 3 usability");
153     free( str );
154
155     str = (char *)malloc( size );
156     CYG_TEST_PASS_FAIL( str != NULL, "allocation 4");
157     CYG_TEST_PASS_FAIL( fill_with_data( str, size ), "allocation 4 usability");
158     free( str );
159
160     str = (char *)malloc( size );
161     CYG_TEST_PASS_FAIL( str != NULL, "allocation 5");
162     CYG_TEST_PASS_FAIL( fill_with_data( str, size ), "allocation 5 usability");
163     free( str );
164
165     str = (char *)malloc( size );
166     CYG_TEST_PASS_FAIL( str != NULL, "allocation 6");
167     CYG_TEST_PASS_FAIL( fill_with_data( str, size ), "allocation 6 usability");
168     free( str );
169
170     str = (char *)malloc( size );
171     CYG_TEST_PASS_FAIL( str != NULL, "allocation 7");
172     CYG_TEST_PASS_FAIL( fill_with_data( str, size ), "allocation 7 usability");
173     free( str );
174
175     str = (char *)malloc( size );
176     CYG_TEST_PASS_FAIL( str != NULL, "allocation 8");
177     CYG_TEST_PASS_FAIL( fill_with_data( str, size ), "allocation 8 usability");
178     free( str );
179
180     str = (char *)malloc( size );
181     CYG_TEST_PASS_FAIL( str != NULL, "allocation 9");
182     CYG_TEST_PASS_FAIL( fill_with_data( str, size ), "allocation 9 usability");
183     free( str );
184
185     str = (char *)malloc( size );
186     CYG_TEST_PASS_FAIL( str != NULL, "allocation 10");
187     CYG_TEST_PASS_FAIL( fill_with_data( str, size ),"allocation 10 usability");
188     free( str );
189
190     CYG_TEST_FINISH("Finished tests from testcase " __FILE__ " for C library "
191                     "malloc() and free() functions");
192
193     return 0;
194 } // main()
195
196 #endif // ifndef NA_MSG
197
198 // EOF malloc3.c