Mercurial > ecos
comparison packages/language/c/libc/current/tests/stdlib/malloc1.c @ 0:3111d98ba7b3 ecos-v1_1-release
Initial commit of eCos version 1.1
| author | jlarmour |
|---|---|
| date | Tue, 11 May 1999 11:16:07 +0000 |
| parents | |
| children | 443894e2e912 |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:3111d98ba7b3 |
|---|---|
| 1 //================================================================= | |
| 2 // | |
| 3 // malloc1.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 Cygnus eCos Public License | |
| 12 // Version 1.0 (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://sourceware.cygnus.com/ecos | |
| 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 Cygnus Operating System, released | |
| 22 // September 30, 1998. | |
| 23 // | |
| 24 // The Initial Developer of the Original Code is Cygnus. Portions created | |
| 25 // by Cygnus are Copyright (C) 1998 Cygnus Solutions. All Rights Reserved. | |
| 26 // ------------------------------------------- | |
| 27 // | |
| 28 //####COPYRIGHTEND#### | |
| 29 //================================================================= | |
| 30 //#####DESCRIPTIONBEGIN#### | |
| 31 // | |
| 32 // Author(s): jlarmour@cygnus.co.uk | |
| 33 // Contributors: jlarmour@cygnus.co.uk | |
| 34 // Date: 1998/6/3 | |
| 35 // Description: Contains testcode for C library malloc(), calloc() and | |
| 36 // free() functions | |
| 37 // | |
| 38 // | |
| 39 //####DESCRIPTIONEND#### | |
| 40 | |
| 41 // Declarations for test system: | |
| 42 // | |
| 43 // TESTCASE_TYPE=CYG_TEST_MODULE | |
| 44 | |
| 45 // INCLUDES | |
| 46 | |
| 47 #include <pkgconf/libc.h> // config header for C library so we can know size | |
| 48 // of malloc pool | |
| 49 #include <stdlib.h> | |
| 50 #include <cyg/infra/testcase.h> | |
| 51 #include <sys/cstartup.h> // C library initialisation | |
| 52 | |
| 53 | |
| 54 // FUNCTIONS | |
| 55 | |
| 56 externC void | |
| 57 cyg_package_start( void ) | |
| 58 { | |
| 59 #ifdef CYGPKG_LIBC | |
| 60 cyg_iso_c_start(); | |
| 61 #else | |
| 62 (void)main(0, NULL); | |
| 63 #endif | |
| 64 } // cyg_package_start() | |
| 65 | |
| 66 | |
| 67 int | |
| 68 main( int argc, char *argv[] ) | |
| 69 { | |
| 70 #if defined(CYGPKG_LIBC) && defined(CYGPKG_LIBC_MALLOC) | |
| 71 int *i; | |
| 72 char *str, *str2, *str3; | |
| 73 int j; | |
| 74 #endif | |
| 75 | |
| 76 CYG_TEST_INIT(); | |
| 77 | |
| 78 CYG_TEST_INFO("Starting tests from testcase " __FILE__ " for C library " | |
| 79 "malloc(), calloc() and free() functions"); | |
| 80 | |
| 81 #if defined(CYGPKG_LIBC) && defined(CYGPKG_LIBC_MALLOC) | |
| 82 | |
| 83 // Test 1 | |
| 84 i = (int *) malloc( sizeof(int) ); | |
| 85 | |
| 86 // check if it should fit into pool | |
| 87 if (sizeof(int) > CYGNUM_LIBC_MALLOC_MEMPOOL_SIZE) | |
| 88 { | |
| 89 // didn't fit into pool, so should be NULL | |
| 90 CYG_TEST_PASS_FAIL( i == NULL, | |
| 91 "1 int malloc with no space left works" ); | |
| 92 } | |
| 93 else | |
| 94 { | |
| 95 // since it should fit into pool, we can fiddle with i | |
| 96 *i=-12345; | |
| 97 CYG_TEST_PASS_FAIL( i && (*i==-12345), | |
| 98 "1 int malloc with space left works" ); | |
| 99 free(i); | |
| 100 } // else | |
| 101 | |
| 102 // Test 2 | |
| 103 str=(char *) malloc( 4096 ); | |
| 104 | |
| 105 if ( 4096 > CYGNUM_LIBC_MALLOC_MEMPOOL_SIZE) | |
| 106 { | |
| 107 // didn't fit into pool, so should be NULL | |
| 108 CYG_TEST_PASS_FAIL( str == NULL,"4K string with no space left works" ); | |
| 109 } | |
| 110 else | |
| 111 { | |
| 112 // since it should fit into pool, we can fiddle with it. | |
| 113 for (j=0; j<1024; j++) | |
| 114 { | |
| 115 str[j*4] = 'f'; | |
| 116 str[(j*4)+1] = 'r'; | |
| 117 str[(j*4)+2] = 'e'; | |
| 118 str[(j*4)+3] = 'd'; | |
| 119 } // for | |
| 120 | |
| 121 for (j=0; j<1024; j++) | |
| 122 { | |
| 123 if ( ((str[j*4] != 'f') || | |
| 124 (str[(j*4)+1] != 'r') || | |
| 125 (str[(j*4)+2] != 'e') || | |
| 126 (str[(j*4)+3] != 'd')) ) | |
| 127 break; | |
| 128 } // for | |
| 129 | |
| 130 // did j reach the top? | |
| 131 CYG_TEST_PASS_FAIL( j==1024, "4K string with space left works" ); | |
| 132 | |
| 133 free(str); | |
| 134 } // else | |
| 135 | |
| 136 | |
| 137 // Test 3 | |
| 138 str=(char *) calloc( 2, 1024 ); | |
| 139 | |
| 140 if ( 2048 > CYGNUM_LIBC_MALLOC_MEMPOOL_SIZE) | |
| 141 { | |
| 142 // didn't fit into pool, so should be NULL | |
| 143 CYG_TEST_PASS_FAIL( str == NULL, | |
| 144 "calloc 2K string with no space left works" ); | |
| 145 } | |
| 146 else | |
| 147 { | |
| 148 // check its zeroed | |
| 149 for ( j=0; j<2048; j++ ) | |
| 150 { | |
| 151 if (str[j] != 0) | |
| 152 break; | |
| 153 } // for | |
| 154 | |
| 155 CYG_TEST_PASS_FAIL( j==2048, "calloc 2K string is cleared" ); | |
| 156 | |
| 157 // since it should fit into pool, we can fiddle with it. | |
| 158 for (j=0; j<512; j++) | |
| 159 { | |
| 160 str[j*4] = 'j'; | |
| 161 str[(j*4)+1] = 'i'; | |
| 162 str[(j*4)+2] = 'f'; | |
| 163 str[(j*4)+3] = 'l'; | |
| 164 } // for | |
| 165 | |
| 166 for (j=0; j<512; j++) | |
| 167 { | |
| 168 if ( ((str[j*4] != 'j') || | |
| 169 (str[(j*4)+1] != 'i') || | |
| 170 (str[(j*4)+2] != 'f') || | |
| 171 (str[(j*4)+3] != 'l')) ) | |
| 172 break; | |
| 173 } // for | |
| 174 | |
| 175 // did j reach the top? | |
| 176 CYG_TEST_PASS_FAIL( j==512, | |
| 177 "calloc 2K string - with space left works" ); | |
| 178 | |
| 179 free(str); | |
| 180 } // else | |
| 181 | |
| 182 | |
| 183 // Test 4 | |
| 184 str=(char *)malloc( CYGNUM_LIBC_MALLOC_MEMPOOL_SIZE+1 ); | |
| 185 | |
| 186 CYG_TEST_PASS_FAIL( str==NULL, "malloc too much data returns NULL" ); | |
| 187 | |
| 188 // Test 5 | |
| 189 str=(char *)calloc( 1, CYGNUM_LIBC_MALLOC_MEMPOOL_SIZE+1 ); | |
| 190 CYG_TEST_PASS_FAIL( str==NULL, "calloc too much data returns NULL" ); | |
| 191 | |
| 192 // Test 6 | |
| 193 str=(char *)malloc(0); | |
| 194 str=(char *)calloc(0, 1); | |
| 195 str=(char *)calloc(1, 0); | |
| 196 str=(char *)calloc(0, 0); | |
| 197 // simply shouldn't barf by this point | |
| 198 | |
| 199 CYG_TEST_PASS_FAIL( 1, "malloc and calloc of 0 bytes doesn't crash" ); | |
| 200 | |
| 201 // Test 7 | |
| 202 str = (char *)malloc(10); | |
| 203 i = (int *)malloc(sizeof(int)); | |
| 204 str2 = (char *)malloc(10); | |
| 205 | |
| 206 str3=(char *)i; | |
| 207 | |
| 208 CYG_TEST_PASS_FAIL( ((str3 <= str-sizeof(int)) || (str >= &str[10])) && | |
| 209 ((str3 <= str2-sizeof(int)) || (str3 >= &str2[10])) && | |
| 210 ((str+10 <= str2) || (str2+10 <= str)), | |
| 211 "Objects don't overlap" ); | |
| 212 | |
| 213 // Test 8 | |
| 214 | |
| 215 free(i); | |
| 216 i=(int *)malloc(sizeof(int)*2); | |
| 217 str3=(char *)i; | |
| 218 | |
| 219 CYG_TEST_PASS_FAIL( ((str3 <= str-sizeof(int)) || (str3 >= &str[10])) && | |
| 220 ((str3 <= str2-sizeof(int)) || (str3 >= &str2[10])) && | |
| 221 ((&str[10] <= str2) || (&str2[10] <= str)), | |
| 222 "Objects don't overlap when middle is freed" ); | |
| 223 | |
| 224 free(i); | |
| 225 free(str); | |
| 226 free(str2); | |
| 227 | |
| 228 #else | |
| 229 CYG_TEST_PASS("Testing is not applicable to this configuration"); | |
| 230 #endif // if defined(CYGPKG_LIBC) && defined(CYGPKG_LIBC_MALLOC) | |
| 231 | |
| 232 CYG_TEST_FINISH("Finished tests from testcase " __FILE__ " for C library " | |
| 233 "malloc(), calloc() and free() functions"); | |
| 234 } // main() | |
| 235 | |
| 236 // EOF malloc1.c |
