unit_str_to_value.c (86458B)
1 /* 2 This file is part of libmicrohttpd 3 Copyright (C) 2016 Karlson2k (Evgeny Grin) 4 5 This test tool is free software; you can redistribute it and/or 6 modify it under the terms of the GNU General Public License as 7 published by the Free Software Foundation; either version 2, or 8 (at your option) any later version. 9 10 This test tool is distributed in the hope that it will be useful, 11 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 Lesser General Public License for more details. 14 15 You should have received a copy of the GNU Lesser General Public 16 License along with this library; if not, write to the Free Software 17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 */ 19 20 /** 21 * @file src/tests/unit/unit_str_test.h 22 * @brief Unit tests for mhd_str functions 23 * @author Karlson2k (Evgeny Grin) 24 */ 25 26 #include "mhd_sys_options.h" 27 28 #include <stdio.h> 29 #include <locale.h> 30 #include <string.h> 31 #ifdef HAVE_INTTYPES_H 32 #include <inttypes.h> 33 #else /* ! HAVE_INTTYPES_H */ 34 #define PRIuFAST64 "llu" 35 #define PRIuPTR "llu" 36 #define PRIXFAST64 "llX" 37 #endif /* ! HAVE_INTTYPES_H */ 38 #include <stdint.h> 39 #ifdef HAVE_STDLIB_H 40 #include <stdlib.h> 41 #endif /* HAVE_STDLIB_H */ 42 43 #include "mhd_str.h" 44 #include "mhd_str.c" 45 46 #include "mhdt_checks.h" 47 #include "mhdt_has_param.h" 48 49 50 static int verbose = 0; /* verbose level (0-3)*/ 51 52 /* Locale names to test. 53 * Functions must not depend of current current locale, 54 * so result must be the same in any locale. 55 */ 56 static const char *const locale_names[] = { 57 "C", 58 "", /* System default locale */ 59 #if defined(_WIN32) && ! defined(__CYGWIN__) 60 ".OCP", /* W32 system default OEM code page */ 61 ".ACP", /* W32 system default ANSI code page */ 62 ".65001", /* UTF-8 */ 63 ".437", 64 ".850", 65 ".857", 66 ".866", 67 ".1250", 68 ".1251", 69 ".1252", 70 "en", 71 "english", 72 "French_France", 73 "Turkish_Turkey.1254", 74 "de", 75 "zh-Hans", 76 "ru-RU.1251" 77 #if 0 /* Disabled extra checks */ 78 , 79 ".1254", 80 ".20866", /* number for KOI8-R */ 81 ".28591", /* number for ISO-8859-1 */ 82 ".28595", /* number for ISO-8859-5 */ 83 ".28599", /* number for ISO-8859-9 */ 84 ".28605", /* number for ISO-8859-15 */ 85 "en-US", 86 "English-US", 87 "en-US.437", 88 "English_United States.437", 89 "en-US.1252", 90 "English_United States.1252", 91 "English_United States.28591", 92 "English_United States.65001", 93 "fra", 94 "french", 95 "fr-FR", 96 "fr-FR.850", 97 "french_france.850", 98 "fr-FR.1252", 99 "French_france.1252", 100 "French_france.28605", 101 "French_France.65001", 102 "de-DE", 103 "de-DE.850", 104 "German_Germany.850", 105 "German_Germany.1250", 106 "de-DE.1252", 107 "German_Germany.1252", 108 "German_Germany.28605", 109 "German_Germany.65001", 110 "tr", 111 "trk", 112 "turkish", 113 "tr-TR", 114 "tr-TR.1254", 115 "tr-TR.857", 116 "Turkish_Turkey.857", 117 "Turkish_Turkey.28599", 118 "Turkish_Turkey.65001", 119 "ru", 120 "ru-RU", 121 "Russian", 122 "ru-RU.866", 123 "Russian_Russia.866", 124 "Russian_Russia.1251", 125 "Russian_Russia.20866", 126 "Russian_Russia.28595", 127 "Russian_Russia.65001", 128 "zh-Hans.936", 129 "chinese-simplified" 130 #endif /* Disabled extra checks */ 131 #else /* ! _WIN32 || __CYGWIN__ */ 132 "C.UTF-8", 133 "POSIX", 134 "en", 135 "en_US", 136 "en_US.ISO-8859-1", 137 "en_US.ISO_8859-1", 138 "en_US.ISO8859-1", 139 "en_US.iso88591", 140 "en_US.ISO-8859-15", 141 "en_US.DIS_8859-15", 142 "en_US.ISO8859-15", 143 "en_US.iso885915", 144 "en_US.1252", 145 "en_US.CP1252", 146 "en_US.UTF-8", 147 "en_US.utf8", 148 "fr", 149 "fr_FR", 150 "fr_FR.850", 151 "fr_FR.IBM850", 152 "fr_FR.1252", 153 "fr_FR.CP1252", 154 "fr_FR.ISO-8859-1", 155 "fr_FR.ISO_8859-1", 156 "fr_FR.ISO8859-1", 157 "fr_FR.iso88591", 158 "fr_FR.ISO-8859-15", 159 "fr_FR.DIS_8859-15", 160 "fr_FR.ISO8859-15", 161 "fr_FR.iso8859-15", 162 "fr_FR.UTF-8", 163 "fr_FR.utf8", 164 "de", 165 "de_DE", 166 "de_DE.850", 167 "de_DE.IBM850", 168 "de_DE.1250", 169 "de_DE.CP1250", 170 "de_DE.1252", 171 "de_DE.CP1252", 172 "de_DE.ISO-8859-1", 173 "de_DE.ISO_8859-1", 174 "de_DE.ISO8859-1", 175 "de_DE.iso88591", 176 "de_DE.ISO-8859-15", 177 "de_DE.DIS_8859-15", 178 "de_DE.ISO8859-15", 179 "de_DE.iso885915", 180 "de_DE.UTF-8", 181 "de_DE.utf8", 182 "tr", 183 "tr_TR", 184 "tr_TR.1254", 185 "tr_TR.CP1254", 186 "tr_TR.857", 187 "tr_TR.IBM857", 188 "tr_TR.ISO-8859-9", 189 "tr_TR.ISO8859-9", 190 "tr_TR.iso88599", 191 "tr_TR.UTF-8", 192 "tr_TR.utf8", 193 "ru", 194 "ru_RU", 195 "ru_RU.1251", 196 "ru_RU.CP1251", 197 "ru_RU.866", 198 "ru_RU.IBM866", 199 "ru_RU.KOI8-R", 200 "ru_RU.koi8-r", 201 "ru_RU.KOI8-RU", 202 "ru_RU.ISO-8859-5", 203 "ru_RU.ISO_8859-5", 204 "ru_RU.ISO8859-5", 205 "ru_RU.iso88595", 206 "ru_RU.UTF-8", 207 "zh_CN", 208 "zh_CN.GB2312", 209 "zh_CN.UTF-8", 210 #endif /* ! _WIN32 || __CYGWIN__ */ 211 }; 212 213 static const unsigned int locale_name_count = sizeof(locale_names) 214 / sizeof(locale_names[0]); 215 216 217 /* 218 * Helper functions 219 */ 220 221 static int 222 set_test_locale (size_t num) 223 { 224 if (num >= locale_name_count) 225 { 226 fprintf (stderr, "Unexpected number of locale.\n"); 227 exit (99); 228 } 229 if (verbose > 2) 230 printf ("Setting locale \"%s\":", locale_names[num]); 231 if (setlocale (LC_ALL, locale_names[num])) 232 { 233 if (verbose > 2) 234 printf (" succeed.\n"); 235 return 1; 236 } 237 if (verbose > 2) 238 printf (" failed.\n"); 239 return 0; 240 } 241 242 243 static const char * 244 get_current_locale_str (void) 245 { 246 char const *loc_str = setlocale (LC_ALL, NULL); 247 return loc_str ? loc_str : "unknown"; 248 } 249 250 251 static char tmp_bufs[4][4 * 1024]; /* should be enough for testing */ 252 static size_t buf_idx = 0; 253 254 /* print non-printable chars as char codes */ 255 static char * 256 n_prnt (const char *str) 257 { 258 static char *buf; /* should be enough for testing */ 259 static const size_t buf_size = sizeof(tmp_bufs[0]); 260 const unsigned char *p = (const unsigned char *) str; 261 size_t w_pos = 0; 262 if (++buf_idx > 3) 263 buf_idx = 0; 264 buf = tmp_bufs[buf_idx]; 265 266 while (*p && w_pos + 1 < buf_size) 267 { 268 const unsigned char c = *p; 269 if ((c == '\\') || (c == '"') ) 270 { 271 if (w_pos + 2 >= buf_size) 272 break; 273 buf[w_pos++] = '\\'; 274 buf[w_pos++] = (char) c; 275 } 276 else if ((c >= 0x20) && (c <= 0x7E) ) 277 buf[w_pos++] = (char) c; 278 else 279 { 280 if (w_pos + 4 >= buf_size) 281 break; 282 if (snprintf (buf + w_pos, buf_size - w_pos, "\\x%02hX", (short unsigned 283 int) c) != 4) 284 break; 285 w_pos += 4; 286 } 287 p++; 288 } 289 if (*p) 290 { /* not full string is printed */ 291 /* enough space for "..." ? */ 292 if (w_pos + 3 > buf_size) 293 w_pos = buf_size - 4; 294 buf[w_pos++] = '.'; 295 buf[w_pos++] = '.'; 296 buf[w_pos++] = '.'; 297 } 298 buf[w_pos] = 0; 299 return buf; 300 } 301 302 303 struct str_with_len 304 { 305 const char *const str; 306 const size_t len; 307 }; 308 309 #define D_STR_W_LEN(s) {(s), (sizeof((s)) / sizeof(char)) - 1} 310 311 /* 312 * Digits in string -> value tests 313 */ 314 315 struct str_with_value 316 { 317 const struct str_with_len str; 318 const size_t num_of_digt; 319 const uint_fast64_t val; 320 }; 321 322 /* valid string for conversion to unsigned integer value */ 323 static const struct str_with_value dstrs_w_values[] = { 324 /* simplest strings */ 325 {D_STR_W_LEN ("1"), 1, 1}, 326 {D_STR_W_LEN ("0"), 1, 0}, 327 {D_STR_W_LEN ("10000"), 5, 10000}, 328 329 /* all digits */ 330 {D_STR_W_LEN ("1234"), 4, 1234}, 331 {D_STR_W_LEN ("4567"), 4, 4567}, 332 {D_STR_W_LEN ("7890"), 4, 7890}, 333 {D_STR_W_LEN ("8021"), 4, 8021}, 334 {D_STR_W_LEN ("9754"), 4, 9754}, 335 {D_STR_W_LEN ("6392"), 4, 6392}, 336 337 /* various prefixes */ 338 {D_STR_W_LEN ("00000000"), 8, 0}, 339 {D_STR_W_LEN ("0755"), 4, 755}, /* not to be interpreted as octal value! */ 340 {D_STR_W_LEN ("002"), 3, 2}, 341 {D_STR_W_LEN ("0001"), 4, 1}, 342 {D_STR_W_LEN ("00000000000000000000000031295483"), 32, 31295483}, 343 344 /* numbers below and above limits */ 345 {D_STR_W_LEN ("127"), 3, 127}, /* 0x7F, SCHAR_MAX */ 346 {D_STR_W_LEN ("128"), 3, 128}, /* 0x80, SCHAR_MAX+1 */ 347 {D_STR_W_LEN ("255"), 3, 255}, /* 0xFF, UCHAR_MAX */ 348 {D_STR_W_LEN ("256"), 3, 256}, /* 0x100, UCHAR_MAX+1 */ 349 {D_STR_W_LEN ("32767"), 5, 32767}, /* 0x7FFF, INT16_MAX */ 350 {D_STR_W_LEN ("32768"), 5, 32768}, /* 0x8000, INT16_MAX+1 */ 351 {D_STR_W_LEN ("65535"), 5, 65535}, /* 0xFFFF, UINT16_MAX */ 352 {D_STR_W_LEN ("65536"), 5, 65536}, /* 0x10000, UINT16_MAX+1 */ 353 {D_STR_W_LEN ("2147483647"), 10, 2147483647}, /* 0x7FFFFFFF, INT32_MAX */ 354 {D_STR_W_LEN ("2147483648"), 10, UINT64_C (2147483648)}, /* 0x80000000, INT32_MAX+1 */ 355 {D_STR_W_LEN ("4294967295"), 10, UINT64_C (4294967295)}, /* 0xFFFFFFFF, UINT32_MAX */ 356 {D_STR_W_LEN ("4294967296"), 10, UINT64_C (4294967296)}, /* 0x100000000, UINT32_MAX+1 */ 357 {D_STR_W_LEN ("9223372036854775807"), 19, UINT64_C (9223372036854775807)}, /* 0x7FFFFFFFFFFFFFFF, INT64_MAX */ 358 {D_STR_W_LEN ("9223372036854775808"), 19, UINT64_C (9223372036854775808)}, /* 0x8000000000000000, INT64_MAX+1 */ 359 {D_STR_W_LEN ("18446744073709551615"), 20, UINT64_C (18446744073709551615)}, /* 0xFFFFFFFFFFFFFFFF, UINT64_MAX */ 360 361 /* random numbers */ 362 {D_STR_W_LEN ("10186753"), 8, 10186753}, 363 {D_STR_W_LEN ("144402566"), 9, 144402566}, 364 {D_STR_W_LEN ("151903144"), 9, 151903144}, 365 {D_STR_W_LEN ("139264621"), 9, 139264621}, 366 {D_STR_W_LEN ("730348"), 6, 730348}, 367 {D_STR_W_LEN ("21584377"), 8, 21584377}, 368 {D_STR_W_LEN ("709"), 3, 709}, 369 {D_STR_W_LEN ("54"), 2, 54}, 370 {D_STR_W_LEN ("8452"), 4, 8452}, 371 {D_STR_W_LEN ("17745098750013624977"), 20, UINT64_C (17745098750013624977)}, 372 {D_STR_W_LEN ("06786878769931678000"), 20, UINT64_C (6786878769931678000)}, 373 374 /* non-digit suffixes */ 375 {D_STR_W_LEN ("1234oa"), 4, 1234}, 376 {D_STR_W_LEN ("20h"), 2, 20}, /* not to be interpreted as hex value! */ 377 {D_STR_W_LEN ("0x1F"), 1, 0}, /* not to be interpreted as hex value! */ 378 {D_STR_W_LEN ("0564`~}"), 4, 564}, 379 {D_STR_W_LEN ("7240146.724"), 7, 7240146}, 380 {D_STR_W_LEN ("2,9"), 1, 2}, 381 {D_STR_W_LEN ("200+1"), 3, 200}, 382 {D_STR_W_LEN ("1a"), 1, 1}, 383 {D_STR_W_LEN ("2E"), 1, 2}, 384 {D_STR_W_LEN ("6c"), 1, 6}, 385 {D_STR_W_LEN ("8F"), 1, 8}, 386 {D_STR_W_LEN ("287416997! And the not too long string."), 9, 287416997} 387 }; 388 389 /* strings that should overflow uint64_t */ 390 static const struct str_with_len str_ovflw[] = { 391 D_STR_W_LEN ("18446744073709551616"), /* 0x10000000000000000, UINT64_MAX+1 */ 392 D_STR_W_LEN ("18446744073709551620"), 393 D_STR_W_LEN ("18446744083709551615"), 394 D_STR_W_LEN ("19234761020556472143"), 395 D_STR_W_LEN ("184467440737095516150"), 396 D_STR_W_LEN ("1844674407370955161500"), 397 D_STR_W_LEN ("000018446744073709551616"), /* 0x10000000000000000, UINT64_MAX+1 */ 398 D_STR_W_LEN ("20000000000000000000"), 399 D_STR_W_LEN ("020000000000000000000"), 400 D_STR_W_LEN ("0020000000000000000000"), 401 D_STR_W_LEN ("100000000000000000000"), 402 D_STR_W_LEN ("434532891232591226417"), 403 D_STR_W_LEN ("99999999999999999999"), 404 D_STR_W_LEN ("18446744073709551616abcd"), /* 0x10000000000000000, UINT64_MAX+1 */ 405 D_STR_W_LEN ("20000000000000000000 suffix"), 406 D_STR_W_LEN ("020000000000000000000x") 407 }; 408 409 /* strings that should not be convertible to numeric value */ 410 static const struct str_with_len str_no_num[] = { 411 D_STR_W_LEN ("zero"), 412 D_STR_W_LEN ("one"), 413 D_STR_W_LEN ("\xb9\xb2\xb3"), /* superscript "123" in ISO-8859-1/CP1252 */ 414 D_STR_W_LEN ("\xc2\xb9\xc2\xb2\xc2\xb3"), /* superscript "123" in UTF-8 */ 415 D_STR_W_LEN ("\xd9\xa1\xd9\xa2\xd9\xa3"), /* Arabic-Indic "١٢٣" in UTF-8 */ 416 D_STR_W_LEN ("\xdb\xb1\xdb\xb2\xdb\xb3"), /* Ext Arabic-Indic "۱۲۳" in UTF-8 */ 417 D_STR_W_LEN ("\xe0\xa5\xa7\xe0\xa5\xa8\xe0\xa5\xa9"), /* Devanagari "१२३" in UTF-8 */ 418 D_STR_W_LEN ("\xe4\xb8\x80\xe4\xba\x8c\xe4\xb8\x89"), /* Chinese "一二三" in UTF-8 */ 419 D_STR_W_LEN ("\xd2\xbb\xb6\xfe\xc8\xfd"), /* Chinese "一二三" in GB2312/CP936 */ 420 D_STR_W_LEN ("\x1B\x24\x29\x41\x0E\x52\x3B\x36\x7E\x48\x7D\x0F") /* Chinese "一二三" in ISO-2022-CN */ 421 }; 422 423 /* valid hex string for conversion to unsigned integer value */ 424 static const struct str_with_value xdstrs_w_values[] = { 425 /* simplest strings */ 426 {D_STR_W_LEN ("1"), 1, 0x1}, 427 {D_STR_W_LEN ("0"), 1, 0x0}, 428 {D_STR_W_LEN ("10000"), 5, 0x10000}, 429 430 /* all digits */ 431 {D_STR_W_LEN ("1234"), 4, 0x1234}, 432 {D_STR_W_LEN ("4567"), 4, 0x4567}, 433 {D_STR_W_LEN ("7890"), 4, 0x7890}, 434 {D_STR_W_LEN ("8021"), 4, 0x8021}, 435 {D_STR_W_LEN ("9754"), 4, 0x9754}, 436 {D_STR_W_LEN ("6392"), 4, 0x6392}, 437 {D_STR_W_LEN ("abcd"), 4, 0xABCD}, 438 {D_STR_W_LEN ("cdef"), 4, 0xCDEF}, 439 {D_STR_W_LEN ("FEAB"), 4, 0xFEAB}, 440 {D_STR_W_LEN ("BCED"), 4, 0xBCED}, 441 {D_STR_W_LEN ("bCeD"), 4, 0xBCED}, 442 {D_STR_W_LEN ("1A5F"), 4, 0x1A5F}, 443 {D_STR_W_LEN ("12AB"), 4, 0x12AB}, 444 {D_STR_W_LEN ("CD34"), 4, 0xCD34}, 445 {D_STR_W_LEN ("56EF"), 4, 0x56EF}, 446 {D_STR_W_LEN ("7a9f"), 4, 0x7A9F}, 447 448 /* various prefixes */ 449 {D_STR_W_LEN ("00000000"), 8, 0x0}, 450 {D_STR_W_LEN ("0755"), 4, 0x755}, /* not to be interpreted as octal value! */ 451 {D_STR_W_LEN ("002"), 3, 0x2}, 452 {D_STR_W_LEN ("0001"), 4, 0x1}, 453 {D_STR_W_LEN ("00a"), 3, 0xA}, 454 {D_STR_W_LEN ("0F"), 2, 0xF}, 455 {D_STR_W_LEN ("0000000000000000000000003A29e4C3"), 32, 0x3A29E4C3}, 456 457 /* numbers below and above limits */ 458 {D_STR_W_LEN ("7F"), 2, 127}, /* 0x7F, SCHAR_MAX */ 459 {D_STR_W_LEN ("7f"), 2, 127}, /* 0x7F, SCHAR_MAX */ 460 {D_STR_W_LEN ("80"), 2, 128}, /* 0x80, SCHAR_MAX+1 */ 461 {D_STR_W_LEN ("fF"), 2, 255}, /* 0xFF, UCHAR_MAX */ 462 {D_STR_W_LEN ("Ff"), 2, 255}, /* 0xFF, UCHAR_MAX */ 463 {D_STR_W_LEN ("FF"), 2, 255}, /* 0xFF, UCHAR_MAX */ 464 {D_STR_W_LEN ("ff"), 2, 255}, /* 0xFF, UCHAR_MAX */ 465 {D_STR_W_LEN ("100"), 3, 256}, /* 0x100, UCHAR_MAX+1 */ 466 {D_STR_W_LEN ("7fff"), 4, 32767}, /* 0x7FFF, INT16_MAX */ 467 {D_STR_W_LEN ("7FFF"), 4, 32767}, /* 0x7FFF, INT16_MAX */ 468 {D_STR_W_LEN ("7Fff"), 4, 32767}, /* 0x7FFF, INT16_MAX */ 469 {D_STR_W_LEN ("8000"), 4, 32768}, /* 0x8000, INT16_MAX+1 */ 470 {D_STR_W_LEN ("ffff"), 4, 65535}, /* 0xFFFF, UINT16_MAX */ 471 {D_STR_W_LEN ("FFFF"), 4, 65535}, /* 0xFFFF, UINT16_MAX */ 472 {D_STR_W_LEN ("FffF"), 4, 65535}, /* 0xFFFF, UINT16_MAX */ 473 {D_STR_W_LEN ("10000"), 5, 65536}, /* 0x10000, UINT16_MAX+1 */ 474 {D_STR_W_LEN ("7FFFFFFF"), 8, 2147483647}, /* 0x7FFFFFFF, INT32_MAX */ 475 {D_STR_W_LEN ("7fffffff"), 8, 2147483647}, /* 0x7FFFFFFF, INT32_MAX */ 476 {D_STR_W_LEN ("7FFffFff"), 8, 2147483647}, /* 0x7FFFFFFF, INT32_MAX */ 477 {D_STR_W_LEN ("80000000"), 8, UINT64_C (2147483648)}, /* 0x80000000, INT32_MAX+1 */ 478 {D_STR_W_LEN ("FFFFFFFF"), 8, UINT64_C (4294967295)}, /* 0xFFFFFFFF, UINT32_MAX */ 479 {D_STR_W_LEN ("ffffffff"), 8, UINT64_C (4294967295)}, /* 0xFFFFFFFF, UINT32_MAX */ 480 {D_STR_W_LEN ("FfFfFfFf"), 8, UINT64_C (4294967295)}, /* 0xFFFFFFFF, UINT32_MAX */ 481 {D_STR_W_LEN ("100000000"), 9, UINT64_C (4294967296)}, /* 0x100000000, UINT32_MAX+1 */ 482 {D_STR_W_LEN ("7fffffffffffffff"), 16, UINT64_C (9223372036854775807)}, /* 0x7FFFFFFFFFFFFFFF, INT64_MAX */ 483 {D_STR_W_LEN ("7FFFFFFFFFFFFFFF"), 16, UINT64_C (9223372036854775807)}, /* 0x7FFFFFFFFFFFFFFF, INT64_MAX */ 484 {D_STR_W_LEN ("7FfffFFFFffFFffF"), 16, UINT64_C (9223372036854775807)}, /* 0x7FFFFFFFFFFFFFFF, INT64_MAX */ 485 {D_STR_W_LEN ("8000000000000000"), 16, UINT64_C (9223372036854775808)}, /* 0x8000000000000000, INT64_MAX+1 */ 486 {D_STR_W_LEN ("ffffffffffffffff"), 16, UINT64_C (18446744073709551615)}, /* 0xFFFFFFFFFFFFFFFF, UINT64_MAX */ 487 {D_STR_W_LEN ("FFFFFFFFFFFFFFFF"), 16, UINT64_C (18446744073709551615)}, /* 0xFFFFFFFFFFFFFFFF, UINT64_MAX */ 488 {D_STR_W_LEN ("FffFffFFffFFfFFF"), 16, UINT64_C (18446744073709551615)}, /* 0xFFFFFFFFFFFFFFFF, UINT64_MAX */ 489 490 /* random numbers */ 491 {D_STR_W_LEN ("10186753"), 8, 0x10186753}, 492 {D_STR_W_LEN ("144402566"), 9, 0x144402566}, 493 {D_STR_W_LEN ("151903144"), 9, 0x151903144}, 494 {D_STR_W_LEN ("139264621"), 9, 0x139264621}, 495 {D_STR_W_LEN ("730348"), 6, 0x730348}, 496 {D_STR_W_LEN ("21584377"), 8, 0x21584377}, 497 {D_STR_W_LEN ("709"), 3, 0x709}, 498 {D_STR_W_LEN ("54"), 2, 0x54}, 499 {D_STR_W_LEN ("8452"), 4, 0x8452}, 500 {D_STR_W_LEN ("22353EC6"), 8, 0x22353EC6}, 501 {D_STR_W_LEN ("307F1655"), 8, 0x307F1655}, 502 {D_STR_W_LEN ("1FCB7226"), 8, 0x1FCB7226}, 503 {D_STR_W_LEN ("82480560"), 8, 0x82480560}, 504 {D_STR_W_LEN ("7386D95"), 7, 0x7386D95}, 505 {D_STR_W_LEN ("EC3AB"), 5, 0xEC3AB}, 506 {D_STR_W_LEN ("6DD05"), 5, 0x6DD05}, 507 {D_STR_W_LEN ("C5DF"), 4, 0xC5DF}, 508 {D_STR_W_LEN ("6CE"), 3, 0x6CE}, 509 {D_STR_W_LEN ("CE6"), 3, 0xCE6}, 510 {D_STR_W_LEN ("ce6"), 3, 0xCE6}, 511 {D_STR_W_LEN ("F27"), 3, 0xF27}, 512 {D_STR_W_LEN ("8497D54277D7E1"), 14, UINT64_C (37321639124785121)}, 513 {D_STR_W_LEN ("8497d54277d7e1"), 14, UINT64_C (37321639124785121)}, 514 {D_STR_W_LEN ("8497d54277d7E1"), 14, UINT64_C (37321639124785121)}, 515 {D_STR_W_LEN ("8C8112D0A06"), 11, UINT64_C (9655374645766)}, 516 {D_STR_W_LEN ("8c8112d0a06"), 11, UINT64_C (9655374645766)}, 517 {D_STR_W_LEN ("8c8112d0A06"), 11, UINT64_C (9655374645766)}, 518 {D_STR_W_LEN ("1774509875001362"), 16, UINT64_C (1690064375898968930)}, 519 {D_STR_W_LEN ("0678687876998000"), 16, UINT64_C (466237428027981824)}, 520 521 /* non-digit suffixes */ 522 {D_STR_W_LEN ("1234oa"), 4, 0x1234}, 523 {D_STR_W_LEN ("20h"), 2, 0x20}, 524 {D_STR_W_LEN ("2CH"), 2, 0x2C}, 525 {D_STR_W_LEN ("2ch"), 2, 0x2C}, 526 {D_STR_W_LEN ("0x1F"), 1, 0x0}, /* not to be interpreted as hex prefix! */ 527 {D_STR_W_LEN ("0564`~}"), 4, 0x564}, 528 {D_STR_W_LEN ("0A64`~}"), 4, 0xA64}, 529 {D_STR_W_LEN ("056c`~}"), 4, 0X56C}, 530 {D_STR_W_LEN ("7240146.724"), 7, 0x7240146}, 531 {D_STR_W_LEN ("7E4c1AB.724"), 7, 0X7E4C1AB}, 532 {D_STR_W_LEN ("F24B1B6.724"), 7, 0xF24B1B6}, 533 {D_STR_W_LEN ("2,9"), 1, 0x2}, 534 {D_STR_W_LEN ("a,9"), 1, 0xA}, 535 {D_STR_W_LEN ("200+1"), 3, 0x200}, 536 {D_STR_W_LEN ("2cc+1"), 3, 0x2CC}, 537 {D_STR_W_LEN ("2cC+1"), 3, 0x2CC}, 538 {D_STR_W_LEN ("27416997! And the not too long string."), 8, 0x27416997}, 539 {D_STR_W_LEN ("27555416997! And the not too long string."), 11, 540 0x27555416997}, 541 {D_STR_W_LEN ("416997And the not too long string."), 7, 0x416997A}, 542 {D_STR_W_LEN ("0F4C3Dabstract addition to make string even longer"), 8, 543 0xF4C3DAB} 544 }; 545 546 /* hex strings that should overflow uint64_t */ 547 static const struct str_with_len strx_ovflw[] = { 548 D_STR_W_LEN ("10000000000000000"), /* 0x10000000000000000, UINT64_MAX+1 */ 549 D_STR_W_LEN ("10000000000000001"), 550 D_STR_W_LEN ("10000000000000002"), 551 D_STR_W_LEN ("1000000000000000A"), 552 D_STR_W_LEN ("11000000000000000"), 553 D_STR_W_LEN ("010000000000000000"), /* 0x10000000000000000, UINT64_MAX+1 */ 554 D_STR_W_LEN ("000010000000000000000"), /* 0x10000000000000000, UINT64_MAX+1 */ 555 D_STR_W_LEN ("20000000000000000000"), 556 D_STR_W_LEN ("020000000000000000000"), 557 D_STR_W_LEN ("0020000000000000000000"), 558 D_STR_W_LEN ("20000000000000000"), 559 D_STR_W_LEN ("A0000000000000000"), 560 D_STR_W_LEN ("F0000000000000000"), 561 D_STR_W_LEN ("a0000000000000000"), 562 D_STR_W_LEN ("11111111111111111"), 563 D_STR_W_LEN ("CcCcCCccCCccCCccC"), 564 D_STR_W_LEN ("f0000000000000000"), 565 D_STR_W_LEN ("100000000000000000000"), 566 D_STR_W_LEN ("434532891232591226417"), 567 D_STR_W_LEN ("10000000000000000a"), 568 D_STR_W_LEN ("10000000000000000E"), 569 D_STR_W_LEN ("100000000000000000 and nothing"), /* 0x10000000000000000, UINT64_MAX+1 */ 570 D_STR_W_LEN ("100000000000000000xx"), /* 0x10000000000000000, UINT64_MAX+1 */ 571 D_STR_W_LEN ("99999999999999999999"), 572 D_STR_W_LEN ("18446744073709551616abcd"), 573 D_STR_W_LEN ("20000000000000000000 suffix"), 574 D_STR_W_LEN ("020000000000000000000x") 575 }; 576 577 578 static size_t 579 check_str_to_uint64_valid (void) 580 { 581 size_t t_failed = 0; 582 size_t i, j; 583 int c_failed[sizeof(dstrs_w_values) 584 / sizeof(dstrs_w_values[0])]; 585 static const size_t n_checks = sizeof(c_failed) / sizeof(c_failed[0]); 586 587 memset (c_failed, 0, sizeof(c_failed)); 588 589 for (j = 0; j < locale_name_count; j++) 590 { 591 set_test_locale (j); /* setlocale() can be slow! */ 592 for (i = 0; i < n_checks; i++) 593 { 594 uint_fast64_t rv; 595 size_t rs; 596 const struct str_with_value *const t = dstrs_w_values + i; 597 598 if (c_failed[i]) 599 continue; /* skip already failed checks */ 600 601 if (t->str.len < t->num_of_digt) 602 { 603 fprintf (stderr, 604 "ERROR: dstrs_w_values[%u] has wrong num_of_digt (%u): num_of_digt is expected" 605 " to be less or equal to str.len (%u).\n", 606 (unsigned int) i, (unsigned int) t->num_of_digt, (unsigned 607 int) t->str. 608 len); 609 exit (99); 610 } 611 rv = 9435223; /* some random value */ 612 rs = mhd_str_to_uint64 (t->str.str, &rv); 613 if (rs != t->num_of_digt) 614 { 615 t_failed++; 616 c_failed[i] = ! 0; 617 fprintf (stderr, 618 "FAILED: mhd_str_to_uint64(\"%s\", ->%" PRIuFAST64 619 ") returned %" 620 PRIuPTR 621 ", while expecting %d." 622 " Locale: %s\n", n_prnt (t->str.str), rv, (uintptr_t) rs, 623 (int) t->num_of_digt, get_current_locale_str ()); 624 } 625 if (rv != t->val) 626 { 627 t_failed++; 628 c_failed[i] = ! 0; 629 fprintf (stderr, 630 "FAILED: mhd_str_to_uint64(\"%s\", ->%" PRIuFAST64 631 ") converted string to value %" 632 PRIuFAST64 "," 633 " while expecting result %" PRIuFAST64 ". Locale: %s\n", 634 n_prnt (t->str.str), rv, rv, 635 t->val, get_current_locale_str ()); 636 } 637 if ((verbose > 1) && (j == locale_name_count - 1) && ! c_failed[i]) 638 printf ("PASSED: mhd_str_to_uint64(\"%s\", ->%" PRIuFAST64 ") == %" \ 639 PRIuPTR "\n", 640 n_prnt (t->str.str), rv, rs); 641 } 642 } 643 return t_failed; 644 } 645 646 647 static size_t 648 check_str_to_uint64_all_chars (void) 649 { 650 int c_failed[256]; /* from 0 to 255 */ 651 static const size_t n_checks = sizeof(c_failed) / sizeof(c_failed[0]); 652 size_t t_failed = 0; 653 size_t j; 654 655 memset (c_failed, 0, sizeof(c_failed)); 656 657 for (j = 0; j < locale_name_count; j++) 658 { 659 unsigned int c; 660 uint_fast64_t test_val; 661 662 set_test_locale (j); /* setlocale() can be slow! */ 663 for (c = 0; c < n_checks; c++) 664 { 665 static const uint_fast64_t rnd_val = 24941852; 666 size_t rs; 667 if ((c >= '0') && (c <= '9') ) 668 continue; /* skip digits */ 669 for (test_val = 0; test_val <= rnd_val && ! c_failed[c]; test_val += 670 rnd_val) 671 { 672 char test_str[] = "0123"; 673 uint_fast64_t rv = test_val; 674 675 test_str[0] = (char) (unsigned char) c; /* replace first char with non-digit char */ 676 rs = mhd_str_to_uint64 (test_str, &rv); 677 if (rs != 0) 678 { 679 t_failed++; 680 c_failed[c] = ! 0; 681 fprintf (stderr, 682 "FAILED: mhd_str_to_uint64(\"%s\", ->%" PRIuFAST64 683 ") returned %" PRIuPTR 684 ", while expecting zero." 685 " Locale: %s\n", n_prnt (test_str), rv, (uintptr_t) rs, 686 get_current_locale_str ()); 687 } 688 else if (rv != test_val) 689 { 690 t_failed++; 691 c_failed[c] = ! 0; 692 fprintf (stderr, 693 "FAILED: mhd_str_to_uint64(\"%s\", &ret_val) modified value of ret_val" 694 " (before call: %" PRIuFAST64 ", after call %" PRIuFAST64 695 "). Locale: %s\n", 696 n_prnt (test_str), test_val, rv, get_current_locale_str ()); 697 } 698 } 699 if ((verbose > 1) && (j == locale_name_count - 1) && ! c_failed[c]) 700 { 701 char test_str[] = "0123"; 702 test_str[0] = (char) (unsigned char) c; /* replace first char with non-digit char */ 703 704 printf ("PASSED: mhd_str_to_uint64(\"%s\", &ret_val) == 0, " 705 "value of ret_val is unmodified\n", 706 n_prnt (test_str)); 707 } 708 } 709 } 710 return t_failed; 711 } 712 713 714 static size_t 715 check_str_to_uint64_overflow (void) 716 { 717 size_t t_failed = 0; 718 size_t i, j; 719 int c_failed[sizeof(str_ovflw) / sizeof(str_ovflw[0])]; 720 static const size_t n_checks = sizeof(c_failed) / sizeof(c_failed[0]); 721 722 memset (c_failed, 0, sizeof(c_failed)); 723 724 for (j = 0; j < locale_name_count; j++) 725 { 726 set_test_locale (j); /* setlocale() can be slow! */ 727 for (i = 0; i < n_checks; i++) 728 { 729 size_t rs; 730 const struct str_with_len *const t = str_ovflw + i; 731 static const uint_fast64_t rnd_val = 2; 732 uint_fast64_t test_val; 733 734 for (test_val = 0; test_val <= rnd_val && ! c_failed[i]; test_val += 735 rnd_val) 736 { 737 uint_fast64_t rv = test_val; 738 739 rs = mhd_str_to_uint64 (t->str, &rv); 740 if (rs != 0) 741 { 742 t_failed++; 743 c_failed[i] = ! 0; 744 fprintf (stderr, 745 "FAILED: mhd_str_to_uint64(\"%s\", ->%" PRIuFAST64 746 ") returned %" PRIuPTR 747 ", while expecting zero." 748 " Locale: %s\n", n_prnt (t->str), rv, (uintptr_t) rs, 749 get_current_locale_str ()); 750 } 751 else if (rv != test_val) 752 { 753 t_failed++; 754 c_failed[i] = ! 0; 755 fprintf (stderr, 756 "FAILED: mhd_str_to_uint64(\"%s\", &ret_val) modified value of ret_val" 757 " (before call: %" PRIuFAST64 ", after call %" PRIuFAST64 758 "). Locale: %s\n", 759 n_prnt (t->str), test_val, rv, get_current_locale_str ()); 760 } 761 } 762 if ((verbose > 1) && (j == locale_name_count - 1) && ! c_failed[i]) 763 printf ("PASSED: mhd_str_to_uint64(\"%s\", &ret_val) == 0, " 764 "value of ret_val is unmodified\n", 765 n_prnt (t->str)); 766 } 767 } 768 return t_failed; 769 } 770 771 772 static size_t 773 check_str_to_uint64_no_val (void) 774 { 775 size_t t_failed = 0; 776 size_t i, j; 777 int c_failed[sizeof(str_no_num) / sizeof(str_no_num[0])]; 778 static const size_t n_checks = sizeof(c_failed) / sizeof(c_failed[0]); 779 780 memset (c_failed, 0, sizeof(c_failed)); 781 782 for (j = 0; j < locale_name_count; j++) 783 { 784 set_test_locale (j); /* setlocale() can be slow! */ 785 for (i = 0; i < n_checks; i++) 786 { 787 size_t rs; 788 const struct str_with_len *const t = str_no_num + i; 789 static const uint_fast64_t rnd_val = 74218431; 790 uint_fast64_t test_val; 791 792 for (test_val = 0; test_val <= rnd_val && ! c_failed[i]; test_val += 793 rnd_val) 794 { 795 uint_fast64_t rv = test_val; 796 797 rs = mhd_str_to_uint64 (t->str, &rv); 798 if (rs != 0) 799 { 800 t_failed++; 801 c_failed[i] = ! 0; 802 fprintf (stderr, 803 "FAILED: mhd_str_to_uint64(\"%s\", ->%" PRIuFAST64 804 ") returned %" PRIuPTR 805 ", while expecting zero." 806 " Locale: %s\n", n_prnt (t->str), rv, (uintptr_t) rs, 807 get_current_locale_str ()); 808 } 809 else if (rv != test_val) 810 { 811 t_failed++; 812 c_failed[i] = ! 0; 813 fprintf (stderr, 814 "FAILED: mhd_str_to_uint64(\"%s\", &ret_val) modified value of ret_val" 815 " (before call: %" PRIuFAST64 ", after call %" PRIuFAST64 816 "). Locale: %s\n", 817 n_prnt (t->str), test_val, rv, get_current_locale_str ()); 818 } 819 } 820 if ((verbose > 1) && (j == locale_name_count - 1) && ! c_failed[i]) 821 printf ("PASSED: mhd_str_to_uint64(\"%s\", &ret_val) == 0, " 822 "value of ret_val is unmodified\n", 823 n_prnt (t->str)); 824 } 825 } 826 return t_failed; 827 } 828 829 830 static size_t 831 check_str_to_uint64_n_valid (void) 832 { 833 size_t t_failed = 0; 834 size_t i, j; 835 int c_failed[sizeof(dstrs_w_values) 836 / sizeof(dstrs_w_values[0])]; 837 static const size_t n_checks = sizeof(c_failed) / sizeof(c_failed[0]); 838 839 memset (c_failed, 0, sizeof(c_failed)); 840 841 for (j = 0; j < locale_name_count; j++) 842 { 843 set_test_locale (j); /* setlocale() can be slow! */ 844 for (i = 0; i < n_checks; i++) 845 { 846 uint_fast64_t rv = 1235572; /* some random value */ 847 size_t rs = 0; 848 size_t len; 849 const struct str_with_value *const t = dstrs_w_values + i; 850 851 if (t->str.len < t->num_of_digt) 852 { 853 fprintf (stderr, 854 "ERROR: dstrs_w_values[%u] has wrong num_of_digt (%u): num_of_digt is expected" 855 " to be less or equal to str.len (%u).\n", 856 (unsigned int) i, (unsigned int) t->num_of_digt, (unsigned 857 int) t->str. 858 len); 859 exit (99); 860 } 861 for (len = t->num_of_digt; len <= t->str.len + 1 && ! c_failed[i]; len++) 862 { 863 rs = mhd_str_to_uint64_n (t->str.str, len, &rv); 864 if (rs != t->num_of_digt) 865 { 866 t_failed++; 867 c_failed[i] = ! 0; 868 fprintf (stderr, 869 "FAILED: mhd_str_to_uint64_n(\"%s\", %" PRIuPTR ", ->%" 870 PRIuFAST64 ")" 871 " returned %" PRIuPTR ", while expecting %d. Locale: %s\n", 872 n_prnt (t->str.str), (uintptr_t) len, rv, (uintptr_t) rs, 873 (int) t->num_of_digt, get_current_locale_str ()); 874 } 875 if (rv != t->val) 876 { 877 t_failed++; 878 c_failed[i] = ! 0; 879 fprintf (stderr, 880 "FAILED: mhd_str_to_uint64_n(\"%s\", %" PRIuPTR ", ->%" 881 PRIuFAST64 ")" 882 " converted string to value %" PRIuFAST64 883 ", while expecting result %" PRIuFAST64 884 ". Locale: %s\n", n_prnt (t->str.str), (uintptr_t) len, rv, 885 rv, 886 t->val, get_current_locale_str ()); 887 } 888 } 889 if ((verbose > 1) && (j == locale_name_count - 1) && ! c_failed[i]) 890 printf ("PASSED: mhd_str_to_uint64_n(\"%s\", %" PRIuPTR "..%" 891 PRIuPTR ", ->%" PRIuFAST64 ")" " == %" PRIuPTR "\n", 892 n_prnt (t->str.str), 893 (uintptr_t) t->num_of_digt, 894 (uintptr_t) t->str.len + 1, rv, rs); 895 } 896 } 897 return t_failed; 898 } 899 900 901 static size_t 902 check_str_to_uint64_n_all_chars (void) 903 { 904 int c_failed[256]; /* from 0 to 255 */ 905 static const size_t n_checks = sizeof(c_failed) / sizeof(c_failed[0]); 906 size_t t_failed = 0; 907 size_t j; 908 909 memset (c_failed, 0, sizeof(c_failed)); 910 911 for (j = 0; j < locale_name_count; j++) 912 { 913 unsigned int c; 914 uint_fast64_t test_val; 915 916 set_test_locale (j); /* setlocale() can be slow! */ 917 for (c = 0; c < n_checks; c++) 918 { 919 static const uint_fast64_t rnd_val = 98372558; 920 size_t rs; 921 size_t len; 922 923 if ((c >= '0') && (c <= '9') ) 924 continue; /* skip digits */ 925 926 for (len = 0; len <= 5; len++) 927 { 928 for (test_val = 0; test_val <= rnd_val && ! c_failed[c]; test_val += 929 rnd_val) 930 { 931 char test_str[] = "0123"; 932 uint_fast64_t rv = test_val; 933 934 test_str[0] = (char) (unsigned char) c; /* replace first char with non-digit char */ 935 rs = mhd_str_to_uint64_n (test_str, len, &rv); 936 if (rs != 0) 937 { 938 t_failed++; 939 c_failed[c] = ! 0; 940 fprintf (stderr, 941 "FAILED: mhd_str_to_uint64_n(\"%s\", %" PRIuPTR ", ->%" 942 PRIuFAST64 ")" 943 " returned %" PRIuPTR 944 ", while expecting zero. Locale: %s\n", 945 n_prnt (test_str), (uintptr_t) len, rv, (uintptr_t) rs, 946 get_current_locale_str ()); 947 } 948 else if (rv != test_val) 949 { 950 t_failed++; 951 c_failed[c] = ! 0; 952 fprintf (stderr, 953 "FAILED: mhd_str_to_uint64_n(\"%s\", %" PRIuPTR 954 ", &ret_val)" 955 " modified value of ret_val (before call: %" PRIuFAST64 956 ", after call %" PRIuFAST64 ")." 957 " Locale: %s\n", 958 n_prnt (test_str), (uintptr_t) len, test_val, rv, 959 get_current_locale_str ()); 960 } 961 } 962 } 963 if ((verbose > 1) && (j == locale_name_count - 1) && ! c_failed[c]) 964 { 965 char test_str[] = "0123"; 966 test_str[0] = (char) (unsigned char) c; /* replace first char with non-digit char */ 967 968 printf ("PASSED: mhd_str_to_uint64_n(\"%s\", 0..5, &ret_val) == 0, " 969 "value of ret_val is unmodified\n", 970 n_prnt (test_str)); 971 } 972 } 973 } 974 return t_failed; 975 } 976 977 978 static size_t 979 check_str_to_uint64_n_overflow (void) 980 { 981 size_t t_failed = 0; 982 size_t i, j; 983 int c_failed[sizeof(str_ovflw) / sizeof(str_ovflw[0])]; 984 static const size_t n_checks = sizeof(c_failed) / sizeof(c_failed[0]); 985 986 memset (c_failed, 0, sizeof(c_failed)); 987 988 for (j = 0; j < locale_name_count; j++) 989 { 990 set_test_locale (j); /* setlocale() can be slow! */ 991 for (i = 0; i < n_checks; i++) 992 { 993 size_t rs; 994 const struct str_with_len *const t = str_ovflw + i; 995 static const uint_fast64_t rnd_val = 3; 996 size_t len; 997 998 for (len = t->len; len <= t->len + 1; len++) 999 { 1000 uint_fast64_t test_val; 1001 for (test_val = 0; test_val <= rnd_val && ! c_failed[i]; test_val += 1002 rnd_val) 1003 { 1004 uint_fast64_t rv = test_val; 1005 1006 rs = mhd_str_to_uint64_n (t->str, len, &rv); 1007 if (rs != 0) 1008 { 1009 t_failed++; 1010 c_failed[i] = ! 0; 1011 fprintf (stderr, 1012 "FAILED: mhd_str_to_uint64_n(\"%s\", %" PRIuPTR ", ->%" 1013 PRIuFAST64 ")" 1014 " returned %" PRIuPTR 1015 ", while expecting zero. Locale: %s\n", 1016 n_prnt (t->str), (uintptr_t) len, rv, (uintptr_t) rs, 1017 get_current_locale_str ()); 1018 } 1019 else if (rv != test_val) 1020 { 1021 t_failed++; 1022 c_failed[i] = ! 0; 1023 fprintf (stderr, 1024 "FAILED: mhd_str_to_uint64_n(\"%s\", %" PRIuPTR 1025 ", &ret_val)" 1026 " modified value of ret_val (before call: %" PRIuFAST64 1027 ", after call %" PRIuFAST64 ")." 1028 " Locale: %s\n", n_prnt (t->str), (uintptr_t) len, 1029 test_val, rv, 1030 get_current_locale_str ()); 1031 } 1032 } 1033 } 1034 if ((verbose > 1) && (j == locale_name_count - 1) && ! c_failed[i]) 1035 printf ("PASSED: mhd_str_to_uint64_n(\"%s\", %" PRIuPTR "..%" PRIuPTR 1036 ", &ret_val) == 0," 1037 " value of ret_val is unmodified\n", n_prnt (t->str), 1038 (uintptr_t) t->len, 1039 (uintptr_t) t->len + 1); 1040 } 1041 } 1042 return t_failed; 1043 } 1044 1045 1046 static size_t 1047 check_str_to_uint64_n_no_val (void) 1048 { 1049 size_t t_failed = 0; 1050 size_t i, j; 1051 int c_failed[sizeof(str_no_num) / sizeof(str_no_num[0])]; 1052 static const size_t n_checks = sizeof(c_failed) / sizeof(c_failed[0]); 1053 1054 memset (c_failed, 0, sizeof(c_failed)); 1055 1056 for (j = 0; j < locale_name_count; j++) 1057 { 1058 set_test_locale (j); /* setlocale() can be slow! */ 1059 for (i = 0; i < n_checks; i++) 1060 { 1061 size_t rs; 1062 const struct str_with_len *const t = str_no_num + i; 1063 static const uint_fast64_t rnd_val = 43255654342; 1064 size_t len; 1065 1066 for (len = 0; len <= t->len + 1; len++) 1067 { 1068 uint_fast64_t test_val; 1069 for (test_val = 0; test_val <= rnd_val && ! c_failed[i]; test_val += 1070 rnd_val) 1071 { 1072 uint_fast64_t rv = test_val; 1073 1074 rs = mhd_str_to_uint64_n (t->str, len, &rv); 1075 if (rs != 0) 1076 { 1077 t_failed++; 1078 c_failed[i] = ! 0; 1079 fprintf (stderr, 1080 "FAILED: mhd_str_to_uint64_n(\"%s\", %" PRIuPTR ", ->%" 1081 PRIuFAST64 ")" 1082 " returned %" PRIuPTR 1083 ", while expecting zero. Locale: %s\n", 1084 n_prnt (t->str), (uintptr_t) len, rv, (uintptr_t) rs, 1085 get_current_locale_str ()); 1086 } 1087 else if (rv != test_val) 1088 { 1089 t_failed++; 1090 c_failed[i] = ! 0; 1091 fprintf (stderr, 1092 "FAILED: mhd_str_to_uint64_n(\"%s\", %" PRIuPTR 1093 ", &ret_val)" 1094 " modified value of ret_val (before call: %" PRIuFAST64 1095 ", after call %" PRIuFAST64 ")." 1096 " Locale: %s\n", n_prnt (t->str), (uintptr_t) len, 1097 test_val, rv, 1098 get_current_locale_str ()); 1099 } 1100 } 1101 } 1102 if ((verbose > 1) && (j == locale_name_count - 1) && ! c_failed[i]) 1103 printf ("PASSED: mhd_str_to_uint64_n(\"%s\", 0..%" PRIuPTR 1104 ", &ret_val) == 0," 1105 " value of ret_val is unmodified\n", n_prnt (t->str), 1106 (uintptr_t) t->len + 1); 1107 } 1108 } 1109 return t_failed; 1110 } 1111 1112 1113 static size_t 1114 check_strx_to_uint32_valid (void) 1115 { 1116 size_t t_failed = 0; 1117 size_t i, j; 1118 int c_failed[sizeof(xdstrs_w_values) 1119 / sizeof(xdstrs_w_values[0])]; 1120 static const size_t n_checks = sizeof(c_failed) / sizeof(c_failed[0]); 1121 1122 memset (c_failed, 0, sizeof(c_failed)); 1123 1124 for (j = 0; j < locale_name_count; j++) 1125 { 1126 set_test_locale (j); /* setlocale() can be slow! */ 1127 for (i = 0; i < n_checks; i++) 1128 { 1129 uint_fast32_t rv; 1130 size_t rs; 1131 const struct str_with_value *const t = xdstrs_w_values + i; 1132 1133 if (t->val != (uint_fast32_t) t->val) 1134 continue; /* number is too high for this function */ 1135 1136 if (c_failed[i]) 1137 continue; /* skip already failed checks */ 1138 1139 if (t->str.len < t->num_of_digt) 1140 { 1141 fprintf (stderr, 1142 "ERROR: xdstrs_w_values[%u] has wrong num_of_digt (%u): num_of_digt is expected" 1143 " to be less or equal to str.len (%u).\n", 1144 (unsigned int) i, (unsigned int) t->num_of_digt, (unsigned 1145 int) t->str. 1146 len); 1147 exit (99); 1148 } 1149 rv = 1458532; /* some random value */ 1150 rs = mhd_strx_to_uint32 (t->str.str, &rv); 1151 if (rs != t->num_of_digt) 1152 { 1153 t_failed++; 1154 c_failed[i] = ! 0; 1155 fprintf (stderr, 1156 "FAILED: mhd_strx_to_uint32(\"%s\", ->0x%" PRIXFAST64 1157 ") returned %" 1158 PRIuPTR " digits, while expecting %d." 1159 " Locale: %s\n", 1160 n_prnt (t->str.str), (uint_fast64_t) rv, 1161 (uintptr_t) rs, 1162 (int) t->num_of_digt, 1163 get_current_locale_str ()); 1164 } 1165 if (rv != t->val) 1166 { 1167 t_failed++; 1168 c_failed[i] = ! 0; 1169 fprintf (stderr, 1170 "FAILED: mhd_strx_to_uint32(\"%s\", ->0x%" PRIXFAST64 1171 ") converted string to value 0x%" 1172 PRIXFAST64 "," 1173 " while expecting result 0x%" PRIXFAST64 ". Locale: %s\n", 1174 n_prnt (t->str.str), (uint_fast64_t) rv, (uint_fast64_t) rv, 1175 t->val, get_current_locale_str ()); 1176 } 1177 if ((verbose > 1) && (j == locale_name_count - 1) && ! c_failed[i]) 1178 printf ("PASSED: mhd_strx_to_uint32(\"%s\", ->0x%" PRIXFAST64 ") == %" 1179 PRIuPTR "\n", 1180 n_prnt (t->str.str), (uint_fast64_t) rv, rs); 1181 } 1182 } 1183 return t_failed; 1184 } 1185 1186 1187 static size_t 1188 check_strx_to_uint32_all_chars (void) 1189 { 1190 int c_failed[256]; /* from 0 to 255 */ 1191 static const size_t n_checks = sizeof(c_failed) / sizeof(c_failed[0]); 1192 size_t t_failed = 0; 1193 size_t j; 1194 1195 memset (c_failed, 0, sizeof(c_failed)); 1196 1197 for (j = 0; j < locale_name_count; j++) 1198 { 1199 unsigned int c; 1200 uint_fast32_t test_val; 1201 1202 set_test_locale (j); /* setlocale() can be slow! */ 1203 for (c = 0; c < n_checks; c++) 1204 { 1205 static const uint_fast32_t rnd_val = 234234; 1206 size_t rs; 1207 if (( (c >= '0') && (c <= '9') ) 1208 || ( (c >= 'A') && (c <= 'F') ) 1209 || ( (c >= 'a') && (c <= 'f') )) 1210 continue; /* skip xdigits */ 1211 for (test_val = 0; test_val <= rnd_val && ! c_failed[c]; test_val += 1212 rnd_val) 1213 { 1214 char test_str[] = "0123"; 1215 uint_fast32_t rv = test_val; 1216 1217 test_str[0] = (char) (unsigned char) c; /* replace first char with non-digit char */ 1218 rs = mhd_strx_to_uint32 (test_str, &rv); 1219 if (rs != 0) 1220 { 1221 t_failed++; 1222 c_failed[c] = ! 0; 1223 fprintf (stderr, 1224 "FAILED: mhd_strx_to_uint32(\"%s\", ->0x%" PRIXFAST64 1225 ") returned %" 1226 PRIuPTR " digits, while expecting zero." 1227 " Locale: %s\n", n_prnt (test_str), (uint_fast64_t) rv, 1228 (uintptr_t) rs, get_current_locale_str ()); 1229 } 1230 else if (rv != test_val) 1231 { 1232 t_failed++; 1233 c_failed[c] = ! 0; 1234 fprintf (stderr, 1235 "FAILED: mhd_strx_to_uint32(\"%s\", &ret_val) modified value of ret_val" 1236 " (before call: 0x%" PRIXFAST64 ", after call 0x%" PRIXFAST64 1237 "). Locale: %s\n", 1238 n_prnt (test_str), (uint_fast64_t) test_val, 1239 (uint_fast64_t) rv, 1240 get_current_locale_str ()); 1241 } 1242 } 1243 if ((verbose > 1) && (j == locale_name_count - 1) && ! c_failed[c]) 1244 { 1245 char test_str[] = "0123"; 1246 test_str[0] = (char) (unsigned char) c; /* replace first char with non-digit char */ 1247 1248 printf ("PASSED: mhd_strx_to_uint32(\"%s\", &ret_val) == 0, " 1249 "value of ret_val is unmodified\n", 1250 n_prnt (test_str)); 1251 } 1252 } 1253 } 1254 return t_failed; 1255 } 1256 1257 1258 static size_t 1259 check_strx_to_uint32_overflow (void) 1260 { 1261 size_t t_failed = 0; 1262 size_t i, j; 1263 static const size_t n_checks1 = sizeof(strx_ovflw) / sizeof(strx_ovflw[0]); 1264 int c_failed[(sizeof(strx_ovflw) / sizeof(strx_ovflw[0])) 1265 + (sizeof(xdstrs_w_values) 1266 / sizeof(xdstrs_w_values[0]))]; 1267 static const size_t n_checks = sizeof(c_failed) / sizeof(c_failed[0]); 1268 1269 memset (c_failed, 0, sizeof(c_failed)); 1270 1271 for (j = 0; j < locale_name_count; j++) 1272 { 1273 set_test_locale (j); /* setlocale() can be slow! */ 1274 for (i = 0; i < n_checks; i++) 1275 { 1276 size_t rs; 1277 static const uint_fast32_t rnd_val = 74218431; 1278 uint_fast32_t test_val; 1279 const char *str; 1280 if (i < n_checks1) 1281 { 1282 const struct str_with_len *const t = strx_ovflw + i; 1283 str = t->str; 1284 } 1285 else 1286 { 1287 const struct str_with_value *const t = xdstrs_w_values + (i 1288 - n_checks1); 1289 if (t->val == (uint_fast32_t) t->val) 1290 continue; /* check only strings that should overflow uint_fast32_t */ 1291 str = t->str.str; 1292 } 1293 1294 1295 for (test_val = 0; test_val <= rnd_val && ! c_failed[i]; test_val += 1296 rnd_val) 1297 { 1298 uint_fast32_t rv = test_val; 1299 1300 rs = mhd_strx_to_uint32 (str, &rv); 1301 if (rs != 0) 1302 { 1303 t_failed++; 1304 c_failed[i] = ! 0; 1305 fprintf (stderr, 1306 "FAILED: mhd_strx_to_uint32(\"%s\", ->0x%" PRIXFAST64 1307 ") returned %" 1308 PRIuPTR ", while expecting zero." 1309 " Locale: %s\n", n_prnt (str), (uint_fast64_t) rv, 1310 (uintptr_t) rs, 1311 get_current_locale_str ()); 1312 } 1313 else if (rv != test_val) 1314 { 1315 t_failed++; 1316 c_failed[i] = ! 0; 1317 fprintf (stderr, 1318 "FAILED: mhd_strx_to_uint32(\"%s\", &ret_val) modified value of ret_val" 1319 " (before call: 0x%" PRIXFAST64 ", after call 0x%" PRIXFAST64 1320 "). Locale: %s\n", 1321 n_prnt (str), (uint_fast64_t) test_val, (uint_fast64_t) rv, 1322 get_current_locale_str ()); 1323 } 1324 } 1325 if ((verbose > 1) && (j == locale_name_count - 1) && ! c_failed[i]) 1326 printf ("PASSED: mhd_strx_to_uint32(\"%s\", &ret_val) == 0, " 1327 "value of ret_val is unmodified\n", 1328 n_prnt (str)); 1329 } 1330 } 1331 return t_failed; 1332 } 1333 1334 1335 static size_t 1336 check_strx_to_uint32_no_val (void) 1337 { 1338 size_t t_failed = 0; 1339 size_t i, j; 1340 int c_failed[sizeof(str_no_num) / sizeof(str_no_num[0])]; 1341 static const size_t n_checks = sizeof(c_failed) / sizeof(c_failed[0]); 1342 1343 memset (c_failed, 0, sizeof(c_failed)); 1344 1345 for (j = 0; j < locale_name_count; j++) 1346 { 1347 set_test_locale (j); /* setlocale() can be slow! */ 1348 for (i = 0; i < n_checks; i++) 1349 { 1350 size_t rs; 1351 const struct str_with_len *const t = str_no_num + i; 1352 static const uint_fast32_t rnd_val = 74218431; 1353 uint_fast32_t test_val; 1354 1355 for (test_val = 0; test_val <= rnd_val && ! c_failed[i]; test_val += 1356 rnd_val) 1357 { 1358 uint_fast32_t rv = test_val; 1359 1360 rs = mhd_strx_to_uint32 (t->str, &rv); 1361 if (rs != 0) 1362 { 1363 t_failed++; 1364 c_failed[i] = ! 0; 1365 fprintf (stderr, 1366 "FAILED: mhd_strx_to_uint32(\"%s\", ->0x%" PRIXFAST64 1367 ") returned %" 1368 PRIuPTR ", while expecting zero." 1369 " Locale: %s\n", n_prnt (t->str), (uint_fast64_t) rv, 1370 (uintptr_t) rs, get_current_locale_str ()); 1371 } 1372 else if (rv != test_val) 1373 { 1374 t_failed++; 1375 c_failed[i] = ! 0; 1376 fprintf (stderr, 1377 "FAILED: mhd_strx_to_uint32(\"%s\", &ret_val) modified value of ret_val" 1378 " (before call: 0x%" PRIXFAST64 ", after call 0x%" PRIXFAST64 1379 "). Locale: %s\n", 1380 n_prnt (t->str), (uint_fast64_t) test_val, 1381 (uint_fast64_t) rv, 1382 get_current_locale_str ()); 1383 } 1384 } 1385 if ((verbose > 1) && (j == locale_name_count - 1) && ! c_failed[i]) 1386 printf ("PASSED: mhd_strx_to_uint32(\"%s\", &ret_val) == 0, " 1387 "value of ret_val is unmodified\n", 1388 n_prnt (t->str)); 1389 } 1390 } 1391 return t_failed; 1392 } 1393 1394 1395 static size_t 1396 check_strx_to_uint32_n_valid (void) 1397 { 1398 size_t t_failed = 0; 1399 size_t i, j; 1400 int c_failed[sizeof(xdstrs_w_values) 1401 / sizeof(xdstrs_w_values[0])]; 1402 static const size_t n_checks = sizeof(c_failed) / sizeof(c_failed[0]); 1403 1404 memset (c_failed, 0, sizeof(c_failed)); 1405 1406 for (j = 0; j < locale_name_count; j++) 1407 { 1408 set_test_locale (j); /* setlocale() can be slow! */ 1409 for (i = 0; i < n_checks; i++) 1410 { 1411 uint_fast32_t rv = 2352932; /* some random value */ 1412 size_t rs = 0; 1413 size_t len; 1414 const struct str_with_value *const t = xdstrs_w_values + i; 1415 1416 if (t->val != (uint_fast32_t) t->val) 1417 continue; /* number is too high for this function */ 1418 1419 if (t->str.len < t->num_of_digt) 1420 { 1421 fprintf (stderr, 1422 "ERROR: xdstrs_w_values[%u] has wrong num_of_digt (%u): num_of_digt is expected" 1423 " to be less or equal to str.len (%u).\n", 1424 (unsigned int) i, (unsigned int) t->num_of_digt, (unsigned 1425 int) t->str. 1426 len); 1427 exit (99); 1428 } 1429 for (len = t->num_of_digt; len <= t->str.len + 1 && ! c_failed[i]; len++) 1430 { 1431 rs = mhd_strx_to_uint32_n (t->str.str, len, &rv); 1432 if (rs != t->num_of_digt) 1433 { 1434 t_failed++; 1435 c_failed[i] = ! 0; 1436 fprintf (stderr, 1437 "FAILED: mhd_strx_to_uint32_n(\"%s\", %" PRIuPTR ", ->0x%" 1438 PRIXFAST64 ")" 1439 " returned %" PRIuPTR ", while expecting %d. Locale: %s\n", 1440 n_prnt (t->str.str), (uintptr_t) len, (uint_fast64_t) rv, 1441 (uintptr_t) rs, 1442 (int) t->num_of_digt, get_current_locale_str ()); 1443 } 1444 if (rv != t->val) 1445 { 1446 t_failed++; 1447 c_failed[i] = ! 0; 1448 fprintf (stderr, 1449 "FAILED: mhd_strx_to_uint32_n(\"%s\", %" PRIuPTR ", ->0x%" 1450 PRIXFAST64 ")" 1451 " converted string to value 0x%" PRIXFAST64 1452 ", while expecting result 0x%" PRIXFAST64 1453 ". Locale: %s\n", n_prnt (t->str.str), (uintptr_t) len, 1454 (uint_fast64_t) rv, (uint_fast64_t) rv, 1455 t->val, get_current_locale_str ()); 1456 } 1457 } 1458 if ((verbose > 1) && (j == locale_name_count - 1) && ! c_failed[i]) 1459 printf ( 1460 "PASSED: mhd_strx_to_uint32_n(\"%s\", %" PRIuPTR "..%" PRIuPTR 1461 ", ->0x%" 1462 PRIXFAST64 ")" 1463 " == %" PRIuPTR "\n", n_prnt (t->str.str), 1464 (uintptr_t) t->num_of_digt, 1465 (uintptr_t) t->str.len + 1, (uint_fast64_t) rv, rs); 1466 } 1467 } 1468 return t_failed; 1469 } 1470 1471 1472 static size_t 1473 check_strx_to_uint32_n_all_chars (void) 1474 { 1475 int c_failed[256]; /* from 0 to 255 */ 1476 static const size_t n_checks = sizeof(c_failed) / sizeof(c_failed[0]); 1477 size_t t_failed = 0; 1478 size_t j; 1479 1480 memset (c_failed, 0, sizeof(c_failed)); 1481 1482 for (j = 0; j < locale_name_count; j++) 1483 { 1484 unsigned int c; 1485 uint_fast32_t test_val; 1486 1487 set_test_locale (j); /* setlocale() can be slow! */ 1488 for (c = 0; c < n_checks; c++) 1489 { 1490 static const uint_fast32_t rnd_val = 98372558; 1491 size_t rs; 1492 size_t len; 1493 1494 if (( (c >= '0') && (c <= '9') ) 1495 || ( (c >= 'A') && (c <= 'F') ) 1496 || ( (c >= 'a') && (c <= 'f') )) 1497 continue; /* skip xdigits */ 1498 1499 for (len = 0; len <= 5; len++) 1500 { 1501 for (test_val = 0; test_val <= rnd_val && ! c_failed[c]; test_val += 1502 rnd_val) 1503 { 1504 char test_str[] = "0123"; 1505 uint_fast32_t rv = test_val; 1506 1507 test_str[0] = (char) (unsigned char) c; /* replace first char with non-digit char */ 1508 rs = mhd_strx_to_uint32_n (test_str, len, &rv); 1509 if (rs != 0) 1510 { 1511 t_failed++; 1512 c_failed[c] = ! 0; 1513 fprintf (stderr, 1514 "FAILED: mhd_strx_to_uint32_n(\"%s\", %" PRIuPTR ", ->0x%" 1515 PRIXFAST64 1516 ")" 1517 " returned %" PRIuPTR 1518 ", while expecting zero. Locale: %s\n", 1519 n_prnt (test_str), (uintptr_t) len, (uint_fast64_t) rv, 1520 (uintptr_t) rs, get_current_locale_str ()); 1521 } 1522 else if (rv != test_val) 1523 { 1524 t_failed++; 1525 c_failed[c] = ! 0; 1526 fprintf (stderr, 1527 "FAILED: mhd_strx_to_uint32_n(\"%s\", %" PRIuPTR 1528 ", &ret_val)" 1529 " modified value of ret_val (before call: 0x%" PRIXFAST64 1530 ", after call 0x%" PRIXFAST64 ")." 1531 " Locale: %s\n", 1532 n_prnt (test_str), (uintptr_t) len, 1533 (uint_fast64_t) test_val, 1534 (uint_fast64_t) rv, get_current_locale_str ()); 1535 } 1536 } 1537 } 1538 if ((verbose > 1) && (j == locale_name_count - 1) && ! c_failed[c]) 1539 { 1540 char test_str[] = "0123"; 1541 test_str[0] = (char) (unsigned char) c; /* replace first char with non-digit char */ 1542 1543 printf ("PASSED: mhd_strx_to_uint32_n(\"%s\", 0..5, &ret_val) == 0, " 1544 "value of ret_val is unmodified\n", 1545 n_prnt (test_str)); 1546 } 1547 } 1548 } 1549 return t_failed; 1550 } 1551 1552 1553 static size_t 1554 check_strx_to_uint32_n_overflow (void) 1555 { 1556 size_t t_failed = 0; 1557 size_t i, j; 1558 static const size_t n_checks1 = sizeof(strx_ovflw) / sizeof(strx_ovflw[0]); 1559 int c_failed[(sizeof(strx_ovflw) / sizeof(strx_ovflw[0])) 1560 + (sizeof(xdstrs_w_values) 1561 / sizeof(xdstrs_w_values[0]))]; 1562 static const size_t n_checks = sizeof(c_failed) / sizeof(c_failed[0]); 1563 1564 memset (c_failed, 0, sizeof(c_failed)); 1565 1566 for (j = 0; j < locale_name_count; j++) 1567 { 1568 set_test_locale (j); /* setlocale() can be slow! */ 1569 for (i = 0; i < n_checks; i++) 1570 { 1571 size_t rs; 1572 static const uint_fast32_t rnd_val = 4; 1573 size_t len; 1574 const char *str; 1575 size_t min_len, max_len; 1576 if (i < n_checks1) 1577 { 1578 const struct str_with_len *const t = strx_ovflw + i; 1579 str = t->str; 1580 min_len = t->len; 1581 max_len = t->len + 1; 1582 } 1583 else 1584 { 1585 const struct str_with_value *const t = xdstrs_w_values + (i 1586 - n_checks1); 1587 if (t->val == (uint_fast32_t) t->val) 1588 continue; /* check only strings that should overflow uint_fast32_t */ 1589 1590 if (t->str.len < t->num_of_digt) 1591 { 1592 fprintf (stderr, 1593 "ERROR: xdstrs_w_values[%u] has wrong num_of_digt (%u): num_of_digt is expected" 1594 " to be less or equal to str.len (%u).\n", 1595 (unsigned int) (i - n_checks1), (unsigned 1596 int) t->num_of_digt, 1597 (unsigned int) t->str.len); 1598 exit (99); 1599 } 1600 str = t->str.str; 1601 min_len = t->num_of_digt; 1602 max_len = t->str.len + 1; 1603 } 1604 1605 for (len = min_len; len <= max_len; len++) 1606 { 1607 uint_fast32_t test_val; 1608 for (test_val = 0; test_val <= rnd_val && ! c_failed[i]; test_val += 1609 rnd_val) 1610 { 1611 uint_fast32_t rv = test_val; 1612 1613 rs = mhd_strx_to_uint32_n (str, len, &rv); 1614 if (rs != 0) 1615 { 1616 t_failed++; 1617 c_failed[i] = ! 0; 1618 fprintf (stderr, 1619 "FAILED: mhd_strx_to_uint32_n(\"%s\", %" PRIuPTR ", ->0x%" 1620 PRIXFAST64 1621 ")" 1622 " returned %" PRIuPTR 1623 ", while expecting zero. Locale: %s\n", 1624 n_prnt (str), (uintptr_t) len, (uint_fast64_t) rv, 1625 (uintptr_t) rs, get_current_locale_str ()); 1626 } 1627 else if (rv != test_val) 1628 { 1629 t_failed++; 1630 c_failed[i] = ! 0; 1631 fprintf (stderr, 1632 "FAILED: mhd_strx_to_uint32_n(\"%s\", %" PRIuPTR 1633 ", &ret_val)" 1634 " modified value of ret_val (before call: 0x%" PRIXFAST64 1635 ", after call 0x%" PRIXFAST64 ")." 1636 " Locale: %s\n", n_prnt (str), (uintptr_t) len, 1637 (uint_fast64_t) test_val, (uint_fast64_t) rv, 1638 get_current_locale_str ()); 1639 } 1640 } 1641 } 1642 if ((verbose > 1) && (j == locale_name_count - 1) && ! c_failed[i]) 1643 printf ("PASSED: mhd_strx_to_uint32_n(\"%s\", %" PRIuPTR "..%" PRIuPTR 1644 ", &ret_val) == 0," 1645 " value of ret_val is unmodified\n", n_prnt (str), 1646 (uintptr_t) min_len, 1647 (uintptr_t) max_len); 1648 } 1649 } 1650 return t_failed; 1651 } 1652 1653 1654 static size_t 1655 check_strx_to_uint32_n_no_val (void) 1656 { 1657 size_t t_failed = 0; 1658 size_t i, j; 1659 int c_failed[sizeof(str_no_num) / sizeof(str_no_num[0])]; 1660 static const size_t n_checks = sizeof(c_failed) / sizeof(c_failed[0]); 1661 1662 memset (c_failed, 0, sizeof(c_failed)); 1663 1664 for (j = 0; j < locale_name_count; j++) 1665 { 1666 set_test_locale (j); /* setlocale() can be slow! */ 1667 for (i = 0; i < n_checks; i++) 1668 { 1669 size_t rs; 1670 const struct str_with_len *const t = str_no_num + i; 1671 static const uint_fast32_t rnd_val = 3214314212UL; 1672 size_t len; 1673 1674 for (len = 0; len <= t->len + 1; len++) 1675 { 1676 uint_fast32_t test_val; 1677 for (test_val = 0; test_val <= rnd_val && ! c_failed[i]; test_val += 1678 rnd_val) 1679 { 1680 uint_fast32_t rv = test_val; 1681 1682 rs = mhd_strx_to_uint32_n (t->str, len, &rv); 1683 if (rs != 0) 1684 { 1685 t_failed++; 1686 c_failed[i] = ! 0; 1687 fprintf (stderr, 1688 "FAILED: mhd_strx_to_uint32_n(\"%s\", %" PRIuPTR ", ->0x%" 1689 PRIXFAST64 1690 ")" 1691 " returned %" PRIuPTR 1692 ", while expecting zero. Locale: %s\n", 1693 n_prnt (t->str), (uintptr_t) len, (uint_fast64_t) rv, 1694 (uintptr_t) rs, get_current_locale_str ()); 1695 } 1696 else if (rv != test_val) 1697 { 1698 t_failed++; 1699 c_failed[i] = ! 0; 1700 fprintf (stderr, 1701 "FAILED: mhd_strx_to_uint32_n(\"%s\", %" PRIuPTR 1702 ", &ret_val)" 1703 " modified value of ret_val (before call: 0x%" PRIXFAST64 1704 ", after call 0x%" PRIXFAST64 ")." 1705 " Locale: %s\n", n_prnt (t->str), (uintptr_t) len, 1706 (uint_fast64_t) test_val, (uint_fast64_t) rv, 1707 get_current_locale_str ()); 1708 } 1709 } 1710 } 1711 if ((verbose > 1) && (j == locale_name_count - 1) && ! c_failed[i]) 1712 printf ("PASSED: mhd_strx_to_uint32_n(\"%s\", 0..%" PRIuPTR 1713 ", &ret_val) == 0," 1714 " value of ret_val is unmodified\n", n_prnt (t->str), 1715 (uintptr_t) t->len + 1); 1716 } 1717 } 1718 return t_failed; 1719 } 1720 1721 1722 static size_t 1723 check_strx_to_uint64_valid (void) 1724 { 1725 size_t t_failed = 0; 1726 size_t i, j; 1727 int c_failed[sizeof(xdstrs_w_values) 1728 / sizeof(xdstrs_w_values[0])]; 1729 static const size_t n_checks = sizeof(c_failed) / sizeof(c_failed[0]); 1730 1731 memset (c_failed, 0, sizeof(c_failed)); 1732 1733 for (j = 0; j < locale_name_count; j++) 1734 { 1735 set_test_locale (j); /* setlocale() can be slow! */ 1736 for (i = 0; i < n_checks; i++) 1737 { 1738 uint_fast64_t rv; 1739 size_t rs; 1740 const struct str_with_value *const t = xdstrs_w_values + i; 1741 1742 if (c_failed[i]) 1743 continue; /* skip already failed checks */ 1744 1745 if (t->str.len < t->num_of_digt) 1746 { 1747 fprintf (stderr, 1748 "ERROR: xdstrs_w_values[%u] has wrong num_of_digt (%u): num_of_digt is expected" 1749 " to be less or equal to str.len (%u).\n", 1750 (unsigned int) i, (unsigned int) t->num_of_digt, (unsigned 1751 int) t->str. 1752 len); 1753 exit (99); 1754 } 1755 rv = 1458532; /* some random value */ 1756 rs = mhd_strx_to_uint64 (t->str.str, &rv); 1757 if (rs != t->num_of_digt) 1758 { 1759 t_failed++; 1760 c_failed[i] = ! 0; 1761 fprintf (stderr, 1762 "FAILED: mhd_strx_to_uint64(\"%s\", ->0x%" PRIXFAST64 1763 ") returned %" 1764 PRIuPTR ", while expecting %d." 1765 " Locale: %s\n", n_prnt (t->str.str), rv, (uintptr_t) rs, 1766 (int) t->num_of_digt, get_current_locale_str ()); 1767 } 1768 if (rv != t->val) 1769 { 1770 t_failed++; 1771 c_failed[i] = ! 0; 1772 fprintf (stderr, 1773 "FAILED: mhd_strx_to_uint64(\"%s\", ->0x%" PRIXFAST64 1774 ") converted string to value 0x%" 1775 PRIXFAST64 "," 1776 " while expecting result 0x%" PRIXFAST64 ". Locale: %s\n", 1777 n_prnt (t->str.str), rv, rv, 1778 t->val, get_current_locale_str ()); 1779 } 1780 if ((verbose > 1) && (j == locale_name_count - 1) && ! c_failed[i]) 1781 printf ("PASSED: mhd_strx_to_uint64(\"%s\", ->0x%" PRIXFAST64 ") == %" 1782 PRIuPTR "\n", 1783 n_prnt (t->str.str), rv, rs); 1784 } 1785 } 1786 return t_failed; 1787 } 1788 1789 1790 static size_t 1791 check_strx_to_uint64_all_chars (void) 1792 { 1793 int c_failed[256]; /* from 0 to 255 */ 1794 static const size_t n_checks = sizeof(c_failed) / sizeof(c_failed[0]); 1795 size_t t_failed = 0; 1796 size_t j; 1797 1798 memset (c_failed, 0, sizeof(c_failed)); 1799 1800 for (j = 0; j < locale_name_count; j++) 1801 { 1802 unsigned int c; 1803 uint_fast64_t test_val; 1804 1805 set_test_locale (j); /* setlocale() can be slow! */ 1806 for (c = 0; c < n_checks; c++) 1807 { 1808 static const uint_fast64_t rnd_val = 234234; 1809 size_t rs; 1810 if (( (c >= '0') && (c <= '9') ) 1811 || ( (c >= 'A') && (c <= 'F') ) 1812 || ( (c >= 'a') && (c <= 'f') )) 1813 continue; /* skip xdigits */ 1814 for (test_val = 0; test_val <= rnd_val && ! c_failed[c]; test_val += 1815 rnd_val) 1816 { 1817 char test_str[] = "0123"; 1818 uint_fast64_t rv = test_val; 1819 1820 test_str[0] = (char) (unsigned char) c; /* replace first char with non-digit char */ 1821 rs = mhd_strx_to_uint64 (test_str, &rv); 1822 if (rs != 0) 1823 { 1824 t_failed++; 1825 c_failed[c] = ! 0; 1826 fprintf (stderr, 1827 "FAILED: mhd_strx_to_uint64(\"%s\", ->0x%" PRIXFAST64 1828 ") returned %" 1829 PRIuPTR ", while expecting zero." 1830 " Locale: %s\n", n_prnt (test_str), rv, (uintptr_t) rs, 1831 get_current_locale_str ()); 1832 } 1833 else if (rv != test_val) 1834 { 1835 t_failed++; 1836 c_failed[c] = ! 0; 1837 fprintf (stderr, 1838 "FAILED: mhd_strx_to_uint64(\"%s\", &ret_val) modified value of ret_val" 1839 " (before call: 0x%" PRIXFAST64 ", after call 0x%" PRIXFAST64 1840 "). Locale: %s\n", 1841 n_prnt (test_str), test_val, rv, get_current_locale_str ()); 1842 } 1843 } 1844 if ((verbose > 1) && (j == locale_name_count - 1) && ! c_failed[c]) 1845 { 1846 char test_str[] = "0123"; 1847 test_str[0] = (char) (unsigned char) c; /* replace first char with non-digit char */ 1848 1849 printf ("PASSED: mhd_strx_to_uint64(\"%s\", &ret_val) == 0, " 1850 "value of ret_val is unmodified\n", 1851 n_prnt (test_str)); 1852 } 1853 } 1854 } 1855 return t_failed; 1856 } 1857 1858 1859 static size_t 1860 check_strx_to_uint64_overflow (void) 1861 { 1862 size_t t_failed = 0; 1863 size_t i, j; 1864 int c_failed[sizeof(strx_ovflw) / sizeof(strx_ovflw[0])]; 1865 static const size_t n_checks = sizeof(c_failed) / sizeof(c_failed[0]); 1866 1867 memset (c_failed, 0, sizeof(c_failed)); 1868 1869 for (j = 0; j < locale_name_count; j++) 1870 { 1871 set_test_locale (j); /* setlocale() can be slow! */ 1872 for (i = 0; i < n_checks; i++) 1873 { 1874 size_t rs; 1875 const struct str_with_len *const t = strx_ovflw + i; 1876 static const uint_fast64_t rnd_val = 74218431; 1877 uint_fast64_t test_val; 1878 1879 for (test_val = 0; test_val <= rnd_val && ! c_failed[i]; test_val += 1880 rnd_val) 1881 { 1882 uint_fast64_t rv = test_val; 1883 1884 rs = mhd_strx_to_uint64 (t->str, &rv); 1885 if (rs != 0) 1886 { 1887 t_failed++; 1888 c_failed[i] = ! 0; 1889 fprintf (stderr, 1890 "FAILED: mhd_strx_to_uint64(\"%s\", ->0x%" PRIXFAST64 1891 ") returned %" 1892 PRIuPTR ", while expecting zero." 1893 " Locale: %s\n", n_prnt (t->str), rv, (uintptr_t) rs, 1894 get_current_locale_str ()); 1895 } 1896 else if (rv != test_val) 1897 { 1898 t_failed++; 1899 c_failed[i] = ! 0; 1900 fprintf (stderr, 1901 "FAILED: mhd_strx_to_uint64(\"%s\", &ret_val) modified value of ret_val" 1902 " (before call: 0x%" PRIXFAST64 ", after call 0x%" PRIXFAST64 1903 "). Locale: %s\n", 1904 n_prnt (t->str), test_val, rv, get_current_locale_str ()); 1905 } 1906 } 1907 if ((verbose > 1) && (j == locale_name_count - 1) && ! c_failed[i]) 1908 printf ("PASSED: mhd_strx_to_uint64(\"%s\", &ret_val) == 0, " 1909 "value of ret_val is unmodified\n", 1910 n_prnt (t->str)); 1911 } 1912 } 1913 return t_failed; 1914 } 1915 1916 1917 static size_t 1918 check_strx_to_uint64_no_val (void) 1919 { 1920 size_t t_failed = 0; 1921 size_t i, j; 1922 int c_failed[sizeof(str_no_num) / sizeof(str_no_num[0])]; 1923 static const size_t n_checks = sizeof(c_failed) / sizeof(c_failed[0]); 1924 1925 memset (c_failed, 0, sizeof(c_failed)); 1926 1927 for (j = 0; j < locale_name_count; j++) 1928 { 1929 set_test_locale (j); /* setlocale() can be slow! */ 1930 for (i = 0; i < n_checks; i++) 1931 { 1932 size_t rs; 1933 const struct str_with_len *const t = str_no_num + i; 1934 static const uint_fast64_t rnd_val = 74218431; 1935 uint_fast64_t test_val; 1936 1937 for (test_val = 0; test_val <= rnd_val && ! c_failed[i]; test_val += 1938 rnd_val) 1939 { 1940 uint_fast64_t rv = test_val; 1941 1942 rs = mhd_strx_to_uint64 (t->str, &rv); 1943 if (rs != 0) 1944 { 1945 t_failed++; 1946 c_failed[i] = ! 0; 1947 fprintf (stderr, 1948 "FAILED: mhd_strx_to_uint64(\"%s\", ->0x%" PRIXFAST64 1949 ") returned %" 1950 PRIuPTR ", while expecting zero." 1951 " Locale: %s\n", n_prnt (t->str), rv, (uintptr_t) rs, 1952 get_current_locale_str ()); 1953 } 1954 else if (rv != test_val) 1955 { 1956 t_failed++; 1957 c_failed[i] = ! 0; 1958 fprintf (stderr, 1959 "FAILED: mhd_strx_to_uint64(\"%s\", &ret_val) modified value of ret_val" 1960 " (before call: 0x%" PRIXFAST64 ", after call 0x%" PRIXFAST64 1961 "). Locale: %s\n", 1962 n_prnt (t->str), test_val, rv, get_current_locale_str ()); 1963 } 1964 } 1965 if ((verbose > 1) && (j == locale_name_count - 1) && ! c_failed[i]) 1966 printf ("PASSED: mhd_strx_to_uint64(\"%s\", &ret_val) == 0, " 1967 "value of ret_val is unmodified\n", 1968 n_prnt (t->str)); 1969 } 1970 } 1971 return t_failed; 1972 } 1973 1974 1975 static size_t 1976 check_strx_to_uint64_n_valid (void) 1977 { 1978 size_t t_failed = 0; 1979 size_t i, j; 1980 int c_failed[sizeof(xdstrs_w_values) / sizeof(xdstrs_w_values[0])]; 1981 static const size_t n_checks = sizeof(c_failed) / sizeof(c_failed[0]); 1982 1983 memset (c_failed, 0, sizeof(c_failed)); 1984 1985 for (j = 0; j < locale_name_count; j++) 1986 { 1987 set_test_locale (j); /* setlocale() can be slow! */ 1988 for (i = 0; i < n_checks; i++) 1989 { 1990 uint_fast64_t rv = 2352932; /* some random value */ 1991 size_t rs = 0; 1992 size_t len; 1993 const struct str_with_value *const t = xdstrs_w_values + i; 1994 1995 if (t->str.len < t->num_of_digt) 1996 { 1997 fprintf (stderr, 1998 "ERROR: xdstrs_w_values[%u] has wrong num_of_digt (%u): num_of_digt is expected" 1999 " to be less or equal to str.len (%u).\n", 2000 (unsigned int) i, (unsigned int) t->num_of_digt, (unsigned 2001 int) t->str. 2002 len); 2003 exit (99); 2004 } 2005 for (len = t->num_of_digt; len <= t->str.len + 1 && ! c_failed[i]; len++) 2006 { 2007 rs = mhd_strx_to_uint64_n (t->str.str, len, &rv); 2008 if (rs != t->num_of_digt) 2009 { 2010 t_failed++; 2011 c_failed[i] = ! 0; 2012 fprintf (stderr, 2013 "FAILED: mhd_strx_to_uint64_n(\"%s\", %" PRIuPTR ", ->0x%" 2014 PRIXFAST64 ")" 2015 " returned %" PRIuPTR ", while expecting %d. Locale: %s\n", 2016 n_prnt (t->str.str), (uintptr_t) len, rv, (uintptr_t) rs, 2017 (int) t->num_of_digt, get_current_locale_str ()); 2018 } 2019 if (rv != t->val) 2020 { 2021 t_failed++; 2022 c_failed[i] = ! 0; 2023 fprintf (stderr, 2024 "FAILED: mhd_strx_to_uint64_n(\"%s\", %" PRIuPTR ", ->0x%" 2025 PRIXFAST64 ")" 2026 " converted string to value 0x%" PRIXFAST64 2027 ", while expecting result 0x%" PRIXFAST64 2028 ". Locale: %s\n", n_prnt (t->str.str), (uintptr_t) len, rv, 2029 rv, 2030 t->val, get_current_locale_str ()); 2031 } 2032 } 2033 if ((verbose > 1) && (j == locale_name_count - 1) && ! c_failed[i]) 2034 printf ("PASSED: mhd_strx_to_uint64_n(\"%s\", %" PRIuPTR "..%" PRIuPTR 2035 ", ->0x%" 2036 PRIXFAST64 ")" 2037 " == %" PRIuPTR "\n", n_prnt (t->str.str), 2038 (uintptr_t) t->num_of_digt, 2039 (uintptr_t) t->str.len + 1, rv, rs); 2040 } 2041 } 2042 return t_failed; 2043 } 2044 2045 2046 static size_t 2047 check_strx_to_uint64_n_all_chars (void) 2048 { 2049 int c_failed[256]; /* from 0 to 255 */ 2050 static const size_t n_checks = sizeof(c_failed) / sizeof(c_failed[0]); 2051 size_t t_failed = 0; 2052 size_t j; 2053 2054 memset (c_failed, 0, sizeof(c_failed)); 2055 2056 for (j = 0; j < locale_name_count; j++) 2057 { 2058 unsigned int c; 2059 uint_fast64_t test_val; 2060 2061 set_test_locale (j); /* setlocale() can be slow! */ 2062 for (c = 0; c < n_checks; c++) 2063 { 2064 static const uint_fast64_t rnd_val = 98372558; 2065 size_t rs; 2066 size_t len; 2067 2068 if (( (c >= '0') && (c <= '9') ) 2069 || ( (c >= 'A') && (c <= 'F') ) 2070 || ( (c >= 'a') && (c <= 'f') )) 2071 continue; /* skip xdigits */ 2072 2073 for (len = 0; len <= 5; len++) 2074 { 2075 for (test_val = 0; test_val <= rnd_val && ! c_failed[c]; test_val += 2076 rnd_val) 2077 { 2078 char test_str[] = "0123"; 2079 uint_fast64_t rv = test_val; 2080 2081 test_str[0] = (char) (unsigned char) c; /* replace first char with non-digit char */ 2082 rs = mhd_strx_to_uint64_n (test_str, len, &rv); 2083 if (rs != 0) 2084 { 2085 t_failed++; 2086 c_failed[c] = ! 0; 2087 fprintf (stderr, 2088 "FAILED: mhd_strx_to_uint64_n(\"%s\", %" PRIuPTR ", ->0x%" 2089 PRIXFAST64 2090 ")" 2091 " returned %" PRIuPTR 2092 ", while expecting zero. Locale: %s\n", 2093 n_prnt (test_str), (uintptr_t) len, rv, (uintptr_t) rs, 2094 get_current_locale_str ()); 2095 } 2096 else if (rv != test_val) 2097 { 2098 t_failed++; 2099 c_failed[c] = ! 0; 2100 fprintf (stderr, 2101 "FAILED: mhd_strx_to_uint64_n(\"%s\", %" PRIuPTR 2102 ", &ret_val)" 2103 " modified value of ret_val (before call: 0x%" PRIXFAST64 2104 ", after call 0x%" PRIXFAST64 ")." 2105 " Locale: %s\n", 2106 n_prnt (test_str), (uintptr_t) len, test_val, rv, 2107 get_current_locale_str ()); 2108 } 2109 } 2110 } 2111 if ((verbose > 1) && (j == locale_name_count - 1) && ! c_failed[c]) 2112 { 2113 char test_str[] = "0123"; 2114 test_str[0] = (char) (unsigned char) c; /* replace first char with non-digit char */ 2115 2116 printf ("PASSED: mhd_strx_to_uint64_n(\"%s\", 0..5, &ret_val) == 0, " 2117 "value of ret_val is unmodified\n", 2118 n_prnt (test_str)); 2119 } 2120 } 2121 } 2122 return t_failed; 2123 } 2124 2125 2126 static size_t 2127 check_strx_to_uint64_n_overflow (void) 2128 { 2129 size_t t_failed = 0; 2130 size_t i, j; 2131 int c_failed[sizeof(strx_ovflw) / sizeof(strx_ovflw[0])]; 2132 static const size_t n_checks = sizeof(c_failed) / sizeof(c_failed[0]); 2133 2134 memset (c_failed, 0, sizeof(c_failed)); 2135 2136 for (j = 0; j < locale_name_count; j++) 2137 { 2138 set_test_locale (j); /* setlocale() can be slow! */ 2139 for (i = 0; i < n_checks; i++) 2140 { 2141 size_t rs; 2142 const struct str_with_len *const t = strx_ovflw + i; 2143 static const uint_fast64_t rnd_val = 4; 2144 size_t len; 2145 2146 for (len = t->len; len <= t->len + 1; len++) 2147 { 2148 uint_fast64_t test_val; 2149 for (test_val = 0; test_val <= rnd_val && ! c_failed[i]; test_val += 2150 rnd_val) 2151 { 2152 uint_fast64_t rv = test_val; 2153 2154 rs = mhd_strx_to_uint64_n (t->str, len, &rv); 2155 if (rs != 0) 2156 { 2157 t_failed++; 2158 c_failed[i] = ! 0; 2159 fprintf (stderr, 2160 "FAILED: mhd_strx_to_uint64_n(\"%s\", %" PRIuPTR ", ->0x%" 2161 PRIXFAST64 2162 ")" 2163 " returned %" PRIuPTR 2164 ", while expecting zero. Locale: %s\n", 2165 n_prnt (t->str), (uintptr_t) len, rv, (uintptr_t) rs, 2166 get_current_locale_str ()); 2167 } 2168 else if (rv != test_val) 2169 { 2170 t_failed++; 2171 c_failed[i] = ! 0; 2172 fprintf (stderr, 2173 "FAILED: mhd_strx_to_uint64_n(\"%s\", %" PRIuPTR 2174 ", &ret_val)" 2175 " modified value of ret_val (before call: 0x%" PRIXFAST64 2176 ", after call 0x%" PRIXFAST64 ")." 2177 " Locale: %s\n", n_prnt (t->str), (uintptr_t) len, 2178 test_val, rv, 2179 get_current_locale_str ()); 2180 } 2181 } 2182 } 2183 if ((verbose > 1) && (j == locale_name_count - 1) && ! c_failed[i]) 2184 printf ("PASSED: mhd_strx_to_uint64_n(\"%s\", %" PRIuPTR "..%" PRIuPTR 2185 ", &ret_val) == 0," 2186 " value of ret_val is unmodified\n", n_prnt (t->str), 2187 (uintptr_t) t->len, 2188 (uintptr_t) t->len + 1); 2189 } 2190 } 2191 return t_failed; 2192 } 2193 2194 2195 static size_t 2196 check_strx_to_uint64_n_no_val (void) 2197 { 2198 size_t t_failed = 0; 2199 size_t i, j; 2200 int c_failed[sizeof(str_no_num) / sizeof(str_no_num[0])]; 2201 static const size_t n_checks = sizeof(c_failed) / sizeof(c_failed[0]); 2202 2203 memset (c_failed, 0, sizeof(c_failed)); 2204 2205 for (j = 0; j < locale_name_count; j++) 2206 { 2207 set_test_locale (j); /* setlocale() can be slow! */ 2208 for (i = 0; i < n_checks; i++) 2209 { 2210 size_t rs; 2211 const struct str_with_len *const t = str_no_num + i; 2212 static const uint_fast64_t rnd_val = 3214314212UL; 2213 size_t len; 2214 2215 for (len = 0; len <= t->len + 1; len++) 2216 { 2217 uint_fast64_t test_val; 2218 for (test_val = 0; test_val <= rnd_val && ! c_failed[i]; test_val += 2219 rnd_val) 2220 { 2221 uint_fast64_t rv = test_val; 2222 2223 rs = mhd_strx_to_uint64_n (t->str, len, &rv); 2224 if (rs != 0) 2225 { 2226 t_failed++; 2227 c_failed[i] = ! 0; 2228 fprintf (stderr, 2229 "FAILED: mhd_strx_to_uint64_n(\"%s\", %" PRIuPTR ", ->0x%" 2230 PRIXFAST64 2231 ")" 2232 " returned %" PRIuPTR 2233 ", while expecting zero. Locale: %s\n", 2234 n_prnt (t->str), (uintptr_t) len, rv, (uintptr_t) rs, 2235 get_current_locale_str ()); 2236 } 2237 else if (rv != test_val) 2238 { 2239 t_failed++; 2240 c_failed[i] = ! 0; 2241 fprintf (stderr, 2242 "FAILED: mhd_strx_to_uint64_n(\"%s\", %" PRIuPTR 2243 ", &ret_val)" 2244 " modified value of ret_val (before call: 0x%" PRIXFAST64 2245 ", after call 0x%" PRIXFAST64 ")." 2246 " Locale: %s\n", n_prnt (t->str), (uintptr_t) len, 2247 test_val, rv, 2248 get_current_locale_str ()); 2249 } 2250 } 2251 } 2252 if ((verbose > 1) && (j == locale_name_count - 1) && ! c_failed[i]) 2253 printf ("PASSED: mhd_strx_to_uint64_n(\"%s\", 0..%" PRIuPTR 2254 ", &ret_val) == 0," 2255 " value of ret_val is unmodified\n", n_prnt (t->str), 2256 (uintptr_t) t->len + 1); 2257 } 2258 } 2259 return t_failed; 2260 } 2261 2262 2263 static int 2264 run_str_to_X_tests (void) 2265 { 2266 size_t str_to_uint64_fails = 0; 2267 size_t str_to_uint64_n_fails = 0; 2268 size_t strx_to_uint32_fails = 0; 2269 size_t strx_to_uint32_n_fails = 0; 2270 size_t strx_to_uint64_fails = 0; 2271 size_t strx_to_uint64_n_fails = 0; 2272 size_t res; 2273 2274 res = check_str_to_uint64_valid (); 2275 if (res != 0) 2276 { 2277 str_to_uint64_fails += res; 2278 fprintf (stderr, 2279 "FAILED: testcase check_str_to_uint64_valid() failed.\n\n"); 2280 } 2281 else if (verbose > 1) 2282 printf ( 2283 "PASSED: testcase check_str_to_uint64_valid() successfully passed.\n\n"); 2284 2285 res = check_str_to_uint64_all_chars (); 2286 if (res != 0) 2287 { 2288 str_to_uint64_fails += res; 2289 fprintf (stderr, 2290 "FAILED: testcase check_str_to_uint64_all_chars() failed.\n\n"); 2291 } 2292 else if (verbose > 1) 2293 printf ("PASSED: testcase check_str_to_uint64_all_chars() " 2294 "successfully passed.\n\n"); 2295 2296 res = check_str_to_uint64_overflow (); 2297 if (res != 0) 2298 { 2299 str_to_uint64_fails += res; 2300 fprintf (stderr, 2301 "FAILED: testcase check_str_to_uint64_overflow() failed.\n\n"); 2302 } 2303 else if (verbose > 1) 2304 printf ("PASSED: testcase check_str_to_uint64_overflow() " 2305 "successfully passed.\n\n"); 2306 2307 res = check_str_to_uint64_no_val (); 2308 if (res != 0) 2309 { 2310 str_to_uint64_fails += res; 2311 fprintf (stderr, 2312 "FAILED: testcase check_str_to_uint64_no_val() failed.\n\n"); 2313 } 2314 else if (verbose > 1) 2315 printf ("PASSED: testcase check_str_to_uint64_no_val() " 2316 "successfully passed.\n\n"); 2317 2318 if (str_to_uint64_fails) 2319 fprintf (stderr, 2320 "FAILED: function mhd_str_to_uint64() failed %lu time%s.\n\n", 2321 (unsigned long) str_to_uint64_fails, 2322 str_to_uint64_fails == 1 ? "" : "s"); 2323 else if (verbose > 0) 2324 printf ("PASSED: function mhd_str_to_uint64() successfully " 2325 "passed all checks.\n\n"); 2326 2327 res = check_str_to_uint64_n_valid (); 2328 if (res != 0) 2329 { 2330 str_to_uint64_n_fails += res; 2331 fprintf (stderr, 2332 "FAILED: testcase check_str_to_uint64_n_valid() failed.\n\n"); 2333 } 2334 else if (verbose > 1) 2335 printf ("PASSED: testcase check_str_to_uint64_n_valid() " 2336 "successfully passed.\n\n"); 2337 2338 res = check_str_to_uint64_n_all_chars (); 2339 if (res != 0) 2340 { 2341 str_to_uint64_n_fails += res; 2342 fprintf (stderr, 2343 "FAILED: testcase check_str_to_uint64_n_all_chars() failed.\n\n"); 2344 } 2345 else if (verbose > 1) 2346 printf ("PASSED: testcase check_str_to_uint64_n_all_chars() " 2347 "successfully passed.\n\n"); 2348 2349 res = check_str_to_uint64_n_overflow (); 2350 if (res != 0) 2351 { 2352 str_to_uint64_n_fails += res; 2353 fprintf (stderr, 2354 "FAILED: testcase check_str_to_uint64_n_overflow() failed.\n\n"); 2355 } 2356 else if (verbose > 1) 2357 printf ("PASSED: testcase check_str_to_uint64_n_overflow() " 2358 "successfully passed.\n\n"); 2359 2360 res = check_str_to_uint64_n_no_val (); 2361 if (res != 0) 2362 { 2363 str_to_uint64_n_fails += res; 2364 fprintf (stderr, 2365 "FAILED: testcase check_str_to_uint64_n_no_val() failed.\n\n"); 2366 } 2367 else if (verbose > 1) 2368 printf ("PASSED: testcase check_str_to_uint64_n_no_val() " 2369 "successfully passed.\n\n"); 2370 2371 if (str_to_uint64_n_fails) 2372 fprintf (stderr, 2373 "FAILED: function mhd_str_to_uint64_n() failed %lu time%s.\n\n", 2374 (unsigned long) str_to_uint64_n_fails, 2375 str_to_uint64_n_fails == 1 ? "" : "s"); 2376 else if (verbose > 0) 2377 printf ("PASSED: function mhd_str_to_uint64_n() successfully " 2378 "passed all checks.\n\n"); 2379 2380 res = check_strx_to_uint32_valid (); 2381 if (res != 0) 2382 { 2383 strx_to_uint32_fails += res; 2384 fprintf (stderr, 2385 "FAILED: testcase check_strx_to_uint32_valid() failed.\n\n"); 2386 } 2387 else if (verbose > 1) 2388 printf ("PASSED: testcase check_strx_to_uint32_valid() " 2389 "successfully passed.\n\n"); 2390 2391 res = check_strx_to_uint32_all_chars (); 2392 if (res != 0) 2393 { 2394 strx_to_uint32_fails += res; 2395 fprintf (stderr, 2396 "FAILED: testcase check_strx_to_uint32_all_chars() failed.\n\n"); 2397 } 2398 else if (verbose > 1) 2399 printf ("PASSED: testcase check_strx_to_uint32_all_chars() " 2400 "successfully passed.\n\n"); 2401 2402 res = check_strx_to_uint32_overflow (); 2403 if (res != 0) 2404 { 2405 strx_to_uint32_fails += res; 2406 fprintf (stderr, 2407 "FAILED: testcase check_strx_to_uint32_overflow() failed.\n\n"); 2408 } 2409 else if (verbose > 1) 2410 printf ("PASSED: testcase check_strx_to_uint32_overflow() " 2411 "successfully passed.\n\n"); 2412 2413 res = check_strx_to_uint32_no_val (); 2414 if (res != 0) 2415 { 2416 strx_to_uint32_fails += res; 2417 fprintf (stderr, 2418 "FAILED: testcase check_strx_to_uint32_no_val() failed.\n\n"); 2419 } 2420 else if (verbose > 1) 2421 printf ("PASSED: testcase check_strx_to_uint32_no_val() " 2422 "successfully passed.\n\n"); 2423 2424 if (strx_to_uint32_fails) 2425 fprintf (stderr, 2426 "FAILED: function mhd_strx_to_uint32() failed %lu time%s.\n\n", 2427 (unsigned long) strx_to_uint32_fails, 2428 strx_to_uint32_fails == 1 ? "" : "s"); 2429 else if (verbose > 0) 2430 printf ("PASSED: function mhd_strx_to_uint32() successfully " 2431 "passed all checks.\n\n"); 2432 2433 res = check_strx_to_uint32_n_valid (); 2434 if (res != 0) 2435 { 2436 strx_to_uint32_n_fails += res; 2437 fprintf (stderr, 2438 "FAILED: testcase check_strx_to_uint32_n_valid() failed.\n\n"); 2439 } 2440 else if (verbose > 1) 2441 printf ("PASSED: testcase check_strx_to_uint32_n_valid() " 2442 "successfully passed.\n\n"); 2443 2444 res = check_strx_to_uint32_n_all_chars (); 2445 if (res != 0) 2446 { 2447 strx_to_uint32_n_fails += res; 2448 fprintf (stderr, 2449 "FAILED: testcase check_strx_to_uint32_n_all_chars() failed.\n\n"); 2450 } 2451 else if (verbose > 1) 2452 printf ("PASSED: testcase check_strx_to_uint32_n_all_chars() " 2453 "successfully passed.\n\n"); 2454 2455 res = check_strx_to_uint32_n_overflow (); 2456 if (res != 0) 2457 { 2458 strx_to_uint32_n_fails += res; 2459 fprintf (stderr, 2460 "FAILED: testcase check_strx_to_uint32_n_overflow() failed.\n\n"); 2461 } 2462 else if (verbose > 1) 2463 printf ("PASSED: testcase check_strx_to_uint32_n_overflow() " 2464 "successfully passed.\n\n"); 2465 2466 res = check_strx_to_uint32_n_no_val (); 2467 if (res != 0) 2468 { 2469 strx_to_uint32_n_fails += res; 2470 fprintf (stderr, 2471 "FAILED: testcase check_strx_to_uint32_n_no_val() failed.\n\n"); 2472 } 2473 else if (verbose > 1) 2474 printf ("PASSED: testcase check_strx_to_uint32_n_no_val() " 2475 "successfully passed.\n\n"); 2476 2477 if (strx_to_uint32_n_fails) 2478 fprintf (stderr, 2479 "FAILED: function mhd_strx_to_uint32_n() failed %lu time%s.\n\n", 2480 (unsigned long) strx_to_uint32_n_fails, 2481 strx_to_uint32_n_fails == 1 ? "" : "s"); 2482 else if (verbose > 0) 2483 printf ("PASSED: function mhd_strx_to_uint32_n() successfully " 2484 "passed all checks.\n\n"); 2485 2486 res = check_strx_to_uint64_valid (); 2487 if (res != 0) 2488 { 2489 strx_to_uint64_fails += res; 2490 fprintf (stderr, 2491 "FAILED: testcase check_strx_to_uint64_valid() failed.\n\n"); 2492 } 2493 else if (verbose > 1) 2494 printf ("PASSED: testcase check_strx_to_uint64_valid() " 2495 "successfully passed.\n\n"); 2496 2497 res = check_strx_to_uint64_all_chars (); 2498 if (res != 0) 2499 { 2500 strx_to_uint64_fails += res; 2501 fprintf (stderr, 2502 "FAILED: testcase check_strx_to_uint64_all_chars() failed.\n\n"); 2503 } 2504 else if (verbose > 1) 2505 printf ("PASSED: testcase check_strx_to_uint64_all_chars() " 2506 "successfully passed.\n\n"); 2507 2508 res = check_strx_to_uint64_overflow (); 2509 if (res != 0) 2510 { 2511 strx_to_uint64_fails += res; 2512 fprintf (stderr, 2513 "FAILED: testcase check_strx_to_uint64_overflow() failed.\n\n"); 2514 } 2515 else if (verbose > 1) 2516 printf ("PASSED: testcase check_strx_to_uint64_overflow() " 2517 "successfully passed.\n\n"); 2518 2519 res = check_strx_to_uint64_no_val (); 2520 if (res != 0) 2521 { 2522 strx_to_uint64_fails += res; 2523 fprintf (stderr, 2524 "FAILED: testcase check_strx_to_uint64_no_val() failed.\n\n"); 2525 } 2526 else if (verbose > 1) 2527 printf ("PASSED: testcase check_strx_to_uint64_no_val() " 2528 "successfully passed.\n\n"); 2529 2530 if (strx_to_uint64_fails) 2531 fprintf (stderr, 2532 "FAILED: function mhd_strx_to_uint64() failed %lu time%s.\n\n", 2533 (unsigned long) strx_to_uint64_fails, 2534 strx_to_uint64_fails == 1 ? "" : "s"); 2535 else if (verbose > 0) 2536 printf ("PASSED: function mhd_strx_to_uint64() successfully " 2537 "passed all checks.\n\n"); 2538 2539 res = check_strx_to_uint64_n_valid (); 2540 if (res != 0) 2541 { 2542 strx_to_uint64_n_fails += res; 2543 fprintf (stderr, 2544 "FAILED: testcase check_strx_to_uint64_n_valid() failed.\n\n"); 2545 } 2546 else if (verbose > 1) 2547 printf ("PASSED: testcase check_strx_to_uint64_n_valid() " 2548 "successfully passed.\n\n"); 2549 2550 res = check_strx_to_uint64_n_all_chars (); 2551 if (res != 0) 2552 { 2553 strx_to_uint64_n_fails += res; 2554 fprintf (stderr, 2555 "FAILED: testcase check_strx_to_uint64_n_all_chars() failed.\n\n"); 2556 } 2557 else if (verbose > 1) 2558 printf ("PASSED: testcase check_strx_to_uint64_n_all_chars() " 2559 "successfully passed.\n\n"); 2560 2561 res = check_strx_to_uint64_n_overflow (); 2562 if (res != 0) 2563 { 2564 strx_to_uint64_n_fails += res; 2565 fprintf (stderr, 2566 "FAILED: testcase check_strx_to_uint64_n_overflow() failed.\n\n"); 2567 } 2568 else if (verbose > 1) 2569 printf ("PASSED: testcase check_strx_to_uint64_n_overflow() " 2570 "successfully passed.\n\n"); 2571 2572 res = check_strx_to_uint64_n_no_val (); 2573 if (res != 0) 2574 { 2575 strx_to_uint64_n_fails += res; 2576 fprintf (stderr, 2577 "FAILED: testcase check_strx_to_uint64_n_no_val() failed.\n\n"); 2578 } 2579 else if (verbose > 1) 2580 printf ("PASSED: testcase check_strx_to_uint64_n_no_val() " 2581 "successfully passed.\n\n"); 2582 2583 if (strx_to_uint64_n_fails) 2584 fprintf (stderr, 2585 "FAILED: function mhd_strx_to_uint64_n() failed %lu time%s.\n\n", 2586 (unsigned long) strx_to_uint64_n_fails, 2587 strx_to_uint64_n_fails == 1 ? "" : "s"); 2588 else if (verbose > 0) 2589 printf ("PASSED: function mhd_strx_to_uint64_n() successfully " 2590 "passed all checks.\n\n"); 2591 2592 if (str_to_uint64_fails || str_to_uint64_n_fails || 2593 strx_to_uint32_fails || strx_to_uint32_n_fails || 2594 strx_to_uint64_fails || strx_to_uint64_n_fails) 2595 { 2596 if (verbose > 0) 2597 printf ("At least one test failed.\n"); 2598 2599 return 1; 2600 } 2601 2602 if (verbose > 0) 2603 printf ("All tests passed successfully.\n"); 2604 2605 return 0; 2606 } 2607 2608 2609 int 2610 main (int argc, char *argv[]) 2611 { 2612 if (mhdt_has_param (argc, argv, "-v") || 2613 mhdt_has_param (argc, argv, "--verbose") || 2614 mhdt_has_param (argc, argv, "--verbose1")) 2615 MHDT_set_verbosity (MHDT_VERB_LVL_BASIC); 2616 if (mhdt_has_param (argc, argv, "-vv") || 2617 mhdt_has_param (argc, argv, "--verbose2")) 2618 MHDT_set_verbosity (MHDT_VERB_LVL_VERBOSE); 2619 2620 return run_str_to_X_tests (); 2621 }