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