Mercurial > ecos
comparison packages/language/c/libc/current/tests/stdlib/realloc.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 // realloc.c | |
| 4 // | |
| 5 // Testcase for C library realloc() | |
| 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/8/31 | |
| 35 // Description: Contains testcode for C library realloc() function | |
| 36 // | |
| 37 // | |
| 38 //####DESCRIPTIONEND#### | |
| 39 | |
| 40 // Declarations for test system: | |
| 41 // | |
| 42 // TESTCASE_TYPE=CYG_TEST_MODULE | |
| 43 | |
| 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 #if defined(CYGPKG_LIBC) && defined(CYGPKG_LIBC_MALLOC) | |
| 68 | |
| 69 static const char alphabet[]="abcdefghijklmnopqrstuvwxyz{-}[]#';:@~!$^&*()" | |
| 70 "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; | |
| 71 | |
| 72 static int | |
| 73 compare_with_alphabet( char *buf, int size, int offset ) | |
| 74 { | |
| 75 int i, buf_offset; | |
| 76 | |
| 77 for (i=offset, buf_offset=0; | |
| 78 buf_offset < size; | |
| 79 buf_offset++,i++ ) { | |
| 80 | |
| 81 if ( i==sizeof(alphabet)-1 ) | |
| 82 i=0; | |
| 83 | |
| 84 if ( buf[buf_offset] != alphabet[i] ) { | |
| 85 CYG_TEST_FAIL( "buffer has not retained correct data!"); | |
| 86 return 0; // fail | |
| 87 } // if | |
| 88 } // for | |
| 89 | |
| 90 return 1; // success | |
| 91 } // compare_with_alphabet() | |
| 92 | |
| 93 static int | |
| 94 fill_with_alphabet( char *buf, int size, int offset ) | |
| 95 { | |
| 96 int i, buf_offset; | |
| 97 | |
| 98 for (i=offset, buf_offset=0; | |
| 99 buf_offset < size; | |
| 100 buf_offset++,i++ ) { | |
| 101 | |
| 102 if ( i==sizeof(alphabet)-1 ) | |
| 103 i=0; | |
| 104 | |
| 105 buf[buf_offset] = alphabet[i]; | |
| 106 | |
| 107 } // for | |
| 108 | |
| 109 return compare_with_alphabet( buf, size, offset); // be sure | |
| 110 } // fill_with_alphabet() | |
| 111 | |
| 112 #endif // if defined(CYGPKG_LIBC) && defined(CYGPKG_LIBC_MALLOC) | |
| 113 | |
| 114 int | |
| 115 main( int argc, char *argv[] ) | |
| 116 { | |
| 117 #if defined(CYGPKG_LIBC) && defined(CYGPKG_LIBC_MALLOC) | |
| 118 char *str; | |
| 119 int size = CYGNUM_LIBC_MALLOC_MEMPOOL_SIZE/2; | |
| 120 #endif | |
| 121 | |
| 122 CYG_TEST_INIT(); | |
| 123 | |
| 124 CYG_TEST_INFO("Starting tests from testcase " __FILE__ " for C library " | |
| 125 "realloc() function"); | |
| 126 | |
| 127 #if defined(CYGPKG_LIBC) && defined(CYGPKG_LIBC_MALLOC) | |
| 128 | |
| 129 str = (char *)realloc( NULL, size ); | |
| 130 CYG_TEST_PASS_FAIL( str != NULL, "realloc doing only allocation"); | |
| 131 CYG_TEST_PASS_FAIL( fill_with_alphabet( str, size, 0 ), | |
| 132 "allocation usability"); | |
| 133 | |
| 134 str = (char *)realloc( str, 0 ); | |
| 135 CYG_TEST_PASS_FAIL( str == NULL, "realloc doing implicit free" ); | |
| 136 | |
| 137 str = (char *)realloc( NULL, size/2 ); | |
| 138 CYG_TEST_PASS_FAIL( str != NULL, "realloc doing allocation to half size"); | |
| 139 CYG_TEST_PASS_FAIL( fill_with_alphabet( str, size/2, 5 ), | |
| 140 "half allocation usability"); | |
| 141 | |
| 142 str = (char *)realloc( str, size ); | |
| 143 CYG_TEST_PASS_FAIL( str != NULL, | |
| 144 "reallocing allocation back to normal size"); | |
| 145 CYG_TEST_PASS_FAIL( compare_with_alphabet(str, size/2, 5), | |
| 146 "after realloc to normal size, old contents kept" ); | |
| 147 CYG_TEST_PASS_FAIL( fill_with_alphabet( str, size, 3 ), | |
| 148 "reallocation normal size usability"); | |
| 149 | |
| 150 str = (char *)realloc( str, size/4 ); | |
| 151 CYG_TEST_PASS_FAIL( str != NULL, "reallocing allocation to quarter size"); | |
| 152 CYG_TEST_PASS_FAIL( compare_with_alphabet(str, size/4, 3), | |
| 153 "after realloc to quarter size, old contents kept" ); | |
| 154 CYG_TEST_PASS_FAIL( fill_with_alphabet( str, size/4, 1 ), | |
| 155 "reallocation quarter size usability"); | |
| 156 | |
| 157 CYG_TEST_PASS_FAIL( realloc( str, size*100 ) == NULL, | |
| 158 "reallocing allocation that is too large" ); | |
| 159 CYG_TEST_PASS_FAIL( compare_with_alphabet( str, size/4, 1 ), | |
| 160 "Checking old contents maintained despite failure" ); | |
| 161 | |
| 162 str = (char *)realloc( str, 0 ); | |
| 163 CYG_TEST_PASS_FAIL( str == NULL, "realloc doing implicit free again" ); | |
| 164 | |
| 165 | |
| 166 #else | |
| 167 CYG_TEST_PASS("Testing is not applicable to this configuration"); | |
| 168 #endif // if defined(CYGPKG_LIBC) && defined(CYGPKG_LIBC_MALLOC) | |
| 169 | |
| 170 CYG_TEST_FINISH("Finished tests from testcase " __FILE__ " for C library " | |
| 171 "realloc() function"); | |
| 172 } // main() | |
| 173 | |
| 174 // EOF realloc.c |
