Mercurial > ecos
annotate packages/language/c/libc/current/tests/stdlib/realloc.c @ 66:bf00f99aec69 ecos-sw-2000-02-02
Merge from eCos master repository on 2000-02-02-19:16:44-GMT
| author | jlarmour |
|---|---|
| date | Wed, 02 Feb 2000 19:57:02 +0000 |
| parents | c38311975d4f |
| children |
| rev | line source |
|---|---|
| 0 | 1 //================================================================= |
| 2 // | |
| 3 // realloc.c | |
| 4 // | |
| 5 // Testcase for C library realloc() | |
| 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 |
|
66
bf00f99aec69
Merge from eCos master repository on 2000-02-02-19:16:44-GMT
jlarmour
parents:
64
diff
changeset
|
12 // Version 1.1 (the "License"); you may not use this file except in |
|
64
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 |
|
66
bf00f99aec69
Merge from eCos master repository on 2000-02-02-19:16:44-GMT
jlarmour
parents:
64
diff
changeset
|
14 // http://www.redhat.com/ |
|
64
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
15 // |
|
66
bf00f99aec69
Merge from eCos master repository on 2000-02-02-19:16:44-GMT
jlarmour
parents:
64
diff
changeset
|
16 // Software distributed under the License is distributed on an "AS IS" |
|
64
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/8/31 |
| 37 // Description: Contains testcode for C library realloc() function | |
| 38 // | |
| 39 // | |
| 40 //####DESCRIPTIONEND#### | |
| 41 | |
| 42 // Declarations for test system: | |
| 43 // | |
| 44 // TESTCASE_TYPE=CYG_TEST_MODULE | |
| 45 | |
| 46 | |
| 47 // INCLUDES | |
| 48 | |
| 2 | 49 #include <pkgconf/system.h> // Overall system configuration |
| 50 #include <pkgconf/libc.h> // config header for C library so we can know | |
| 51 // size of malloc pool | |
| 52 #ifdef CYGPKG_KERNEL | |
| 53 # include <pkgconf/kernel.h> // CYGSEM_KERNEL_MEMORY_COALESCE | |
| 54 #endif | |
| 0 | 55 #include <stdlib.h> |
| 56 #include <cyg/infra/testcase.h> | |
| 57 #include <sys/cstartup.h> // C library initialisation | |
| 58 | |
| 59 | |
| 60 // FUNCTIONS | |
| 61 | |
| 62 externC void | |
| 63 cyg_package_start( void ) | |
| 64 { | |
| 65 #ifdef CYGPKG_LIBC | |
| 66 cyg_iso_c_start(); | |
| 67 #else | |
| 68 (void)main(0, NULL); | |
| 69 #endif | |
| 70 } // cyg_package_start() | |
| 71 | |
| 72 | |
| 2 | 73 #if defined(CYGPKG_LIBC) && defined(CYGPKG_LIBC_MALLOC) && \ |
| 74 defined(CYGSEM_KERNEL_MEMORY_COALESCE) | |
| 0 | 75 |
| 76 static const char alphabet[]="abcdefghijklmnopqrstuvwxyz{-}[]#';:@~!$^&*()" | |
| 77 "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; | |
| 78 | |
| 79 static int | |
| 80 compare_with_alphabet( char *buf, int size, int offset ) | |
| 81 { | |
| 82 int i, buf_offset; | |
| 83 | |
| 84 for (i=offset, buf_offset=0; | |
| 85 buf_offset < size; | |
| 86 buf_offset++,i++ ) { | |
| 87 | |
| 88 if ( i==sizeof(alphabet)-1 ) | |
| 89 i=0; | |
| 90 | |
| 91 if ( buf[buf_offset] != alphabet[i] ) { | |
| 92 CYG_TEST_FAIL( "buffer has not retained correct data!"); | |
| 93 return 0; // fail | |
| 94 } // if | |
| 95 } // for | |
| 96 | |
| 97 return 1; // success | |
| 98 } // compare_with_alphabet() | |
| 99 | |
| 100 static int | |
| 101 fill_with_alphabet( char *buf, int size, int offset ) | |
| 102 { | |
| 103 int i, buf_offset; | |
| 104 | |
| 105 for (i=offset, buf_offset=0; | |
| 106 buf_offset < size; | |
| 107 buf_offset++,i++ ) { | |
| 108 | |
| 109 if ( i==sizeof(alphabet)-1 ) | |
| 110 i=0; | |
| 111 | |
| 112 buf[buf_offset] = alphabet[i]; | |
| 113 | |
| 114 } // for | |
| 115 | |
| 116 return compare_with_alphabet( buf, size, offset); // be sure | |
| 117 } // fill_with_alphabet() | |
| 118 | |
| 2 | 119 #endif // if defined(CYGPKG_LIBC) && defined(CYGPKG_LIBC_MALLOC) && |
| 120 // defined(CYGSEM_KERNEL_MEMORY_COALESCE) | |
| 121 | |
| 0 | 122 |
| 123 int | |
| 124 main( int argc, char *argv[] ) | |
| 125 { | |
| 2 | 126 #if defined(CYGPKG_LIBC) && defined(CYGPKG_LIBC_MALLOC) && \ |
| 127 defined(CYGSEM_KERNEL_MEMORY_COALESCE) | |
| 128 | |
| 0 | 129 char *str; |
| 130 int size = CYGNUM_LIBC_MALLOC_MEMPOOL_SIZE/2; | |
| 131 #endif | |
| 132 | |
| 133 CYG_TEST_INIT(); | |
| 134 | |
| 135 CYG_TEST_INFO("Starting tests from testcase " __FILE__ " for C library " | |
| 136 "realloc() function"); | |
| 137 | |
| 2 | 138 #if defined(CYGPKG_LIBC) && defined(CYGPKG_LIBC_MALLOC) && \ |
| 139 defined(CYGSEM_KERNEL_MEMORY_COALESCE) | |
| 0 | 140 |
| 141 str = (char *)realloc( NULL, size ); | |
| 142 CYG_TEST_PASS_FAIL( str != NULL, "realloc doing only allocation"); | |
| 143 CYG_TEST_PASS_FAIL( fill_with_alphabet( str, size, 0 ), | |
| 144 "allocation usability"); | |
| 145 | |
| 146 str = (char *)realloc( str, 0 ); | |
| 147 CYG_TEST_PASS_FAIL( str == NULL, "realloc doing implicit free" ); | |
| 148 | |
| 149 str = (char *)realloc( NULL, size/2 ); | |
| 150 CYG_TEST_PASS_FAIL( str != NULL, "realloc doing allocation to half size"); | |
| 151 CYG_TEST_PASS_FAIL( fill_with_alphabet( str, size/2, 5 ), | |
| 152 "half allocation usability"); | |
| 153 | |
| 154 str = (char *)realloc( str, size ); | |
| 155 CYG_TEST_PASS_FAIL( str != NULL, | |
| 156 "reallocing allocation back to normal size"); | |
| 157 CYG_TEST_PASS_FAIL( compare_with_alphabet(str, size/2, 5), | |
| 158 "after realloc to normal size, old contents kept" ); | |
| 159 CYG_TEST_PASS_FAIL( fill_with_alphabet( str, size, 3 ), | |
| 160 "reallocation normal size usability"); | |
| 161 | |
| 162 str = (char *)realloc( str, size/4 ); | |
| 163 CYG_TEST_PASS_FAIL( str != NULL, "reallocing allocation to quarter size"); | |
| 164 CYG_TEST_PASS_FAIL( compare_with_alphabet(str, size/4, 3), | |
| 165 "after realloc to quarter size, old contents kept" ); | |
| 166 CYG_TEST_PASS_FAIL( fill_with_alphabet( str, size/4, 1 ), | |
| 167 "reallocation quarter size usability"); | |
| 168 | |
| 169 CYG_TEST_PASS_FAIL( realloc( str, size*100 ) == NULL, | |
| 170 "reallocing allocation that is too large" ); | |
| 171 CYG_TEST_PASS_FAIL( compare_with_alphabet( str, size/4, 1 ), | |
| 172 "Checking old contents maintained despite failure" ); | |
| 173 | |
| 174 str = (char *)realloc( str, 0 ); | |
| 175 CYG_TEST_PASS_FAIL( str == NULL, "realloc doing implicit free again" ); | |
| 176 | |
| 177 | |
| 178 #else | |
| 2 | 179 CYG_TEST_NA("Testing is not applicable to this configuration"); |
| 180 #endif // if defined(CYGPKG_LIBC) && defined(CYGPKG_LIBC_MALLOC) && | |
| 181 // defined(CYGSEM_KERNEL_MEMORY_COALESCE) | |
| 182 | |
| 0 | 183 |
| 184 CYG_TEST_FINISH("Finished tests from testcase " __FILE__ " for C library " | |
| 185 "realloc() function"); | |
| 186 } // main() | |
| 187 | |
| 188 // EOF realloc.c |
