Mercurial > flash_v2
comparison packages/language/c/libc/stdio/current/tests/sscanf.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 | e0c0827131d1 |
comparison
equal
deleted
inserted
replaced
| 114:5ad2b71d525e | 115:6ed91473a1cd |
|---|---|
| 1 //================================================================= | |
| 2 // | |
| 3 // sscanf.c | |
| 4 // | |
| 5 // Testcase for C library sscanf() | |
| 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): ctarpy, jlarmour | |
| 35 // Contributors: | |
| 36 // Date: 2000-04-20 | |
| 37 // Description: Contains testcode for C library sscanf() function | |
| 38 // | |
| 39 // | |
| 40 //####DESCRIPTIONEND#### | |
| 41 | |
| 42 // CONFIGURATION | |
| 43 | |
| 44 #include <pkgconf/libc_stdio.h> // Configuration header | |
| 45 | |
| 46 // INCLUDES | |
| 47 | |
| 48 #include <stdio.h> | |
| 49 #include <cyg/infra/testcase.h> | |
| 50 | |
| 51 // HOW TO START TESTS | |
| 52 | |
| 53 #if defined(CYGFUN_LIBC_STDIO_ungetc) | |
| 54 | |
| 55 # define START_TEST( test ) test(0) | |
| 56 | |
| 57 #else | |
| 58 | |
| 59 # define START_TEST( test ) CYG_EMPTY_STATEMENT | |
| 60 | |
| 61 #endif | |
| 62 | |
| 63 | |
| 64 // FUNCTIONS | |
| 65 | |
| 66 #if defined(CYGFUN_LIBC_STDIO_ungetc) | |
| 67 | |
| 68 // Functions to avoid having to use libc strings | |
| 69 | |
| 70 static int | |
| 71 my_strcmp(const char *s1, const char *s2) | |
| 72 { | |
| 73 for ( ; *s1 == *s2 ; s1++,s2++ ) | |
| 74 { | |
| 75 if ( *s1 == '\0') | |
| 76 break; | |
| 77 } // for | |
| 78 | |
| 79 return (*s1 - *s2); | |
| 80 } // my_strcmp() | |
| 81 | |
| 82 | |
| 83 static char * | |
| 84 my_strcpy(char *s1, const char *s2) | |
| 85 { | |
| 86 while (*s2 != '\0') { | |
| 87 *(s1++) = *(s2++); | |
| 88 } | |
| 89 *s1 = '\0'; | |
| 90 | |
| 91 return s1; | |
| 92 } // my_strcpy() | |
| 93 | |
| 94 | |
| 95 static void | |
| 96 test( CYG_ADDRWORD data ) | |
| 97 { | |
| 98 static char x[300]; | |
| 99 static char y[300]; | |
| 100 char z[20]; | |
| 101 int ret; | |
| 102 int int_test, int_test2, int_test3, int_test4; | |
| 103 | |
| 104 // Check 1 | |
| 105 my_strcpy(x, "20"); | |
| 106 ret = sscanf(x, "%d", &int_test); | |
| 107 CYG_TEST_PASS_FAIL(int_test==20, "%d test"); | |
| 108 CYG_TEST_PASS_FAIL(ret == 1, "%d test return code"); | |
| 109 | |
| 110 // Check 2 | |
| 111 my_strcpy(y, "PigsNoses"); | |
| 112 ret = sscanf(y, "%s", x); | |
| 113 CYG_TEST_PASS_FAIL(my_strcmp(x, y)==0, "%s test"); | |
| 114 CYG_TEST_PASS_FAIL(ret == 1, "%s test return code"); | |
| 115 | |
| 116 // Check 3 | |
| 117 my_strcpy(x, "FE8C"); | |
| 118 ret = sscanf(x, "%X", &int_test); | |
| 119 CYG_TEST_PASS_FAIL(int_test == 65164, "hex read leading zeroes"); | |
| 120 CYG_TEST_PASS_FAIL(ret == 1, "hex read leading zeroes return code"); | |
| 121 | |
| 122 // Check 4 | |
| 123 my_strcpy(x, "000053Ba"); | |
| 124 ret = sscanf(x, "%x", &int_test); | |
| 125 CYG_TEST_PASS_FAIL(int_test == 21434, "hex read leading zeroes"); | |
| 126 CYG_TEST_PASS_FAIL(ret == 1, "hex read leading zeros return code"); | |
| 127 | |
| 128 // Check 5 | |
| 129 my_strcpy(x, "53Ba"); | |
| 130 ret = sscanf(x, "%3x", &int_test); | |
| 131 CYG_TEST_PASS_FAIL(int_test == 1339, "hex read constrained width"); | |
| 132 CYG_TEST_PASS_FAIL(ret == 1, "hex read constrained width return code"); | |
| 133 | |
| 134 // Check 6 | |
| 135 my_strcpy(x, "get the first char test"); | |
| 136 ret = sscanf(x, "%c", &y[0]); | |
| 137 CYG_TEST_PASS_FAIL(y[0] == 'g', "%c test"); | |
| 138 CYG_TEST_PASS_FAIL(ret == 1, "%c test return code"); | |
| 139 | |
| 140 // Check 7 | |
| 141 my_strcpy(x, "-5"); | |
| 142 ret = sscanf(x, "%d", &int_test); | |
| 143 CYG_TEST_PASS_FAIL(int_test == -5, "negative integer"); | |
| 144 CYG_TEST_PASS_FAIL(ret == 1, "negative integer return code"); | |
| 145 | |
| 146 // Check 8 | |
| 147 my_strcpy(x, "53"); | |
| 148 ret = sscanf(x, "%o", &int_test); | |
| 149 CYG_TEST_PASS_FAIL(int_test == 43, "%o test"); | |
| 150 CYG_TEST_PASS_FAIL(ret == 1, "%o test return code"); | |
| 151 | |
| 152 // Check 9 | |
| 153 int_test=10; | |
| 154 my_strcpy(x, "-5 0"); | |
| 155 ret = sscanf(x, "%*d %d", &int_test); | |
| 156 CYG_TEST_PASS_FAIL(int_test == 0, "assignment suppression"); | |
| 157 CYG_TEST_PASS_FAIL(ret == 1, "assignment suppression return code"); | |
| 158 | |
| 159 // Check 10 | |
| 160 int_test=0; | |
| 161 my_strcpy(x, "052"); | |
| 162 ret = sscanf(x, "%i", &int_test); | |
| 163 CYG_TEST_PASS_FAIL(int_test == 42, "octal with %i"); | |
| 164 CYG_TEST_PASS_FAIL(ret == 1, "octal with %i return code"); | |
| 165 | |
| 166 // Check 10 | |
| 167 int_test=0; | |
| 168 my_strcpy(x, "0x05f"); | |
| 169 ret = sscanf(x, "%i", &int_test); | |
| 170 CYG_TEST_PASS_FAIL(int_test == 95, "hex with %i"); | |
| 171 CYG_TEST_PASS_FAIL(ret == 1, "hex with %i return code"); | |
| 172 | |
| 173 // Check 11 | |
| 174 int_test=0; | |
| 175 my_strcpy(x, "+023456"); | |
| 176 ret = sscanf(x, "%u", &int_test); | |
| 177 CYG_TEST_PASS_FAIL(int_test == 23456, "unsigned int with %u"); | |
| 178 CYG_TEST_PASS_FAIL(ret == 1, "unsigned int with %u return code"); | |
| 179 | |
| 180 // Check 12 | |
| 181 my_strcpy(x, "aString 2747 D"); | |
| 182 ret = sscanf(x, "%s %d %c", y, &int_test, z); | |
| 183 CYG_TEST_PASS_FAIL( (my_strcmp(y, "aString")==0) && | |
| 184 (int_test == 2747) && | |
| 185 (z[0] == 'D'), "A few combinations #1"); | |
| 186 CYG_TEST_PASS_FAIL(ret == 3, "Combinations #1 return code"); | |
| 187 | |
| 188 | |
| 189 // Check 13 | |
| 190 my_strcpy(x, "-6 52 awurble 2747 xxx"); | |
| 191 ret = sscanf(x, "%d %u %s %2d %n %c", | |
| 192 &int_test, &int_test2, y, &int_test3, &int_test4, z); | |
| 193 CYG_TEST_PASS_FAIL( (int_test == -6) && (int_test2 == 52) && | |
| 194 (my_strcmp(y, "awurble")==0) && | |
| 195 (int_test3 == 27) && | |
| 196 (int_test4 == 16) && | |
| 197 (z[0] == '4'), "A few combinations #2"); | |
| 198 CYG_TEST_PASS_FAIL(ret == 5, "Combinations #2 return code"); | |
| 199 | |
| 200 | |
| 201 #ifdef CYGSEM_LIBC_STDIO_SCANF_FLOATING_POINT | |
| 202 | |
| 203 // How much _relative_ error do we allow? | |
| 204 #define FLT_TOLERANCE 1.0e-3 | |
| 205 | |
| 206 #define MY_FABS(x) ((x) < 0.0 ? -(x) : (x)) | |
| 207 | |
| 208 CYG_TEST_INFO("Starting floating point specific tests"); | |
| 209 | |
| 210 { | |
| 211 float flt_test, wanted; | |
| 212 | |
| 213 // Check 14 | |
| 214 my_strcpy(x, "2.5"); | |
| 215 wanted = 2.5; | |
| 216 ret = sscanf(x, "%f", &flt_test); | |
| 217 CYG_TEST_PASS_FAIL( MY_FABS(flt_test - wanted) < | |
| 218 MY_FABS(wanted * FLT_TOLERANCE), | |
| 219 "Simple %f test #1" ); | |
| 220 CYG_TEST_PASS_FAIL( ret == 1, "Simple %f test #1 return code" ); | |
| 221 | |
| 222 | |
| 223 // Check 15 | |
| 224 my_strcpy(x, "-23.472345"); | |
| 225 wanted = -23.472345; | |
| 226 ret = sscanf(x, "%f", &flt_test); | |
| 227 CYG_TEST_PASS_FAIL( MY_FABS(flt_test - wanted) < | |
| 228 MY_FABS(wanted * FLT_TOLERANCE), | |
| 229 "Simple %f test #2" ); | |
| 230 CYG_TEST_PASS_FAIL( ret == 1, "Simple %f test #2 return code" ); | |
| 231 | |
| 232 // Check 16 | |
| 233 my_strcpy(x, "0.0"); | |
| 234 ret = sscanf(x, "%f", &flt_test); | |
| 235 CYG_TEST_PASS_FAIL( flt_test==0.0, "Simple %f test 0.0" ); | |
| 236 CYG_TEST_PASS_FAIL( ret == 1, "Simple %f test 0.0 return code" ); | |
| 237 | |
| 238 | |
| 239 // Check 17 | |
| 240 my_strcpy(x, "-2.6334e00"); | |
| 241 wanted = -2.6334; | |
| 242 ret = sscanf(x, "%e", &flt_test); | |
| 243 CYG_TEST_PASS_FAIL( MY_FABS(flt_test - wanted) < | |
| 244 MY_FABS(wanted * FLT_TOLERANCE), | |
| 245 "Simple %e test #1" ); | |
| 246 CYG_TEST_PASS_FAIL( ret == 1, "Simple %e test #1 return code" ); | |
| 247 | |
| 248 | |
| 249 // Check 18 | |
| 250 my_strcpy(x, "-2.56789e-06"); | |
| 251 wanted = -2.56789e-06; | |
| 252 ret = sscanf(x, "%e", &flt_test); | |
| 253 CYG_TEST_PASS_FAIL( MY_FABS(flt_test-wanted) < | |
| 254 MY_FABS(wanted * FLT_TOLERANCE), | |
| 255 "Simple %e test #2" ); | |
| 256 CYG_TEST_PASS_FAIL( ret == 1, "Simple %e test #2 return code" ); | |
| 257 | |
| 258 | |
| 259 // Check 19 | |
| 260 my_strcpy(x, "-2.12389e-06"); | |
| 261 wanted = -2.12389e-06; | |
| 262 ret = sscanf(x, "%g", &flt_test); | |
| 263 CYG_TEST_PASS_FAIL( MY_FABS(flt_test-wanted) < | |
| 264 MY_FABS(wanted * FLT_TOLERANCE), | |
| 265 "Simple %g test #1" ); | |
| 266 CYG_TEST_PASS_FAIL( ret == 1, "Simple %g test #1 return code" ); | |
| 267 | |
| 268 // Check 20 | |
| 269 my_strcpy(x, "1.3423345"); | |
| 270 wanted = 1.342335; | |
| 271 ret = sscanf(x, "%g", &flt_test); | |
| 272 CYG_TEST_PASS_FAIL( MY_FABS(flt_test-wanted) < | |
| 273 MY_FABS(wanted * FLT_TOLERANCE), | |
| 274 "Simple %g test #2" ); | |
| 275 CYG_TEST_PASS_FAIL( ret == 1, "Simple %g test #2 return code" ); | |
| 276 | |
| 277 | |
| 278 } // compound | |
| 279 | |
| 280 #else | |
| 281 CYG_TEST_PASS("Floating point tests skipped - not configured"); | |
| 282 #endif // ifdef CYGSEM_LIBC_STDIO_SCANF_FLOATING_POINT | |
| 283 | |
| 284 | |
| 285 CYG_TEST_FINISH("Finished tests from testcase " __FILE__ " for C library " | |
| 286 "sscanf() function"); | |
| 287 | |
| 288 } // test() | |
| 289 | |
| 290 #endif // defined(CYGFUN_LIBC_STDIO_ungetc) | |
| 291 | |
| 292 | |
| 293 int | |
| 294 main(int argc, char *argv[]) | |
| 295 { | |
| 296 CYG_TEST_INIT(); | |
| 297 | |
| 298 CYG_TEST_INFO("Starting tests from testcase " __FILE__ " for C library " | |
| 299 "sscanf() function"); | |
| 300 | |
| 301 START_TEST( test ); | |
| 302 | |
| 303 CYG_TEST_NA("Testing is not applicable to this configuration"); | |
| 304 } // main() | |
| 305 | |
| 306 | |
| 307 // EOF sscanf.c |
