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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-2.0/] [packages/] [services/] [memalloc/] [common/] [v2_0/] [tests/] [malloc1.c] - Blame information for rev 174

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 27 unneback
//=================================================================
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 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>
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
#include <limits.h> // INT_MAX
62
 
63
 
64
#if !defined(CYGPKG_ISOINFRA)
65
# define NA_MSG "Requires isoinfra package"
66
#elif !CYGINT_ISO_MAIN_STARTUP
67
# define NA_MSG "Requires main() to be called"
68
#elif !CYGINT_ISO_MALLOC
69
# define NA_MSG "Requires malloc"
70
#elif !CYGINT_ISO_MALLINFO
71
# define NA_MSG "Requires mallinfo"
72
#endif
73
 
74
#ifdef NA_MSG
75
void
76
cyg_start(void)
77
{
78
    CYG_TEST_INIT();
79
    CYG_TEST_NA( NA_MSG );
80
    CYG_TEST_FINISH("Done");
81
}
82
#else
83
 
84
 
85
// FUNCTIONS
86
 
87
int
88
main( int argc, char *argv[] )
89
{
90
    int *i;
91
    char *str, *str2, *str3;
92
    int j;
93
    int poolmax;
94
 
95
    CYG_TEST_INIT();
96
 
97
    CYG_TEST_INFO("Starting tests from testcase " __FILE__ " for C library "
98
                  "malloc(), calloc() and free() functions");
99
 
100
    poolmax = mallinfo().maxfree;
101
 
102
    if ( poolmax <= 0 ) {
103
        CYG_TEST_FAIL_FINISH( "Can't determine allocation size to use" );
104
    }
105
 
106
    // Test 1
107
    i = (int *) malloc( sizeof(int) );
108
 
109
    // check if it should fit into pool
110
    if (sizeof(int) > poolmax)
111
    {
112
        // didn't fit into pool, so should be NULL
113
        CYG_TEST_PASS_FAIL( i == NULL,
114
                            "1 int malloc with no space left works" );
115
    }
116
    else
117
    {
118
        // since it should fit into pool, we can fiddle with i
119
        *i=-12345;
120
        CYG_TEST_PASS_FAIL( i && (*i==-12345),
121
                            "1 int malloc with space left works" );
122
        free(i);
123
    } // else
124
 
125
    // Test 2
126
    str=(char *) malloc( 4096 );
127
 
128
    if ( 4096 > poolmax)
129
    {
130
        // didn't fit into pool, so should be NULL
131
        CYG_TEST_PASS_FAIL( str == NULL,"4K string with no space left works" );
132
    }
133
    else
134
    {
135
        // since it should fit into pool, we can fiddle with it.
136
        for (j=0; j<1024; j++)
137
        {
138
            str[j*4] = 'f';
139
            str[(j*4)+1] = 'r';
140
            str[(j*4)+2] = 'e';
141
            str[(j*4)+3] = 'd';
142
        } // for
143
 
144
        for (j=0; j<1024; j++)
145
        {
146
            if ( ((str[j*4] != 'f') ||
147
                  (str[(j*4)+1] != 'r') ||
148
                  (str[(j*4)+2] != 'e') ||
149
                  (str[(j*4)+3] != 'd')) )
150
                break;
151
        } // for
152
 
153
        // did j reach the top?
154
        CYG_TEST_PASS_FAIL( j==1024, "4K string with space left works" );
155
 
156
        free(str);
157
    } // else       
158
 
159
 
160
    // Test 3
161
    str=(char *) calloc( 2, 1024 );
162
 
163
    if ( 2048 > poolmax)
164
    {
165
        // didn't fit into pool, so should be NULL
166
        CYG_TEST_PASS_FAIL( str == NULL,
167
                            "calloc 2K string with no space left works" );
168
    }
169
    else
170
    {
171
        // check its zeroed
172
        for ( j=0; j<2048; j++ )
173
        {
174
            if (str[j] != 0)
175
                break;
176
        } // for
177
 
178
        CYG_TEST_PASS_FAIL( j==2048, "calloc 2K string is cleared" );
179
 
180
        // since it should fit into pool, we can fiddle with it.
181
        for (j=0; j<512; j++)
182
        {
183
            str[j*4] = 'j';
184
            str[(j*4)+1] = 'i';
185
            str[(j*4)+2] = 'f';
186
            str[(j*4)+3] = 'l';
187
        } // for
188
 
189
        for (j=0; j<512; j++)
190
        {
191
            if ( ((str[j*4] != 'j') ||
192
                  (str[(j*4)+1] != 'i') ||
193
                  (str[(j*4)+2] != 'f') ||
194
                  (str[(j*4)+3] != 'l')) )
195
                break;
196
        } // for
197
 
198
        // did j reach the top?
199
        CYG_TEST_PASS_FAIL( j==512,
200
                            "calloc 2K string - with space left works" );
201
 
202
        free(str);
203
    } // else       
204
 
205
    // Test 4
206
#if defined(CYGIMP_MEMALLOC_MALLOC_VARIABLE_SIMPLE) && \
207
      defined(CYGSEM_MEMALLOC_ALLOCATOR_VARIABLE_COALESCE)
208
    poolmax = mallinfo().maxfree; // recalculate for non-coalescing allocator
209
#endif
210
    str=(char *)malloc( poolmax+1 );
211
    CYG_TEST_PASS_FAIL( str==NULL, "malloc too much data returns NULL" );
212
 
213
    // Test 5
214
    str=(char *)calloc( 1, poolmax+1 );
215
    CYG_TEST_PASS_FAIL( str==NULL, "calloc too much data returns NULL" );
216
 
217
    // Test 6
218
    str=(char *)malloc(0); if (str != NULL) free(str);
219
    str=(char *)calloc(0, 1); if (str != NULL) free(str);
220
    str=(char *)calloc(1, 0); if (str != NULL) free(str);
221
    str=(char *)calloc(0, 0); if (str != NULL) free(str);
222
    // simply shouldn't barf by this point
223
 
224
    CYG_TEST_PASS_FAIL( 1, "malloc and calloc of 0 bytes doesn't crash" );
225
 
226
    // Test 7
227
    str = (char *)malloc(10);
228
    i = (int *)malloc(sizeof(int));
229
    str2 = (char *)malloc(10);
230
 
231
    str3=(char *)i;
232
 
233
    CYG_TEST_PASS_FAIL( ((str3 <= str-sizeof(int))  || (str3 >= &str[10])) &&
234
                        ((str3 <= str2-sizeof(int)) || (str3 >= &str2[10])) &&
235
                        ((str+10 <= str2) || (str2+10 <= str)),
236
                        "Objects don't overlap" );
237
 
238
    // Test 8
239
 
240
    free(i);
241
    i=(int *)malloc(sizeof(int)*2);
242
    str3=(char *)i;
243
 
244
    CYG_TEST_PASS_FAIL( ((str3 <= str-sizeof(int))  || (str3 >= &str[10])) &&
245
                        ((str3 <= str2-sizeof(int)) || (str3 >= &str2[10])) &&
246
                        ((&str[10] <= str2) || (&str2[10] <= str)),
247
                        "Objects don't overlap when middle is freed" );
248
 
249
    free(i);
250
    free(str);
251
    free(str2);
252
 
253
    // Test 9
254
 
255
#if defined(CYGIMP_MEMALLOC_MALLOC_VARIABLE_SIMPLE) && \
256
      defined(CYGSEM_MEMALLOC_ALLOCATOR_VARIABLE_COALESCE)
257
    poolmax = mallinfo().maxfree; // recalculate for non-coalescing allocator
258
#endif
259
    str = (char *)malloc( poolmax );
260
    CYG_TEST_PASS_FAIL( str != NULL, "malloc of maximum free block size works");
261
    free(str);
262
 
263
    CYG_TEST_FINISH("Finished tests from testcase " __FILE__ " for C library "
264
                    "malloc(), calloc() and free() functions");
265
 
266
    return 0;
267
} // main()
268
 
269
#endif // ifndef NA_MSG
270
 
271
// EOF malloc1.c

powered by: WebSVN 2.1.0

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