Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
test_ah_errors.cc
Go to the documentation of this file.
1
7#include <iostream>
8#include <sstream>
9#include <cassert>
10#include <stdexcept>
11#include <string>
12#include "../ah-errors.H"
13
14using namespace std;
15
16// Test counter
19
20// Helper macro to test exceptions
21#define TEST_EXCEPTION(exception_type, code, should_throw) \
22 do { \
23 bool caught = false; \
24 string msg; \
25 try { \
26 code; \
27 } catch (const exception_type& e) { \
28 caught = true; \
29 msg = e.what(); \
30 } catch (...) { \
31 caught = true; \
32 cerr << "Wrong exception type caught!" << endl; \
33 tests_failed++; \
34 break; \
35 } \
36 if (should_throw && !caught) { \
37 cerr << "FAIL: Expected " #exception_type " was not thrown" << endl; \
38 tests_failed++; \
39 } else if (!should_throw && caught) { \
40 cerr << "FAIL: Unexpected " #exception_type " was thrown" << endl; \
41 tests_failed++; \
42 } else { \
43 if (should_throw) { \
44 if (msg.find("test_ah_errors.cc") == string::npos) { \
45 cerr << "FAIL: Message doesn't contain file name: " << msg << endl;\
46 tests_failed++; \
47 } else { \
48 tests_passed++; \
49 } \
50 } else { \
51 tests_passed++; \
52 } \
53 } \
54 } while(0)
55
56// ============================================================================
57// Tests for RANGE_ERROR macros
58// ============================================================================
59
61{
62 cout << "Testing ah_range_error_if..." << endl;
63
64 // Should throw when condition is true
65 TEST_EXCEPTION(range_error,
66 ah_range_error_if(true) << "This should throw",
67 true);
68
69 // Should not throw when condition is false
70 TEST_EXCEPTION(range_error,
71 ah_range_error_if(false) << "This should not throw",
72 false);
73}
74
76{
77 cout << "Testing ah_range_error_unless..." << endl;
78
79 // Should throw when condition is false
80 TEST_EXCEPTION(range_error,
81 ah_range_error_unless(false) << "This should throw",
82 true);
83
84 // Should not throw when condition is true
85 TEST_EXCEPTION(range_error,
86 ah_range_error_unless(true) << "This should not throw",
87 false);
88}
89
91{
92 cout << "Testing ah_range_error..." << endl;
93
94 // Should always throw
95 TEST_EXCEPTION(range_error,
96 ah_range_error() << "Unconditional throw",
97 true);
98}
99
100// ============================================================================
101// Tests for RUNTIME_ERROR macros
102// ============================================================================
103
105{
106 cout << "Testing ah_runtime_error_if..." << endl;
107
108 TEST_EXCEPTION(runtime_error,
109 ah_runtime_error_if(true) << "This should throw",
110 true);
111
112 TEST_EXCEPTION(runtime_error,
113 ah_runtime_error_if(false) << "This should not throw",
114 false);
115}
116
118{
119 cout << "Testing ah_runtime_error_unless..." << endl;
120
121 TEST_EXCEPTION(runtime_error,
122 ah_runtime_error_unless(false) << "This should throw",
123 true);
124
125 TEST_EXCEPTION(runtime_error,
126 ah_runtime_error_unless(true) << "This should not throw",
127 false);
128}
129
131{
132 cout << "Testing ah_runtime_error..." << endl;
133
134 TEST_EXCEPTION(runtime_error,
135 ah_runtime_error() << "Unconditional throw",
136 true);
137}
138
139// ============================================================================
140// Tests for LOGIC_ERROR macros
141// ============================================================================
142
144{
145 cout << "Testing ah_logic_error_if..." << endl;
146
147 TEST_EXCEPTION(logic_error,
148 ah_logic_error_if(true) << "This should throw",
149 true);
150
151 TEST_EXCEPTION(logic_error,
152 ah_logic_error_if(false) << "This should not throw",
153 false);
154}
155
157{
158 cout << "Testing ah_logic_error_unless..." << endl;
159
160 TEST_EXCEPTION(logic_error,
161 ah_logic_error_unless(false) << "This should throw",
162 true);
163
164 TEST_EXCEPTION(logic_error,
165 ah_logic_error_unless(true) << "This should not throw",
166 false);
167}
168
170{
171 cout << "Testing ah_logic_error..." << endl;
172
173 TEST_EXCEPTION(logic_error,
174 ah_logic_error() << "Unconditional throw",
175 true);
176}
177
178// ============================================================================
179// Tests for UNDERFLOW_ERROR macros
180// ============================================================================
181
183{
184 cout << "Testing ah_underflow_error_if..." << endl;
185
186 TEST_EXCEPTION(underflow_error,
187 ah_underflow_error_if(true) << "This should throw",
188 true);
189
190 TEST_EXCEPTION(underflow_error,
191 ah_underflow_error_if(false) << "This should not throw",
192 false);
193}
194
196{
197 cout << "Testing ah_underflow_error_unless..." << endl;
198
199 TEST_EXCEPTION(underflow_error,
200 ah_underflow_error_unless(false) << "This should throw",
201 true);
202
203 TEST_EXCEPTION(underflow_error,
204 ah_underflow_error_unless(true) << "This should not throw",
205 false);
206}
207
209{
210 cout << "Testing ah_underflow_error..." << endl;
211
212 TEST_EXCEPTION(underflow_error,
213 ah_underflow_error() << "Unconditional throw",
214 true);
215}
216
217// ============================================================================
218// Tests for OVERFLOW_ERROR macros
219// ============================================================================
220
222{
223 cout << "Testing ah_overflow_error_if..." << endl;
224
225 TEST_EXCEPTION(overflow_error,
226 ah_overflow_error_if(true) << "This should throw",
227 true);
228
229 TEST_EXCEPTION(overflow_error,
230 ah_overflow_error_if(false) << "This should not throw",
231 false);
232}
233
235{
236 cout << "Testing ah_overflow_error_unless..." << endl;
237
238 TEST_EXCEPTION(overflow_error,
239 ah_overflow_error_unless(false) << "This should throw",
240 true);
241
242 TEST_EXCEPTION(overflow_error,
243 ah_overflow_error_unless(true) << "This should not throw",
244 false);
245}
246
248{
249 cout << "Testing ah_overflow_error..." << endl;
250
251 TEST_EXCEPTION(overflow_error,
252 ah_overflow_error() << "Unconditional throw",
253 true);
254}
255
256// ============================================================================
257// Tests for DOMAIN_ERROR macros
258// ============================================================================
259
261{
262 cout << "Testing ah_domain_error_if..." << endl;
263
264 TEST_EXCEPTION(domain_error,
265 ah_domain_error_if(true) << "This should throw",
266 true);
267
268 TEST_EXCEPTION(domain_error,
269 ah_domain_error_if(false) << "This should not throw",
270 false);
271}
272
274{
275 cout << "Testing ah_domain_error_unless..." << endl;
276
277 TEST_EXCEPTION(domain_error,
278 ah_domain_error_unless(false) << "This should throw",
279 true);
280
281 TEST_EXCEPTION(domain_error,
282 ah_domain_error_unless(true) << "This should not throw",
283 false);
284}
285
287{
288 cout << "Testing ah_domain_error..." << endl;
289
290 TEST_EXCEPTION(domain_error,
291 ah_domain_error() << "Unconditional throw",
292 true);
293}
294
295// ============================================================================
296// Tests for OUT_OF_RANGE macros
297// ============================================================================
298
300{
301 cout << "Testing ah_out_of_range_error_if..." << endl;
302
303 TEST_EXCEPTION(out_of_range,
304 ah_out_of_range_error_if(true) << "This should throw",
305 true);
306
307 TEST_EXCEPTION(out_of_range,
308 ah_out_of_range_error_if(false) << "This should not throw",
309 false);
310}
311
313{
314 cout << "Testing ah_out_of_range_error_unless..." << endl;
315
316 TEST_EXCEPTION(out_of_range,
317 ah_out_of_range_error_unless(false) << "This should throw",
318 true);
319
320 TEST_EXCEPTION(out_of_range,
321 ah_out_of_range_error_unless(true) << "This should not throw",
322 false);
323}
324
326{
327 cout << "Testing ah_out_of_range_error..." << endl;
328
329 TEST_EXCEPTION(out_of_range,
330 ah_out_of_range_error() << "Unconditional throw",
331 true);
332}
333
334// ============================================================================
335// Tests for INVALID_ARGUMENT macros
336// ============================================================================
337
339{
340 cout << "Testing ah_invalid_argument_if..." << endl;
341
342 TEST_EXCEPTION(invalid_argument,
343 ah_invalid_argument_if(true) << "This should throw",
344 true);
345
346 TEST_EXCEPTION(invalid_argument,
347 ah_invalid_argument_if(false) << "This should not throw",
348 false);
349}
350
352{
353 cout << "Testing ah_invalid_argument_unless..." << endl;
354
355 TEST_EXCEPTION(invalid_argument,
356 ah_invalid_argument_unless(false) << "This should throw",
357 true);
358
359 TEST_EXCEPTION(invalid_argument,
360 ah_invalid_argument_unless(true) << "This should not throw",
361 false);
362}
363
365{
366 cout << "Testing ah_invalid_argument..." << endl;
367
368 TEST_EXCEPTION(invalid_argument,
369 ah_invalid_argument() << "Unconditional throw",
370 true);
371}
372
373// ============================================================================
374// Tests for LENGTH_ERROR macros
375// ============================================================================
376
378{
379 cout << "Testing ah_length_error_if..." << endl;
380
381 TEST_EXCEPTION(length_error,
382 ah_length_error_if(true) << "This should throw",
383 true);
384
385 TEST_EXCEPTION(length_error,
386 ah_length_error_if(false) << "This should not throw",
387 false);
388}
389
391{
392 cout << "Testing ah_length_error_unless..." << endl;
393
394 TEST_EXCEPTION(length_error,
395 ah_length_error_unless(false) << "This should throw",
396 true);
397
398 TEST_EXCEPTION(length_error,
399 ah_length_error_unless(true) << "This should not throw",
400 false);
401}
402
404{
405 cout << "Testing ah_length_error..." << endl;
406
407 TEST_EXCEPTION(length_error,
408 ah_length_error() << "Unconditional throw",
409 true);
410}
411
412// ============================================================================
413// Tests for FATAL_ERROR macro
414// ============================================================================
415
417{
418 cout << "Testing ah_fatal_error..." << endl;
419
420 TEST_EXCEPTION(runtime_error,
421 ah_fatal_error() << "Fatal error",
422 true);
423}
424
425// ============================================================================
426// Tests for WARNING macros
427// ============================================================================
428
430{
431 cout << "Testing warning macros..." << endl;
432
433 ostringstream oss;
434
435 // Test ah_warning_if
436 ah_warning_if(oss, true) << "Warning when true";
437 string result = oss.str();
438 if (result.find("WARNING") != string::npos &&
439 result.find("test_ah_errors.cc") != string::npos) {
440 tests_passed++;
441 } else {
442 cerr << "FAIL: ah_warning_if didn't produce expected output" << endl;
443 tests_failed++;
444 }
445
446 oss.str("");
447 oss.clear();
448
449 // Test ah_warning_unless
450 ah_warning_unless(oss, false) << "Warning when false";
451 result = oss.str();
452 if (result.find("WARNING") != string::npos &&
453 result.find("test_ah_errors.cc") != string::npos) {
454 tests_passed++;
455 } else {
456 cerr << "FAIL: ah_warning_unless didn't produce expected output" << endl;
457 tests_failed++;
458 }
459
460 oss.str("");
461 oss.clear();
462
463 // Test ah_warning (unconditional)
464 ah_warning(oss) << "Unconditional warning";
465 result = oss.str();
466 if (result.find("WARNING") != string::npos &&
467 result.find("test_ah_errors.cc") != string::npos) {
468 tests_passed++;
469 } else {
470 cerr << "FAIL: ah_warning didn't produce expected output" << endl;
471 tests_failed++;
472 }
473}
474
475// ============================================================================
476// Tests for message formatting
477// ============================================================================
478
480{
481 cout << "Testing message formatting with stream operators..." << endl;
482
483 try {
484 int value = 42;
485 ah_range_error() << "Value is " << value << " but expected " << 100;
486 } catch (const range_error& e) {
487 string msg = e.what();
488 if (msg.find("Value is 42 but expected 100") != string::npos) {
489 tests_passed++;
490 } else {
491 cerr << "FAIL: Message formatting incorrect: " << msg << endl;
492 tests_failed++;
493 }
494 }
495}
496
497// ============================================================================
498// Main test runner
499// ============================================================================
500
501int main()
502{
503 cout << "==================================================" << endl;
504 cout << "Running ah-errors.H test suite" << endl;
505 cout << "==================================================" << endl;
506 cout << endl;
507
508 // Test all exception types
512
516
520
524
528
532
536
540
544
546
547 // Test warnings
549
550 // Test message formatting
552
553 // Print results
554 cout << endl;
555 cout << "==================================================" << endl;
556 cout << "Test Results:" << endl;
557 cout << " Passed: " << tests_passed << endl;
558 cout << " Failed: " << tests_failed << endl;
559 cout << " Total: " << (tests_passed + tests_failed) << endl;
560 cout << "==================================================" << endl;
561
562 if (tests_failed == 0) {
563 cout << "SUCCESS: All tests passed!" << endl;
564 return 0;
565 } else {
566 cout << "FAILURE: Some tests failed!" << endl;
567 return 1;
568 }
569}
#define ah_domain_error()
Throws std::domain_error unconditionally.
Definition ah-errors.H:554
#define ah_overflow_error_unless(C)
Throws std::overflow_error if condition does NOT hold.
Definition ah-errors.H:479
#define ah_runtime_error_unless(C)
Throws std::runtime_error if condition does NOT hold.
Definition ah-errors.H:250
#define ah_runtime_error()
Throws std::runtime_error unconditionally.
Definition ah-errors.H:282
#define ah_length_error_if(C)
Throws std::length_error if condition holds.
Definition ah-errors.H:698
#define ah_domain_error_unless(C)
Throws std::domain_error if condition does NOT hold.
Definition ah-errors.H:538
#define ah_length_error()
Throws std::length_error unconditionally.
Definition ah-errors.H:730
#define ah_out_of_range_error_if(C)
Throws std::out_of_range if condition holds.
Definition ah-errors.H:579
#define ah_underflow_error_if(C)
Throws std::underflow_error if condition holds.
Definition ah-errors.H:368
#define ah_out_of_range_error_unless(C)
Throws std::out_of_range if condition does NOT hold.
Definition ah-errors.H:595
#define ah_fatal_error()
Throws an unconditional std::runtime_error (fatal error)
Definition ah-errors.H:759
#define ah_warning(out)
Emits an unconditional warning to a stream.
Definition ah-errors.H:172
#define ah_logic_error_unless(C)
Throws std::logic_error if condition does NOT hold.
Definition ah-errors.H:309
#define ah_overflow_error_if(C)
Throws std::overflow_error if condition holds.
Definition ah-errors.H:463
#define ah_warning_if(out, C)
Emits a warning to a stream if the condition holds.
Definition ah-errors.H:157
#define ah_invalid_argument()
Throws std::invalid_argument unconditionally.
Definition ah-errors.H:671
#define ah_overflow_error()
Throws std::overflow_error unconditionally.
Definition ah-errors.H:495
#define ah_invalid_argument_unless(C)
Throws std::invalid_argument if condition does NOT hold.
Definition ah-errors.H:655
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
Definition ah-errors.H:522
#define ah_logic_error_if(C)
Throws std::logic_error if condition holds.
Definition ah-errors.H:325
#define ah_logic_error()
Throws std::logic_error unconditionally.
Definition ah-errors.H:341
#define ah_length_error_unless(C)
Throws std::length_error if condition does NOT hold.
Definition ah-errors.H:714
#define ah_runtime_error_if(C)
Throws std::runtime_error if condition holds.
Definition ah-errors.H:266
#define ah_invalid_argument_if(C)
Throws std::invalid_argument if condition holds.
Definition ah-errors.H:639
#define ah_range_error_unless(C)
Throws std::range_error if condition does NOT hold.
Definition ah-errors.H:191
#define ah_underflow_error()
Throws std::underflow_error unconditionally.
Definition ah-errors.H:400
#define ah_underflow_error_unless(C)
Throws std::underflow_error if condition does NOT hold.
Definition ah-errors.H:384
#define ah_warning_unless(out, C)
Emits a warning to a stream if the condition does NOT hold.
Definition ah-errors.H:141
#define ah_range_error()
Throws std::range_error unconditionally.
Definition ah-errors.H:223
#define ah_range_error_if(C)
Throws std::range_error if condition holds.
Definition ah-errors.H:207
#define ah_out_of_range_error()
Throws std::out_of_range unconditionally.
Definition ah-errors.H:611
STL namespace.
void test_warning_macros()
void test_underflow_error_unless()
void test_length_error()
void test_range_error_unless()
#define TEST_EXCEPTION(exception_type, code, should_throw)
void test_overflow_error_if()
void test_overflow_error_unless()
void test_out_of_range_error()
void test_length_error_if()
void test_invalid_argument_unless()
void test_out_of_range_error_unless()
void test_range_error_if()
void test_underflow_error_if()
void test_runtime_error_if()
void test_logic_error_if()
void test_length_error_unless()
void test_domain_error_if()
void test_range_error()
void test_underflow_error()
void test_invalid_argument()
void test_domain_error()
void test_runtime_error_unless()
int tests_passed
int tests_failed
void test_runtime_error()
void test_logic_error_unless()
void test_out_of_range_error_if()
void test_domain_error_unless()
void test_message_formatting()
int main()
void test_fatal_error()
void test_logic_error()
void test_overflow_error()
void test_invalid_argument_if()