]> git.karo-electronics.de Git - karo-tx-redboot.git/blob - packages/services/memalloc/common/v2_0/tests/heaptest.c
Initial revision
[karo-tx-redboot.git] / packages / services / memalloc / common / v2_0 / tests / heaptest.c
1 //=================================================================
2 //
3 //        heaptest.cxx
4 //
5 //        Test all the memory used by heaps to check it's all valid
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:          2001-07-17
46 // Description:   Tests all memory allocated for use by heaps.
47 //
48 //
49 //####DESCRIPTIONEND####
50
51 // INCLUDES
52
53 #include <pkgconf/system.h>
54 #include <pkgconf/hal.h>
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_MALLOC
65 # define NA_MSG "Requires malloc"
66 #elif !CYGINT_ISO_MALLINFO
67 # define NA_MSG "Requires mallinfo"
68 #endif
69
70 #ifdef NA_MSG
71
72 externC void
73 cyg_start(void)
74 {
75     CYG_TEST_INIT();
76     CYG_TEST_NA( NA_MSG );
77     CYG_TEST_FINISH("Done");
78 }
79 #else
80
81 #include <cyg/infra/diag.h>
82
83 #define ERRORTHRESHOLD 10
84 #define ITERS (cyg_test_is_simulator ? 1 : 10)
85 #define INTALIGNED(_x_) (!((unsigned long)(_x_) & (sizeof(int)-1)))
86
87 int
88 test_pat(unsigned char *buf, int size,
89          unsigned int pat, cyg_bool addrpat,
90          const char *testname)
91 {
92     unsigned char *bufptr=buf;
93     register unsigned int *ibufptr;
94     unsigned char *endptr=buf+size;
95     register unsigned int *endptra; // int aligned
96     int errors=0;
97     unsigned char bpat = pat & 0xFF;
98
99     endptra = (unsigned int *)((unsigned long)endptr & ~(sizeof(int)-1));
100     
101     // Set to the pattern
102     while (!INTALIGNED(bufptr)) {
103         if (addrpat)
104             bpat = ((int)bufptr)&0xFF;
105         *bufptr++ = bpat;
106     }
107
108     ibufptr = (unsigned int *)bufptr;
109     
110     while ( ibufptr < endptra ) {
111         if (addrpat)
112             pat = (unsigned int)ibufptr;
113         *ibufptr++ = pat;
114     }
115
116     bufptr = (unsigned char *)ibufptr;
117     while ( bufptr < endptr ) {
118         if (addrpat)
119             bpat = ((int)bufptr)&0xFF;
120         *bufptr++ = bpat;
121     }
122
123     // Now compare to the pattern
124     bufptr = buf;
125     while ( !INTALIGNED(bufptr) ) {
126         if (addrpat)
127             bpat = ((int)bufptr)&0xFF;
128         if ( *bufptr != bpat ) {
129             diag_printf( "FAIL:<Memory at 0x%08x: expected 0x%02x, read 0x%02x>\n", 
130                          (unsigned int)bufptr, (unsigned int)bpat, 
131                          (unsigned int)*bufptr );
132             if ( errors++ == ERRORTHRESHOLD )
133                 CYG_TEST_FAIL_FINISH( testname );
134         }
135         bufptr++;
136     }
137
138     ibufptr = (unsigned int *)bufptr;
139     
140     while ( ibufptr < endptra ) {
141         if (addrpat)
142             pat = (unsigned int)ibufptr;
143         if ( *ibufptr != pat ) {
144             diag_printf( "FAIL:<Memory at 0x%08x: expected 0x%08x, read 0x%08x>\n", 
145                          (unsigned int) ibufptr, pat, *ibufptr );
146             if ( errors++ == ERRORTHRESHOLD )
147                 CYG_TEST_FAIL_FINISH( testname );
148         }
149         ibufptr++;
150     }
151
152     bufptr = (unsigned char *)ibufptr;
153     while ( bufptr < endptr ) {
154         if (addrpat)
155             bpat = ((int)bufptr)&0xFF;
156         if ( *bufptr != bpat ) {
157             diag_printf( "FAIL:<Memory at 0x%08x: expected 0x%02x, read 0x%02x>\n", 
158                          (unsigned int) bufptr, (unsigned int)bpat, 
159                          (unsigned int)*bufptr );
160             if ( errors++ == ERRORTHRESHOLD )
161                 CYG_TEST_FAIL_FINISH( testname );
162         }
163         bufptr++;
164     }
165     if (errors)
166         CYG_TEST_FAIL( testname );
167     else
168         CYG_TEST_PASS( testname );
169     return errors;
170 } // test_pat()
171
172 externC void
173 cyg_start(void)
174 {
175     unsigned int allonesint=0, checkerboardint1=0, checkerboardint2=0;
176     int i;
177     int errors=0;
178     
179 #ifdef CYGSEM_HAL_STOP_CONSTRUCTORS_ON_FLAG
180     cyg_hal_invoke_constructors();
181 #endif
182     CYG_TEST_INIT();
183     CYG_TEST_INFO("Starting heaptest - testing all memory usable as heap");
184     CYG_TEST_INFO("Any failures reported may indicate failing RAM hardware,");
185     CYG_TEST_INFO("or an invalid memory map");
186
187     for (i=0; i<sizeof(int); i++) {
188         allonesint = allonesint << 8;
189         allonesint |= 0xFF;
190         checkerboardint1 = checkerboardint1 << 8;
191         checkerboardint1 |= 0xAA;
192         checkerboardint2 = checkerboardint2 << 8;
193         checkerboardint2 |= 0x55;
194     }
195
196     for (;;) {
197         struct mallinfo info;
198         unsigned char *buf;
199         
200         info = mallinfo();
201
202         if ( info.maxfree <= 0 )
203             break;
204
205         buf = malloc(info.maxfree);
206         if (!buf) {
207             diag_printf("Couldn't malloc %d bytes claimed as available",
208                         info.maxfree);
209             CYG_TEST_FAIL_FINISH("heaptest");
210         }
211
212         diag_printf( "INFO:<Testing memory at 0x%08x of size %d for %d iterations>\n",
213                      (unsigned int)buf, info.maxfree, ITERS );
214         for (i=0; i<ITERS; i++) {
215             errors += test_pat( buf, info.maxfree, 0, 0, "all zeroes" );
216             errors += test_pat( buf, info.maxfree, allonesint, 0,
217                                 "all ones" );
218             errors += test_pat( buf, info.maxfree, checkerboardint1, 0,
219                                 "checkerboard 1" );
220             errors += test_pat( buf, info.maxfree, checkerboardint2, 0,
221                                 "checkerboard 2" );
222             errors += test_pat( buf, info.maxfree, 0, 1,
223                                 "memory addr" );
224         }
225
226         // deliberately don't free so we get the next space
227     }
228
229     if (errors)
230         CYG_TEST_FAIL_FINISH( "heaptest errors found" );
231     else
232         CYG_TEST_PASS_FINISH( "heaptest OK" );
233 } // cyg_start()
234
235 #endif // !NA_MSG
236
237 // EOF heaptest.cxx