Mercurial > flash_v2
comparison packages/language/c/libc/current/tests/stdlib/strtoul.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 // strtoul.c | |
| 4 // | |
| 5 // Testcase for C library strtoul() | |
| 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): ctarpy@cygnus.co.uk, jlarmour@cygnus.co.uk | |
| 33 // Contributors: jlarmour@cygnus.co.uk | |
| 34 // Date: 1998/6/3 | |
| 35 // Description: Contains testcode for C library strtoul() function | |
| 36 // | |
| 37 // | |
| 38 //####DESCRIPTIONEND#### | |
| 39 | |
| 40 // Declarations for test system: | |
| 41 // | |
| 42 // TESTCASE_TYPE=CYG_TEST_MODULE | |
| 43 | |
| 44 | |
| 45 // CONFIGURATION | |
| 46 | |
| 47 #include <pkgconf/libc.h> // Configuration header | |
| 48 | |
| 49 | |
| 50 // INCLUDES | |
| 51 | |
| 52 #include <stdlib.h> | |
| 53 #include <errno.h> | |
| 54 #include <limits.h> | |
| 55 #include <cyg/infra/testcase.h> | |
| 56 #include <sys/cstartup.h> // C library initialisation | |
| 57 | |
| 58 | |
| 59 // HOW TO START TESTS | |
| 60 | |
| 61 #if defined(CYGPKG_LIBC) | |
| 62 | |
| 63 # define START_TEST( test ) test(0) | |
| 64 | |
| 65 #else | |
| 66 | |
| 67 # define START_TEST( test ) CYG_EMPTY_STATEMENT | |
| 68 | |
| 69 #endif | |
| 70 | |
| 71 | |
| 72 // FUNCTIONS | |
| 73 | |
| 74 | |
| 75 externC void | |
| 76 cyg_package_start( void ) | |
| 77 { | |
| 78 #ifdef CYGPKG_LIBC | |
| 79 cyg_iso_c_start(); | |
| 80 #else | |
| 81 (void)main(0, NULL); | |
| 82 #endif | |
| 83 } // cyg_package_start() | |
| 84 | |
| 85 | |
| 86 #ifdef CYGPKG_LIBC | |
| 87 static char * | |
| 88 my_strcpy(char *s1, const char *s2) | |
| 89 { | |
| 90 while (*s2 != '\0') { | |
| 91 *(s1++) = *(s2++); | |
| 92 } | |
| 93 *s1 = '\0'; | |
| 94 | |
| 95 return s1; | |
| 96 } // my_strcpy() | |
| 97 | |
| 98 | |
| 99 static void | |
| 100 test( CYG_ADDRWORD data ) | |
| 101 { | |
| 102 char x[30]; | |
| 103 unsigned long z; | |
| 104 char *endptr; | |
| 105 | |
| 106 my_strcpy(x, "20"); | |
| 107 CYG_TEST_PASS_FAIL( strtoul(x, (char**)NULL, 10) == 20, | |
| 108 "Simple strtoul(20 ,...)" ); | |
| 109 | |
| 110 my_strcpy(x, "1972100"); | |
| 111 CYG_TEST_PASS_FAIL( strtoul(x, (char**)NULL, 10) == 1972100, | |
| 112 "Simple strtoul(1972100, ..." ); | |
| 113 | |
| 114 my_strcpy(x, "0xFFEE"); | |
| 115 z = strtoul(x, (char**)NULL, 16); | |
| 116 CYG_TEST_PASS_FAIL(z == 65518, "Hex base strtoul()"); | |
| 117 | |
| 118 my_strcpy(x, "100111011"); | |
| 119 z = strtoul(x, (char**)NULL, 2); | |
| 120 CYG_TEST_PASS_FAIL(z == 315, "Binary base strtoul()"); | |
| 121 | |
| 122 my_strcpy(x, "10372"); | |
| 123 z = strtoul(x, (char**)NULL, 8); | |
| 124 CYG_TEST_PASS_FAIL(z == 4346, "Octal base strtoul()" ); | |
| 125 | |
| 126 my_strcpy(x, "317823"); | |
| 127 z = strtoul(x, (char**)NULL, 8); | |
| 128 CYG_TEST_PASS_FAIL(z == 207, "Partial string" ); | |
| 129 | |
| 130 my_strcpy(x, " 53ab823"); | |
| 131 z = strtoul(x, &endptr, 10); | |
| 132 CYG_TEST_PASS_FAIL( (z == 53) && (endptr==&x[3]), "Correct end pointer" ); | |
| 133 | |
| 134 my_strcpy(x, "-479"); | |
| 135 z = strtoul(x, (char**)NULL, 0); | |
| 136 CYG_TEST_PASS_FAIL( (z == (unsigned long) -479), | |
| 137 "Negative string"); | |
| 138 | |
| 139 my_strcpy(x, "+4796"); | |
| 140 z = strtoul(x, (char**)NULL, 10); | |
| 141 CYG_TEST_PASS_FAIL(z == 4796, "Positive string"); | |
| 142 | |
| 143 my_strcpy(x, ""); | |
| 144 z = strtoul(x, (char**)NULL, 10); | |
| 145 CYG_TEST_PASS_FAIL(z == 0, "Empty string"); | |
| 146 | |
| 147 my_strcpy(x, ""); | |
| 148 z = strtoul(x, &endptr, 10); | |
| 149 CYG_TEST_PASS_FAIL( (z == 0) && (endptr==x), | |
| 150 "Empty string sets endptr correctly"); | |
| 151 | |
| 152 my_strcpy(x, " "); | |
| 153 z = strtoul(x, &endptr, 10); | |
| 154 CYG_TEST_PASS_FAIL( (z == 0) && (endptr==x), | |
| 155 "White space only string sets endptr correctly"); | |
| 156 | |
| 157 my_strcpy(x, "0XFFEE"); | |
| 158 z = strtoul(x, (char**)NULL, 0); | |
| 159 CYG_TEST_PASS_FAIL(z == 65518, "Base 0 but hex"); | |
| 160 | |
| 161 my_strcpy(x, "\t 0629"); | |
| 162 z = strtoul(x, (char**)NULL, 0); | |
| 163 CYG_TEST_PASS_FAIL(z == 50, "Base 0 but octal"); | |
| 164 | |
| 165 my_strcpy(x, "42"); | |
| 166 z = strtoul(x, (char**)NULL, 0); | |
| 167 CYG_TEST_PASS_FAIL(z == 42, "Base 0 but decimal"); | |
| 168 | |
| 169 my_strcpy(x, "hello"); | |
| 170 z = strtoul(x, &endptr, 0); | |
| 171 CYG_TEST_PASS_FAIL((z == 0) && (endptr==x), | |
| 172 "endptr set correctly on conversion failure"); | |
| 173 | |
| 174 my_strcpy(x, "z2f"); | |
| 175 z = strtoul(x, (char**)NULL, 36); | |
| 176 CYG_TEST_PASS_FAIL(z == 45447, "Base==36"); | |
| 177 | |
| 178 my_strcpy(x, "h547324"); | |
| 179 z = strtoul(x, (char**)NULL, 10); | |
| 180 CYG_TEST_PASS_FAIL(z == 0, "No valid number string"); | |
| 181 | |
| 182 my_strcpy(x, "545425876654547324"); | |
| 183 z = strtoul(x, (char**)NULL, 10); | |
| 184 CYG_TEST_PASS_FAIL( (z == ULONG_MAX) && (errno == ERANGE), | |
| 185 "Number out of range"); | |
| 186 | |
| 187 CYG_TEST_FINISH("Finished tests from testcase " __FILE__ " for C library " | |
| 188 "strtoul() function"); | |
| 189 } // test() | |
| 190 | |
| 191 #endif // ifndef CYGPKG_LIBC | |
| 192 | |
| 193 int | |
| 194 main(int argc, char *argv[]) | |
| 195 { | |
| 196 CYG_TEST_INIT(); | |
| 197 | |
| 198 CYG_TEST_INFO("Starting tests from testcase " __FILE__ " for C library " | |
| 199 "strtoul() function"); | |
| 200 CYG_TEST_INFO("This testcase provides simple basic tests"); | |
| 201 | |
| 202 START_TEST( test ); | |
| 203 | |
| 204 CYG_TEST_PASS_FINISH("Testing is not applicable to this configuration"); | |
| 205 } // main() | |
| 206 | |
| 207 // EOF strtoul.c |
