Mercurial > flash_v2
comparison packages/services/memalloc/common/current/tests/malloc1.c @ 115:6ed91473a1cd ecos-sw-2000-08-21
Merge from eCos master repository on 2000-08-21-22:40:54-BST
| author | jlarmour |
|---|---|
| date | Fri, 25 Aug 2000 17:32:38 +0000 |
| parents | |
| children | 518f42066aba |
comparison
equal
deleted
inserted
replaced
| 114:5ad2b71d525e | 115:6ed91473a1cd |
|---|---|
| 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 Red Hat eCos Public License | |
| 12 // Version 1.1 (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://www.redhat.com/ | |
| 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 Configurable Operating System, | |
| 22 // released September 30, 1998. | |
| 23 // | |
| 24 // The Initial Developer of the Original Code is Red Hat. | |
| 25 // Portions created by Red Hat are | |
| 26 // Copyright (C) 1998, 1999, 2000 Red Hat, Inc. | |
| 27 // All Rights Reserved. | |
| 28 // ------------------------------------------- | |
| 29 // | |
| 30 //####COPYRIGHTEND#### | |
| 31 //================================================================= | |
| 32 //#####DESCRIPTIONBEGIN#### | |
| 33 // | |
| 34 // Author(s): jlarmour | |
| 35 // Contributors: | |
| 36 // Date: 2000-04-30 | |
| 37 // Description: Contains testcode for C library malloc(), calloc() and | |
| 38 // free() functions | |
| 39 // | |
| 40 // | |
| 41 //####DESCRIPTIONEND#### | |
| 42 | |
| 43 // INCLUDES | |
| 44 | |
| 45 #include <pkgconf/system.h> | |
| 46 #include <pkgconf/memalloc.h> // config header | |
| 47 #ifdef CYGPKG_ISOINFRA | |
| 48 # include <pkgconf/isoinfra.h> | |
| 49 # include <stdlib.h> | |
| 50 #endif | |
| 51 #include <cyg/infra/testcase.h> | |
| 52 #include <limits.h> // INT_MAX | |
| 53 | |
| 54 | |
| 55 #if !defined(CYGPKG_ISOINFRA) | |
| 56 # define NA_MSG "Requires isoinfra package" | |
| 57 #elif !CYGINT_ISO_MAIN_STARTUP | |
| 58 # define NA_MSG "Requires main() to be called" | |
| 59 #elif !CYGINT_ISO_MALLOC | |
| 60 # define NA_MSG "Requires malloc" | |
| 61 #elif !CYGINT_ISO_MALLINFO | |
| 62 # define NA_MSG "Requires mallinfo" | |
| 63 #endif | |
| 64 | |
| 65 #ifdef NA_MSG | |
| 66 void | |
| 67 cyg_start() | |
| 68 { | |
| 69 CYG_TEST_INIT(); | |
| 70 CYG_TEST_NA( NA_MSG ); | |
| 71 CYG_TEST_FINISH("Done"); | |
| 72 } | |
| 73 #else | |
| 74 | |
| 75 | |
| 76 // FUNCTIONS | |
| 77 | |
| 78 int | |
| 79 main( int argc, char *argv[] ) | |
| 80 { | |
| 81 int *i; | |
| 82 char *str, *str2, *str3; | |
| 83 int j; | |
| 84 int poolmax; | |
| 85 | |
| 86 CYG_TEST_INIT(); | |
| 87 | |
| 88 CYG_TEST_INFO("Starting tests from testcase " __FILE__ " for C library " | |
| 89 "malloc(), calloc() and free() functions"); | |
| 90 | |
| 91 poolmax = mallinfo().maxfree; | |
| 92 | |
| 93 if ( poolmax <= 0 ) { | |
| 94 CYG_TEST_FAIL_FINISH( "Can't determine allocation size to use" ); | |
| 95 } | |
| 96 | |
| 97 // Test 1 | |
| 98 i = (int *) malloc( sizeof(int) ); | |
| 99 | |
| 100 // check if it should fit into pool | |
| 101 if (sizeof(int) > poolmax) | |
| 102 { | |
| 103 // didn't fit into pool, so should be NULL | |
| 104 CYG_TEST_PASS_FAIL( i == NULL, | |
| 105 "1 int malloc with no space left works" ); | |
| 106 } | |
| 107 else | |
| 108 { | |
| 109 // since it should fit into pool, we can fiddle with i | |
| 110 *i=-12345; | |
| 111 CYG_TEST_PASS_FAIL( i && (*i==-12345), | |
| 112 "1 int malloc with space left works" ); | |
| 113 free(i); | |
| 114 } // else | |
| 115 | |
| 116 // Test 2 | |
| 117 str=(char *) malloc( 4096 ); | |
| 118 | |
| 119 if ( 4096 > poolmax) | |
| 120 { | |
| 121 // didn't fit into pool, so should be NULL | |
| 122 CYG_TEST_PASS_FAIL( str == NULL,"4K string with no space left works" ); | |
| 123 } | |
| 124 else | |
| 125 { | |
| 126 // since it should fit into pool, we can fiddle with it. | |
| 127 for (j=0; j<1024; j++) | |
| 128 { | |
| 129 str[j*4] = 'f'; | |
| 130 str[(j*4)+1] = 'r'; | |
| 131 str[(j*4)+2] = 'e'; | |
| 132 str[(j*4)+3] = 'd'; | |
| 133 } // for | |
| 134 | |
| 135 for (j=0; j<1024; j++) | |
| 136 { | |
| 137 if ( ((str[j*4] != 'f') || | |
| 138 (str[(j*4)+1] != 'r') || | |
| 139 (str[(j*4)+2] != 'e') || | |
| 140 (str[(j*4)+3] != 'd')) ) | |
| 141 break; | |
| 142 } // for | |
| 143 | |
| 144 // did j reach the top? | |
| 145 CYG_TEST_PASS_FAIL( j==1024, "4K string with space left works" ); | |
| 146 | |
| 147 free(str); | |
| 148 } // else | |
| 149 | |
| 150 | |
| 151 // Test 3 | |
| 152 str=(char *) calloc( 2, 1024 ); | |
| 153 | |
| 154 if ( 2048 > poolmax) | |
| 155 { | |
| 156 // didn't fit into pool, so should be NULL | |
| 157 CYG_TEST_PASS_FAIL( str == NULL, | |
| 158 "calloc 2K string with no space left works" ); | |
| 159 } | |
| 160 else | |
| 161 { | |
| 162 // check its zeroed | |
| 163 for ( j=0; j<2048; j++ ) | |
| 164 { | |
| 165 if (str[j] != 0) | |
| 166 break; | |
| 167 } // for | |
| 168 | |
| 169 CYG_TEST_PASS_FAIL( j==2048, "calloc 2K string is cleared" ); | |
| 170 | |
| 171 // since it should fit into pool, we can fiddle with it. | |
| 172 for (j=0; j<512; j++) | |
| 173 { | |
| 174 str[j*4] = 'j'; | |
| 175 str[(j*4)+1] = 'i'; | |
| 176 str[(j*4)+2] = 'f'; | |
| 177 str[(j*4)+3] = 'l'; | |
| 178 } // for | |
| 179 | |
| 180 for (j=0; j<512; j++) | |
| 181 { | |
| 182 if ( ((str[j*4] != 'j') || | |
| 183 (str[(j*4)+1] != 'i') || | |
| 184 (str[(j*4)+2] != 'f') || | |
| 185 (str[(j*4)+3] != 'l')) ) | |
| 186 break; | |
| 187 } // for | |
| 188 | |
| 189 // did j reach the top? | |
| 190 CYG_TEST_PASS_FAIL( j==512, | |
| 191 "calloc 2K string - with space left works" ); | |
| 192 | |
| 193 free(str); | |
| 194 } // else | |
| 195 | |
| 196 | |
| 197 // Test 4 | |
| 198 str=(char *)malloc( poolmax+1 ); | |
| 199 | |
| 200 CYG_TEST_PASS_FAIL( str==NULL, "malloc too much data returns NULL" ); | |
| 201 | |
| 202 // Test 5 | |
| 203 str=(char *)calloc( 1, poolmax+1 ); | |
| 204 CYG_TEST_PASS_FAIL( str==NULL, "calloc too much data returns NULL" ); | |
| 205 | |
| 206 // Test 6 | |
| 207 str=(char *)malloc(0); | |
| 208 str=(char *)calloc(0, 1); | |
| 209 str=(char *)calloc(1, 0); | |
| 210 str=(char *)calloc(0, 0); | |
| 211 // simply shouldn't barf by this point | |
| 212 | |
| 213 CYG_TEST_PASS_FAIL( 1, "malloc and calloc of 0 bytes doesn't crash" ); | |
| 214 | |
| 215 // Test 7 | |
| 216 str = (char *)malloc(10); | |
| 217 i = (int *)malloc(sizeof(int)); | |
| 218 str2 = (char *)malloc(10); | |
| 219 | |
| 220 str3=(char *)i; | |
| 221 | |
| 222 CYG_TEST_PASS_FAIL( ((str3 <= str-sizeof(int)) || (str3 >= &str[10])) && | |
| 223 ((str3 <= str2-sizeof(int)) || (str3 >= &str2[10])) && | |
| 224 ((str+10 <= str2) || (str2+10 <= str)), | |
| 225 "Objects don't overlap" ); | |
| 226 | |
| 227 // Test 8 | |
| 228 | |
| 229 free(i); | |
| 230 i=(int *)malloc(sizeof(int)*2); | |
| 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 when middle is freed" ); | |
| 237 | |
| 238 free(i); | |
| 239 free(str); | |
| 240 free(str2); | |
| 241 | |
| 242 CYG_TEST_FINISH("Finished tests from testcase " __FILE__ " for C library " | |
| 243 "malloc(), calloc() and free() functions"); | |
| 244 } // main() | |
| 245 | |
| 246 #endif // ifndef NA_MSG | |
| 247 | |
| 248 // EOF malloc1.c |
