test/error_test.cc
changeset 119 82a2639a05bb
parent 66 5f7a8570687d
child 118 407c08a0eae9
equal deleted inserted replaced
0:50d483c63bda 1:0f06e9da6f8e
    20 
    20 
    21 #include <lemon/error.h>
    21 #include <lemon/error.h>
    22 #include "test_tools.h"
    22 #include "test_tools.h"
    23 
    23 
    24 using namespace lemon;
    24 using namespace lemon;
    25 using std::cout;
    25 
    26 using std::endl;
    26 #ifdef LEMON_ENABLE_ASSERTS
    27 
    27 #undef LEMON_ENABLE_ASSERTS
    28 void faulty_fn() {
    28 #endif
    29   fault("This is a fault message");
    29 
    30 }
    30 #ifdef LEMON_DISABLE_ASSERTS
    31 
    31 #undef LEMON_DISABLE_ASSERTS
    32 void exception_fn() {
    32 #endif
    33   throw Exception("This is a function throwing exception with some args: ")
    33 
    34     << 5 << ", " << 18;
    34 //checking disabled asserts
    35 }
    35 #define LEMON_DISABLE_ASSERTS
    36 
    36 #include <lemon/assert.h>
    37 void unfinished_fn() {
    37 
    38   LEMON_FIXME("unfinished_fn() is unfinished!");
    38 void no_assertion_text_disable() {
    39 }
    39   LEMON_ASSERT(true, "This is a fault message");
       
    40 }
       
    41 
       
    42 void no_assertion_exception_disable() {
       
    43   LEMON_ASSERT(true, Exception());
       
    44 }
       
    45 
       
    46 void assertion_text_disable() {
       
    47   LEMON_ASSERT(false, "This is a fault message");
       
    48 }
       
    49 
       
    50 void assertion_exception_disable() {
       
    51   LEMON_ASSERT(false, Exception());
       
    52 }
       
    53 
       
    54 void fixme_disable() {
       
    55   LEMON_FIXME("fixme_disable() is fixme!");
       
    56 }
       
    57 
       
    58 void check_assertion_disable() {
       
    59   no_assertion_text_disable();
       
    60   no_assertion_exception_disable();
       
    61   assertion_exception_disable();
       
    62   assertion_text_disable();
       
    63   fixme_disable();
       
    64 }
       
    65 #undef LEMON_DISABLE_ASSERTS
       
    66 
       
    67 
       
    68 #define LEMON_ASSERT_ERROR
       
    69 #include <lemon/assert.h>
       
    70 
       
    71 void no_assertion_text_error() {
       
    72   LEMON_ASSERT(true, "This is a fault message");
       
    73 }
       
    74 
       
    75 void no_assertion_exception_error() {
       
    76   LEMON_ASSERT(true, Exception());
       
    77 }
       
    78 
       
    79 void assertion_text_error() {
       
    80   LEMON_ASSERT(false, "This is a fault message");
       
    81 }
       
    82 
       
    83 void assertion_exception_error() {
       
    84   LEMON_ASSERT(false, Exception());
       
    85 }
       
    86 
       
    87 void fixme_error() {
       
    88   LEMON_FIXME("fixme_error() is fixme!");
       
    89 }
       
    90 
       
    91 void check_assertion_error() {
       
    92   no_assertion_text_error();
       
    93   no_assertion_exception_error();
       
    94   try {
       
    95     assertion_exception_error();
       
    96     check(false, "Assertion error");
       
    97   } catch (const AssertionFailedError& e) {
       
    98   }
       
    99 
       
   100   try {
       
   101     assertion_text_error();
       
   102     check(false, "Assertion error");
       
   103   } catch (const AssertionFailedError& e) {
       
   104   }
       
   105 
       
   106   try {
       
   107     fixme_error();
       
   108     check(false, "Assertion error");
       
   109   } catch (const AssertionFailedError& e) {
       
   110   }
       
   111 }
       
   112 #undef LEMON_ASSERT_ERROR
       
   113 
       
   114 #define LEMON_ASSERT_EXCEPTION
       
   115 #include <lemon/assert.h>
       
   116 
       
   117 void no_assertion_text_exception() {
       
   118   LEMON_ASSERT(true, "This is a fault message");
       
   119 }
       
   120 
       
   121 void no_assertion_exception_exception() {
       
   122   LEMON_ASSERT(true, Exception());
       
   123 }
       
   124 
       
   125 void assertion_text_exception() {
       
   126   LEMON_ASSERT(false, "This is a fault message");
       
   127 }
       
   128 
       
   129 void assertion_exception_exception() {
       
   130   LEMON_ASSERT(false, Exception());
       
   131 }
       
   132 
       
   133 void fixme_exception() {
       
   134   LEMON_FIXME("fixme_exception() is fixme!");
       
   135 }
       
   136 
       
   137 void check_assertion_exception() {
       
   138   no_assertion_text_exception();
       
   139   no_assertion_exception_exception();
       
   140   try {
       
   141     assertion_exception_exception();
       
   142     check(false, "Assertion error");
       
   143   } catch (const Exception& e) {
       
   144   }
       
   145 
       
   146   try {
       
   147     assertion_text_exception();
       
   148     check(false, "Assertion error");
       
   149   } catch (const AssertionFailedError& e) {
       
   150   }
       
   151 
       
   152   try {
       
   153     assertion_text_exception();
       
   154     check(false, "Assertion error");
       
   155   } catch (const AssertionFailedError& e) {
       
   156   }
       
   157 
       
   158   try {
       
   159     fixme_exception();
       
   160     check(false, "Assertion error");
       
   161   } catch (const AssertionFailedError& e) {
       
   162   }
       
   163 }
       
   164 #undef LEMON_ASSERT_EXCEPTION
       
   165 
       
   166 #define LEMON_ASSERT_LOG
       
   167 
       
   168 #include <lemon/assert.h>
       
   169 
       
   170 void no_assertion_text_log() {
       
   171   LEMON_ASSERT(true, "This is a fault message");
       
   172 }
       
   173 
       
   174 void no_assertion_exception_log() {
       
   175   LEMON_ASSERT(true, Exception());
       
   176 }
       
   177 
       
   178 void assertion_text_log() {
       
   179   LEMON_ASSERT(false, "This is a fault message");
       
   180 }
       
   181 
       
   182 void assertion_exception_log() {
       
   183   LEMON_ASSERT(false, Exception());
       
   184 }
       
   185 
       
   186 void fixme_log() {
       
   187   LEMON_FIXME("fixme_log() is fixme!");
       
   188 }
       
   189 
       
   190 void check_assertion_log() {
       
   191   no_assertion_text_log();
       
   192   no_assertion_exception_log();
       
   193   std::cerr << "The next 3 failure messages are expected: " << std::endl;
       
   194   assertion_exception_log();
       
   195   assertion_text_log();
       
   196   fixme_log();
       
   197   std::cerr << "End of expected error messages" << std::endl;
       
   198 }
       
   199 #undef LEMON_ASSERT_LOG
       
   200 
       
   201 #define LEMON_ASSERT_CUSTOM
       
   202 
       
   203 static int cnt = 0;
       
   204 void my_assert_handler(const char*, int, const char*, 
       
   205 		       const char*, const char*) {
       
   206   ++cnt;
       
   207 }
       
   208 
       
   209 void my_assert_handler(const char*, int, const char*, 
       
   210 		       const std::exception&, const char*) {
       
   211   ++cnt;
       
   212 }
       
   213 
       
   214 void my_assert_handler(const char*, int, const char*, 
       
   215 		       const std::string&, const char*) {
       
   216   ++cnt;
       
   217 }
       
   218 
       
   219 
       
   220 #define LEMON_CUSTOM_ASSERT_HANDLER my_assert_handler
       
   221 #include <lemon/assert.h>
       
   222 
       
   223 void no_assertion_text_custom() {
       
   224   LEMON_ASSERT(true, "This is a fault message");
       
   225 }
       
   226 
       
   227 void no_assertion_exception_custom() {
       
   228   LEMON_ASSERT(true, Exception());
       
   229 }
       
   230 
       
   231 void assertion_text_custom() {
       
   232   LEMON_ASSERT(false, "This is a fault message");
       
   233 }
       
   234 
       
   235 void assertion_exception_custom() {
       
   236   LEMON_ASSERT(false, Exception());
       
   237 }
       
   238 
       
   239 void fixme_custom() {
       
   240   LEMON_FIXME("fixme_custom() is fixme!");
       
   241 }
       
   242 
       
   243 void check_assertion_custom() {
       
   244   no_assertion_text_custom();
       
   245   no_assertion_exception_custom();
       
   246   assertion_exception_custom();
       
   247   assertion_text_custom();
       
   248   fixme_custom();
       
   249   check(cnt == 3, "The custom assert handler does not work");
       
   250 }
       
   251 
       
   252 #undef LEMON_ASSERT_CUSTOM
    40 
   253 
    41 
   254 
    42 int main() {
   255 int main() {
    43   try {
   256   check_assertion_disable();
    44     faulty_fn();
   257   check_assertion_error();
    45     check(false, "A faulty function did not fail.");
   258   check_assertion_exception();
    46   }
   259   check_assertion_log();
    47   catch(const Exception &e) {
   260   check_assertion_custom();
    48     cout << "Exeption = \"" << e.what() << "\" (Right behaviour)" << endl;
       
    49   }
       
    50 
       
    51   try {
       
    52     exception_fn();
       
    53     check(false, "The function did not throw Exception.");
       
    54   }
       
    55   catch(const Exception &e) {
       
    56     cout << "Exeption = \"" << e.what() << "\" (Right behaviour)" << endl;
       
    57   }
       
    58 
       
    59   try {
       
    60     unfinished_fn();
       
    61     check(false, "FIXME macro does not work.");
       
    62   }
       
    63   catch(const Exception &e) {
       
    64     cout << "Exeption = \"" << e.what() << "\" (Right behaviour)" << endl;
       
    65   }
       
    66 
   261 
    67   return 0;
   262   return 0;
    68 }
   263 }