test_str_to_value.c (85588B)
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 microhttpd/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 #include "../mhd2/mhd_str.h" 28 #include "../mhd2/mhd_str.c" 29 #include "../mhdt_checks.h" 30 31 #include <stdio.h> 32 #include <locale.h> 33 #include <string.h> 34 #ifdef HAVE_INTTYPES_H 35 #include <inttypes.h> 36 #else /* ! HAVE_INTTYPES_H */ 37 #define PRIu64 "llu" 38 #define PRIuPTR "u" 39 #define PRIX64 "llX" 40 #endif /* ! HAVE_INTTYPES_H */ 41 #include <stdint.h> 42 #ifdef HAVE_STDLIB_H 43 #include <stdlib.h> 44 #endif /* HAVE_STDLIB_H */ 45 #include "mhd_limits.h" 46 #include "mhd_str.h" 47 #include "test_helpers.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 uint64_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 uint64_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\", ->%" PRIu64 ") returned %" 619 PRIuPTR 620 ", while expecting %d." 621 " Locale: %s\n", n_prnt (t->str.str), rv, (uintptr_t) rs, 622 (int) t->num_of_digt, get_current_locale_str ()); 623 } 624 if (rv != t->val) 625 { 626 t_failed++; 627 c_failed[i] = ! 0; 628 fprintf (stderr, 629 "FAILED: mhd_str_to_uint64(\"%s\", ->%" PRIu64 630 ") converted string to value %" 631 PRIu64 "," 632 " while expecting result %" PRIu64 ". Locale: %s\n", 633 n_prnt (t->str.str), rv, rv, 634 t->val, get_current_locale_str ()); 635 } 636 if ((verbose > 1) && (j == locale_name_count - 1) && ! c_failed[i]) 637 printf ("PASSED: mhd_str_to_uint64(\"%s\", ->%" PRIu64 ") == %" \ 638 PRIuPTR "\n", 639 n_prnt (t->str.str), rv, rs); 640 } 641 } 642 return t_failed; 643 } 644 645 646 static size_t 647 check_str_to_uint64_all_chars (void) 648 { 649 int c_failed[256]; /* from 0 to 255 */ 650 static const size_t n_checks = sizeof(c_failed) / sizeof(c_failed[0]); 651 size_t t_failed = 0; 652 size_t j; 653 654 memset (c_failed, 0, sizeof(c_failed)); 655 656 for (j = 0; j < locale_name_count; j++) 657 { 658 unsigned int c; 659 uint64_t test_val; 660 661 set_test_locale (j); /* setlocale() can be slow! */ 662 for (c = 0; c < n_checks; c++) 663 { 664 static const uint64_t rnd_val = 24941852; 665 size_t rs; 666 if ((c >= '0') && (c <= '9') ) 667 continue; /* skip digits */ 668 for (test_val = 0; test_val <= rnd_val && ! c_failed[c]; test_val += 669 rnd_val) 670 { 671 char test_str[] = "0123"; 672 uint64_t rv = test_val; 673 674 test_str[0] = (char) (unsigned char) c; /* replace first char with non-digit char */ 675 rs = mhd_str_to_uint64 (test_str, &rv); 676 if (rs != 0) 677 { 678 t_failed++; 679 c_failed[c] = ! 0; 680 fprintf (stderr, 681 "FAILED: mhd_str_to_uint64(\"%s\", ->%" PRIu64 682 ") returned %" PRIuPTR 683 ", while expecting zero." 684 " Locale: %s\n", n_prnt (test_str), rv, (uintptr_t) rs, 685 get_current_locale_str ()); 686 } 687 else if (rv != test_val) 688 { 689 t_failed++; 690 c_failed[c] = ! 0; 691 fprintf (stderr, 692 "FAILED: mhd_str_to_uint64(\"%s\", &ret_val) modified value of ret_val" 693 " (before call: %" PRIu64 ", after call %" PRIu64 694 "). Locale: %s\n", 695 n_prnt (test_str), test_val, rv, get_current_locale_str ()); 696 } 697 } 698 if ((verbose > 1) && (j == locale_name_count - 1) && ! c_failed[c]) 699 { 700 char test_str[] = "0123"; 701 test_str[0] = (char) (unsigned char) c; /* replace first char with non-digit char */ 702 703 printf ("PASSED: mhd_str_to_uint64(\"%s\", &ret_val) == 0, " 704 "value of ret_val is unmodified\n", 705 n_prnt (test_str)); 706 } 707 } 708 } 709 return t_failed; 710 } 711 712 713 static size_t 714 check_str_to_uint64_overflow (void) 715 { 716 size_t t_failed = 0; 717 size_t i, j; 718 int c_failed[sizeof(str_ovflw) / sizeof(str_ovflw[0])]; 719 static const size_t n_checks = sizeof(c_failed) / sizeof(c_failed[0]); 720 721 memset (c_failed, 0, sizeof(c_failed)); 722 723 for (j = 0; j < locale_name_count; j++) 724 { 725 set_test_locale (j); /* setlocale() can be slow! */ 726 for (i = 0; i < n_checks; i++) 727 { 728 size_t rs; 729 const struct str_with_len *const t = str_ovflw + i; 730 static const uint64_t rnd_val = 2; 731 uint64_t test_val; 732 733 for (test_val = 0; test_val <= rnd_val && ! c_failed[i]; test_val += 734 rnd_val) 735 { 736 uint64_t rv = test_val; 737 738 rs = mhd_str_to_uint64 (t->str, &rv); 739 if (rs != 0) 740 { 741 t_failed++; 742 c_failed[i] = ! 0; 743 fprintf (stderr, 744 "FAILED: mhd_str_to_uint64(\"%s\", ->%" PRIu64 745 ") returned %" PRIuPTR 746 ", while expecting zero." 747 " Locale: %s\n", n_prnt (t->str), rv, (uintptr_t) rs, 748 get_current_locale_str ()); 749 } 750 else if (rv != test_val) 751 { 752 t_failed++; 753 c_failed[i] = ! 0; 754 fprintf (stderr, 755 "FAILED: mhd_str_to_uint64(\"%s\", &ret_val) modified value of ret_val" 756 " (before call: %" PRIu64 ", after call %" PRIu64 757 "). Locale: %s\n", 758 n_prnt (t->str), test_val, rv, get_current_locale_str ()); 759 } 760 } 761 if ((verbose > 1) && (j == locale_name_count - 1) && ! c_failed[i]) 762 printf ("PASSED: mhd_str_to_uint64(\"%s\", &ret_val) == 0, " 763 "value of ret_val is unmodified\n", 764 n_prnt (t->str)); 765 } 766 } 767 return t_failed; 768 } 769 770 771 static size_t 772 check_str_to_uint64_no_val (void) 773 { 774 size_t t_failed = 0; 775 size_t i, j; 776 int c_failed[sizeof(str_no_num) / sizeof(str_no_num[0])]; 777 static const size_t n_checks = sizeof(c_failed) / sizeof(c_failed[0]); 778 779 memset (c_failed, 0, sizeof(c_failed)); 780 781 for (j = 0; j < locale_name_count; j++) 782 { 783 set_test_locale (j); /* setlocale() can be slow! */ 784 for (i = 0; i < n_checks; i++) 785 { 786 size_t rs; 787 const struct str_with_len *const t = str_no_num + i; 788 static const uint64_t rnd_val = 74218431; 789 uint64_t test_val; 790 791 for (test_val = 0; test_val <= rnd_val && ! c_failed[i]; test_val += 792 rnd_val) 793 { 794 uint64_t rv = test_val; 795 796 rs = mhd_str_to_uint64 (t->str, &rv); 797 if (rs != 0) 798 { 799 t_failed++; 800 c_failed[i] = ! 0; 801 fprintf (stderr, 802 "FAILED: mhd_str_to_uint64(\"%s\", ->%" PRIu64 803 ") returned %" PRIuPTR 804 ", while expecting zero." 805 " Locale: %s\n", n_prnt (t->str), rv, (uintptr_t) rs, 806 get_current_locale_str ()); 807 } 808 else if (rv != test_val) 809 { 810 t_failed++; 811 c_failed[i] = ! 0; 812 fprintf (stderr, 813 "FAILED: mhd_str_to_uint64(\"%s\", &ret_val) modified value of ret_val" 814 " (before call: %" PRIu64 ", after call %" PRIu64 815 "). Locale: %s\n", 816 n_prnt (t->str), test_val, rv, get_current_locale_str ()); 817 } 818 } 819 if ((verbose > 1) && (j == locale_name_count - 1) && ! c_failed[i]) 820 printf ("PASSED: mhd_str_to_uint64(\"%s\", &ret_val) == 0, " 821 "value of ret_val is unmodified\n", 822 n_prnt (t->str)); 823 } 824 } 825 return t_failed; 826 } 827 828 829 static size_t 830 check_str_to_uint64_n_valid (void) 831 { 832 size_t t_failed = 0; 833 size_t i, j; 834 int c_failed[sizeof(dstrs_w_values) 835 / sizeof(dstrs_w_values[0])]; 836 static const size_t n_checks = sizeof(c_failed) / sizeof(c_failed[0]); 837 838 memset (c_failed, 0, sizeof(c_failed)); 839 840 for (j = 0; j < locale_name_count; j++) 841 { 842 set_test_locale (j); /* setlocale() can be slow! */ 843 for (i = 0; i < n_checks; i++) 844 { 845 uint64_t rv = 1235572; /* some random value */ 846 size_t rs = 0; 847 size_t len; 848 const struct str_with_value *const t = dstrs_w_values + i; 849 850 if (t->str.len < t->num_of_digt) 851 { 852 fprintf (stderr, 853 "ERROR: dstrs_w_values[%u] has wrong num_of_digt (%u): num_of_digt is expected" 854 " to be less or equal to str.len (%u).\n", 855 (unsigned int) i, (unsigned int) t->num_of_digt, (unsigned 856 int) t->str. 857 len); 858 exit (99); 859 } 860 for (len = t->num_of_digt; len <= t->str.len + 1 && ! c_failed[i]; len++) 861 { 862 rs = mhd_str_to_uint64_n (t->str.str, len, &rv); 863 if (rs != t->num_of_digt) 864 { 865 t_failed++; 866 c_failed[i] = ! 0; 867 fprintf (stderr, 868 "FAILED: mhd_str_to_uint64_n(\"%s\", %" PRIuPTR ", ->%" 869 PRIu64 ")" 870 " returned %" PRIuPTR ", while expecting %d. Locale: %s\n", 871 n_prnt (t->str.str), (uintptr_t) len, rv, (uintptr_t) rs, 872 (int) t->num_of_digt, get_current_locale_str ()); 873 } 874 if (rv != t->val) 875 { 876 t_failed++; 877 c_failed[i] = ! 0; 878 fprintf (stderr, 879 "FAILED: mhd_str_to_uint64_n(\"%s\", %" PRIuPTR ", ->%" 880 PRIu64 ")" 881 " converted string to value %" PRIu64 882 ", while expecting result %" PRIu64 883 ". Locale: %s\n", n_prnt (t->str.str), (uintptr_t) len, rv, 884 rv, 885 t->val, get_current_locale_str ()); 886 } 887 } 888 if ((verbose > 1) && (j == locale_name_count - 1) && ! c_failed[i]) 889 printf ("PASSED: mhd_str_to_uint64_n(\"%s\", %" PRIuPTR "..%" 890 PRIuPTR ", ->%" PRIu64 ")" " == %" PRIuPTR "\n", 891 n_prnt (t->str.str), 892 (uintptr_t) t->num_of_digt, 893 (uintptr_t) t->str.len + 1, rv, rs); 894 } 895 } 896 return t_failed; 897 } 898 899 900 static size_t 901 check_str_to_uint64_n_all_chars (void) 902 { 903 int c_failed[256]; /* from 0 to 255 */ 904 static const size_t n_checks = sizeof(c_failed) / sizeof(c_failed[0]); 905 size_t t_failed = 0; 906 size_t j; 907 908 memset (c_failed, 0, sizeof(c_failed)); 909 910 for (j = 0; j < locale_name_count; j++) 911 { 912 unsigned int c; 913 uint64_t test_val; 914 915 set_test_locale (j); /* setlocale() can be slow! */ 916 for (c = 0; c < n_checks; c++) 917 { 918 static const uint64_t rnd_val = 98372558; 919 size_t rs; 920 size_t len; 921 922 if ((c >= '0') && (c <= '9') ) 923 continue; /* skip digits */ 924 925 for (len = 0; len <= 5; len++) 926 { 927 for (test_val = 0; test_val <= rnd_val && ! c_failed[c]; test_val += 928 rnd_val) 929 { 930 char test_str[] = "0123"; 931 uint64_t rv = test_val; 932 933 test_str[0] = (char) (unsigned char) c; /* replace first char with non-digit char */ 934 rs = mhd_str_to_uint64_n (test_str, len, &rv); 935 if (rs != 0) 936 { 937 t_failed++; 938 c_failed[c] = ! 0; 939 fprintf (stderr, 940 "FAILED: mhd_str_to_uint64_n(\"%s\", %" PRIuPTR ", ->%" 941 PRIu64 ")" 942 " returned %" PRIuPTR 943 ", while expecting zero. Locale: %s\n", 944 n_prnt (test_str), (uintptr_t) len, rv, (uintptr_t) rs, 945 get_current_locale_str ()); 946 } 947 else if (rv != test_val) 948 { 949 t_failed++; 950 c_failed[c] = ! 0; 951 fprintf (stderr, 952 "FAILED: mhd_str_to_uint64_n(\"%s\", %" PRIuPTR 953 ", &ret_val)" 954 " modified value of ret_val (before call: %" PRIu64 955 ", after call %" PRIu64 ")." 956 " Locale: %s\n", 957 n_prnt (test_str), (uintptr_t) len, test_val, rv, 958 get_current_locale_str ()); 959 } 960 } 961 } 962 if ((verbose > 1) && (j == locale_name_count - 1) && ! c_failed[c]) 963 { 964 char test_str[] = "0123"; 965 test_str[0] = (char) (unsigned char) c; /* replace first char with non-digit char */ 966 967 printf ("PASSED: mhd_str_to_uint64_n(\"%s\", 0..5, &ret_val) == 0, " 968 "value of ret_val is unmodified\n", 969 n_prnt (test_str)); 970 } 971 } 972 } 973 return t_failed; 974 } 975 976 977 static size_t 978 check_str_to_uint64_n_overflow (void) 979 { 980 size_t t_failed = 0; 981 size_t i, j; 982 int c_failed[sizeof(str_ovflw) / sizeof(str_ovflw[0])]; 983 static const size_t n_checks = sizeof(c_failed) / sizeof(c_failed[0]); 984 985 memset (c_failed, 0, sizeof(c_failed)); 986 987 for (j = 0; j < locale_name_count; j++) 988 { 989 set_test_locale (j); /* setlocale() can be slow! */ 990 for (i = 0; i < n_checks; i++) 991 { 992 size_t rs; 993 const struct str_with_len *const t = str_ovflw + i; 994 static const uint64_t rnd_val = 3; 995 size_t len; 996 997 for (len = t->len; len <= t->len + 1; len++) 998 { 999 uint64_t test_val; 1000 for (test_val = 0; test_val <= rnd_val && ! c_failed[i]; test_val += 1001 rnd_val) 1002 { 1003 uint64_t rv = test_val; 1004 1005 rs = mhd_str_to_uint64_n (t->str, len, &rv); 1006 if (rs != 0) 1007 { 1008 t_failed++; 1009 c_failed[i] = ! 0; 1010 fprintf (stderr, 1011 "FAILED: mhd_str_to_uint64_n(\"%s\", %" PRIuPTR ", ->%" 1012 PRIu64 ")" 1013 " returned %" PRIuPTR 1014 ", while expecting zero. Locale: %s\n", 1015 n_prnt (t->str), (uintptr_t) len, rv, (uintptr_t) rs, 1016 get_current_locale_str ()); 1017 } 1018 else if (rv != test_val) 1019 { 1020 t_failed++; 1021 c_failed[i] = ! 0; 1022 fprintf (stderr, 1023 "FAILED: mhd_str_to_uint64_n(\"%s\", %" PRIuPTR 1024 ", &ret_val)" 1025 " modified value of ret_val (before call: %" PRIu64 1026 ", after call %" PRIu64 ")." 1027 " Locale: %s\n", n_prnt (t->str), (uintptr_t) len, 1028 test_val, rv, 1029 get_current_locale_str ()); 1030 } 1031 } 1032 } 1033 if ((verbose > 1) && (j == locale_name_count - 1) && ! c_failed[i]) 1034 printf ("PASSED: mhd_str_to_uint64_n(\"%s\", %" PRIuPTR "..%" PRIuPTR 1035 ", &ret_val) == 0," 1036 " value of ret_val is unmodified\n", n_prnt (t->str), 1037 (uintptr_t) t->len, 1038 (uintptr_t) t->len + 1); 1039 } 1040 } 1041 return t_failed; 1042 } 1043 1044 1045 static size_t 1046 check_str_to_uint64_n_no_val (void) 1047 { 1048 size_t t_failed = 0; 1049 size_t i, j; 1050 int c_failed[sizeof(str_no_num) / sizeof(str_no_num[0])]; 1051 static const size_t n_checks = sizeof(c_failed) / sizeof(c_failed[0]); 1052 1053 memset (c_failed, 0, sizeof(c_failed)); 1054 1055 for (j = 0; j < locale_name_count; j++) 1056 { 1057 set_test_locale (j); /* setlocale() can be slow! */ 1058 for (i = 0; i < n_checks; i++) 1059 { 1060 size_t rs; 1061 const struct str_with_len *const t = str_no_num + i; 1062 static const uint64_t rnd_val = 43255654342; 1063 size_t len; 1064 1065 for (len = 0; len <= t->len + 1; len++) 1066 { 1067 uint64_t test_val; 1068 for (test_val = 0; test_val <= rnd_val && ! c_failed[i]; test_val += 1069 rnd_val) 1070 { 1071 uint64_t rv = test_val; 1072 1073 rs = mhd_str_to_uint64_n (t->str, len, &rv); 1074 if (rs != 0) 1075 { 1076 t_failed++; 1077 c_failed[i] = ! 0; 1078 fprintf (stderr, 1079 "FAILED: mhd_str_to_uint64_n(\"%s\", %" PRIuPTR ", ->%" 1080 PRIu64 ")" 1081 " returned %" PRIuPTR 1082 ", while expecting zero. Locale: %s\n", 1083 n_prnt (t->str), (uintptr_t) len, rv, (uintptr_t) rs, 1084 get_current_locale_str ()); 1085 } 1086 else if (rv != test_val) 1087 { 1088 t_failed++; 1089 c_failed[i] = ! 0; 1090 fprintf (stderr, 1091 "FAILED: mhd_str_to_uint64_n(\"%s\", %" PRIuPTR 1092 ", &ret_val)" 1093 " modified value of ret_val (before call: %" PRIu64 1094 ", after call %" PRIu64 ")." 1095 " Locale: %s\n", n_prnt (t->str), (uintptr_t) len, 1096 test_val, rv, 1097 get_current_locale_str ()); 1098 } 1099 } 1100 } 1101 if ((verbose > 1) && (j == locale_name_count - 1) && ! c_failed[i]) 1102 printf ("PASSED: mhd_str_to_uint64_n(\"%s\", 0..%" PRIuPTR 1103 ", &ret_val) == 0," 1104 " value of ret_val is unmodified\n", n_prnt (t->str), 1105 (uintptr_t) t->len + 1); 1106 } 1107 } 1108 return t_failed; 1109 } 1110 1111 1112 static size_t 1113 check_strx_to_uint32_valid (void) 1114 { 1115 size_t t_failed = 0; 1116 size_t i, j; 1117 int c_failed[sizeof(xdstrs_w_values) 1118 / sizeof(xdstrs_w_values[0])]; 1119 static const size_t n_checks = sizeof(c_failed) / sizeof(c_failed[0]); 1120 1121 memset (c_failed, 0, sizeof(c_failed)); 1122 1123 for (j = 0; j < locale_name_count; j++) 1124 { 1125 set_test_locale (j); /* setlocale() can be slow! */ 1126 for (i = 0; i < n_checks; i++) 1127 { 1128 uint_fast32_t rv; 1129 size_t rs; 1130 const struct str_with_value *const t = xdstrs_w_values + i; 1131 1132 if (t->val > UINT32_MAX) 1133 continue; /* number is too high for this function */ 1134 1135 if (c_failed[i]) 1136 continue; /* skip already failed checks */ 1137 1138 if (t->str.len < t->num_of_digt) 1139 { 1140 fprintf (stderr, 1141 "ERROR: xdstrs_w_values[%u] has wrong num_of_digt (%u): num_of_digt is expected" 1142 " to be less or equal to str.len (%u).\n", 1143 (unsigned int) i, (unsigned int) t->num_of_digt, (unsigned 1144 int) t->str. 1145 len); 1146 exit (99); 1147 } 1148 rv = 1458532; /* some random value */ 1149 rs = mhd_strx_to_uint32 (t->str.str, &rv); 1150 if (rs != t->num_of_digt) 1151 { 1152 t_failed++; 1153 c_failed[i] = ! 0; 1154 fprintf (stderr, 1155 "FAILED: mhd_strx_to_uint32(\"%s\", ->0x%" PRIX64 1156 ") returned %" 1157 PRIuPTR ", while expecting %d." 1158 " Locale: %s\n", n_prnt (t->str.str), (uint64_t) rv, 1159 (uintptr_t) rs, (int) t->num_of_digt, 1160 get_current_locale_str ()); 1161 } 1162 if (rv != t->val) 1163 { 1164 t_failed++; 1165 c_failed[i] = ! 0; 1166 fprintf (stderr, 1167 "FAILED: mhd_strx_to_uint32(\"%s\", ->0x%" PRIX64 1168 ") converted string to value 0x%" 1169 PRIX64 "," 1170 " while expecting result 0x%" PRIX64 ". Locale: %s\n", 1171 n_prnt (t->str.str), (uint64_t) rv, (uint64_t) rv, 1172 t->val, get_current_locale_str ()); 1173 } 1174 if ((verbose > 1) && (j == locale_name_count - 1) && ! c_failed[i]) 1175 printf ("PASSED: mhd_strx_to_uint32(\"%s\", ->0x%" PRIX64 ") == %" 1176 PRIuPTR "\n", 1177 n_prnt (t->str.str), (uint64_t) rv, rs); 1178 } 1179 } 1180 return t_failed; 1181 } 1182 1183 1184 static size_t 1185 check_strx_to_uint32_all_chars (void) 1186 { 1187 int c_failed[256]; /* from 0 to 255 */ 1188 static const size_t n_checks = sizeof(c_failed) / sizeof(c_failed[0]); 1189 size_t t_failed = 0; 1190 size_t j; 1191 1192 memset (c_failed, 0, sizeof(c_failed)); 1193 1194 for (j = 0; j < locale_name_count; j++) 1195 { 1196 unsigned int c; 1197 uint_fast32_t test_val; 1198 1199 set_test_locale (j); /* setlocale() can be slow! */ 1200 for (c = 0; c < n_checks; c++) 1201 { 1202 static const uint_fast32_t rnd_val = 234234; 1203 size_t rs; 1204 if (( (c >= '0') && (c <= '9') ) 1205 || ( (c >= 'A') && (c <= 'F') ) 1206 || ( (c >= 'a') && (c <= 'f') )) 1207 continue; /* skip xdigits */ 1208 for (test_val = 0; test_val <= rnd_val && ! c_failed[c]; test_val += 1209 rnd_val) 1210 { 1211 char test_str[] = "0123"; 1212 uint_fast32_t rv = test_val; 1213 1214 test_str[0] = (char) (unsigned char) c; /* replace first char with non-digit char */ 1215 rs = mhd_strx_to_uint32 (test_str, &rv); 1216 if (rs != 0) 1217 { 1218 t_failed++; 1219 c_failed[c] = ! 0; 1220 fprintf (stderr, 1221 "FAILED: mhd_strx_to_uint32(\"%s\", ->0x%" PRIX64 1222 ") returned %" 1223 PRIuPTR ", while expecting zero." 1224 " Locale: %s\n", n_prnt (test_str), (uint64_t) rv, 1225 (uintptr_t) rs, get_current_locale_str ()); 1226 } 1227 else if (rv != test_val) 1228 { 1229 t_failed++; 1230 c_failed[c] = ! 0; 1231 fprintf (stderr, 1232 "FAILED: mhd_strx_to_uint32(\"%s\", &ret_val) modified value of ret_val" 1233 " (before call: 0x%" PRIX64 ", after call 0x%" PRIX64 1234 "). Locale: %s\n", 1235 n_prnt (test_str), (uint64_t) test_val, (uint64_t) rv, 1236 get_current_locale_str ()); 1237 } 1238 } 1239 if ((verbose > 1) && (j == locale_name_count - 1) && ! c_failed[c]) 1240 { 1241 char test_str[] = "0123"; 1242 test_str[0] = (char) (unsigned char) c; /* replace first char with non-digit char */ 1243 1244 printf ("PASSED: mhd_strx_to_uint32(\"%s\", &ret_val) == 0, " 1245 "value of ret_val is unmodified\n", 1246 n_prnt (test_str)); 1247 } 1248 } 1249 } 1250 return t_failed; 1251 } 1252 1253 1254 static size_t 1255 check_strx_to_uint32_overflow (void) 1256 { 1257 size_t t_failed = 0; 1258 size_t i, j; 1259 static const size_t n_checks1 = sizeof(strx_ovflw) / sizeof(strx_ovflw[0]); 1260 int c_failed[(sizeof(strx_ovflw) / sizeof(strx_ovflw[0])) 1261 + (sizeof(xdstrs_w_values) 1262 / sizeof(xdstrs_w_values[0]))]; 1263 static const size_t n_checks = sizeof(c_failed) / sizeof(c_failed[0]); 1264 1265 memset (c_failed, 0, sizeof(c_failed)); 1266 1267 for (j = 0; j < locale_name_count; j++) 1268 { 1269 set_test_locale (j); /* setlocale() can be slow! */ 1270 for (i = 0; i < n_checks; i++) 1271 { 1272 size_t rs; 1273 static const uint_fast32_t rnd_val = 74218431; 1274 uint_fast32_t test_val; 1275 const char *str; 1276 if (i < n_checks1) 1277 { 1278 const struct str_with_len *const t = strx_ovflw + i; 1279 str = t->str; 1280 } 1281 else 1282 { 1283 const struct str_with_value *const t = xdstrs_w_values + (i 1284 - n_checks1); 1285 if (t->val <= UINT32_MAX) 1286 continue; /* check only strings that should overflow uint32_t */ 1287 str = t->str.str; 1288 } 1289 1290 1291 for (test_val = 0; test_val <= rnd_val && ! c_failed[i]; test_val += 1292 rnd_val) 1293 { 1294 uint_fast32_t rv = test_val; 1295 1296 rs = mhd_strx_to_uint32 (str, &rv); 1297 if (rs != 0) 1298 { 1299 t_failed++; 1300 c_failed[i] = ! 0; 1301 fprintf (stderr, 1302 "FAILED: mhd_strx_to_uint32(\"%s\", ->0x%" PRIX64 1303 ") returned %" 1304 PRIuPTR ", while expecting zero." 1305 " Locale: %s\n", n_prnt (str), (uint64_t) rv, (uintptr_t) rs, 1306 get_current_locale_str ()); 1307 } 1308 else if (rv != test_val) 1309 { 1310 t_failed++; 1311 c_failed[i] = ! 0; 1312 fprintf (stderr, 1313 "FAILED: mhd_strx_to_uint32(\"%s\", &ret_val) modified value of ret_val" 1314 " (before call: 0x%" PRIX64 ", after call 0x%" PRIX64 1315 "). Locale: %s\n", 1316 n_prnt (str), (uint64_t) test_val, (uint64_t) rv, 1317 get_current_locale_str ()); 1318 } 1319 } 1320 if ((verbose > 1) && (j == locale_name_count - 1) && ! c_failed[i]) 1321 printf ("PASSED: mhd_strx_to_uint32(\"%s\", &ret_val) == 0, " 1322 "value of ret_val is unmodified\n", 1323 n_prnt (str)); 1324 } 1325 } 1326 return t_failed; 1327 } 1328 1329 1330 static size_t 1331 check_strx_to_uint32_no_val (void) 1332 { 1333 size_t t_failed = 0; 1334 size_t i, j; 1335 int c_failed[sizeof(str_no_num) / sizeof(str_no_num[0])]; 1336 static const size_t n_checks = sizeof(c_failed) / sizeof(c_failed[0]); 1337 1338 memset (c_failed, 0, sizeof(c_failed)); 1339 1340 for (j = 0; j < locale_name_count; j++) 1341 { 1342 set_test_locale (j); /* setlocale() can be slow! */ 1343 for (i = 0; i < n_checks; i++) 1344 { 1345 size_t rs; 1346 const struct str_with_len *const t = str_no_num + i; 1347 static const uint_fast32_t rnd_val = 74218431; 1348 uint_fast32_t test_val; 1349 1350 for (test_val = 0; test_val <= rnd_val && ! c_failed[i]; test_val += 1351 rnd_val) 1352 { 1353 uint_fast32_t rv = test_val; 1354 1355 rs = mhd_strx_to_uint32 (t->str, &rv); 1356 if (rs != 0) 1357 { 1358 t_failed++; 1359 c_failed[i] = ! 0; 1360 fprintf (stderr, 1361 "FAILED: mhd_strx_to_uint32(\"%s\", ->0x%" PRIX64 1362 ") returned %" 1363 PRIuPTR ", while expecting zero." 1364 " Locale: %s\n", n_prnt (t->str), (uint64_t) rv, 1365 (uintptr_t) rs, get_current_locale_str ()); 1366 } 1367 else if (rv != test_val) 1368 { 1369 t_failed++; 1370 c_failed[i] = ! 0; 1371 fprintf (stderr, 1372 "FAILED: mhd_strx_to_uint32(\"%s\", &ret_val) modified value of ret_val" 1373 " (before call: 0x%" PRIX64 ", after call 0x%" PRIX64 1374 "). Locale: %s\n", 1375 n_prnt (t->str), (uint64_t) test_val, (uint64_t) rv, 1376 get_current_locale_str ()); 1377 } 1378 } 1379 if ((verbose > 1) && (j == locale_name_count - 1) && ! c_failed[i]) 1380 printf ("PASSED: mhd_strx_to_uint32(\"%s\", &ret_val) == 0, " 1381 "value of ret_val is unmodified\n", 1382 n_prnt (t->str)); 1383 } 1384 } 1385 return t_failed; 1386 } 1387 1388 1389 static size_t 1390 check_strx_to_uint32_n_valid (void) 1391 { 1392 size_t t_failed = 0; 1393 size_t i, j; 1394 int c_failed[sizeof(xdstrs_w_values) 1395 / sizeof(xdstrs_w_values[0])]; 1396 static const size_t n_checks = sizeof(c_failed) / sizeof(c_failed[0]); 1397 1398 memset (c_failed, 0, sizeof(c_failed)); 1399 1400 for (j = 0; j < locale_name_count; j++) 1401 { 1402 set_test_locale (j); /* setlocale() can be slow! */ 1403 for (i = 0; i < n_checks; i++) 1404 { 1405 uint_fast32_t rv = 2352932; /* some random value */ 1406 size_t rs = 0; 1407 size_t len; 1408 const struct str_with_value *const t = xdstrs_w_values + i; 1409 1410 if (t->val > UINT32_MAX) 1411 continue; /* number is too high for this function */ 1412 1413 if (t->str.len < t->num_of_digt) 1414 { 1415 fprintf (stderr, 1416 "ERROR: xdstrs_w_values[%u] has wrong num_of_digt (%u): num_of_digt is expected" 1417 " to be less or equal to str.len (%u).\n", 1418 (unsigned int) i, (unsigned int) t->num_of_digt, (unsigned 1419 int) t->str. 1420 len); 1421 exit (99); 1422 } 1423 for (len = t->num_of_digt; len <= t->str.len + 1 && ! c_failed[i]; len++) 1424 { 1425 rs = mhd_strx_to_uint32_n (t->str.str, len, &rv); 1426 if (rs != t->num_of_digt) 1427 { 1428 t_failed++; 1429 c_failed[i] = ! 0; 1430 fprintf (stderr, 1431 "FAILED: mhd_strx_to_uint32_n(\"%s\", %" PRIuPTR ", ->0x%" 1432 PRIX64 ")" 1433 " returned %" PRIuPTR ", while expecting %d. Locale: %s\n", 1434 n_prnt (t->str.str), (uintptr_t) len, (uint64_t) rv, 1435 (uintptr_t) rs, 1436 (int) t->num_of_digt, get_current_locale_str ()); 1437 } 1438 if (rv != t->val) 1439 { 1440 t_failed++; 1441 c_failed[i] = ! 0; 1442 fprintf (stderr, 1443 "FAILED: mhd_strx_to_uint32_n(\"%s\", %" PRIuPTR ", ->0x%" 1444 PRIX64 ")" 1445 " converted string to value 0x%" PRIX64 1446 ", while expecting result 0x%" PRIX64 1447 ". Locale: %s\n", n_prnt (t->str.str), (uintptr_t) len, 1448 (uint64_t) rv, (uint64_t) rv, 1449 t->val, get_current_locale_str ()); 1450 } 1451 } 1452 if ((verbose > 1) && (j == locale_name_count - 1) && ! c_failed[i]) 1453 printf ( 1454 "PASSED: mhd_strx_to_uint32_n(\"%s\", %" PRIuPTR "..%" PRIuPTR 1455 ", ->0x%" 1456 PRIX64 ")" 1457 " == %" PRIuPTR "\n", n_prnt (t->str.str), 1458 (uintptr_t) t->num_of_digt, 1459 (uintptr_t) t->str.len + 1, (uint64_t) rv, rs); 1460 } 1461 } 1462 return t_failed; 1463 } 1464 1465 1466 static size_t 1467 check_strx_to_uint32_n_all_chars (void) 1468 { 1469 int c_failed[256]; /* from 0 to 255 */ 1470 static const size_t n_checks = sizeof(c_failed) / sizeof(c_failed[0]); 1471 size_t t_failed = 0; 1472 size_t j; 1473 1474 memset (c_failed, 0, sizeof(c_failed)); 1475 1476 for (j = 0; j < locale_name_count; j++) 1477 { 1478 unsigned int c; 1479 uint_fast32_t test_val; 1480 1481 set_test_locale (j); /* setlocale() can be slow! */ 1482 for (c = 0; c < n_checks; c++) 1483 { 1484 static const uint_fast32_t rnd_val = 98372558; 1485 size_t rs; 1486 size_t len; 1487 1488 if (( (c >= '0') && (c <= '9') ) 1489 || ( (c >= 'A') && (c <= 'F') ) 1490 || ( (c >= 'a') && (c <= 'f') )) 1491 continue; /* skip xdigits */ 1492 1493 for (len = 0; len <= 5; len++) 1494 { 1495 for (test_val = 0; test_val <= rnd_val && ! c_failed[c]; test_val += 1496 rnd_val) 1497 { 1498 char test_str[] = "0123"; 1499 uint_fast32_t rv = test_val; 1500 1501 test_str[0] = (char) (unsigned char) c; /* replace first char with non-digit char */ 1502 rs = mhd_strx_to_uint32_n (test_str, len, &rv); 1503 if (rs != 0) 1504 { 1505 t_failed++; 1506 c_failed[c] = ! 0; 1507 fprintf (stderr, 1508 "FAILED: mhd_strx_to_uint32_n(\"%s\", %" PRIuPTR ", ->0x%" 1509 PRIX64 1510 ")" 1511 " returned %" PRIuPTR 1512 ", while expecting zero. Locale: %s\n", 1513 n_prnt (test_str), (uintptr_t) len, (uint64_t) rv, 1514 (uintptr_t) rs, get_current_locale_str ()); 1515 } 1516 else if (rv != test_val) 1517 { 1518 t_failed++; 1519 c_failed[c] = ! 0; 1520 fprintf (stderr, 1521 "FAILED: mhd_strx_to_uint32_n(\"%s\", %" PRIuPTR 1522 ", &ret_val)" 1523 " modified value of ret_val (before call: 0x%" PRIX64 1524 ", after call 0x%" PRIX64 ")." 1525 " Locale: %s\n", 1526 n_prnt (test_str), (uintptr_t) len, (uint64_t) test_val, 1527 (uint64_t) rv, get_current_locale_str ()); 1528 } 1529 } 1530 } 1531 if ((verbose > 1) && (j == locale_name_count - 1) && ! c_failed[c]) 1532 { 1533 char test_str[] = "0123"; 1534 test_str[0] = (char) (unsigned char) c; /* replace first char with non-digit char */ 1535 1536 printf ("PASSED: mhd_strx_to_uint32_n(\"%s\", 0..5, &ret_val) == 0, " 1537 "value of ret_val is unmodified\n", 1538 n_prnt (test_str)); 1539 } 1540 } 1541 } 1542 return t_failed; 1543 } 1544 1545 1546 static size_t 1547 check_strx_to_uint32_n_overflow (void) 1548 { 1549 size_t t_failed = 0; 1550 size_t i, j; 1551 static const size_t n_checks1 = sizeof(strx_ovflw) / sizeof(strx_ovflw[0]); 1552 int c_failed[(sizeof(strx_ovflw) / sizeof(strx_ovflw[0])) 1553 + (sizeof(xdstrs_w_values) 1554 / sizeof(xdstrs_w_values[0]))]; 1555 static const size_t n_checks = sizeof(c_failed) / sizeof(c_failed[0]); 1556 1557 memset (c_failed, 0, sizeof(c_failed)); 1558 1559 for (j = 0; j < locale_name_count; j++) 1560 { 1561 set_test_locale (j); /* setlocale() can be slow! */ 1562 for (i = 0; i < n_checks; i++) 1563 { 1564 size_t rs; 1565 static const uint_fast32_t rnd_val = 4; 1566 size_t len; 1567 const char *str; 1568 size_t min_len, max_len; 1569 if (i < n_checks1) 1570 { 1571 const struct str_with_len *const t = strx_ovflw + i; 1572 str = t->str; 1573 min_len = t->len; 1574 max_len = t->len + 1; 1575 } 1576 else 1577 { 1578 const struct str_with_value *const t = xdstrs_w_values + (i 1579 - n_checks1); 1580 if (t->val <= UINT32_MAX) 1581 continue; /* check only strings that should overflow uint32_t */ 1582 1583 if (t->str.len < t->num_of_digt) 1584 { 1585 fprintf (stderr, 1586 "ERROR: xdstrs_w_values[%u] has wrong num_of_digt (%u): num_of_digt is expected" 1587 " to be less or equal to str.len (%u).\n", 1588 (unsigned int) (i - n_checks1), (unsigned 1589 int) t->num_of_digt, 1590 (unsigned int) t->str.len); 1591 exit (99); 1592 } 1593 str = t->str.str; 1594 min_len = t->num_of_digt; 1595 max_len = t->str.len + 1; 1596 } 1597 1598 for (len = min_len; len <= max_len; len++) 1599 { 1600 uint_fast32_t test_val; 1601 for (test_val = 0; test_val <= rnd_val && ! c_failed[i]; test_val += 1602 rnd_val) 1603 { 1604 uint_fast32_t rv = test_val; 1605 1606 rs = mhd_strx_to_uint32_n (str, len, &rv); 1607 if (rs != 0) 1608 { 1609 t_failed++; 1610 c_failed[i] = ! 0; 1611 fprintf (stderr, 1612 "FAILED: mhd_strx_to_uint32_n(\"%s\", %" PRIuPTR ", ->0x%" 1613 PRIX64 1614 ")" 1615 " returned %" PRIuPTR 1616 ", while expecting zero. Locale: %s\n", 1617 n_prnt (str), (uintptr_t) len, (uint64_t) rv, 1618 (uintptr_t) rs, get_current_locale_str ()); 1619 } 1620 else if (rv != test_val) 1621 { 1622 t_failed++; 1623 c_failed[i] = ! 0; 1624 fprintf (stderr, 1625 "FAILED: mhd_strx_to_uint32_n(\"%s\", %" PRIuPTR 1626 ", &ret_val)" 1627 " modified value of ret_val (before call: 0x%" PRIX64 1628 ", after call 0x%" PRIX64 ")." 1629 " Locale: %s\n", n_prnt (str), (uintptr_t) len, 1630 (uint64_t) test_val, (uint64_t) rv, 1631 get_current_locale_str ()); 1632 } 1633 } 1634 } 1635 if ((verbose > 1) && (j == locale_name_count - 1) && ! c_failed[i]) 1636 printf ("PASSED: mhd_strx_to_uint32_n(\"%s\", %" PRIuPTR "..%" PRIuPTR 1637 ", &ret_val) == 0," 1638 " value of ret_val is unmodified\n", n_prnt (str), 1639 (uintptr_t) min_len, 1640 (uintptr_t) max_len); 1641 } 1642 } 1643 return t_failed; 1644 } 1645 1646 1647 static size_t 1648 check_strx_to_uint32_n_no_val (void) 1649 { 1650 size_t t_failed = 0; 1651 size_t i, j; 1652 int c_failed[sizeof(str_no_num) / sizeof(str_no_num[0])]; 1653 static const size_t n_checks = sizeof(c_failed) / sizeof(c_failed[0]); 1654 1655 memset (c_failed, 0, sizeof(c_failed)); 1656 1657 for (j = 0; j < locale_name_count; j++) 1658 { 1659 set_test_locale (j); /* setlocale() can be slow! */ 1660 for (i = 0; i < n_checks; i++) 1661 { 1662 size_t rs; 1663 const struct str_with_len *const t = str_no_num + i; 1664 static const uint_fast32_t rnd_val = 3214314212UL; 1665 size_t len; 1666 1667 for (len = 0; len <= t->len + 1; len++) 1668 { 1669 uint_fast32_t test_val; 1670 for (test_val = 0; test_val <= rnd_val && ! c_failed[i]; test_val += 1671 rnd_val) 1672 { 1673 uint_fast32_t rv = test_val; 1674 1675 rs = mhd_strx_to_uint32_n (t->str, len, &rv); 1676 if (rs != 0) 1677 { 1678 t_failed++; 1679 c_failed[i] = ! 0; 1680 fprintf (stderr, 1681 "FAILED: mhd_strx_to_uint32_n(\"%s\", %" PRIuPTR ", ->0x%" 1682 PRIX64 1683 ")" 1684 " returned %" PRIuPTR 1685 ", while expecting zero. Locale: %s\n", 1686 n_prnt (t->str), (uintptr_t) len, (uint64_t) rv, 1687 (uintptr_t) rs, get_current_locale_str ()); 1688 } 1689 else if (rv != test_val) 1690 { 1691 t_failed++; 1692 c_failed[i] = ! 0; 1693 fprintf (stderr, 1694 "FAILED: mhd_strx_to_uint32_n(\"%s\", %" PRIuPTR 1695 ", &ret_val)" 1696 " modified value of ret_val (before call: 0x%" PRIX64 1697 ", after call 0x%" PRIX64 ")." 1698 " Locale: %s\n", n_prnt (t->str), (uintptr_t) len, 1699 (uint64_t) test_val, (uint64_t) rv, 1700 get_current_locale_str ()); 1701 } 1702 } 1703 } 1704 if ((verbose > 1) && (j == locale_name_count - 1) && ! c_failed[i]) 1705 printf ("PASSED: mhd_strx_to_uint32_n(\"%s\", 0..%" PRIuPTR 1706 ", &ret_val) == 0," 1707 " value of ret_val is unmodified\n", n_prnt (t->str), 1708 (uintptr_t) t->len + 1); 1709 } 1710 } 1711 return t_failed; 1712 } 1713 1714 1715 static size_t 1716 check_strx_to_uint64_valid (void) 1717 { 1718 size_t t_failed = 0; 1719 size_t i, j; 1720 int c_failed[sizeof(xdstrs_w_values) 1721 / sizeof(xdstrs_w_values[0])]; 1722 static const size_t n_checks = sizeof(c_failed) / sizeof(c_failed[0]); 1723 1724 memset (c_failed, 0, sizeof(c_failed)); 1725 1726 for (j = 0; j < locale_name_count; j++) 1727 { 1728 set_test_locale (j); /* setlocale() can be slow! */ 1729 for (i = 0; i < n_checks; i++) 1730 { 1731 uint64_t rv; 1732 size_t rs; 1733 const struct str_with_value *const t = xdstrs_w_values + i; 1734 1735 if (c_failed[i]) 1736 continue; /* skip already failed checks */ 1737 1738 if (t->str.len < t->num_of_digt) 1739 { 1740 fprintf (stderr, 1741 "ERROR: xdstrs_w_values[%u] has wrong num_of_digt (%u): num_of_digt is expected" 1742 " to be less or equal to str.len (%u).\n", 1743 (unsigned int) i, (unsigned int) t->num_of_digt, (unsigned 1744 int) t->str. 1745 len); 1746 exit (99); 1747 } 1748 rv = 1458532; /* some random value */ 1749 rs = mhd_strx_to_uint64 (t->str.str, &rv); 1750 if (rs != t->num_of_digt) 1751 { 1752 t_failed++; 1753 c_failed[i] = ! 0; 1754 fprintf (stderr, 1755 "FAILED: mhd_strx_to_uint64(\"%s\", ->0x%" PRIX64 1756 ") returned %" 1757 PRIuPTR ", while expecting %d." 1758 " Locale: %s\n", n_prnt (t->str.str), rv, (uintptr_t) rs, 1759 (int) t->num_of_digt, get_current_locale_str ()); 1760 } 1761 if (rv != t->val) 1762 { 1763 t_failed++; 1764 c_failed[i] = ! 0; 1765 fprintf (stderr, 1766 "FAILED: mhd_strx_to_uint64(\"%s\", ->0x%" PRIX64 1767 ") converted string to value 0x%" 1768 PRIX64 "," 1769 " while expecting result 0x%" PRIX64 ". Locale: %s\n", 1770 n_prnt (t->str.str), rv, rv, 1771 t->val, get_current_locale_str ()); 1772 } 1773 if ((verbose > 1) && (j == locale_name_count - 1) && ! c_failed[i]) 1774 printf ("PASSED: mhd_strx_to_uint64(\"%s\", ->0x%" PRIX64 ") == %" 1775 PRIuPTR "\n", 1776 n_prnt (t->str.str), rv, rs); 1777 } 1778 } 1779 return t_failed; 1780 } 1781 1782 1783 static size_t 1784 check_strx_to_uint64_all_chars (void) 1785 { 1786 int c_failed[256]; /* from 0 to 255 */ 1787 static const size_t n_checks = sizeof(c_failed) / sizeof(c_failed[0]); 1788 size_t t_failed = 0; 1789 size_t j; 1790 1791 memset (c_failed, 0, sizeof(c_failed)); 1792 1793 for (j = 0; j < locale_name_count; j++) 1794 { 1795 unsigned int c; 1796 uint64_t test_val; 1797 1798 set_test_locale (j); /* setlocale() can be slow! */ 1799 for (c = 0; c < n_checks; c++) 1800 { 1801 static const uint64_t rnd_val = 234234; 1802 size_t rs; 1803 if (( (c >= '0') && (c <= '9') ) 1804 || ( (c >= 'A') && (c <= 'F') ) 1805 || ( (c >= 'a') && (c <= 'f') )) 1806 continue; /* skip xdigits */ 1807 for (test_val = 0; test_val <= rnd_val && ! c_failed[c]; test_val += 1808 rnd_val) 1809 { 1810 char test_str[] = "0123"; 1811 uint64_t rv = test_val; 1812 1813 test_str[0] = (char) (unsigned char) c; /* replace first char with non-digit char */ 1814 rs = mhd_strx_to_uint64 (test_str, &rv); 1815 if (rs != 0) 1816 { 1817 t_failed++; 1818 c_failed[c] = ! 0; 1819 fprintf (stderr, 1820 "FAILED: mhd_strx_to_uint64(\"%s\", ->0x%" PRIX64 1821 ") returned %" 1822 PRIuPTR ", while expecting zero." 1823 " Locale: %s\n", n_prnt (test_str), rv, (uintptr_t) rs, 1824 get_current_locale_str ()); 1825 } 1826 else if (rv != test_val) 1827 { 1828 t_failed++; 1829 c_failed[c] = ! 0; 1830 fprintf (stderr, 1831 "FAILED: mhd_strx_to_uint64(\"%s\", &ret_val) modified value of ret_val" 1832 " (before call: 0x%" PRIX64 ", after call 0x%" PRIX64 1833 "). Locale: %s\n", 1834 n_prnt (test_str), test_val, rv, get_current_locale_str ()); 1835 } 1836 } 1837 if ((verbose > 1) && (j == locale_name_count - 1) && ! c_failed[c]) 1838 { 1839 char test_str[] = "0123"; 1840 test_str[0] = (char) (unsigned char) c; /* replace first char with non-digit char */ 1841 1842 printf ("PASSED: mhd_strx_to_uint64(\"%s\", &ret_val) == 0, " 1843 "value of ret_val is unmodified\n", 1844 n_prnt (test_str)); 1845 } 1846 } 1847 } 1848 return t_failed; 1849 } 1850 1851 1852 static size_t 1853 check_strx_to_uint64_overflow (void) 1854 { 1855 size_t t_failed = 0; 1856 size_t i, j; 1857 int c_failed[sizeof(strx_ovflw) / sizeof(strx_ovflw[0])]; 1858 static const size_t n_checks = sizeof(c_failed) / sizeof(c_failed[0]); 1859 1860 memset (c_failed, 0, sizeof(c_failed)); 1861 1862 for (j = 0; j < locale_name_count; j++) 1863 { 1864 set_test_locale (j); /* setlocale() can be slow! */ 1865 for (i = 0; i < n_checks; i++) 1866 { 1867 size_t rs; 1868 const struct str_with_len *const t = strx_ovflw + i; 1869 static const uint64_t rnd_val = 74218431; 1870 uint64_t test_val; 1871 1872 for (test_val = 0; test_val <= rnd_val && ! c_failed[i]; test_val += 1873 rnd_val) 1874 { 1875 uint64_t rv = test_val; 1876 1877 rs = mhd_strx_to_uint64 (t->str, &rv); 1878 if (rs != 0) 1879 { 1880 t_failed++; 1881 c_failed[i] = ! 0; 1882 fprintf (stderr, 1883 "FAILED: mhd_strx_to_uint64(\"%s\", ->0x%" PRIX64 1884 ") returned %" 1885 PRIuPTR ", while expecting zero." 1886 " Locale: %s\n", n_prnt (t->str), rv, (uintptr_t) rs, 1887 get_current_locale_str ()); 1888 } 1889 else if (rv != test_val) 1890 { 1891 t_failed++; 1892 c_failed[i] = ! 0; 1893 fprintf (stderr, 1894 "FAILED: mhd_strx_to_uint64(\"%s\", &ret_val) modified value of ret_val" 1895 " (before call: 0x%" PRIX64 ", after call 0x%" PRIX64 1896 "). Locale: %s\n", 1897 n_prnt (t->str), test_val, rv, get_current_locale_str ()); 1898 } 1899 } 1900 if ((verbose > 1) && (j == locale_name_count - 1) && ! c_failed[i]) 1901 printf ("PASSED: mhd_strx_to_uint64(\"%s\", &ret_val) == 0, " 1902 "value of ret_val is unmodified\n", 1903 n_prnt (t->str)); 1904 } 1905 } 1906 return t_failed; 1907 } 1908 1909 1910 static size_t 1911 check_strx_to_uint64_no_val (void) 1912 { 1913 size_t t_failed = 0; 1914 size_t i, j; 1915 int c_failed[sizeof(str_no_num) / sizeof(str_no_num[0])]; 1916 static const size_t n_checks = sizeof(c_failed) / sizeof(c_failed[0]); 1917 1918 memset (c_failed, 0, sizeof(c_failed)); 1919 1920 for (j = 0; j < locale_name_count; j++) 1921 { 1922 set_test_locale (j); /* setlocale() can be slow! */ 1923 for (i = 0; i < n_checks; i++) 1924 { 1925 size_t rs; 1926 const struct str_with_len *const t = str_no_num + i; 1927 static const uint64_t rnd_val = 74218431; 1928 uint64_t test_val; 1929 1930 for (test_val = 0; test_val <= rnd_val && ! c_failed[i]; test_val += 1931 rnd_val) 1932 { 1933 uint64_t rv = test_val; 1934 1935 rs = mhd_strx_to_uint64 (t->str, &rv); 1936 if (rs != 0) 1937 { 1938 t_failed++; 1939 c_failed[i] = ! 0; 1940 fprintf (stderr, 1941 "FAILED: mhd_strx_to_uint64(\"%s\", ->0x%" PRIX64 1942 ") returned %" 1943 PRIuPTR ", while expecting zero." 1944 " Locale: %s\n", n_prnt (t->str), rv, (uintptr_t) rs, 1945 get_current_locale_str ()); 1946 } 1947 else if (rv != test_val) 1948 { 1949 t_failed++; 1950 c_failed[i] = ! 0; 1951 fprintf (stderr, 1952 "FAILED: mhd_strx_to_uint64(\"%s\", &ret_val) modified value of ret_val" 1953 " (before call: 0x%" PRIX64 ", after call 0x%" PRIX64 1954 "). Locale: %s\n", 1955 n_prnt (t->str), test_val, rv, get_current_locale_str ()); 1956 } 1957 } 1958 if ((verbose > 1) && (j == locale_name_count - 1) && ! c_failed[i]) 1959 printf ("PASSED: mhd_strx_to_uint64(\"%s\", &ret_val) == 0, " 1960 "value of ret_val is unmodified\n", 1961 n_prnt (t->str)); 1962 } 1963 } 1964 return t_failed; 1965 } 1966 1967 1968 static size_t 1969 check_strx_to_uint64_n_valid (void) 1970 { 1971 size_t t_failed = 0; 1972 size_t i, j; 1973 int c_failed[sizeof(xdstrs_w_values) / sizeof(xdstrs_w_values[0])]; 1974 static const size_t n_checks = sizeof(c_failed) / sizeof(c_failed[0]); 1975 1976 memset (c_failed, 0, sizeof(c_failed)); 1977 1978 for (j = 0; j < locale_name_count; j++) 1979 { 1980 set_test_locale (j); /* setlocale() can be slow! */ 1981 for (i = 0; i < n_checks; i++) 1982 { 1983 uint64_t rv = 2352932; /* some random value */ 1984 size_t rs = 0; 1985 size_t len; 1986 const struct str_with_value *const t = xdstrs_w_values + i; 1987 1988 if (t->str.len < t->num_of_digt) 1989 { 1990 fprintf (stderr, 1991 "ERROR: xdstrs_w_values[%u] has wrong num_of_digt (%u): num_of_digt is expected" 1992 " to be less or equal to str.len (%u).\n", 1993 (unsigned int) i, (unsigned int) t->num_of_digt, (unsigned 1994 int) t->str. 1995 len); 1996 exit (99); 1997 } 1998 for (len = t->num_of_digt; len <= t->str.len + 1 && ! c_failed[i]; len++) 1999 { 2000 rs = mhd_strx_to_uint64_n (t->str.str, len, &rv); 2001 if (rs != t->num_of_digt) 2002 { 2003 t_failed++; 2004 c_failed[i] = ! 0; 2005 fprintf (stderr, 2006 "FAILED: mhd_strx_to_uint64_n(\"%s\", %" PRIuPTR ", ->0x%" 2007 PRIX64 ")" 2008 " returned %" PRIuPTR ", while expecting %d. Locale: %s\n", 2009 n_prnt (t->str.str), (uintptr_t) len, rv, (uintptr_t) rs, 2010 (int) t->num_of_digt, get_current_locale_str ()); 2011 } 2012 if (rv != t->val) 2013 { 2014 t_failed++; 2015 c_failed[i] = ! 0; 2016 fprintf (stderr, 2017 "FAILED: mhd_strx_to_uint64_n(\"%s\", %" PRIuPTR ", ->0x%" 2018 PRIX64 ")" 2019 " converted string to value 0x%" PRIX64 2020 ", while expecting result 0x%" PRIX64 2021 ". Locale: %s\n", n_prnt (t->str.str), (uintptr_t) len, rv, 2022 rv, 2023 t->val, get_current_locale_str ()); 2024 } 2025 } 2026 if ((verbose > 1) && (j == locale_name_count - 1) && ! c_failed[i]) 2027 printf ("PASSED: mhd_strx_to_uint64_n(\"%s\", %" PRIuPTR "..%" PRIuPTR 2028 ", ->0x%" 2029 PRIX64 ")" 2030 " == %" PRIuPTR "\n", n_prnt (t->str.str), 2031 (uintptr_t) t->num_of_digt, 2032 (uintptr_t) t->str.len + 1, rv, rs); 2033 } 2034 } 2035 return t_failed; 2036 } 2037 2038 2039 static size_t 2040 check_strx_to_uint64_n_all_chars (void) 2041 { 2042 int c_failed[256]; /* from 0 to 255 */ 2043 static const size_t n_checks = sizeof(c_failed) / sizeof(c_failed[0]); 2044 size_t t_failed = 0; 2045 size_t j; 2046 2047 memset (c_failed, 0, sizeof(c_failed)); 2048 2049 for (j = 0; j < locale_name_count; j++) 2050 { 2051 unsigned int c; 2052 uint64_t test_val; 2053 2054 set_test_locale (j); /* setlocale() can be slow! */ 2055 for (c = 0; c < n_checks; c++) 2056 { 2057 static const uint64_t rnd_val = 98372558; 2058 size_t rs; 2059 size_t len; 2060 2061 if (( (c >= '0') && (c <= '9') ) 2062 || ( (c >= 'A') && (c <= 'F') ) 2063 || ( (c >= 'a') && (c <= 'f') )) 2064 continue; /* skip xdigits */ 2065 2066 for (len = 0; len <= 5; len++) 2067 { 2068 for (test_val = 0; test_val <= rnd_val && ! c_failed[c]; test_val += 2069 rnd_val) 2070 { 2071 char test_str[] = "0123"; 2072 uint64_t rv = test_val; 2073 2074 test_str[0] = (char) (unsigned char) c; /* replace first char with non-digit char */ 2075 rs = mhd_strx_to_uint64_n (test_str, len, &rv); 2076 if (rs != 0) 2077 { 2078 t_failed++; 2079 c_failed[c] = ! 0; 2080 fprintf (stderr, 2081 "FAILED: mhd_strx_to_uint64_n(\"%s\", %" PRIuPTR ", ->0x%" 2082 PRIX64 2083 ")" 2084 " returned %" PRIuPTR 2085 ", while expecting zero. Locale: %s\n", 2086 n_prnt (test_str), (uintptr_t) len, rv, (uintptr_t) rs, 2087 get_current_locale_str ()); 2088 } 2089 else if (rv != test_val) 2090 { 2091 t_failed++; 2092 c_failed[c] = ! 0; 2093 fprintf (stderr, 2094 "FAILED: mhd_strx_to_uint64_n(\"%s\", %" PRIuPTR 2095 ", &ret_val)" 2096 " modified value of ret_val (before call: 0x%" PRIX64 2097 ", after call 0x%" PRIX64 ")." 2098 " Locale: %s\n", 2099 n_prnt (test_str), (uintptr_t) len, test_val, rv, 2100 get_current_locale_str ()); 2101 } 2102 } 2103 } 2104 if ((verbose > 1) && (j == locale_name_count - 1) && ! c_failed[c]) 2105 { 2106 char test_str[] = "0123"; 2107 test_str[0] = (char) (unsigned char) c; /* replace first char with non-digit char */ 2108 2109 printf ("PASSED: mhd_strx_to_uint64_n(\"%s\", 0..5, &ret_val) == 0, " 2110 "value of ret_val is unmodified\n", 2111 n_prnt (test_str)); 2112 } 2113 } 2114 } 2115 return t_failed; 2116 } 2117 2118 2119 static size_t 2120 check_strx_to_uint64_n_overflow (void) 2121 { 2122 size_t t_failed = 0; 2123 size_t i, j; 2124 int c_failed[sizeof(strx_ovflw) / sizeof(strx_ovflw[0])]; 2125 static const size_t n_checks = sizeof(c_failed) / sizeof(c_failed[0]); 2126 2127 memset (c_failed, 0, sizeof(c_failed)); 2128 2129 for (j = 0; j < locale_name_count; j++) 2130 { 2131 set_test_locale (j); /* setlocale() can be slow! */ 2132 for (i = 0; i < n_checks; i++) 2133 { 2134 size_t rs; 2135 const struct str_with_len *const t = strx_ovflw + i; 2136 static const uint64_t rnd_val = 4; 2137 size_t len; 2138 2139 for (len = t->len; len <= t->len + 1; len++) 2140 { 2141 uint64_t test_val; 2142 for (test_val = 0; test_val <= rnd_val && ! c_failed[i]; test_val += 2143 rnd_val) 2144 { 2145 uint64_t rv = test_val; 2146 2147 rs = mhd_strx_to_uint64_n (t->str, len, &rv); 2148 if (rs != 0) 2149 { 2150 t_failed++; 2151 c_failed[i] = ! 0; 2152 fprintf (stderr, 2153 "FAILED: mhd_strx_to_uint64_n(\"%s\", %" PRIuPTR ", ->0x%" 2154 PRIX64 2155 ")" 2156 " returned %" PRIuPTR 2157 ", while expecting zero. Locale: %s\n", 2158 n_prnt (t->str), (uintptr_t) len, rv, (uintptr_t) rs, 2159 get_current_locale_str ()); 2160 } 2161 else if (rv != test_val) 2162 { 2163 t_failed++; 2164 c_failed[i] = ! 0; 2165 fprintf (stderr, 2166 "FAILED: mhd_strx_to_uint64_n(\"%s\", %" PRIuPTR 2167 ", &ret_val)" 2168 " modified value of ret_val (before call: 0x%" PRIX64 2169 ", after call 0x%" PRIX64 ")." 2170 " Locale: %s\n", n_prnt (t->str), (uintptr_t) len, 2171 test_val, rv, 2172 get_current_locale_str ()); 2173 } 2174 } 2175 } 2176 if ((verbose > 1) && (j == locale_name_count - 1) && ! c_failed[i]) 2177 printf ("PASSED: mhd_strx_to_uint64_n(\"%s\", %" PRIuPTR "..%" PRIuPTR 2178 ", &ret_val) == 0," 2179 " value of ret_val is unmodified\n", n_prnt (t->str), 2180 (uintptr_t) t->len, 2181 (uintptr_t) t->len + 1); 2182 } 2183 } 2184 return t_failed; 2185 } 2186 2187 2188 static size_t 2189 check_strx_to_uint64_n_no_val (void) 2190 { 2191 size_t t_failed = 0; 2192 size_t i, j; 2193 int c_failed[sizeof(str_no_num) / sizeof(str_no_num[0])]; 2194 static const size_t n_checks = sizeof(c_failed) / sizeof(c_failed[0]); 2195 2196 memset (c_failed, 0, sizeof(c_failed)); 2197 2198 for (j = 0; j < locale_name_count; j++) 2199 { 2200 set_test_locale (j); /* setlocale() can be slow! */ 2201 for (i = 0; i < n_checks; i++) 2202 { 2203 size_t rs; 2204 const struct str_with_len *const t = str_no_num + i; 2205 static const uint64_t rnd_val = 3214314212UL; 2206 size_t len; 2207 2208 for (len = 0; len <= t->len + 1; len++) 2209 { 2210 uint64_t test_val; 2211 for (test_val = 0; test_val <= rnd_val && ! c_failed[i]; test_val += 2212 rnd_val) 2213 { 2214 uint64_t rv = test_val; 2215 2216 rs = mhd_strx_to_uint64_n (t->str, len, &rv); 2217 if (rs != 0) 2218 { 2219 t_failed++; 2220 c_failed[i] = ! 0; 2221 fprintf (stderr, 2222 "FAILED: mhd_strx_to_uint64_n(\"%s\", %" PRIuPTR ", ->0x%" 2223 PRIX64 2224 ")" 2225 " returned %" PRIuPTR 2226 ", while expecting zero. Locale: %s\n", 2227 n_prnt (t->str), (uintptr_t) len, rv, (uintptr_t) rs, 2228 get_current_locale_str ()); 2229 } 2230 else if (rv != test_val) 2231 { 2232 t_failed++; 2233 c_failed[i] = ! 0; 2234 fprintf (stderr, 2235 "FAILED: mhd_strx_to_uint64_n(\"%s\", %" PRIuPTR 2236 ", &ret_val)" 2237 " modified value of ret_val (before call: 0x%" PRIX64 2238 ", after call 0x%" PRIX64 ")." 2239 " Locale: %s\n", n_prnt (t->str), (uintptr_t) len, 2240 test_val, rv, 2241 get_current_locale_str ()); 2242 } 2243 } 2244 } 2245 if ((verbose > 1) && (j == locale_name_count - 1) && ! c_failed[i]) 2246 printf ("PASSED: mhd_strx_to_uint64_n(\"%s\", 0..%" PRIuPTR 2247 ", &ret_val) == 0," 2248 " value of ret_val is unmodified\n", n_prnt (t->str), 2249 (uintptr_t) t->len + 1); 2250 } 2251 } 2252 return t_failed; 2253 } 2254 2255 2256 static int 2257 run_str_to_X_tests (void) 2258 { 2259 size_t str_to_uint64_fails = 0; 2260 size_t str_to_uint64_n_fails = 0; 2261 size_t strx_to_uint32_fails = 0; 2262 size_t strx_to_uint32_n_fails = 0; 2263 size_t strx_to_uint64_fails = 0; 2264 size_t strx_to_uint64_n_fails = 0; 2265 size_t res; 2266 2267 res = check_str_to_uint64_valid (); 2268 if (res != 0) 2269 { 2270 str_to_uint64_fails += res; 2271 fprintf (stderr, 2272 "FAILED: testcase check_str_to_uint64_valid() failed.\n\n"); 2273 } 2274 else if (verbose > 1) 2275 printf ( 2276 "PASSED: testcase check_str_to_uint64_valid() successfully passed.\n\n"); 2277 2278 res = check_str_to_uint64_all_chars (); 2279 if (res != 0) 2280 { 2281 str_to_uint64_fails += res; 2282 fprintf (stderr, 2283 "FAILED: testcase check_str_to_uint64_all_chars() failed.\n\n"); 2284 } 2285 else if (verbose > 1) 2286 printf ("PASSED: testcase check_str_to_uint64_all_chars() " 2287 "successfully passed.\n\n"); 2288 2289 res = check_str_to_uint64_overflow (); 2290 if (res != 0) 2291 { 2292 str_to_uint64_fails += res; 2293 fprintf (stderr, 2294 "FAILED: testcase check_str_to_uint64_overflow() failed.\n\n"); 2295 } 2296 else if (verbose > 1) 2297 printf ("PASSED: testcase check_str_to_uint64_overflow() " 2298 "successfully passed.\n\n"); 2299 2300 res = check_str_to_uint64_no_val (); 2301 if (res != 0) 2302 { 2303 str_to_uint64_fails += res; 2304 fprintf (stderr, 2305 "FAILED: testcase check_str_to_uint64_no_val() failed.\n\n"); 2306 } 2307 else if (verbose > 1) 2308 printf ("PASSED: testcase check_str_to_uint64_no_val() " 2309 "successfully passed.\n\n"); 2310 2311 if (str_to_uint64_fails) 2312 fprintf (stderr, 2313 "FAILED: function mhd_str_to_uint64() failed %lu time%s.\n\n", 2314 (unsigned long) str_to_uint64_fails, 2315 str_to_uint64_fails == 1 ? "" : "s"); 2316 else if (verbose > 0) 2317 printf ("PASSED: function mhd_str_to_uint64() successfully " 2318 "passed all checks.\n\n"); 2319 2320 res = check_str_to_uint64_n_valid (); 2321 if (res != 0) 2322 { 2323 str_to_uint64_n_fails += res; 2324 fprintf (stderr, 2325 "FAILED: testcase check_str_to_uint64_n_valid() failed.\n\n"); 2326 } 2327 else if (verbose > 1) 2328 printf ("PASSED: testcase check_str_to_uint64_n_valid() " 2329 "successfully passed.\n\n"); 2330 2331 res = check_str_to_uint64_n_all_chars (); 2332 if (res != 0) 2333 { 2334 str_to_uint64_n_fails += res; 2335 fprintf (stderr, 2336 "FAILED: testcase check_str_to_uint64_n_all_chars() failed.\n\n"); 2337 } 2338 else if (verbose > 1) 2339 printf ("PASSED: testcase check_str_to_uint64_n_all_chars() " 2340 "successfully passed.\n\n"); 2341 2342 res = check_str_to_uint64_n_overflow (); 2343 if (res != 0) 2344 { 2345 str_to_uint64_n_fails += res; 2346 fprintf (stderr, 2347 "FAILED: testcase check_str_to_uint64_n_overflow() failed.\n\n"); 2348 } 2349 else if (verbose > 1) 2350 printf ("PASSED: testcase check_str_to_uint64_n_overflow() " 2351 "successfully passed.\n\n"); 2352 2353 res = check_str_to_uint64_n_no_val (); 2354 if (res != 0) 2355 { 2356 str_to_uint64_n_fails += res; 2357 fprintf (stderr, 2358 "FAILED: testcase check_str_to_uint64_n_no_val() failed.\n\n"); 2359 } 2360 else if (verbose > 1) 2361 printf ("PASSED: testcase check_str_to_uint64_n_no_val() " 2362 "successfully passed.\n\n"); 2363 2364 if (str_to_uint64_n_fails) 2365 fprintf (stderr, 2366 "FAILED: function mhd_str_to_uint64_n() failed %lu time%s.\n\n", 2367 (unsigned long) str_to_uint64_n_fails, 2368 str_to_uint64_n_fails == 1 ? "" : "s"); 2369 else if (verbose > 0) 2370 printf ("PASSED: function mhd_str_to_uint64_n() successfully " 2371 "passed all checks.\n\n"); 2372 2373 res = check_strx_to_uint32_valid (); 2374 if (res != 0) 2375 { 2376 strx_to_uint32_fails += res; 2377 fprintf (stderr, 2378 "FAILED: testcase check_strx_to_uint32_valid() failed.\n\n"); 2379 } 2380 else if (verbose > 1) 2381 printf ("PASSED: testcase check_strx_to_uint32_valid() " 2382 "successfully passed.\n\n"); 2383 2384 res = check_strx_to_uint32_all_chars (); 2385 if (res != 0) 2386 { 2387 strx_to_uint32_fails += res; 2388 fprintf (stderr, 2389 "FAILED: testcase check_strx_to_uint32_all_chars() failed.\n\n"); 2390 } 2391 else if (verbose > 1) 2392 printf ("PASSED: testcase check_strx_to_uint32_all_chars() " 2393 "successfully passed.\n\n"); 2394 2395 res = check_strx_to_uint32_overflow (); 2396 if (res != 0) 2397 { 2398 strx_to_uint32_fails += res; 2399 fprintf (stderr, 2400 "FAILED: testcase check_strx_to_uint32_overflow() failed.\n\n"); 2401 } 2402 else if (verbose > 1) 2403 printf ("PASSED: testcase check_strx_to_uint32_overflow() " 2404 "successfully passed.\n\n"); 2405 2406 res = check_strx_to_uint32_no_val (); 2407 if (res != 0) 2408 { 2409 strx_to_uint32_fails += res; 2410 fprintf (stderr, 2411 "FAILED: testcase check_strx_to_uint32_no_val() failed.\n\n"); 2412 } 2413 else if (verbose > 1) 2414 printf ("PASSED: testcase check_strx_to_uint32_no_val() " 2415 "successfully passed.\n\n"); 2416 2417 if (strx_to_uint32_fails) 2418 fprintf (stderr, 2419 "FAILED: function mhd_strx_to_uint32() failed %lu time%s.\n\n", 2420 (unsigned long) strx_to_uint32_fails, 2421 strx_to_uint32_fails == 1 ? "" : "s"); 2422 else if (verbose > 0) 2423 printf ("PASSED: function mhd_strx_to_uint32() successfully " 2424 "passed all checks.\n\n"); 2425 2426 res = check_strx_to_uint32_n_valid (); 2427 if (res != 0) 2428 { 2429 strx_to_uint32_n_fails += res; 2430 fprintf (stderr, 2431 "FAILED: testcase check_strx_to_uint32_n_valid() failed.\n\n"); 2432 } 2433 else if (verbose > 1) 2434 printf ("PASSED: testcase check_strx_to_uint32_n_valid() " 2435 "successfully passed.\n\n"); 2436 2437 res = check_strx_to_uint32_n_all_chars (); 2438 if (res != 0) 2439 { 2440 strx_to_uint32_n_fails += res; 2441 fprintf (stderr, 2442 "FAILED: testcase check_strx_to_uint32_n_all_chars() failed.\n\n"); 2443 } 2444 else if (verbose > 1) 2445 printf ("PASSED: testcase check_strx_to_uint32_n_all_chars() " 2446 "successfully passed.\n\n"); 2447 2448 res = check_strx_to_uint32_n_overflow (); 2449 if (res != 0) 2450 { 2451 strx_to_uint32_n_fails += res; 2452 fprintf (stderr, 2453 "FAILED: testcase check_strx_to_uint32_n_overflow() failed.\n\n"); 2454 } 2455 else if (verbose > 1) 2456 printf ("PASSED: testcase check_strx_to_uint32_n_overflow() " 2457 "successfully passed.\n\n"); 2458 2459 res = check_strx_to_uint32_n_no_val (); 2460 if (res != 0) 2461 { 2462 strx_to_uint32_n_fails += res; 2463 fprintf (stderr, 2464 "FAILED: testcase check_strx_to_uint32_n_no_val() failed.\n\n"); 2465 } 2466 else if (verbose > 1) 2467 printf ("PASSED: testcase check_strx_to_uint32_n_no_val() " 2468 "successfully passed.\n\n"); 2469 2470 if (strx_to_uint32_n_fails) 2471 fprintf (stderr, 2472 "FAILED: function mhd_strx_to_uint32_n() failed %lu time%s.\n\n", 2473 (unsigned long) strx_to_uint32_n_fails, 2474 strx_to_uint32_n_fails == 1 ? "" : "s"); 2475 else if (verbose > 0) 2476 printf ("PASSED: function mhd_strx_to_uint32_n() successfully " 2477 "passed all checks.\n\n"); 2478 2479 res = check_strx_to_uint64_valid (); 2480 if (res != 0) 2481 { 2482 strx_to_uint64_fails += res; 2483 fprintf (stderr, 2484 "FAILED: testcase check_strx_to_uint64_valid() failed.\n\n"); 2485 } 2486 else if (verbose > 1) 2487 printf ("PASSED: testcase check_strx_to_uint64_valid() " 2488 "successfully passed.\n\n"); 2489 2490 res = check_strx_to_uint64_all_chars (); 2491 if (res != 0) 2492 { 2493 strx_to_uint64_fails += res; 2494 fprintf (stderr, 2495 "FAILED: testcase check_strx_to_uint64_all_chars() failed.\n\n"); 2496 } 2497 else if (verbose > 1) 2498 printf ("PASSED: testcase check_strx_to_uint64_all_chars() " 2499 "successfully passed.\n\n"); 2500 2501 res = check_strx_to_uint64_overflow (); 2502 if (res != 0) 2503 { 2504 strx_to_uint64_fails += res; 2505 fprintf (stderr, 2506 "FAILED: testcase check_strx_to_uint64_overflow() failed.\n\n"); 2507 } 2508 else if (verbose > 1) 2509 printf ("PASSED: testcase check_strx_to_uint64_overflow() " 2510 "successfully passed.\n\n"); 2511 2512 res = check_strx_to_uint64_no_val (); 2513 if (res != 0) 2514 { 2515 strx_to_uint64_fails += res; 2516 fprintf (stderr, 2517 "FAILED: testcase check_strx_to_uint64_no_val() failed.\n\n"); 2518 } 2519 else if (verbose > 1) 2520 printf ("PASSED: testcase check_strx_to_uint64_no_val() " 2521 "successfully passed.\n\n"); 2522 2523 if (strx_to_uint64_fails) 2524 fprintf (stderr, 2525 "FAILED: function mhd_strx_to_uint64() failed %lu time%s.\n\n", 2526 (unsigned long) strx_to_uint64_fails, 2527 strx_to_uint64_fails == 1 ? "" : "s"); 2528 else if (verbose > 0) 2529 printf ("PASSED: function mhd_strx_to_uint64() successfully " 2530 "passed all checks.\n\n"); 2531 2532 res = check_strx_to_uint64_n_valid (); 2533 if (res != 0) 2534 { 2535 strx_to_uint64_n_fails += res; 2536 fprintf (stderr, 2537 "FAILED: testcase check_strx_to_uint64_n_valid() failed.\n\n"); 2538 } 2539 else if (verbose > 1) 2540 printf ("PASSED: testcase check_strx_to_uint64_n_valid() " 2541 "successfully passed.\n\n"); 2542 2543 res = check_strx_to_uint64_n_all_chars (); 2544 if (res != 0) 2545 { 2546 strx_to_uint64_n_fails += res; 2547 fprintf (stderr, 2548 "FAILED: testcase check_strx_to_uint64_n_all_chars() failed.\n\n"); 2549 } 2550 else if (verbose > 1) 2551 printf ("PASSED: testcase check_strx_to_uint64_n_all_chars() " 2552 "successfully passed.\n\n"); 2553 2554 res = check_strx_to_uint64_n_overflow (); 2555 if (res != 0) 2556 { 2557 strx_to_uint64_n_fails += res; 2558 fprintf (stderr, 2559 "FAILED: testcase check_strx_to_uint64_n_overflow() failed.\n\n"); 2560 } 2561 else if (verbose > 1) 2562 printf ("PASSED: testcase check_strx_to_uint64_n_overflow() " 2563 "successfully passed.\n\n"); 2564 2565 res = check_strx_to_uint64_n_no_val (); 2566 if (res != 0) 2567 { 2568 strx_to_uint64_n_fails += res; 2569 fprintf (stderr, 2570 "FAILED: testcase check_strx_to_uint64_n_no_val() failed.\n\n"); 2571 } 2572 else if (verbose > 1) 2573 printf ("PASSED: testcase check_strx_to_uint64_n_no_val() " 2574 "successfully passed.\n\n"); 2575 2576 if (strx_to_uint64_n_fails) 2577 fprintf (stderr, 2578 "FAILED: function mhd_strx_to_uint64_n() failed %lu time%s.\n\n", 2579 (unsigned long) strx_to_uint64_n_fails, 2580 strx_to_uint64_n_fails == 1 ? "" : "s"); 2581 else if (verbose > 0) 2582 printf ("PASSED: function mhd_strx_to_uint64_n() successfully " 2583 "passed all checks.\n\n"); 2584 2585 if (str_to_uint64_fails || str_to_uint64_n_fails || 2586 strx_to_uint32_fails || strx_to_uint32_n_fails || 2587 strx_to_uint64_fails || strx_to_uint64_n_fails) 2588 { 2589 if (verbose > 0) 2590 printf ("At least one test failed.\n"); 2591 2592 return 1; 2593 } 2594 2595 if (verbose > 0) 2596 printf ("All tests passed successfully.\n"); 2597 2598 return 0; 2599 } 2600 2601 2602 int 2603 main (int argc, char *argv[]) 2604 { 2605 if (has_param (argc, argv, "-v") || 2606 has_param (argc, argv, "--verbose") || 2607 has_param (argc, argv, "--verbose1")) 2608 MHDT_set_verbosity (MHDT_VERB_LVL_BASIC); 2609 if (has_param (argc, argv, "-vv") || 2610 has_param (argc, argv, "--verbose2")) 2611 MHDT_set_verbosity (MHDT_VERB_LVL_VERBOSE); 2612 2613 return run_str_to_X_tests (); 2614 }