mhdt_checks.h (36650B)
1 /* SPDX-License-Identifier: LGPL-2.1-or-later OR (GPL-2.0-or-later WITH eCos-exception-2.0) */ 2 /* 3 This file is part of GNU libmicrohttpd. 4 Copyright (C) 2016-2025 Karlson2k (Evgeny Grin) 5 6 GNU libmicrohttpd is free software; you can redistribute it and/or 7 modify it under the terms of the GNU Lesser General Public 8 License as published by the Free Software Foundation; either 9 version 2.1 of the License, or (at your option) any later version. 10 11 GNU libmicrohttpd is distributed in the hope that it will be useful, 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 Lesser General Public License for more details. 15 16 Alternatively, you can redistribute GNU libmicrohttpd and/or 17 modify it under the terms of the GNU General Public License as 18 published by the Free Software Foundation; either version 2 of 19 the License, or (at your option) any later version, together 20 with the eCos exception, as follows: 21 22 As a special exception, if other files instantiate templates or 23 use macros or inline functions from this file, or you compile this 24 file and link it with other works to produce a work based on this 25 file, this file does not by itself cause the resulting work to be 26 covered by the GNU General Public License. However the source code 27 for this file must still be made available in accordance with 28 section (3) of the GNU General Public License v2. 29 30 This exception does not invalidate any other reasons why a work 31 based on this file might be covered by the GNU General Public 32 License. 33 34 You should have received copies of the GNU Lesser General Public 35 License and the GNU General Public License along with this library; 36 if not, see <https://www.gnu.org/licenses/>. 37 */ 38 39 /** 40 * @file src/tests/mhdt_checks.h 41 * @brief MHD test framework helpers 42 * @author Karlson2k (Evgeny Grin) 43 */ 44 #ifndef MHDT_CHECKS_H 45 #define MHDT_CHECKS_H 1 46 47 #include "mhd_sys_options.h" 48 #include <stdio.h> 49 #include <stdlib.h> 50 #include <stdint.h> 51 #include <string.h> 52 #ifdef HAVE_INTTYPES_H 53 # include <inttypes.h> 54 #endif 55 56 #if defined(_MSC_VER) 57 # pragma warning(push) 58 /* Disable C4505 "unreferenced local function has been removed" */ 59 # pragma warning(disable:4505) 60 #endif /* _MSC_VER */ 61 62 #ifndef HAVE_SNPRINTF 63 # ifdef _WIN32 64 # define snprintf _snprintf 65 # define HAVE_SNPRINTF 1 66 # endif 67 #endif 68 69 /* 70 * ****** Generic test helpers ****** 71 */ 72 73 #ifdef MHD_HAVE_MHD_FUNC_ 74 # define MHDT_ERR_EXIT() \ 75 do { fprintf (stderr, "\n\n%s:%lu:%s(): Unexpected error exit!\n", \ 76 __FILE__, (unsigned long) __LINE__, MHD_FUNC_); \ 77 fflush (stderr); exit (99);} while (0) 78 # define MHDT_ERR_EXIT_D(desc) \ 79 do { fprintf (stderr, "\n\n%s:%lu:%s(): Unexpected error exit: %s\n", \ 80 __FILE__, (unsigned long) __LINE__, MHD_FUNC_, (desc)); \ 81 fflush (stderr); exit (99);} while (0) 82 #else 83 # define MHDT_ERR_EXIT() \ 84 do { fprintf (stderr, "\n\n%s:%lu: Unexpected error exit!\n", \ 85 __FILE__, (unsigned long) __LINE__); \ 86 fflush (stderr); exit (99);} while (0) 87 # define MHDT_ERR_EXIT_D(desc) \ 88 do { fprintf (stderr, "\n\n%s:%lu: Unexpected error exit: %s\n", \ 89 __FILE__, (unsigned long) __LINE__, (desc)); \ 90 fflush (stderr); exit (99);} while (0) 91 #endif 92 93 /** 94 * Stringify parameter 95 */ 96 #define mhdt_STR_MACRO(x) #x 97 /** 98 * Stringify parameter after expansion 99 */ 100 #define mhdt_STR_MACRO_EXP(x) mhdt_STR_MACRO (x) 101 102 103 /* 104 * ****** Internal data and functions ****** 105 */ 106 107 #define MHDT_NULL_PRNT "[NULL]" 108 109 /** Counter for failed checks within one test */ 110 static unsigned int MHDT_checks_err_counter = 0u; 111 112 /** Counter for total number of failed tests */ 113 static unsigned int MHDT_tests_err_counter = 0u; 114 115 /** Non-zero if current test printed something */ 116 static int MHDT_cur_test_printed_something = 0; 117 118 119 enum MHDT_VerbosityLevelEnum 120 { 121 MHDT_VERB_LVL_SILENT = 0, /**< only failures are printed */ 122 MHDT_VERB_LVL_BASIC = 1, /**< failures and all tests results are printed */ 123 MHDT_VERB_LVL_VERBOSE = 2 /**< all checks and test results are printed */ 124 }; 125 static enum MHDT_VerbosityLevelEnum MHDT_verbosity_level = MHDT_VERB_LVL_BASIC; 126 127 /* Return non-zero if even succeed check result should be printed */ 128 static inline int 129 mhdt_print_check_succeed (void) 130 { 131 return (MHDT_VERB_LVL_VERBOSE <= MHDT_verbosity_level); 132 } 133 134 135 /* Return non-zero if succeed check test should be printed */ 136 static inline int 137 mhdt_print_test_succeed (void) 138 { 139 return (MHDT_VERB_LVL_BASIC <= MHDT_verbosity_level); 140 } 141 142 143 static inline void 144 mhdt_mark_print_in_test_set (void) 145 { 146 if (MHDT_cur_test_printed_something) 147 return; 148 fprintf (stdout, "_____\n"); 149 fflush (stdout); 150 MHDT_cur_test_printed_something = !0; 151 } 152 153 154 static inline void 155 mhdt_mark_print_in_test_reset (void) 156 { 157 MHDT_cur_test_printed_something = 0; 158 } 159 160 161 static inline int 162 mhdt_mark_print_in_test_is_set (void) 163 { 164 return MHDT_cur_test_printed_something; 165 } 166 167 168 /* 169 * ****** Configuration functions ****** 170 */ 171 172 static inline void 173 MHDT_set_verbosity (enum MHDT_VerbosityLevelEnum level) 174 { 175 MHDT_verbosity_level = level; 176 } 177 178 179 /* 180 * ****** Functions for getting results ****** 181 */ 182 183 /* Return non-zero if all checks of the current test have succeeded so far, 184 zero if any has failed */ 185 static inline int 186 MHDT_are_all_checks_succeed (void) 187 { 188 return (0u == MHDT_checks_err_counter); 189 } 190 191 192 /* Return non-zero if succeeded, zero if failed */ 193 static inline int 194 MHDT_test_result (const char *filename, 195 long line, 196 const char *func_name, 197 const char *description) 198 { 199 #ifdef MHD_HAVE_MHD_FUNC_ 200 const int have_func_name = ((NULL != func_name) && (0 != func_name[0])); 201 #else /* ! MHD_HAVE_MHD_FUNC_ */ 202 const int have_func_name = 0; 203 #endif 204 const int have_descr = ((NULL != description) && (0 != description[0])); 205 const int test_succeed = (0u == MHDT_checks_err_counter); 206 207 MHDT_checks_err_counter = 0u; /* Reset the counter for the next test */ 208 209 if (test_succeed && !mhdt_print_test_succeed ()) 210 return test_succeed; 211 212 fprintf (test_succeed ? stdout : stderr, 213 "%s" 214 "%s:%s%s\n" 215 "%s:%ld: %s%s%s\n\n" 216 "%s", 217 (mhdt_mark_print_in_test_is_set () ? "-----\n" : ""), 218 (test_succeed ? "Succeed" : "FAILED"), 219 (have_descr ? " " : ""), 220 (have_descr ? description : ""), 221 filename, 222 line, 223 (have_func_name ? func_name : ""), 224 (have_func_name ? "(): " : ""), 225 (test_succeed ? "All checks passed" : "Some checks failed"), 226 (mhdt_print_check_succeed () ? "\n" : "")); 227 fflush (test_succeed ? stdout : stderr); 228 mhdt_mark_print_in_test_reset (); 229 230 MHDT_tests_err_counter += (test_succeed ? 0 : 1); 231 232 return test_succeed; 233 } 234 235 236 #define MHDT_TEST_RESULT() \ 237 MHDT_test_result (__FILE__, __LINE__, MHD_FUNC_, NULL) 238 #define MHDT_TEST_RESULT_D(desc) \ 239 MHDT_test_result (__FILE__, __LINE__, MHD_FUNC_, desc) 240 241 242 /* Return non-zero if all completed tests have succeeded, zero if any has 243 failed. A test is completed by MHDT_test_result(). */ 244 static inline int 245 MHDT_are_all_tests_succeed (void) 246 { 247 return (0u == MHDT_tests_err_counter); 248 } 249 250 251 /* Return ZERO on success, ONE if any test failed */ 252 static inline int 253 MHDT_final_result (const char *exec_selfname, 254 const char *filename) 255 { 256 const char *my_name; 257 const int no_errors = (0u == MHDT_tests_err_counter); 258 259 if ((NULL != exec_selfname) && (0 != exec_selfname[0])) 260 my_name = exec_selfname; 261 else if ((NULL != filename) && (0 != filename[0])) 262 my_name = filename; 263 else 264 my_name = "test"; 265 266 fprintf ((no_errors ? stdout : stderr), 267 "%s: %s\n", 268 (no_errors ? "SUCCEED" : "FAILED"), 269 my_name); 270 271 fflush (no_errors ? stdout : stderr); 272 273 return no_errors ? 0 : 1; 274 } 275 276 277 #define MHDT_FINAL_RESULT(exec_selfname) \ 278 MHDT_final_result (exec_selfname, __FILE__) 279 280 281 /* 282 * ****** Individual checkers ****** 283 */ 284 285 /* Return non-zero if succeeded, zero if failed */ 286 static inline int 287 MHDT_check_bool (int result, 288 int expected, 289 const char *check_str, 290 const char *check_str_exp, 291 const char *filename, 292 long line, 293 const char *func_name, 294 const char *description) 295 { 296 #ifdef MHD_HAVE_MHD_FUNC_ 297 const int have_func_name = ((NULL != func_name) && (0 != func_name[0])); 298 #else /* ! MHD_HAVE_MHD_FUNC_ */ 299 const int have_func_name = 0; 300 #endif 301 const int have_descr = ((NULL != description) && (0 != description[0])); 302 int exp_different; 303 int check_succeed; 304 305 check_succeed = ((!!result) == (!!expected)); 306 307 if (check_succeed && !mhdt_print_check_succeed ()) 308 return check_succeed; 309 310 mhdt_mark_print_in_test_set (); 311 exp_different = (0 != strcmp (check_str, check_str_exp)); 312 fprintf (check_succeed ? stdout : stderr, 313 "%s:%s%s\n" 314 "%s:%ld: %s%sthe result of the evaluation of the\n" 315 "\texpression: '%s'%s%s%s\n" 316 "\tActual: '%s'\tExpected: '%s'\n", 317 (check_succeed ? "Pass" : "FAIL"), 318 (have_descr ? " " : ""), 319 (have_descr ? description : ""), 320 filename, 321 line, 322 (have_func_name ? func_name : ""), 323 (have_func_name ? "(): " : ""), 324 check_str, 325 (exp_different ? "\n\texpanded: '" : ""), 326 (exp_different ? check_str_exp : ""), 327 (exp_different ? "'" : ""), 328 ((!!result) ? "NON-zero" : "ZERO"), 329 ((!!expected) ? "NON-zero" : "ZERO")); 330 fflush (check_succeed ? stdout : stderr); 331 332 MHDT_checks_err_counter += (check_succeed ? 0 : 1); 333 334 return check_succeed; 335 } 336 337 338 #define MHDT_EXPECT_TRUE(cond) \ 339 MHDT_check_bool ((cond), ! 0, #cond, \ 340 mhdt_STR_MACRO_EXP (cond), __FILE__, __LINE__, \ 341 MHD_FUNC_, NULL) 342 #define MHDT_EXPECT_FALSE(cond) \ 343 MHDT_check_bool ((cond), 0, #cond, \ 344 mhdt_STR_MACRO_EXP (cond), __FILE__, __LINE__, \ 345 MHD_FUNC_, NULL) 346 347 #define MHDT_EXPECT_TRUE_D(cond, desc) \ 348 MHDT_check_bool ((cond), ! 0, #cond, \ 349 mhdt_STR_MACRO_EXP (cond), __FILE__, __LINE__, \ 350 MHD_FUNC_, desc) 351 #define MHDT_EXPECT_FALSE_D(cond, desc) \ 352 MHDT_check_bool ((cond), 0, #cond, \ 353 mhdt_STR_MACRO_EXP (cond), __FILE__, __LINE__, \ 354 MHD_FUNC_, desc) 355 356 #define MHDT_ASSERT MHDT_EXPECT_TRUE 357 #define MHDT_ASSERT_D MHDT_EXPECT_TRUE_D 358 359 /* Return non-zero if succeeded, zero if failed */ 360 static inline int 361 MHDT_check_ptr (const void *ptr, 362 int expected_non_null, 363 const char *check_str, 364 const char *check_str_exp, 365 const char *filename, 366 long line, 367 const char *func_name, 368 const char *description) 369 { 370 #ifdef MHD_HAVE_MHD_FUNC_ 371 const int have_func_name = ((NULL != func_name) && (0 != func_name[0])); 372 #else /* ! MHD_HAVE_MHD_FUNC_ */ 373 const int have_func_name = 0; 374 #endif 375 const int have_descr = ((NULL != description) && (0 != description[0])); 376 int exp_different; 377 int check_succeed; 378 379 check_succeed = ((NULL != ptr) == (!!expected_non_null)); 380 381 if (check_succeed && !mhdt_print_check_succeed ()) 382 return check_succeed; 383 384 mhdt_mark_print_in_test_set (); 385 exp_different = (0 != strcmp (check_str, check_str_exp)); 386 fprintf (check_succeed ? stdout : stderr, 387 "%s:%s%s\n" 388 "%s:%ld: %s%sthe value of the\n" 389 "\tpointer: '%s'%s%s%s\n" 390 "\tActual: '%s'\tExpected: '%s'\n", 391 (check_succeed ? "Pass" : "FAIL"), 392 (have_descr ? " " : ""), 393 (have_descr ? description : ""), 394 filename, 395 line, 396 (have_func_name ? func_name : ""), 397 (have_func_name ? "(): " : ""), 398 check_str, 399 (exp_different ? "\n\texpanded: '" : ""), 400 (exp_different ? check_str_exp : ""), 401 (exp_different ? "'" : ""), 402 ((NULL != ptr) ? "non-NULL" : "NULL"), 403 ((!!expected_non_null) ? "non-NULL" : "NULL")); 404 fflush (check_succeed ? stdout : stderr); 405 406 MHDT_checks_err_counter += (check_succeed ? 0 : 1); 407 408 return check_succeed; 409 } 410 411 412 #define MHDT_EXPECT_PTR_NULL(ptr) \ 413 MHDT_check_ptr ((ptr), 0, #ptr, \ 414 mhdt_STR_MACRO_EXP (ptr), __FILE__, __LINE__, MHD_FUNC_, \ 415 NULL) 416 #define MHDT_EXPECT_PTR_NONNULL(ptr) \ 417 MHDT_check_ptr ((ptr), ! 0, #ptr, \ 418 mhdt_STR_MACRO_EXP (ptr), __FILE__, __LINE__, MHD_FUNC_, \ 419 NULL) 420 421 #define MHDT_EXPECT_PTR_NULL_D(ptr, desc) \ 422 MHDT_check_ptr ((ptr), 0, #ptr, \ 423 mhdt_STR_MACRO_EXP (ptr), __FILE__, __LINE__, MHD_FUNC_, \ 424 desc) 425 #define MHDT_EXPECT_PTR_NONNULL_D(ptr, desc) \ 426 MHDT_check_ptr ((ptr), ! 0, #ptr, \ 427 mhdt_STR_MACRO_EXP (ptr), __FILE__, __LINE__, MHD_FUNC_, \ 428 desc) 429 430 #ifdef PRIuMAX 431 typedef uintmax_t mhdt_uint_t; 432 # define MHDT_UINT_PRI PRIuMAX 433 # define MHDT_UINT_PRI_CAST(val) val 434 #elif defined(PRIuFAST64) 435 typedef uint_fast64_t mhdt_uint_t; 436 # define MHDT_UINT_PRI PRIuFAST64 437 # define MHDT_UINT_PRI_CAST(val) val 438 #else 439 typedef uint_fast64_t mhdt_uint_t; 440 # define MHDT_UINT_PRI "lu" 441 # define MHDT_UINT_PRI_CAST(val) ((unsigned long) val) 442 #endif 443 444 /** 445 * Comparison type 446 */ 447 enum mhdt_CmpType 448 { 449 MHDT_CMP_EQ,/**< a == b */ 450 MHDT_CMP_LT,/**< a < b */ 451 MHDT_CMP_LE,/**< a <= b */ 452 MHDT_CMP_NE,/**< a != b */ 453 MHDT_CMP_GE,/**< a >= b */ 454 MHDT_CMP_GT /**< a > b */ 455 }; 456 457 458 /* Return non-zero if succeeded, zero if failed */ 459 static inline int 460 MHDT_check_ui_cmp (mhdt_uint_t val_a, 461 enum mhdt_CmpType cmp_t, 462 mhdt_uint_t val_b, 463 const char *val_a_str, 464 const char *val_b_str, 465 const char *val_a_str_exp, 466 const char *val_b_str_exp, 467 const char *filename, 468 long line, 469 const char *func_name, 470 const char *description) 471 { 472 #ifdef MHD_HAVE_MHD_FUNC_ 473 const int have_func_name = ((NULL != func_name) && (0 != func_name[0])); 474 #else /* ! MHD_HAVE_MHD_FUNC_ */ 475 const int have_func_name = 0; 476 #endif 477 const int have_descr = ((NULL != description) && (0 != description[0])); 478 char val_a_buf[512] = ""; 479 char val_b_buf[512] = ""; 480 int use_buf_print = 0; 481 int str_has_suff; 482 int both_str_has_suff; 483 const size_t buf_size = (sizeof(val_a_buf) / sizeof(char)); 484 const char *cmp_str; 485 int check_succeed; 486 487 switch (cmp_t) 488 { 489 case MHDT_CMP_EQ: 490 check_succeed = (val_a == val_b); 491 cmp_str = "=="; 492 break; 493 case MHDT_CMP_LT: 494 check_succeed = (val_a < val_b); 495 cmp_str = "<"; 496 break; 497 case MHDT_CMP_LE: 498 check_succeed = (val_a <= val_b); 499 cmp_str = "<="; 500 break; 501 case MHDT_CMP_NE: 502 check_succeed = (val_a != val_b); 503 cmp_str = "!="; 504 break; 505 case MHDT_CMP_GE: 506 check_succeed = (val_a >= val_b); 507 cmp_str = ">="; 508 break; 509 case MHDT_CMP_GT: 510 check_succeed = (val_a > val_b); 511 cmp_str = ">"; 512 break; 513 default: 514 MHDT_ERR_EXIT (); 515 break; 516 } 517 518 if (check_succeed && !mhdt_print_check_succeed ()) 519 return check_succeed; 520 521 both_str_has_suff = !0; 522 str_has_suff = 0; 523 if (1) /* For local scope */ 524 { 525 const mhdt_uint_t val = val_a; 526 char *const print_buf = val_a_buf; 527 const char *const str = val_a_str_exp; 528 int print_res; 529 530 print_res = 531 #ifdef HAVE_SNPRINTF 532 snprintf (print_buf, buf_size, 533 #else 534 sprintf (print_buf, 535 #endif 536 "%" MHDT_UINT_PRI, 537 MHDT_UINT_PRI_CAST (val)); 538 if ((0 > print_res) 539 || (buf_size <= (unsigned int)print_res)) 540 MHDT_ERR_EXIT (); 541 542 if (0 != strcmp (str, print_buf)) 543 { 544 const size_t str_len = strlen (str); 545 if (str_len - 1 != (unsigned int)print_res) 546 use_buf_print = !0; 547 else if ((('u' == str[str_len - 1u]) || ('U' == str[str_len - 1u])) 548 && (0 == memcmp (str, print_buf, str_len - 1u))) 549 str_has_suff = !0; 550 else 551 use_buf_print = !0; 552 } 553 else 554 both_str_has_suff = 0; 555 } 556 if (1) /* For local scope */ 557 { 558 const mhdt_uint_t val = val_b; 559 char *const print_buf = val_b_buf; 560 const char *const str = val_b_str_exp; 561 int print_res; 562 563 print_res = 564 #ifdef HAVE_SNPRINTF 565 snprintf (print_buf, buf_size, 566 #else 567 sprintf (print_buf, 568 #endif 569 "%" MHDT_UINT_PRI, 570 MHDT_UINT_PRI_CAST (val)); 571 if ((0 > print_res) 572 || (buf_size <= (unsigned int)print_res)) 573 MHDT_ERR_EXIT (); 574 575 if (0 != strcmp (str, print_buf)) 576 { 577 const size_t str_len = strlen (str); 578 if (str_len - 1 != (unsigned int)print_res) 579 use_buf_print = !0; 580 else if ((('u' == str[str_len - 1u]) || ('U' == str[str_len - 1u])) 581 && (0 == memcmp (str, print_buf, str_len - 1u))) 582 str_has_suff = !0; 583 else 584 use_buf_print = !0; 585 } 586 else 587 both_str_has_suff = 0; 588 } 589 if (!use_buf_print && str_has_suff && !both_str_has_suff) 590 use_buf_print = !0; 591 592 mhdt_mark_print_in_test_set (); 593 fprintf (check_succeed ? stdout : stderr, 594 "%s:%s%s\n" 595 "%s:%ld: %s%sThe %s check:\n" 596 "\toriginal: '%s %s %s'\n", 597 (check_succeed ? "Pass" : "FAIL"), 598 (have_descr ? " " : ""), 599 (have_descr ? description : ""), 600 filename, 601 line, 602 (have_func_name ? func_name : ""), 603 (have_func_name ? "(): " : ""), 604 (check_succeed ? "passed" : "failed"), 605 val_a_str, cmp_str, val_b_str); 606 if ((0 != strcmp (val_a_str, val_a_str_exp)) 607 || (0 != strcmp (val_b_str, val_b_str_exp))) 608 fprintf (check_succeed ? stdout : stderr, 609 "\texpanded: '%s %s %s'\n", 610 val_a_str_exp, cmp_str, val_b_str_exp); 611 if (use_buf_print) 612 fprintf (check_succeed ? stdout : stderr, 613 "\tvalues: '%s %s %s'\n", 614 val_a_buf, cmp_str, val_b_buf); 615 616 fflush (check_succeed ? stdout : stderr); 617 618 MHDT_checks_err_counter += (check_succeed ? 0 : 1); 619 620 return check_succeed; 621 } 622 623 624 #define MHDT_EXPECT_UINT_EQ_VAL(uiv_l, uiv_r) \ 625 MHDT_check_ui_cmp ((uiv_l), MHDT_CMP_EQ, (uiv_r), \ 626 #uiv_l, #uiv_r, \ 627 mhdt_STR_MACRO_EXP (uiv_l), \ 628 mhdt_STR_MACRO_EXP (uiv_r), \ 629 __FILE__, __LINE__, MHD_FUNC_, NULL) 630 #define MHDT_EXPECT_UINT_LT_VAL(uiv_l, uiv_r) \ 631 MHDT_check_ui_cmp ((uiv_l), MHDT_CMP_LT, (uiv_r), \ 632 #uiv_l, #uiv_r, \ 633 mhdt_STR_MACRO_EXP (uiv_l), \ 634 mhdt_STR_MACRO_EXP (uiv_r), \ 635 __FILE__, __LINE__, MHD_FUNC_, NULL) 636 #define MHDT_EXPECT_UINT_LE_VAL(uiv_l, uiv_r) \ 637 MHDT_check_ui_cmp ((uiv_l), MHDT_CMP_LE, (uiv_r), \ 638 #uiv_l, #uiv_r, \ 639 mhdt_STR_MACRO_EXP (uiv_l), \ 640 mhdt_STR_MACRO_EXP (uiv_r), \ 641 __FILE__, __LINE__, MHD_FUNC_, NULL) 642 #define MHDT_EXPECT_UINT_NE_VAL(uiv_l, uiv_r) \ 643 MHDT_check_ui_cmp ((uiv_l), MHDT_CMP_NE, (uiv_r), \ 644 #uiv_l, #uiv_r, \ 645 mhdt_STR_MACRO_EXP (uiv_l), \ 646 mhdt_STR_MACRO_EXP (uiv_r), \ 647 __FILE__, __LINE__, MHD_FUNC_, NULL) 648 #define MHDT_EXPECT_UINT_GE_VAL(uiv_l, uiv_r) \ 649 MHDT_check_ui_cmp ((uiv_l), MHDT_CMP_GE, (uiv_r), \ 650 #uiv_l, #uiv_r, \ 651 mhdt_STR_MACRO_EXP (uiv_l), \ 652 mhdt_STR_MACRO_EXP (uiv_r), \ 653 __FILE__, __LINE__, MHD_FUNC_, NULL) 654 #define MHDT_EXPECT_UINT_GT_VAL(uiv_l, uiv_r) \ 655 MHDT_check_ui_cmp ((uiv_l), MHDT_CMP_GT, (uiv_r), \ 656 #uiv_l, #uiv_r, \ 657 mhdt_STR_MACRO_EXP (uiv_l), \ 658 mhdt_STR_MACRO_EXP (uiv_r), \ 659 __FILE__, __LINE__, MHD_FUNC_, NULL) 660 661 #define MHDT_EXPECT_UINT_EQ_VAL_D(uiv_l, uiv_r, desc) \ 662 MHDT_check_ui_cmp ((uiv_l), MHDT_CMP_EQ, (uiv_r), \ 663 #uiv_l, #uiv_r, \ 664 mhdt_STR_MACRO_EXP (uiv_l), \ 665 mhdt_STR_MACRO_EXP (uiv_r), \ 666 __FILE__, __LINE__, MHD_FUNC_, desc) 667 #define MHDT_EXPECT_UINT_LT_VAL_D(uiv_l, uiv_r, desc) \ 668 MHDT_check_ui_cmp ((uiv_l), MHDT_CMP_LT, (uiv_r), \ 669 #uiv_l, #uiv_r, \ 670 mhdt_STR_MACRO_EXP (uiv_l), \ 671 mhdt_STR_MACRO_EXP (uiv_r), \ 672 __FILE__, __LINE__, MHD_FUNC_, desc) 673 #define MHDT_EXPECT_UINT_LE_VAL_D(uiv_l, uiv_r, desc) \ 674 MHDT_check_ui_cmp ((uiv_l), MHDT_CMP_LE, (uiv_r), \ 675 #uiv_l, #uiv_r, \ 676 mhdt_STR_MACRO_EXP (uiv_l), \ 677 mhdt_STR_MACRO_EXP (uiv_r), \ 678 __FILE__, __LINE__, MHD_FUNC_, desc) 679 #define MHDT_EXPECT_UINT_NE_VAL_D(uiv_l, uiv_r, desc) \ 680 MHDT_check_ui_cmp ((uiv_l), MHDT_CMP_NE, (uiv_r), \ 681 #uiv_l, #uiv_r, \ 682 mhdt_STR_MACRO_EXP (uiv_l), \ 683 mhdt_STR_MACRO_EXP (uiv_r), \ 684 __FILE__, __LINE__, MHD_FUNC_, desc) 685 #define MHDT_EXPECT_UINT_GE_VAL_D(uiv_l, uiv_r, desc) \ 686 MHDT_check_ui_cmp ((uiv_l), MHDT_CMP_GE, (uiv_r), \ 687 #uiv_l, #uiv_r, \ 688 mhdt_STR_MACRO_EXP (uiv_l), \ 689 mhdt_STR_MACRO_EXP (uiv_r), \ 690 __FILE__, __LINE__, MHD_FUNC_, desc) 691 #define MHDT_EXPECT_UINT_GT_VAL_D(uiv_l, uiv_r, desc) \ 692 MHDT_check_ui_cmp ((uiv_l), MHDT_CMP_GT, (uiv_r), \ 693 #uiv_l, #uiv_r, \ 694 mhdt_STR_MACRO_EXP (uiv_l), \ 695 mhdt_STR_MACRO_EXP (uiv_r), \ 696 __FILE__, __LINE__, MHD_FUNC_, desc) 697 698 699 #ifdef PRIdMAX 700 typedef intmax_t mhdt_int_t; 701 # define MHDT_INT_PRI PRIdMAX 702 # define MHDT_INT_PRI_CAST(val) val 703 #elif defined(PRIdFAST64) 704 typedef int_fast64_t mhdt_int_t; 705 # define MHDT_INT_PRI PRIdFAST64 706 # define MHDT_INT_PRI_CAST(val) val 707 #else 708 typedef int_fast64_t mhdt_int_t; 709 # define MHDT_INT_PRI "ld" 710 # define MHDT_INT_PRI_CAST(val) ((long) val) 711 #endif 712 713 714 /* Return non-zero if succeeded, zero if failed */ 715 static inline int 716 MHDT_check_i_cmp (mhdt_int_t val_a, 717 enum mhdt_CmpType cmp_t, 718 mhdt_int_t val_b, 719 const char *val_a_str, 720 const char *val_b_str, 721 const char *val_a_str_exp, 722 const char *val_b_str_exp, 723 const char *filename, 724 long line, 725 const char *func_name, 726 const char *description) 727 { 728 #ifdef MHD_HAVE_MHD_FUNC_ 729 const int have_func_name = ((NULL != func_name) && (0 != func_name[0])); 730 #else /* ! MHD_HAVE_MHD_FUNC_ */ 731 const int have_func_name = 0; 732 #endif 733 const int have_descr = ((NULL != description) && (0 != description[0])); 734 char val_a_buf[512] = ""; 735 char val_b_buf[512] = ""; 736 int use_buf_print = 0; 737 const size_t buf_size = (sizeof(val_a_buf) / sizeof(char)); 738 const char *cmp_str; 739 int check_succeed; 740 741 switch (cmp_t) 742 { 743 case MHDT_CMP_EQ: 744 check_succeed = (val_a == val_b); 745 cmp_str = "=="; 746 break; 747 case MHDT_CMP_LT: 748 check_succeed = (val_a < val_b); 749 cmp_str = "<"; 750 break; 751 case MHDT_CMP_LE: 752 check_succeed = (val_a <= val_b); 753 cmp_str = "<="; 754 break; 755 case MHDT_CMP_NE: 756 check_succeed = (val_a != val_b); 757 cmp_str = "!="; 758 break; 759 case MHDT_CMP_GE: 760 check_succeed = (val_a >= val_b); 761 cmp_str = ">="; 762 break; 763 case MHDT_CMP_GT: 764 check_succeed = (val_a > val_b); 765 cmp_str = ">"; 766 break; 767 default: 768 MHDT_ERR_EXIT (); 769 break; 770 } 771 772 if (check_succeed && !mhdt_print_check_succeed ()) 773 return check_succeed; 774 775 if (1) /* For local scope */ 776 { 777 const mhdt_int_t val = val_a; 778 char *const print_buf = val_a_buf; 779 const char *const str = val_a_str_exp; 780 int print_res; 781 782 print_res = 783 #ifdef HAVE_SNPRINTF 784 snprintf (print_buf, buf_size, 785 #else 786 sprintf (print_buf, 787 #endif 788 "%" MHDT_INT_PRI, 789 MHDT_INT_PRI_CAST (val)); 790 if ((0 > print_res) 791 || (buf_size <= (unsigned int)print_res)) 792 MHDT_ERR_EXIT (); 793 794 if (0 != strcmp (str, print_buf)) 795 use_buf_print = !0; 796 } 797 if (1) /* For local scope */ 798 { 799 const mhdt_int_t val = val_b; 800 char *const print_buf = val_b_buf; 801 const char *const str = val_b_str_exp; 802 int print_res; 803 804 print_res = 805 #ifdef HAVE_SNPRINTF 806 snprintf (print_buf, buf_size, 807 #else 808 sprintf (print_buf, 809 #endif 810 "%" MHDT_INT_PRI, 811 MHDT_INT_PRI_CAST (val)); 812 if ((0 > print_res) 813 || (buf_size <= (unsigned int)print_res)) 814 MHDT_ERR_EXIT (); 815 816 if (0 != strcmp (str, print_buf)) 817 use_buf_print = !0; 818 } 819 820 mhdt_mark_print_in_test_set (); 821 fprintf (check_succeed ? stdout : stderr, 822 "%s:%s%s\n" 823 "%s:%ld: %s%sThe %s check:\n" 824 "\toriginal: '%s %s %s'\n", 825 (check_succeed ? "Pass" : "FAIL"), 826 (have_descr ? " " : ""), 827 (have_descr ? description : ""), 828 filename, 829 line, 830 (have_func_name ? func_name : ""), 831 (have_func_name ? "(): " : ""), 832 (check_succeed ? "passed" : "failed"), 833 val_a_str, cmp_str, val_b_str); 834 if ((0 != strcmp (val_a_str, val_a_str_exp)) 835 || (0 != strcmp (val_b_str, val_b_str_exp))) 836 fprintf (check_succeed ? stdout : stderr, 837 "\texpanded: '%s %s %s'\n", 838 val_a_str_exp, cmp_str, val_b_str_exp); 839 if (use_buf_print) 840 fprintf (check_succeed ? stdout : stderr, 841 "\tvalues: '%s %s %s'\n", 842 val_a_buf, cmp_str, val_b_buf); 843 844 fflush (check_succeed ? stdout : stderr); 845 846 MHDT_checks_err_counter += (check_succeed ? 0 : 1); 847 848 return check_succeed; 849 } 850 851 852 #define MHDT_EXPECT_INT_EQ_VAL(iv_l, iv_r) \ 853 MHDT_check_i_cmp ((iv_l), MHDT_CMP_EQ, (iv_r), \ 854 #iv_l, #iv_r, \ 855 mhdt_STR_MACRO_EXP (iv_l), mhdt_STR_MACRO_EXP (iv_r), \ 856 __FILE__, __LINE__, MHD_FUNC_, NULL) 857 #define MHDT_EXPECT_INT_LT_VAL(iv_l, iv_r) \ 858 MHDT_check_i_cmp ((iv_l), MHDT_CMP_LT, (iv_r), \ 859 #iv_l, #iv_r, \ 860 mhdt_STR_MACRO_EXP (iv_l), mhdt_STR_MACRO_EXP (iv_r), \ 861 __FILE__, __LINE__, MHD_FUNC_, NULL) 862 #define MHDT_EXPECT_INT_LE_VAL(iv_l, iv_r) \ 863 MHDT_check_i_cmp ((iv_l), MHDT_CMP_LE, (iv_r), \ 864 #iv_l, #iv_r, \ 865 mhdt_STR_MACRO_EXP (iv_l), mhdt_STR_MACRO_EXP (iv_r), \ 866 __FILE__, __LINE__, MHD_FUNC_, NULL) 867 #define MHDT_EXPECT_INT_NE_VAL(iv_l, iv_r) \ 868 MHDT_check_i_cmp ((iv_l), MHDT_CMP_NE, (iv_r), \ 869 #iv_l, #iv_r, \ 870 mhdt_STR_MACRO_EXP (iv_l), mhdt_STR_MACRO_EXP (iv_r), \ 871 __FILE__, __LINE__, MHD_FUNC_, NULL) 872 #define MHDT_EXPECT_INT_GE_VAL(iv_l, iv_r) \ 873 MHDT_check_i_cmp ((iv_l), MHDT_CMP_GE, (iv_r), \ 874 #iv_l, #iv_r, \ 875 mhdt_STR_MACRO_EXP (iv_l), mhdt_STR_MACRO_EXP (iv_r), \ 876 __FILE__, __LINE__, MHD_FUNC_, NULL) 877 #define MHDT_EXPECT_INT_GT_VAL(iv_l, iv_r) \ 878 MHDT_check_i_cmp ((iv_l), MHDT_CMP_GT, (iv_r), \ 879 #iv_l, #iv_r, \ 880 mhdt_STR_MACRO_EXP (iv_l), mhdt_STR_MACRO_EXP (iv_r), \ 881 __FILE__, __LINE__, MHD_FUNC_, NULL) 882 883 #define MHDT_EXPECT_INT_EQ_VAL_D(iv_l, iv_r, desc) \ 884 MHDT_check_i_cmp ((iv_l), MHDT_CMP_EQ, (iv_r), \ 885 #iv_l, #iv_r, \ 886 mhdt_STR_MACRO_EXP (iv_l), mhdt_STR_MACRO_EXP (iv_r), \ 887 __FILE__, __LINE__, MHD_FUNC_, desc) 888 #define MHDT_EXPECT_INT_LT_VAL_D(iv_l, iv_r, desc) \ 889 MHDT_check_i_cmp ((iv_l), MHDT_CMP_LT, (iv_r), \ 890 #iv_l, #iv_r, \ 891 mhdt_STR_MACRO_EXP (iv_l), mhdt_STR_MACRO_EXP (iv_r), \ 892 __FILE__, __LINE__, MHD_FUNC_, desc) 893 #define MHDT_EXPECT_INT_LE_VAL_D(iv_l, iv_r, desc) \ 894 MHDT_check_i_cmp ((iv_l), MHDT_CMP_LE, (iv_r), \ 895 #iv_l, #iv_r, \ 896 mhdt_STR_MACRO_EXP (iv_l), mhdt_STR_MACRO_EXP (iv_r), \ 897 __FILE__, __LINE__, MHD_FUNC_, desc) 898 #define MHDT_EXPECT_INT_NE_VAL_D(iv_l, iv_r, desc) \ 899 MHDT_check_i_cmp ((iv_l), MHDT_CMP_NE, (iv_r), \ 900 #iv_l, #iv_r, \ 901 mhdt_STR_MACRO_EXP (iv_l), mhdt_STR_MACRO_EXP (iv_r), \ 902 __FILE__, __LINE__, MHD_FUNC_, desc) 903 #define MHDT_EXPECT_INT_GE_VAL_D(iv_l, iv_r, desc) \ 904 MHDT_check_i_cmp ((iv_l), MHDT_CMP_GE, (iv_r), \ 905 #iv_l, #iv_r, \ 906 mhdt_STR_MACRO_EXP (iv_l), mhdt_STR_MACRO_EXP (iv_r), \ 907 __FILE__, __LINE__, MHD_FUNC_, desc) 908 #define MHDT_EXPECT_INT_GT_VAL_D(iv_l, iv_r, desc) \ 909 MHDT_check_i_cmp ((iv_l), MHDT_CMP_GT, (iv_r), \ 910 #iv_l, #iv_r, \ 911 mhdt_STR_MACRO_EXP (iv_l), mhdt_STR_MACRO_EXP (iv_r), \ 912 __FILE__, __LINE__, MHD_FUNC_, desc) 913 914 915 /* Return non-zero if succeeded, zero if failed */ 916 static MHD_FN_PAR_CSTR_ (1) MHD_FN_PAR_CSTR_ (2) inline int 917 MHDT_check_str (const char *result, 918 const char *expected, 919 const char *check_str, 920 const char *check_str_exp, 921 const char *filename, 922 long line, 923 const char *func_name, 924 const char *description) 925 { 926 #ifdef MHD_HAVE_MHD_FUNC_ 927 const int have_func_name = ((NULL != func_name) && (0 != func_name[0])); 928 #else /* ! MHD_HAVE_MHD_FUNC_ */ 929 const int have_func_name = 0; 930 #endif 931 const int have_descr = ((NULL != description) && (0 != description[0])); 932 int exp_different; 933 int check_succeed; 934 935 if (NULL == expected) 936 MHDT_ERR_EXIT_D ("'expected' must not be NULL"); 937 938 check_succeed = ((NULL != result) && (0 == strcmp (result, expected))); 939 940 if (check_succeed && !mhdt_print_check_succeed ()) 941 return check_succeed; 942 943 mhdt_mark_print_in_test_set (); 944 exp_different = (0 != strcmp (check_str, check_str_exp)); 945 fprintf (check_succeed ? stdout : stderr, 946 "%s:%s%s\n" 947 "%s:%ld: %s%sThe value of '%s'%s%s%s:\n" 948 "\tActual: \"%s\"\tExpected: \"%s\"\n", 949 (check_succeed ? "Pass" : "FAIL"), 950 (have_descr ? " " : ""), 951 (have_descr ? description : ""), 952 filename, 953 line, 954 (have_func_name ? func_name : ""), 955 (have_func_name ? "(): " : ""), 956 check_str, 957 (exp_different ? " (expanded: '" : ""), 958 (exp_different ? check_str_exp : ""), 959 (exp_different ? "')" : ""), 960 ((NULL == result) ? MHDT_NULL_PRNT : result), 961 expected); 962 fflush (check_succeed ? stdout : stderr); 963 964 MHDT_checks_err_counter += (check_succeed ? 0 : 1); 965 966 return check_succeed; 967 } 968 969 970 #define MHDT_EXPECT_STR_EQ(str, expected) \ 971 MHDT_check_str ((str), (expected), \ 972 #str, mhdt_STR_MACRO_EXP (str), \ 973 __FILE__, __LINE__, MHD_FUNC_, \ 974 NULL) 975 #define MHDT_EXPECT_STR_EQ_D(str, expected, desc) \ 976 MHDT_check_str ((str), (expected), \ 977 #str, mhdt_STR_MACRO_EXP (str), \ 978 __FILE__, __LINE__, MHD_FUNC_, \ 979 desc) 980 981 /* 'result' must not be NULL */ 982 /* Return non-zero if succeeded, zero if failed */ 983 static MHD_FN_PAR_CSTR_ (3) inline int 984 MHDT_check_strn_str (size_t result_len, 985 const char *result, 986 const char *expected, 987 const char *filename, 988 long line, 989 const char *func_name, 990 const char *description) 991 { 992 #ifdef MHD_HAVE_MHD_FUNC_ 993 const int have_func_name = ((NULL != func_name) && (0 != func_name[0])); 994 #else /* ! MHD_HAVE_MHD_FUNC_ */ 995 const int have_func_name = 0; 996 #endif 997 const int have_descr = ((NULL != description) && (0 != description[0])); 998 int check_succeed; 999 1000 if (NULL == expected) 1001 MHDT_ERR_EXIT_D ("'expected' must not be NULL"); 1002 1003 check_succeed = ((NULL != result) 1004 && (result_len == strlen (expected)) 1005 && (0 == memcmp (result, expected, result_len))); 1006 1007 if (check_succeed && !mhdt_print_check_succeed ()) 1008 return check_succeed; 1009 1010 mhdt_mark_print_in_test_set (); 1011 fprintf (check_succeed ? stdout : stderr, 1012 "%s:%s%s\n" 1013 "%s:%ld: %s%s\n" 1014 "\tActual: \"%.*s\" (len: %lu)" /* mis-print binary zero */ 1015 "\tExpected: \"%s\" (len: %lu)\n", 1016 (check_succeed ? "Pass" : "FAIL"), 1017 (have_descr ? " " : ""), 1018 (have_descr ? description : ""), 1019 filename, 1020 line, 1021 (have_func_name ? func_name : ""), 1022 (have_func_name ? "(): " : ""), 1023 (int)(result ? result_len : strlen (MHDT_NULL_PRNT)), 1024 (result ? result : MHDT_NULL_PRNT), (unsigned long)result_len, 1025 expected, (unsigned long)strlen (expected)); 1026 fflush (check_succeed ? stdout : stderr); 1027 1028 MHDT_checks_err_counter += (check_succeed ? 0 : 1); 1029 1030 return check_succeed; 1031 } 1032 1033 1034 #define MHDT_EXPECT_STRN_EQ_STR_D(str_len, str, expected, desc) \ 1035 MHDT_check_strn_str ((str_len), (str), (expected), \ 1036 __FILE__, __LINE__, MHD_FUNC_, \ 1037 desc) 1038 1039 1040 /* if 'result_len' is zero then 'result' is ignored */ 1041 /* Return non-zero if succeeded, zero if failed */ 1042 static inline int 1043 MHDT_check_strn (size_t result_len, 1044 const char *result, 1045 size_t expected_len, 1046 const char *expected, 1047 const char *filename, 1048 long line, 1049 const char *func_name, 1050 const char *description) 1051 { 1052 #ifdef MHD_HAVE_MHD_FUNC_ 1053 const int have_func_name = ((NULL != func_name) && (0 != func_name[0])); 1054 #else /* ! MHD_HAVE_MHD_FUNC_ */ 1055 const int have_func_name = 0; 1056 #endif 1057 const int have_descr = ((NULL != description) && (0 != description[0])); 1058 int check_succeed; 1059 1060 if ((NULL == expected) && (0u != expected_len)) 1061 MHDT_ERR_EXIT_D ("'expected' must not be NULL or " \ 1062 "'expected_len' must be zero "); 1063 1064 check_succeed = (result_len == expected_len); 1065 if (check_succeed && (0u != expected_len)) 1066 check_succeed = (0 == memcmp (result, expected, result_len)); 1067 1068 if (check_succeed && !mhdt_print_check_succeed ()) 1069 return check_succeed; 1070 1071 mhdt_mark_print_in_test_set (); 1072 fprintf (check_succeed ? stdout : stderr, 1073 "%s:%s%s\n" 1074 "%s:%ld: %s%s\n" 1075 "\tActual: \"%.*s\" (len: %lu)" /* mis-print binary zero */ 1076 "\tExpected: \"%.*s\" (len: %lu)\n", /* mis-print binary zero */ 1077 (check_succeed ? "Pass" : "FAIL"), 1078 (have_descr ? " " : ""), 1079 (have_descr ? description : ""), 1080 filename, 1081 line, 1082 (have_func_name ? func_name : ""), 1083 (have_func_name ? "(): " : ""), 1084 (int)(result ? result_len : strlen (MHDT_NULL_PRNT)), 1085 (result ? result : MHDT_NULL_PRNT), (unsigned long)result_len, 1086 (int)(expected ? expected_len : strlen (MHDT_NULL_PRNT)), 1087 (expected ? expected : MHDT_NULL_PRNT), 1088 (unsigned long)expected_len); 1089 fflush (check_succeed ? stdout : stderr); 1090 1091 MHDT_checks_err_counter += (check_succeed ? 0 : 1); 1092 1093 return check_succeed; 1094 } 1095 1096 1097 #define MHDT_EXPECT_STRN_EQ_D(str_len, str, expected_len, expected, desc) \ 1098 MHDT_check_strn ((str_len), (str), (expected_len), (expected), \ 1099 __FILE__, __LINE__, MHD_FUNC_, \ 1100 desc) 1101 1102 1103 #if defined(_MSC_VER) 1104 /* Restore warnings */ 1105 # pragma warning(pop) 1106 #endif /* _MSC_VER */ 1107 1108 #endif /* ! MHDT_CHECKS_H */