?login_element?

Subversion Repositories NedoOS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. #ifndef UNITTEST_CHECKMACROS_H
  2. #define UNITTEST_CHECKMACROS_H
  3.  
  4. #include "HelperMacros.h"
  5. #include "ExceptionMacros.h"
  6. #include "Checks.h"
  7. #include "AssertException.h"
  8. #include "RequiredCheckException.h"
  9. #include "MemoryOutStream.h"
  10. #include "TestDetails.h"
  11. #include "CurrentTest.h"
  12. #include "ReportAssertImpl.h"
  13.  
  14. #define UNITTEST_CHECK(value)                                      \
  15.    UNITTEST_MULTILINE_MACRO_BEGIN                                                                                                     \
  16.    UNITTEST_IMPL_TRY                                                                                                                             \
  17.    ({                                                                                                                                 \
  18.       if (!UnitTest::Check(value))                                                                                                    \
  19.          UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), #value); \
  20.    })                                                                                                                                 \
  21.    UNITTEST_IMPL_RETHROW (UnitTest::RequiredCheckException)                                                                                                               \
  22.    UNITTEST_IMPL_CATCH (std::exception, e,                                                                                                       \
  23.    {                                                                                                                                  \
  24.       UnitTest::MemoryOutStream UnitTest_message;                                                                                     \
  25.       UnitTest_message << "Unhandled exception (" << e.what() << ") in CHECK(" #value ")";                                            \
  26.       UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__),             \
  27.                                                       UnitTest_message.GetText());                                                    \
  28.    })                                                                                                                                 \
  29.    UNITTEST_IMPL_CATCH_ALL                                                                                                                       \
  30.       ({                                                                                                                              \
  31.       UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__),             \
  32.                                                       "Unhandled exception in CHECK(" #value ")");                                    \
  33.    })                                                                                                                                 \
  34.    UNITTEST_MULTILINE_MACRO_END
  35.  
  36. #define UNITTEST_CHECK_EQUAL(expected, actual)                                                                                                                \
  37.    UNITTEST_MULTILINE_MACRO_BEGIN                                                                                                                    \
  38.    UNITTEST_IMPL_TRY                                                                                                                                            \
  39.    ({                                                                                                                                                \
  40.       UnitTest::CheckEqual(*UnitTest::CurrentTest::Results(), expected, actual, UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__)); \
  41.    })                                                                                                                                                \
  42.    UNITTEST_IMPL_RETHROW (UnitTest::RequiredCheckException)                                                                                                               \
  43.    UNITTEST_IMPL_CATCH (std::exception, e,                                                                                                                      \
  44.    {                                                                                                                                                 \
  45.       UnitTest::MemoryOutStream UnitTest_message;                                                                                                    \
  46.       UnitTest_message << "Unhandled exception (" << e.what() << ") in CHECK_EQUAL(" #expected ", " #actual ")";                                     \
  47.       UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__),                            \
  48.                                                       UnitTest_message.GetText());                                                                   \
  49.    })                                                                                                                                                \
  50.    UNITTEST_IMPL_CATCH_ALL                                                                                                                                      \
  51.    ({                                                                                                                                                \
  52.       UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__),                            \
  53.                                                       "Unhandled exception in CHECK_EQUAL(" #expected ", " #actual ")");                             \
  54.    })                                                                                                                                                \
  55.    UNITTEST_MULTILINE_MACRO_END
  56.  
  57. #define UNITTEST_CHECK_CLOSE(expected, actual, tolerance)                                                                                                                \
  58.    UNITTEST_MULTILINE_MACRO_BEGIN                                                                                                                               \
  59.    UNITTEST_IMPL_TRY                                                                                                                                                       \
  60.    ({                                                                                                                                                           \
  61.       UnitTest::CheckClose(*UnitTest::CurrentTest::Results(), expected, actual, tolerance, UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__)); \
  62.    })                                                                                                                                                           \
  63.    UNITTEST_IMPL_RETHROW (UnitTest::RequiredCheckException)                                                                                                               \
  64.    UNITTEST_IMPL_CATCH (std::exception, e,                                                                                                                                 \
  65.    {                                                                                                                                                            \
  66.       UnitTest::MemoryOutStream UnitTest_message;                                                                                                               \
  67.       UnitTest_message << "Unhandled exception (" << e.what() << ") in CHECK_CLOSE(" #expected ", " #actual ")";                                                \
  68.       UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__),                                       \
  69.                                                       UnitTest_message.GetText());                                                                              \
  70.    })                                                                                                                                                           \
  71.    UNITTEST_IMPL_CATCH_ALL                                                                                                                                                 \
  72.    ({                                                                                                                                                           \
  73.       UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__),                                       \
  74.                                                       "Unhandled exception in CHECK_CLOSE(" #expected ", " #actual ")");                                        \
  75.    })                                                                                                                                                           \
  76.    UNITTEST_MULTILINE_MACRO_END
  77.  
  78. #define UNITTEST_CHECK_ARRAY_EQUAL(expected, actual, count)                                                                                                               \
  79.    UNITTEST_MULTILINE_MACRO_BEGIN                                                                                                                                \
  80.    UNITTEST_IMPL_TRY                                                                                                                                                        \
  81.       ({                                                                                                                                                         \
  82.       UnitTest::CheckArrayEqual(*UnitTest::CurrentTest::Results(), expected, actual, count, UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__)); \
  83.    })                                                                                                                                                            \
  84.    UNITTEST_IMPL_RETHROW (UnitTest::RequiredCheckException)                                                                                                               \
  85.    UNITTEST_IMPL_CATCH (std::exception, e,                                                                                                                                  \
  86.    {                                                                                                                                                             \
  87.       UnitTest::MemoryOutStream UnitTest_message;                                                                                                                \
  88.       UnitTest_message << "Unhandled exception (" << e.what() << ") in CHECK_ARRAY_EQUAL(" #expected ", " #actual ")";                                           \
  89.       UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__),                                        \
  90.                                                       UnitTest_message.GetText());                                                                               \
  91.    })                                                                                                                                                            \
  92.    UNITTEST_IMPL_CATCH_ALL                                                                                                                                                  \
  93.       ({                                                                                                                                                         \
  94.       UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__),                                        \
  95.                                                       "Unhandled exception in CHECK_ARRAY_EQUAL(" #expected ", " #actual ")");                                   \
  96.    })                                                                                                                                                            \
  97.    UNITTEST_MULTILINE_MACRO_END
  98.  
  99. #define UNITTEST_CHECK_ARRAY_CLOSE(expected, actual, count, tolerance)                                                                                                               \
  100.    UNITTEST_MULTILINE_MACRO_BEGIN                                                                                                                                           \
  101.    UNITTEST_IMPL_TRY                                                                                                                                                                   \
  102.       ({                                                                                                                                                                    \
  103.       UnitTest::CheckArrayClose(*UnitTest::CurrentTest::Results(), expected, actual, count, tolerance, UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__)); \
  104.    })                                                                                                                                                                       \
  105.    UNITTEST_IMPL_RETHROW (UnitTest::RequiredCheckException)                                                                                                               \
  106.    UNITTEST_IMPL_CATCH (std::exception, e,                                                                                                                                             \
  107.    {                                                                                                                                                                        \
  108.       UnitTest::MemoryOutStream UnitTest_message;                                                                                                                           \
  109.       UnitTest_message << "Unhandled exception (" << e.what() << ") in CHECK_ARRAY_CLOSE(" #expected ", " #actual ")";                                                      \
  110.       UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__),                                                   \
  111.                                                       UnitTest_message.GetText());                                                                                          \
  112.    })                                                                                                                                                                       \
  113.    UNITTEST_IMPL_CATCH_ALL                                                                                                                                                             \
  114.       ({                                                                                                                                                                    \
  115.       UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__),                                                   \
  116.                                                       "Unhandled exception in CHECK_ARRAY_CLOSE(" #expected ", " #actual ")");                                              \
  117.    })                                                                                                                                                                       \
  118.    UNITTEST_MULTILINE_MACRO_END
  119.  
  120. #define UNITTEST_CHECK_ARRAY2D_CLOSE(expected, actual, rows, columns, tolerance)                                                                                                               \
  121.    UNITTEST_MULTILINE_MACRO_BEGIN                                                                                                                                                     \
  122.    UNITTEST_IMPL_TRY                                                                                                                                                                             \
  123.       ({                                                                                                                                                                              \
  124.       UnitTest::CheckArray2DClose(*UnitTest::CurrentTest::Results(), expected, actual, rows, columns, tolerance, UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__)); \
  125.    })                                                                                                                                                                                 \
  126.    UNITTEST_IMPL_RETHROW (UnitTest::RequiredCheckException)                                                                                                               \
  127.    UNITTEST_IMPL_CATCH (std::exception, e,                                                                                                                                                       \
  128.    {                                                                                                                                                                                  \
  129.       UnitTest::MemoryOutStream UnitTest_message;                                                                                                                                     \
  130.       UnitTest_message << "Unhandled exception (" << e.what() << ") in CHECK_ARRAY2D_CLOSE(" #expected ", " #actual ")";                                                              \
  131.       UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__),                                                             \
  132.                                                       UnitTest_message.GetText());                                                                                                    \
  133.    })                                                                                                                                                                                 \
  134.    UNITTEST_IMPL_CATCH_ALL                                                                                                                                                                       \
  135.       ({                                                                                                                                                                              \
  136.       UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__),                                                             \
  137.                                                       "Unhandled exception in CHECK_ARRAY2D_CLOSE(" #expected ", " #actual ")");                                                      \
  138.    })                                                                                                                                                                                 \
  139.    UNITTEST_MULTILINE_MACRO_END
  140.  
  141. #ifndef UNITTEST_DISABLE_SHORT_MACROS
  142.    #ifdef CHECK
  143.       #error CHECK already defined, re-configure with UNITTEST_ENABLE_SHORT_MACROS set to 0 and use UNITTEST_CHECK instead
  144.    #else
  145.       #define CHECK UNITTEST_CHECK
  146.    #endif
  147.  
  148.    #ifdef CHECK_EQUAL
  149.       #error CHECK_EQUAL already defined, re-configure with UNITTEST_ENABLE_SHORT_MACROS set to 0 and use UNITTEST_CHECK_EQUAL instead
  150.    #else
  151.       #define CHECK_EQUAL UNITTEST_CHECK_EQUAL
  152.    #endif
  153.  
  154.    #ifdef CHECK_CLOSE
  155.       #error CHECK_CLOSE already defined, re-configure with UNITTEST_ENABLE_SHORT_MACROS set to 0 and use UNITTEST_CHECK_CLOSE instead
  156.    #else
  157.       #define CHECK_CLOSE UNITTEST_CHECK_CLOSE
  158.    #endif
  159.  
  160.    #ifdef CHECK_ARRAY_EQUAL
  161.       #error CHECK_ARRAY_EQUAL already defined, re-configure with UNITTEST_ENABLE_SHORT_MACROS set to 0 and use UNITTEST_CHECK_ARRAY_EQUAL instead
  162.    #else
  163.       #define CHECK_ARRAY_EQUAL UNITTEST_CHECK_ARRAY_EQUAL
  164.    #endif
  165.  
  166.    #ifdef CHECK_ARRAY_CLOSE
  167.       #error CHECK_ARRAY_CLOSE already defined, re-configure with UNITTEST_ENABLE_SHORT_MACROS set to 0 and use UNITTEST_CHECK_ARRAY_CLOSE instead
  168.    #else
  169.       #define CHECK_ARRAY_CLOSE UNITTEST_CHECK_ARRAY_CLOSE
  170.    #endif
  171.  
  172.    #ifdef CHECK_ARRAY2D_CLOSE
  173.       #error CHECK_ARRAY2D_CLOSE already defined, re-configure with UNITTEST_ENABLE_SHORT_MACROS set to 0 and use UNITTEST_CHECK_ARRAY2D_CLOSE instead
  174.    #else
  175.       #define CHECK_ARRAY2D_CLOSE UNITTEST_CHECK_ARRAY2D_CLOSE
  176.    #endif
  177. #endif
  178.  
  179. // CHECK_THROW and CHECK_ASSERT only exist when UNITTEST_NO_EXCEPTIONS isn't defined (see config.h)
  180. #ifndef UNITTEST_NO_EXCEPTIONS
  181.  
  182. #define UNITTEST_CHECK_THROW(expression, ExpectedExceptionType)                                                                                                                                      \
  183.    UNITTEST_MULTILINE_MACRO_BEGIN                                                                                                                                                           \
  184.    bool caught_ = false;                                                                                                                                                                    \
  185.    try { expression; }                                                                                                                                                                      \
  186.    catch (ExpectedExceptionType const&) { caught_ = true; }                                                                                                                                 \
  187.    catch (...) {}                                                                                                                                                                           \
  188.    if (!caught_)                                                                                                                                                                            \
  189.       UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), "Expected exception: \"" #ExpectedExceptionType "\" not thrown"); \
  190.    UNITTEST_MULTILINE_MACRO_END
  191.  
  192.  
  193. #define UNITTEST_CHECK_ASSERT(expression)                       \
  194.    UNITTEST_MULTILINE_MACRO_BEGIN                      \
  195.    UnitTest::Detail::ExpectAssert(true);               \
  196.    CHECK_THROW(expression, UnitTest::AssertException); \
  197.    UnitTest::Detail::ExpectAssert(false);              \
  198.    UNITTEST_MULTILINE_MACRO_END
  199. #endif
  200.  
  201. #ifndef UNITTEST_DISABLE_SHORT_MACROS
  202.    #ifdef CHECK_THROW
  203.       #error CHECK_THROW already defined, re-configure with UNITTEST_ENABLE_SHORT_MACROS set to 0 and use UNITTEST_CHECK_THROW instead
  204.    #else
  205.       #define CHECK_THROW UNITTEST_CHECK_THROW
  206.    #endif
  207.  
  208.    #ifdef CHECK_ASSERT
  209.       #error CHECK_ASSERT already defined, re-configure with UNITTEST_ENABLE_SHORT_MACROS set to 0 and use UNITTEST_CHECK_ASSERT instead
  210.    #else
  211.       #define CHECK_ASSERT UNITTEST_CHECK_ASSERT
  212.    #endif
  213. #endif
  214.  
  215. #endif
  216.  
  217.