test/error_test.cc
changeset 108 889d0c289d19
parent 66 5f7a8570687d
child 118 407c08a0eae9
     1.1 --- a/test/error_test.cc	Fri Mar 21 22:25:40 2008 +0000
     1.2 +++ b/test/error_test.cc	Tue Mar 25 16:36:44 2008 +0100
     1.3 @@ -22,47 +22,242 @@
     1.4  #include "test_tools.h"
     1.5  
     1.6  using namespace lemon;
     1.7 -using std::cout;
     1.8 -using std::endl;
     1.9  
    1.10 -void faulty_fn() {
    1.11 -  fault("This is a fault message");
    1.12 +#ifdef LEMON_ENABLE_ASSERTS
    1.13 +#undef LEMON_ENABLE_ASSERTS
    1.14 +#endif
    1.15 +
    1.16 +#ifdef LEMON_DISABLE_ASSERTS
    1.17 +#undef LEMON_DISABLE_ASSERTS
    1.18 +#endif
    1.19 +
    1.20 +//checking disabled asserts
    1.21 +#define LEMON_DISABLE_ASSERTS
    1.22 +#include <lemon/assert.h>
    1.23 +
    1.24 +void no_assertion_text_disable() {
    1.25 +  LEMON_ASSERT(true, "This is a fault message");
    1.26  }
    1.27  
    1.28 -void exception_fn() {
    1.29 -  throw Exception("This is a function throwing exception with some args: ")
    1.30 -    << 5 << ", " << 18;
    1.31 +void no_assertion_exception_disable() {
    1.32 +  LEMON_ASSERT(true, Exception());
    1.33  }
    1.34  
    1.35 -void unfinished_fn() {
    1.36 -  LEMON_FIXME("unfinished_fn() is unfinished!");
    1.37 +void assertion_text_disable() {
    1.38 +  LEMON_ASSERT(false, "This is a fault message");
    1.39  }
    1.40  
    1.41 +void assertion_exception_disable() {
    1.42 +  LEMON_ASSERT(false, Exception());
    1.43 +}
    1.44  
    1.45 -int main() {
    1.46 +void fixme_disable() {
    1.47 +  LEMON_FIXME("fixme_disable() is fixme!");
    1.48 +}
    1.49 +
    1.50 +void check_assertion_disable() {
    1.51 +  no_assertion_text_disable();
    1.52 +  no_assertion_exception_disable();
    1.53 +  assertion_exception_disable();
    1.54 +  assertion_text_disable();
    1.55 +  fixme_disable();
    1.56 +}
    1.57 +#undef LEMON_DISABLE_ASSERTS
    1.58 +
    1.59 +
    1.60 +#define LEMON_ASSERT_ERROR
    1.61 +#include <lemon/assert.h>
    1.62 +
    1.63 +void no_assertion_text_error() {
    1.64 +  LEMON_ASSERT(true, "This is a fault message");
    1.65 +}
    1.66 +
    1.67 +void no_assertion_exception_error() {
    1.68 +  LEMON_ASSERT(true, Exception());
    1.69 +}
    1.70 +
    1.71 +void assertion_text_error() {
    1.72 +  LEMON_ASSERT(false, "This is a fault message");
    1.73 +}
    1.74 +
    1.75 +void assertion_exception_error() {
    1.76 +  LEMON_ASSERT(false, Exception());
    1.77 +}
    1.78 +
    1.79 +void fixme_error() {
    1.80 +  LEMON_FIXME("fixme_error() is fixme!");
    1.81 +}
    1.82 +
    1.83 +void check_assertion_error() {
    1.84 +  no_assertion_text_error();
    1.85 +  no_assertion_exception_error();
    1.86    try {
    1.87 -    faulty_fn();
    1.88 -    check(false, "A faulty function did not fail.");
    1.89 -  }
    1.90 -  catch(const Exception &e) {
    1.91 -    cout << "Exeption = \"" << e.what() << "\" (Right behaviour)" << endl;
    1.92 +    assertion_exception_error();
    1.93 +    check(false, "Assertion error");
    1.94 +  } catch (const AssertionFailedError& e) {
    1.95    }
    1.96  
    1.97    try {
    1.98 -    exception_fn();
    1.99 -    check(false, "The function did not throw Exception.");
   1.100 -  }
   1.101 -  catch(const Exception &e) {
   1.102 -    cout << "Exeption = \"" << e.what() << "\" (Right behaviour)" << endl;
   1.103 +    assertion_text_error();
   1.104 +    check(false, "Assertion error");
   1.105 +  } catch (const AssertionFailedError& e) {
   1.106    }
   1.107  
   1.108    try {
   1.109 -    unfinished_fn();
   1.110 -    check(false, "FIXME macro does not work.");
   1.111 +    fixme_error();
   1.112 +    check(false, "Assertion error");
   1.113 +  } catch (const AssertionFailedError& e) {
   1.114    }
   1.115 -  catch(const Exception &e) {
   1.116 -    cout << "Exeption = \"" << e.what() << "\" (Right behaviour)" << endl;
   1.117 +}
   1.118 +#undef LEMON_ASSERT_ERROR
   1.119 +
   1.120 +#define LEMON_ASSERT_EXCEPTION
   1.121 +#include <lemon/assert.h>
   1.122 +
   1.123 +void no_assertion_text_exception() {
   1.124 +  LEMON_ASSERT(true, "This is a fault message");
   1.125 +}
   1.126 +
   1.127 +void no_assertion_exception_exception() {
   1.128 +  LEMON_ASSERT(true, Exception());
   1.129 +}
   1.130 +
   1.131 +void assertion_text_exception() {
   1.132 +  LEMON_ASSERT(false, "This is a fault message");
   1.133 +}
   1.134 +
   1.135 +void assertion_exception_exception() {
   1.136 +  LEMON_ASSERT(false, Exception());
   1.137 +}
   1.138 +
   1.139 +void fixme_exception() {
   1.140 +  LEMON_FIXME("fixme_exception() is fixme!");
   1.141 +}
   1.142 +
   1.143 +void check_assertion_exception() {
   1.144 +  no_assertion_text_exception();
   1.145 +  no_assertion_exception_exception();
   1.146 +  try {
   1.147 +    assertion_exception_exception();
   1.148 +    check(false, "Assertion error");
   1.149 +  } catch (const Exception& e) {
   1.150    }
   1.151  
   1.152 +  try {
   1.153 +    assertion_text_exception();
   1.154 +    check(false, "Assertion error");
   1.155 +  } catch (const AssertionFailedError& e) {
   1.156 +  }
   1.157 +
   1.158 +  try {
   1.159 +    assertion_text_exception();
   1.160 +    check(false, "Assertion error");
   1.161 +  } catch (const AssertionFailedError& e) {
   1.162 +  }
   1.163 +
   1.164 +  try {
   1.165 +    fixme_exception();
   1.166 +    check(false, "Assertion error");
   1.167 +  } catch (const AssertionFailedError& e) {
   1.168 +  }
   1.169 +}
   1.170 +#undef LEMON_ASSERT_EXCEPTION
   1.171 +
   1.172 +#define LEMON_ASSERT_LOG
   1.173 +
   1.174 +#include <lemon/assert.h>
   1.175 +
   1.176 +void no_assertion_text_log() {
   1.177 +  LEMON_ASSERT(true, "This is a fault message");
   1.178 +}
   1.179 +
   1.180 +void no_assertion_exception_log() {
   1.181 +  LEMON_ASSERT(true, Exception());
   1.182 +}
   1.183 +
   1.184 +void assertion_text_log() {
   1.185 +  LEMON_ASSERT(false, "This is a fault message");
   1.186 +}
   1.187 +
   1.188 +void assertion_exception_log() {
   1.189 +  LEMON_ASSERT(false, Exception());
   1.190 +}
   1.191 +
   1.192 +void fixme_log() {
   1.193 +  LEMON_FIXME("fixme_log() is fixme!");
   1.194 +}
   1.195 +
   1.196 +void check_assertion_log() {
   1.197 +  no_assertion_text_log();
   1.198 +  no_assertion_exception_log();
   1.199 +  std::cerr << "The next 3 failure messages are expected: " << std::endl;
   1.200 +  assertion_exception_log();
   1.201 +  assertion_text_log();
   1.202 +  fixme_log();
   1.203 +  std::cerr << "End of expected error messages" << std::endl;
   1.204 +}
   1.205 +#undef LEMON_ASSERT_LOG
   1.206 +
   1.207 +#define LEMON_ASSERT_CUSTOM
   1.208 +
   1.209 +static int cnt = 0;
   1.210 +void my_assert_handler(const char*, int, const char*, 
   1.211 +		       const char*, const char*) {
   1.212 +  ++cnt;
   1.213 +}
   1.214 +
   1.215 +void my_assert_handler(const char*, int, const char*, 
   1.216 +		       const std::exception&, const char*) {
   1.217 +  ++cnt;
   1.218 +}
   1.219 +
   1.220 +void my_assert_handler(const char*, int, const char*, 
   1.221 +		       const std::string&, const char*) {
   1.222 +  ++cnt;
   1.223 +}
   1.224 +
   1.225 +
   1.226 +#define LEMON_CUSTOM_ASSERT_HANDLER my_assert_handler
   1.227 +#include <lemon/assert.h>
   1.228 +
   1.229 +void no_assertion_text_custom() {
   1.230 +  LEMON_ASSERT(true, "This is a fault message");
   1.231 +}
   1.232 +
   1.233 +void no_assertion_exception_custom() {
   1.234 +  LEMON_ASSERT(true, Exception());
   1.235 +}
   1.236 +
   1.237 +void assertion_text_custom() {
   1.238 +  LEMON_ASSERT(false, "This is a fault message");
   1.239 +}
   1.240 +
   1.241 +void assertion_exception_custom() {
   1.242 +  LEMON_ASSERT(false, Exception());
   1.243 +}
   1.244 +
   1.245 +void fixme_custom() {
   1.246 +  LEMON_FIXME("fixme_custom() is fixme!");
   1.247 +}
   1.248 +
   1.249 +void check_assertion_custom() {
   1.250 +  no_assertion_text_custom();
   1.251 +  no_assertion_exception_custom();
   1.252 +  assertion_exception_custom();
   1.253 +  assertion_text_custom();
   1.254 +  fixme_custom();
   1.255 +  check(cnt == 3, "The custom assert handler does not work");
   1.256 +}
   1.257 +
   1.258 +#undef LEMON_ASSERT_CUSTOM
   1.259 +
   1.260 +
   1.261 +int main() {
   1.262 +  check_assertion_disable();
   1.263 +  check_assertion_error();
   1.264 +  check_assertion_exception();
   1.265 +  check_assertion_log();
   1.266 +  check_assertion_custom();
   1.267 +
   1.268    return 0;
   1.269  }