Changeset 6b646dc in mainline
- Timestamp:
- 2014-09-19T12:41:34Z (10 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 6019983
- Parents:
- bf45993
- Location:
- uspace/lib/posix
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/posix/source/stdio/scanf.c
rbf45993 r6b646dc 1220 1220 } 1221 1221 1222 // FIXME: put the testcases to the app/tester after scanf is included into libc1223 1224 #if 01225 1226 //#include <stdio.h>1227 //#include <malloc.h>1228 //#include <string.h>1229 1230 #define test_val(fmt, exp_val, act_val) \1231 if (exp_val == act_val) { \1232 printf("succ, expected "fmt", actual "fmt"\n", exp_val, act_val); \1233 } else { \1234 printf("fail, expected "fmt", actual "fmt"\n", exp_val, act_val); \1235 ++fail; \1236 }1237 1238 #define test_str(fmt, exp_str, act_str) \1239 if (posix_strcmp(exp_str, act_str) == 0) { \1240 printf("succ, expected "fmt", actual "fmt"\n", exp_str, act_str); \1241 } else { \1242 printf("fail, expected "fmt", actual "fmt"\n", exp_str, act_str); \1243 ++fail; \1244 }1245 1246 void __posix_scanf_test(void);1247 void __posix_scanf_test(void)1248 {1249 int fail = 0;1250 1251 int ret;1252 1253 unsigned char uhh;1254 signed char shh;1255 unsigned short uh;1256 short sh;1257 unsigned udef;1258 int sdef;1259 unsigned long ul;1260 long sl;1261 unsigned long long ull;1262 long long sll;1263 void *p;1264 1265 float f;1266 double d;1267 long double ld;1268 1269 char str[20];1270 char seq[20];1271 char scanset[20];1272 1273 char *pstr;1274 char *pseq;1275 char *pscanset;1276 1277 ret = posix_sscanf(1278 "\n j tt % \t -121314 98765 aqw 0765 0x77 0xABCDEF88 -99 884",1279 " j tt %%%3hhd%1hhu%3hd %3hu%u aqw%n %lo%llx %p %li %lld",1280 &shh, &uhh, &sh, &uh, &udef, &sdef, &ul, &ull, &p, &sl, &sll);1281 test_val("%d", -12, shh);1282 test_val("%u", 1, uhh);1283 test_val("%d", 314, sh);1284 test_val("%u", 987, uh);1285 test_val("%u", 65, udef);1286 test_val("%d", 28, sdef);1287 test_val("%lo", (unsigned long) 0765, ul);1288 test_val("%llx", (unsigned long long) 0x77, ull);1289 test_val("%p", (void *) 0xABCDEF88, p);1290 test_val("%ld", (long) -99, sl);1291 test_val("%lld", (long long) 884, sll);1292 test_val("%d", 10, ret);1293 1294 ret = posix_sscanf(1295 "\n \t\t1.0 -0x555.AP10 1234.5678e12",1296 "%f %lf %Lf",1297 &f, &d, &ld);1298 test_val("%f", 1.0, f);1299 test_val("%lf", (double) -0x555.AP10, d);1300 test_val("%Lf", (long double) 1234.5678e12, ld);1301 test_val("%d", 3, ret);1302 1303 ret = posix_sscanf(1304 "\n\n\thello world \n",1305 "%5s %ms",1306 str, &pstr);1307 test_str("%s", "hello", str);1308 test_str("%s", "world", pstr);1309 test_val("%d", 2, ret);1310 free(pstr);1311 1312 ret = posix_sscanf(1313 "\n\n\thello world \n",1314 " %5c %mc",1315 seq, &pseq);1316 seq[5] = '\0';1317 pseq[1] = '\0';1318 test_str("%s", "hello", seq);1319 test_str("%s", "w", pseq);1320 test_val("%d", 2, ret);1321 free(pseq);1322 1323 ret = posix_sscanf(1324 "\n\n\th-e-l-l-o world-] \n",1325 " %9[-eh-o] %m[^]-]",1326 scanset, &pscanset);1327 test_str("%s", "h-e-l-l-o", scanset);1328 test_str("%s", "world", pscanset);1329 test_val("%d", 2, ret);1330 free(pscanset);1331 1332 printf("Failed: %d\n", fail);1333 }1334 1335 #endif1336 1337 1222 /** @} 1338 1223 */ -
uspace/lib/posix/test/scanf.c
rbf45993 r6b646dc 35 35 #include <pcut/pcut.h> 36 36 37 #define EPSILON 0.000001 37 38 38 39 PCUT_INIT … … 62 63 } 63 64 65 /* 66 * The following tests were copied from stdio/scanf.c where they were 67 * commented-out. We ought to convert them to more independent tests 68 * eventually. 69 */ 70 71 PCUT_TEST(int_misc) { 72 unsigned char uhh; 73 signed char shh; 74 unsigned short uh; 75 short sh; 76 unsigned udef; 77 int sdef; 78 unsigned long ul; 79 long sl; 80 unsigned long long ull; 81 long long sll; 82 void *p; 83 84 int rc = posix_sscanf( 85 "\n j tt % \t -121314 98765 aqw 0765 0x77 0xABCDEF88 -99 884", 86 " j tt %%%3hhd%1hhu%3hd %3hu%u aqw%n %lo%llx %p %li %lld", 87 &shh, &uhh, &sh, &uh, &udef, &sdef, &ul, &ull, &p, &sl, &sll); 88 89 PCUT_ASSERT_INT_EQUALS(10, rc); 90 91 PCUT_ASSERT_INT_EQUALS(-12, shh); 92 PCUT_ASSERT_INT_EQUALS(1, uhh); 93 PCUT_ASSERT_INT_EQUALS(314, sh); 94 PCUT_ASSERT_INT_EQUALS(987, uh); 95 PCUT_ASSERT_INT_EQUALS(65, udef); 96 PCUT_ASSERT_INT_EQUALS(28, sdef); 97 PCUT_ASSERT_INT_EQUALS(0765, ul); 98 PCUT_ASSERT_INT_EQUALS(0x77, ull); 99 PCUT_ASSERT_INT_EQUALS(0xABCDEF88, (long long) (uintptr_t) p); 100 PCUT_ASSERT_INT_EQUALS(-99, sl); 101 PCUT_ASSERT_INT_EQUALS(884, sll); 102 } 103 104 PCUT_TEST(double_misc) { 105 float f; 106 double d; 107 long double ld; 108 109 int rc = posix_sscanf( 110 "\n \t\t1.0 -0x555.AP10 1234.5678e12", 111 "%f %lf %Lf", 112 &f, &d, &ld); 113 114 PCUT_ASSERT_INT_EQUALS(3, rc); 115 116 PCUT_ASSERT_DOUBLE_EQUALS(1.0, f, EPSILON); 117 PCUT_ASSERT_DOUBLE_EQUALS(-0x555.AP10, d, EPSILON); 118 PCUT_ASSERT_DOUBLE_EQUALS(1234.5678e12, ld, EPSILON); 119 } 120 121 PCUT_TEST(str_misc) { 122 char str[20]; 123 char *pstr; 124 125 int rc = posix_sscanf( 126 "\n\n\thello world \n", 127 "%5s %ms", 128 str, &pstr); 129 130 PCUT_ASSERT_INT_EQUALS(2, rc); 131 132 PCUT_ASSERT_STR_EQUALS("hello", str); 133 PCUT_ASSERT_STR_EQUALS("world", pstr); 134 135 free(pstr); 136 } 137 138 PCUT_TEST(str_matchers) { 139 char scanset[20]; 140 char *pscanset; 141 142 int rc = posix_sscanf( 143 "\n\n\th-e-l-l-o world-] \n", 144 " %9[-eh-o] %m[^]-]", 145 scanset, &pscanset); 146 147 PCUT_ASSERT_INT_EQUALS(2, rc); 148 149 PCUT_ASSERT_STR_EQUALS("h-e-l-l-o", scanset); 150 PCUT_ASSERT_STR_EQUALS("world", pscanset); 151 152 free(pscanset); 153 } 154 155 PCUT_TEST(char_misc) { 156 char seq[20]; 157 char *pseq; 158 159 int rc = posix_sscanf( 160 "\n\n\thello world \n", 161 " %5c %mc", 162 seq, &pseq); 163 164 PCUT_ASSERT_INT_EQUALS(2, rc); 165 166 /* Manually terminate the strings. */ 167 seq[5] = 0; 168 pseq[1] = 0; 169 170 PCUT_ASSERT_STR_EQUALS("hello", seq); 171 PCUT_ASSERT_STR_EQUALS("w", pseq); 172 173 free(pseq); 174 } 175 64 176 #endif 65 177
Note:
See TracChangeset
for help on using the changeset viewer.