Commit 4c1150395b4fffd0ccad21bd1b230f532f7b9a3e
1 parent
2cde884c
Add test_value() method for integer comparisons.
Added a test_value() method for integer comparisons that automatically informs about the mismatches. This increases the readability of the test results.
Showing
3 changed files
with
70 additions
and
1 deletions
include/GTestSuite.hpp
| ... | ... | @@ -77,6 +77,10 @@ public: |
| 77 | 77 | void test_assert(bool result, |
| 78 | 78 | const std::string& name, |
| 79 | 79 | const std::string& message=""); |
| 80 | + void test_value(const int& value, | |
| 81 | + const int& expected, | |
| 82 | + const std::string& name="", | |
| 83 | + const std::string& message=""); | |
| 80 | 84 | void test_value(const double& value, |
| 81 | 85 | const double& expected, |
| 82 | 86 | const double& eps = 1.0e-10, | ... | ... |
pyext/GTestSuite.i
| ... | ... | @@ -158,6 +158,10 @@ public: |
| 158 | 158 | void test_assert(bool result, |
| 159 | 159 | const std::string& name, |
| 160 | 160 | const std::string& message = ""); |
| 161 | + void test_value(const int& value, | |
| 162 | + const int& expected, | |
| 163 | + const std::string& name="", | |
| 164 | + const std::string& message=""); | |
| 161 | 165 | void test_value(const double& value, |
| 162 | 166 | const double& expected, |
| 163 | 167 | const double& eps = 1.0e-10, | ... | ... |
src/test/GTestSuite.cpp
| ... | ... | @@ -443,6 +443,67 @@ void GTestSuite::test_assert(bool assert, |
| 443 | 443 | * |
| 444 | 444 | * @param[in] value Value to test. |
| 445 | 445 | * @param[in] expected Expected value. |
| 446 | + * @param[in] name Test case name (defaults to ""). | |
| 447 | + * @param[in] message Test case message (defaults to ""). | |
| 448 | + * | |
| 449 | + * Test if the value is comprised in the interval | |
| 450 | + * [expected-eps, expected+eps]. | |
| 451 | + ***************************************************************************/ | |
| 452 | +void GTestSuite::test_value(const int& value, | |
| 453 | + const int& expected, | |
| 454 | + const std::string& name, | |
| 455 | + const std::string& message) | |
| 456 | +{ | |
| 457 | + // Set test case name. If no name is specify then build the name from | |
| 458 | + // the actual test parameters. | |
| 459 | + std::string formated_name; | |
| 460 | + if (name != "") { | |
| 461 | + formated_name = format_name(name); | |
| 462 | + } | |
| 463 | + else { | |
| 464 | + formated_name = format_name("Test if " + str(value) + " is " + | |
| 465 | + str(expected)); | |
| 466 | + } | |
| 467 | + | |
| 468 | + // Create a test case of failure type | |
| 469 | + GTestCase* testcase = new GTestCase(GTestCase::FAIL_TEST, formated_name); | |
| 470 | + | |
| 471 | + // If value is not the expected one then signal test as failed and | |
| 472 | + // increment the number of failures | |
| 473 | + if (value != expected) { | |
| 474 | + testcase->passed(false); | |
| 475 | + m_failures++; | |
| 476 | + } | |
| 477 | + | |
| 478 | + // If no message is specified then build message from test result | |
| 479 | + std::string formated_message; | |
| 480 | + if (message != "") { | |
| 481 | + formated_message = message; | |
| 482 | + } | |
| 483 | + else { | |
| 484 | + formated_message = "Value " + str(value) + " equals not the " + | |
| 485 | + "expected value of " + str(expected) + "."; | |
| 486 | + } | |
| 487 | + | |
| 488 | + // Set message | |
| 489 | + testcase->message(formated_message); | |
| 490 | + | |
| 491 | + // Log the result (".","F" or, "E") | |
| 492 | + std::cout << testcase->print(); | |
| 493 | + | |
| 494 | + // Add test case to test suite | |
| 495 | + m_tests.push_back(testcase); | |
| 496 | + | |
| 497 | + // Return | |
| 498 | + return; | |
| 499 | +} | |
| 500 | + | |
| 501 | + | |
| 502 | +/***********************************************************************//** | |
| 503 | + * @brief Test a value | |
| 504 | + * | |
| 505 | + * @param[in] value Value to test. | |
| 506 | + * @param[in] expected Expected value. | |
| 446 | 507 | * @param[in] eps Precision of the test (default 1e-30). |
| 447 | 508 | * @param[in] name Test case name (defaults to ""). |
| 448 | 509 | * @param[in] message Test case message (defaults to ""). |
| ... | ... | @@ -472,7 +533,7 @@ void GTestSuite::test_value(const double& value, |
| 472 | 533 | |
| 473 | 534 | // If value is not between in interval [expected-eps, expected+eps] |
| 474 | 535 | // then signal test as failed and increment the number of failures |
| 475 | - if(value > expected + eps || value < expected - eps) { | |
| 536 | + if (value > expected + eps || value < expected - eps) { | |
| 476 | 537 | testcase->passed(false); |
| 477 | 538 | m_failures++; |
| 478 | 539 | } | ... | ... |