OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [services/] [memalloc/] [common/] [current/] [tests/] [malloc1.c] - Blame information for rev 838

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 786 skrzyp
//=================================================================
2
//
3
//        malloc1.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 Free Software Foundation, 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      
16
// version.                                                                 
17
//
18
// eCos is distributed in the hope that it will be useful, but WITHOUT      
19
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or    
20
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License    
21
// for more details.                                                        
22
//
23
// You should have received a copy of the GNU General Public License        
24
// along with eCos; if not, write to the Free Software Foundation, Inc.,    
25
// 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.            
26
//
27
// As a special exception, if other files instantiate templates or use      
28
// macros or inline functions from this file, or you compile this file      
29
// and link it with other works to produce a work based on this file,       
30
// this file does not by itself cause the resulting work to be covered by   
31
// the GNU General Public License. However the source code for this file    
32
// must still be made available in accordance with section (3) of the GNU   
33
// General Public License v2.                                               
34
//
35
// This exception does not invalidate any other reasons why a work based    
36
// on this file might be covered by the GNU General Public License.         
37
// -------------------------------------------                              
38
// ####ECOSGPLCOPYRIGHTEND####                                              
39
//=================================================================
40
//#####DESCRIPTIONBEGIN####
41
//
42
// Author(s):     jlarmour
43
// Contributors:  
44
// Date:          2000-04-30
45
// Description:   Contains testcode for C library malloc(), calloc() and
46
//                free() functions
47
//
48
//
49
//####DESCRIPTIONEND####
50
 
51
// INCLUDES
52
 
53
#include <pkgconf/system.h>
54
#include <pkgconf/memalloc.h> // config header
55
#ifdef CYGPKG_ISOINFRA
56
# include <pkgconf/isoinfra.h>
57
# include <stdlib.h>
58
#endif
59
#include <cyg/infra/testcase.h>
60
#include <limits.h> // INT_MAX
61
 
62
 
63
#if !defined(CYGPKG_ISOINFRA)
64
# define NA_MSG "Requires isoinfra package"
65
#elif !CYGINT_ISO_MAIN_STARTUP
66
# define NA_MSG "Requires main() to be called"
67
#elif !CYGINT_ISO_MALLOC
68
# define NA_MSG "Requires malloc"
69
#elif !CYGINT_ISO_MALLINFO
70
# define NA_MSG "Requires mallinfo"
71
#endif
72
 
73
#ifdef NA_MSG
74
void
75
cyg_start(void)
76
{
77
    CYG_TEST_INIT();
78
    CYG_TEST_NA( NA_MSG );
79
    CYG_TEST_FINISH("Done");
80
}
81
#else
82
 
83
 
84
// FUNCTIONS
85
 
86
int
87
main( int argc, char *argv[] )
88
{
89
    int *i;
90
    char *str, *str2, *str3;
91
    int j;
92
    int poolmax;
93
 
94
    CYG_TEST_INIT();
95
 
96
    CYG_TEST_INFO("Starting tests from testcase " __FILE__ " for C library "
97
                  "malloc(), calloc() and free() functions");
98
 
99
    poolmax = mallinfo().maxfree;
100
 
101
    if ( poolmax <= 0 ) {
102
        CYG_TEST_FAIL_FINISH( "Can't determine allocation size to use" );
103
    }
104
 
105
    // Test 1
106
    i = (int *) malloc( sizeof(int) );
107
 
108
    // check if it should fit into pool
109
    if (sizeof(int) > poolmax)
110
    {
111
        // didn't fit into pool, so should be NULL
112
        CYG_TEST_PASS_FAIL( i == NULL,
113
                            "1 int malloc with no space left works" );
114
    }
115
    else
116
    {
117
        // since it should fit into pool, we can fiddle with i
118
        *i=-12345;
119
        CYG_TEST_PASS_FAIL( i && (*i==-12345),
120
                            "1 int malloc with space left works" );
121
        free(i);
122
    } // else
123
 
124
    // Test 2
125
    str=(char *) malloc( 4096 );
126
 
127
    if ( 4096 > poolmax)
128
    {
129
        // didn't fit into pool, so should be NULL
130
        CYG_TEST_PASS_FAIL( str == NULL,"4K string with no space left works" );
131
    }
132
    else
133
    {
134
        // since it should fit into pool, we can fiddle with it.
135
        for (j=0; j<1024; j++)
136
        {
137
            str[j*4] = 'f';
138
            str[(j*4)+1] = 'r';
139
            str[(j*4)+2] = 'e';
140
            str[(j*4)+3] = 'd';
141
        } // for
142
 
143
        for (j=0; j<1024; j++)
144
        {
145
            if ( ((str[j*4] != 'f') ||
146
                  (str[(j*4)+1] != 'r') ||
147
                  (str[(j*4)+2] != 'e') ||
148
                  (str[(j*4)+3] != 'd')) )
149
                break;
150
        } // for
151
 
152
        // did j reach the top?
153
        CYG_TEST_PASS_FAIL( j==1024, "4K string with space left works" );
154
 
155
        free(str);
156
    } // else       
157
 
158
 
159
    // Test 3
160
    str=(char *) calloc( 2, 1024 );
161
 
162
    if ( 2048 > poolmax)
163
    {
164
        // didn't fit into pool, so should be NULL
165
        CYG_TEST_PASS_FAIL( str == NULL,
166
                            "calloc 2K string with no space left works" );
167
    }
168
    else
169
    {
170
        // check its zeroed
171
        for ( j=0; j<2048; j++ )
172
        {
173
            if (str[j] != 0)
174
                break;
175
        } // for
176
 
177
        CYG_TEST_PASS_FAIL( j==2048, "calloc 2K string is cleared" );
178
 
179
        // since it should fit into pool, we can fiddle with it.
180
        for (j=0; j<512; j++)
181
        {
182
            str[j*4] = 'j';
183
            str[(j*4)+1] = 'i';
184
            str[(j*4)+2] = 'f';
185
            str[(j*4)+3] = 'l';
186
        } // for
187
 
188
        for (j=0; j<512; j++)
189
        {
190
            if ( ((str[j*4] != 'j') ||
191
                  (str[(j*4)+1] != 'i') ||
192
                  (str[(j*4)+2] != 'f') ||
193
                  (str[(j*4)+3] != 'l')) )
194
                break;
195
        } // for
196
 
197
        // did j reach the top?
198
        CYG_TEST_PASS_FAIL( j==512,
199
                            "calloc 2K string - with space left works" );
200
 
201
        free(str);
202
    } // else       
203
 
204
    // Test 4
205
#if defined(CYGIMP_MEMALLOC_MALLOC_VARIABLE_SIMPLE) && \
206
      defined(CYGSEM_MEMALLOC_ALLOCATOR_VARIABLE_COALESCE)
207
    poolmax = mallinfo().maxfree; // recalculate for non-coalescing allocator
208
#endif
209
    str=(char *)malloc( poolmax+1 );
210
    CYG_TEST_PASS_FAIL( str==NULL, "malloc too much data returns NULL" );
211
 
212
    // Test 5
213
    str=(char *)calloc( 1, poolmax+1 );
214
    CYG_TEST_PASS_FAIL( str==NULL, "calloc too much data returns NULL" );
215
 
216
    // Test 6
217
    str=(char *)malloc(0); if (str != NULL) free(str);
218
    str=(char *)calloc(0, 1); if (str != NULL) free(str);
219
    str=(char *)calloc(1, 0); if (str != NULL) free(str);
220
    str=(char *)calloc(0, 0); if (str != NULL) free(str);
221
    // simply shouldn't barf by this point
222
 
223
    CYG_TEST_PASS_FAIL( 1, "malloc and calloc of 0 bytes doesn't crash" );
224
 
225
    // Test 7
226
    str = (char *)malloc(10);
227
    i = (int *)malloc(sizeof(int));
228
    str2 = (char *)malloc(10);
229
 
230
    str3=(char *)i;
231
 
232
    CYG_TEST_PASS_FAIL( ((str3 <= str-sizeof(int))  || (str3 >= &str[10])) &&
233
                        ((str3 <= str2-sizeof(int)) || (str3 >= &str2[10])) &&
234
                        ((str+10 <= str2) || (str2+10 <= str)),
235
                        "Objects don't overlap" );
236
 
237
    // Test 8
238
 
239
    free(i);
240
    i=(int *)malloc(sizeof(int)*2);
241
    str3=(char *)i;
242
 
243
    CYG_TEST_PASS_FAIL( ((str3 <= str-sizeof(int))  || (str3 >= &str[10])) &&
244
                        ((str3 <= str2-sizeof(int)) || (str3 >= &str2[10])) &&
245
                        ((&str[10] <= str2) || (&str2[10] <= str)),
246
                        "Objects don't overlap when middle is freed" );
247
 
248
    free(i);
249
    free(str);
250
    free(str2);
251
 
252
    // Test 9
253
 
254
#if defined(CYGIMP_MEMALLOC_MALLOC_VARIABLE_SIMPLE) && \
255
      defined(CYGSEM_MEMALLOC_ALLOCATOR_VARIABLE_COALESCE)
256
    poolmax = mallinfo().maxfree; // recalculate for non-coalescing allocator
257
#endif
258
    str = (char *)malloc( poolmax );
259
    CYG_TEST_PASS_FAIL( str != NULL, "malloc of maximum free block size works");
260
    free(str);
261
 
262
    CYG_TEST_FINISH("Finished tests from testcase " __FILE__ " for C library "
263
                    "malloc(), calloc() and free() functions");
264
 
265
    return 0;
266
} // main()
267
 
268
#endif // ifndef NA_MSG
269
 
270
// EOF malloc1.c

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.