test/error_test.cc
author Balazs Dezso <deba@inf.elte.hu>
Wed, 26 Mar 2008 17:28:28 +0100
changeset 109 abddaa08b507
parent 66 5f7a8570687d
child 118 407c08a0eae9
permissions -rw-r--r--
Porting SmartGraph from svn -r 3481
kpeter@66
     1
/* -*- C++ -*-
kpeter@66
     2
 *
kpeter@66
     3
 * This file is a part of LEMON, a generic C++ optimization library
kpeter@66
     4
 *
kpeter@66
     5
 * Copyright (C) 2003-2008
kpeter@66
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
kpeter@66
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
kpeter@66
     8
 *
kpeter@66
     9
 * Permission to use, modify and distribute this software is granted
kpeter@66
    10
 * provided that this copyright notice appears in all copies. For
kpeter@66
    11
 * precise terms see the accompanying LICENSE file.
kpeter@66
    12
 *
kpeter@66
    13
 * This software is provided "AS IS" with no warranty of any kind,
kpeter@66
    14
 * express or implied, and with no claim as to its suitability for any
kpeter@66
    15
 * purpose.
kpeter@66
    16
 *
kpeter@66
    17
 */
kpeter@66
    18
kpeter@66
    19
#include <iostream>
kpeter@66
    20
kpeter@66
    21
#include <lemon/error.h>
kpeter@66
    22
#include "test_tools.h"
kpeter@66
    23
kpeter@66
    24
using namespace lemon;
kpeter@66
    25
deba@108
    26
#ifdef LEMON_ENABLE_ASSERTS
deba@108
    27
#undef LEMON_ENABLE_ASSERTS
deba@108
    28
#endif
deba@108
    29
deba@108
    30
#ifdef LEMON_DISABLE_ASSERTS
deba@108
    31
#undef LEMON_DISABLE_ASSERTS
deba@108
    32
#endif
deba@108
    33
deba@108
    34
//checking disabled asserts
deba@108
    35
#define LEMON_DISABLE_ASSERTS
deba@108
    36
#include <lemon/assert.h>
deba@108
    37
deba@108
    38
void no_assertion_text_disable() {
deba@108
    39
  LEMON_ASSERT(true, "This is a fault message");
kpeter@66
    40
}
kpeter@66
    41
deba@108
    42
void no_assertion_exception_disable() {
deba@108
    43
  LEMON_ASSERT(true, Exception());
kpeter@66
    44
}
kpeter@66
    45
deba@108
    46
void assertion_text_disable() {
deba@108
    47
  LEMON_ASSERT(false, "This is a fault message");
kpeter@66
    48
}
kpeter@66
    49
deba@108
    50
void assertion_exception_disable() {
deba@108
    51
  LEMON_ASSERT(false, Exception());
deba@108
    52
}
kpeter@66
    53
deba@108
    54
void fixme_disable() {
deba@108
    55
  LEMON_FIXME("fixme_disable() is fixme!");
deba@108
    56
}
deba@108
    57
deba@108
    58
void check_assertion_disable() {
deba@108
    59
  no_assertion_text_disable();
deba@108
    60
  no_assertion_exception_disable();
deba@108
    61
  assertion_exception_disable();
deba@108
    62
  assertion_text_disable();
deba@108
    63
  fixme_disable();
deba@108
    64
}
deba@108
    65
#undef LEMON_DISABLE_ASSERTS
deba@108
    66
deba@108
    67
deba@108
    68
#define LEMON_ASSERT_ERROR
deba@108
    69
#include <lemon/assert.h>
deba@108
    70
deba@108
    71
void no_assertion_text_error() {
deba@108
    72
  LEMON_ASSERT(true, "This is a fault message");
deba@108
    73
}
deba@108
    74
deba@108
    75
void no_assertion_exception_error() {
deba@108
    76
  LEMON_ASSERT(true, Exception());
deba@108
    77
}
deba@108
    78
deba@108
    79
void assertion_text_error() {
deba@108
    80
  LEMON_ASSERT(false, "This is a fault message");
deba@108
    81
}
deba@108
    82
deba@108
    83
void assertion_exception_error() {
deba@108
    84
  LEMON_ASSERT(false, Exception());
deba@108
    85
}
deba@108
    86
deba@108
    87
void fixme_error() {
deba@108
    88
  LEMON_FIXME("fixme_error() is fixme!");
deba@108
    89
}
deba@108
    90
deba@108
    91
void check_assertion_error() {
deba@108
    92
  no_assertion_text_error();
deba@108
    93
  no_assertion_exception_error();
kpeter@66
    94
  try {
deba@108
    95
    assertion_exception_error();
deba@108
    96
    check(false, "Assertion error");
deba@108
    97
  } catch (const AssertionFailedError& e) {
kpeter@66
    98
  }
kpeter@66
    99
kpeter@66
   100
  try {
deba@108
   101
    assertion_text_error();
deba@108
   102
    check(false, "Assertion error");
deba@108
   103
  } catch (const AssertionFailedError& e) {
kpeter@66
   104
  }
kpeter@66
   105
kpeter@66
   106
  try {
deba@108
   107
    fixme_error();
deba@108
   108
    check(false, "Assertion error");
deba@108
   109
  } catch (const AssertionFailedError& e) {
kpeter@66
   110
  }
deba@108
   111
}
deba@108
   112
#undef LEMON_ASSERT_ERROR
deba@108
   113
deba@108
   114
#define LEMON_ASSERT_EXCEPTION
deba@108
   115
#include <lemon/assert.h>
deba@108
   116
deba@108
   117
void no_assertion_text_exception() {
deba@108
   118
  LEMON_ASSERT(true, "This is a fault message");
deba@108
   119
}
deba@108
   120
deba@108
   121
void no_assertion_exception_exception() {
deba@108
   122
  LEMON_ASSERT(true, Exception());
deba@108
   123
}
deba@108
   124
deba@108
   125
void assertion_text_exception() {
deba@108
   126
  LEMON_ASSERT(false, "This is a fault message");
deba@108
   127
}
deba@108
   128
deba@108
   129
void assertion_exception_exception() {
deba@108
   130
  LEMON_ASSERT(false, Exception());
deba@108
   131
}
deba@108
   132
deba@108
   133
void fixme_exception() {
deba@108
   134
  LEMON_FIXME("fixme_exception() is fixme!");
deba@108
   135
}
deba@108
   136
deba@108
   137
void check_assertion_exception() {
deba@108
   138
  no_assertion_text_exception();
deba@108
   139
  no_assertion_exception_exception();
deba@108
   140
  try {
deba@108
   141
    assertion_exception_exception();
deba@108
   142
    check(false, "Assertion error");
deba@108
   143
  } catch (const Exception& e) {
kpeter@66
   144
  }
kpeter@66
   145
deba@108
   146
  try {
deba@108
   147
    assertion_text_exception();
deba@108
   148
    check(false, "Assertion error");
deba@108
   149
  } catch (const AssertionFailedError& e) {
deba@108
   150
  }
deba@108
   151
deba@108
   152
  try {
deba@108
   153
    assertion_text_exception();
deba@108
   154
    check(false, "Assertion error");
deba@108
   155
  } catch (const AssertionFailedError& e) {
deba@108
   156
  }
deba@108
   157
deba@108
   158
  try {
deba@108
   159
    fixme_exception();
deba@108
   160
    check(false, "Assertion error");
deba@108
   161
  } catch (const AssertionFailedError& e) {
deba@108
   162
  }
deba@108
   163
}
deba@108
   164
#undef LEMON_ASSERT_EXCEPTION
deba@108
   165
deba@108
   166
#define LEMON_ASSERT_LOG
deba@108
   167
deba@108
   168
#include <lemon/assert.h>
deba@108
   169
deba@108
   170
void no_assertion_text_log() {
deba@108
   171
  LEMON_ASSERT(true, "This is a fault message");
deba@108
   172
}
deba@108
   173
deba@108
   174
void no_assertion_exception_log() {
deba@108
   175
  LEMON_ASSERT(true, Exception());
deba@108
   176
}
deba@108
   177
deba@108
   178
void assertion_text_log() {
deba@108
   179
  LEMON_ASSERT(false, "This is a fault message");
deba@108
   180
}
deba@108
   181
deba@108
   182
void assertion_exception_log() {
deba@108
   183
  LEMON_ASSERT(false, Exception());
deba@108
   184
}
deba@108
   185
deba@108
   186
void fixme_log() {
deba@108
   187
  LEMON_FIXME("fixme_log() is fixme!");
deba@108
   188
}
deba@108
   189
deba@108
   190
void check_assertion_log() {
deba@108
   191
  no_assertion_text_log();
deba@108
   192
  no_assertion_exception_log();
deba@108
   193
  std::cerr << "The next 3 failure messages are expected: " << std::endl;
deba@108
   194
  assertion_exception_log();
deba@108
   195
  assertion_text_log();
deba@108
   196
  fixme_log();
deba@108
   197
  std::cerr << "End of expected error messages" << std::endl;
deba@108
   198
}
deba@108
   199
#undef LEMON_ASSERT_LOG
deba@108
   200
deba@108
   201
#define LEMON_ASSERT_CUSTOM
deba@108
   202
deba@108
   203
static int cnt = 0;
deba@108
   204
void my_assert_handler(const char*, int, const char*, 
deba@108
   205
		       const char*, const char*) {
deba@108
   206
  ++cnt;
deba@108
   207
}
deba@108
   208
deba@108
   209
void my_assert_handler(const char*, int, const char*, 
deba@108
   210
		       const std::exception&, const char*) {
deba@108
   211
  ++cnt;
deba@108
   212
}
deba@108
   213
deba@108
   214
void my_assert_handler(const char*, int, const char*, 
deba@108
   215
		       const std::string&, const char*) {
deba@108
   216
  ++cnt;
deba@108
   217
}
deba@108
   218
deba@108
   219
deba@108
   220
#define LEMON_CUSTOM_ASSERT_HANDLER my_assert_handler
deba@108
   221
#include <lemon/assert.h>
deba@108
   222
deba@108
   223
void no_assertion_text_custom() {
deba@108
   224
  LEMON_ASSERT(true, "This is a fault message");
deba@108
   225
}
deba@108
   226
deba@108
   227
void no_assertion_exception_custom() {
deba@108
   228
  LEMON_ASSERT(true, Exception());
deba@108
   229
}
deba@108
   230
deba@108
   231
void assertion_text_custom() {
deba@108
   232
  LEMON_ASSERT(false, "This is a fault message");
deba@108
   233
}
deba@108
   234
deba@108
   235
void assertion_exception_custom() {
deba@108
   236
  LEMON_ASSERT(false, Exception());
deba@108
   237
}
deba@108
   238
deba@108
   239
void fixme_custom() {
deba@108
   240
  LEMON_FIXME("fixme_custom() is fixme!");
deba@108
   241
}
deba@108
   242
deba@108
   243
void check_assertion_custom() {
deba@108
   244
  no_assertion_text_custom();
deba@108
   245
  no_assertion_exception_custom();
deba@108
   246
  assertion_exception_custom();
deba@108
   247
  assertion_text_custom();
deba@108
   248
  fixme_custom();
deba@108
   249
  check(cnt == 3, "The custom assert handler does not work");
deba@108
   250
}
deba@108
   251
deba@108
   252
#undef LEMON_ASSERT_CUSTOM
deba@108
   253
deba@108
   254
deba@108
   255
int main() {
deba@108
   256
  check_assertion_disable();
deba@108
   257
  check_assertion_error();
deba@108
   258
  check_assertion_exception();
deba@108
   259
  check_assertion_log();
deba@108
   260
  check_assertion_custom();
deba@108
   261
kpeter@66
   262
  return 0;
kpeter@66
   263
}