mxlib
c++ tools for analyzing astronomical data and other tasks by Jared R. Males. [git repo]
Loading...
Searching...
No Matches
error_test.cpp
Go to the documentation of this file.
1/** \file error_test.cpp
2 * \brief Tests error reporting and macros.
3 */
4#include "../../catch2/catch.hpp"
5
7
8namespace unitTest
9{
10namespace errorTest
11{
12namespace errorTest
13{
14/// Test error_t boolean operators and functions
15/**
16 * \ingroup error_error_unit_tests
17 */
18TEST_CASE( "Test error_t boolean operators and functions", "[error::error]" )
19{
20 #if MXLIBTEST_DOXYGEN_REF //doxygen links
24 #endif
25
26 SECTION( "operator!" )
27 {
29
30 REQUIRE(!errc == true);
31 REQUIRE(!!errc == false);
32
33 errc = mx::error_t::error;
34 REQUIRE(!errc == false);
35 REQUIRE(!!errc == true);
36 }
37
38 SECTION( "operator==" )
39 {
41
42 REQUIRE((errc == false));
43 REQUIRE(!(errc == true));
44
45 errc = mx::error_t::error;
46 REQUIRE((errc == true));
47 REQUIRE(!(errc == false));
48 }
49
50 SECTION( "operator!=" )
51 {
53
54 REQUIRE(!(errc != false));
55 REQUIRE((errc != true));
56
57 errc = mx::error_t::error;
58 REQUIRE(!(errc != true));
59 REQUIRE((errc != false));
60 }
61
62 SECTION( "isError" )
63 {
65
66 REQUIRE(!mx::isError(errc));
67
68 errc = mx::error_t::error;
69 REQUIRE(mx::isError(errc));
70 }
71}
72
73/// Internal error messages
74/**
75 * \ingroup error_error_unit_tests
76 */
77TEST_CASE( "Internal error messages", "[error::error]" )
78{
79 #ifdef MXLIBTEST_DOXYGEN_REF //doxygen link
81 #endif
82
83 SECTION("verbose::o")
84 {
86 REQUIRE(msg == "");
87
89 REQUIRE(msg == "");
90
92 REQUIRE(msg == "");
93
95 REQUIRE(msg == "");
96 }
97
98 SECTION("verbose::v")
99 {
101 REQUIRE(msg != "");
102
104 REQUIRE(msg != "");
105
107 REQUIRE(msg != "");
108
110 REQUIRE(msg != "");
111 }
112
113 SECTION("verbose::vv")
114 {
116 REQUIRE(msg != "");
117
119 REQUIRE(msg != "");
120
122 REQUIRE(msg != "");
123
125 REQUIRE(msg != "");
126 }
127
128 SECTION("verbose::vvv")
129 {
131 REQUIRE(msg != "");
132
134 REQUIRE(msg != "");
135
137 REQUIRE(msg != "");
138
140 REQUIRE(msg != "");
141 }
142}
143
144/// Internal error reports
145/**
146 * \ingroup error_error_unit_tests
147 */
148TEST_CASE( "Internal error reports", "[error::error]" )
149{
150 #ifdef MXLIBTEST_DOXYGEN_REF //doxygen link
152 #endif
153
154 SECTION("verbose::o")
155 {
157 REQUIRE(errc == mx::error_t::noerror);
158
160 REQUIRE(errc == mx::error_t::error);
161
163 REQUIRE(errc == mx::error_t::noerror);
164
166 REQUIRE(errc == mx::error_t::error);
167 }
168
169 SECTION("verbose::v")
170 {
172 REQUIRE(errc == mx::error_t::noerror);
173
175 REQUIRE(errc == mx::error_t::error);
176
178 REQUIRE(errc == mx::error_t::noerror);
179
181 REQUIRE(errc == mx::error_t::error);
182 }
183
184 SECTION("verbose::vv")
185 {
187 REQUIRE(errc == mx::error_t::noerror);
188
190 REQUIRE(errc == mx::error_t::error);
191
193 REQUIRE(errc == mx::error_t::noerror);
194
196 REQUIRE(errc == mx::error_t::error);
197 }
198
199 SECTION("verbose::vvv")
200 {
202 REQUIRE(errc == mx::error_t::noerror);
203
205 REQUIRE(errc == mx::error_t::error);
206
208 REQUIRE(errc == mx::error_t::noerror);
209
211 REQUIRE(errc == mx::error_t::error);
212 }
213}
214
215/// Error messages
216/**
217 * \ingroup error_error_unit_tests
218 */
219TEST_CASE( "Error messages", "[error::error]" )
220{
221 #ifdef MXLIBTEST_DOXYGEN_REF //doxygen link
223 #endif
224
225 SECTION("verbose::o")
226 {
228 REQUIRE(msg == "");
229
231 REQUIRE(msg == "");
232
234 REQUIRE(msg == "");
235
237 REQUIRE(msg == "");
238 }
239
240 SECTION("verbose::v")
241 {
243 REQUIRE(msg != "");
244
246 REQUIRE(msg != "");
247
249 REQUIRE(msg != "");
250
252 REQUIRE(msg != "");
253 }
254
255 SECTION("verbose::vv")
256 {
258 REQUIRE(msg != "");
259
261 REQUIRE(msg != "");
262
264 REQUIRE(msg != "");
265
267 REQUIRE(msg != "");
268 }
269
270 SECTION("verbose::vvv")
271 {
273 REQUIRE(msg != "");
274
276 REQUIRE(msg != "");
277
279 REQUIRE(msg != "");
280
282 REQUIRE(msg != "");
283 }
284}
285
286/// Error reports
287/**
288 * \ingroup error_error_unit_tests
289 */
290TEST_CASE( "Error reports", "[error::error]" )
291{
292 #ifdef MXLIBTEST_DOXYGEN_REF //doxygen link
294 #endif
295
296 SECTION("verbose::o")
297 {
299 REQUIRE(errc == mx::error_t::noerror);
300
302 REQUIRE(errc == mx::error_t::error);
303
305 REQUIRE(errc == mx::error_t::noerror);
306
308 REQUIRE(errc == mx::error_t::error);
309 }
310
311 SECTION("verbose::v")
312 {
314 REQUIRE(errc == mx::error_t::noerror);
315
317 REQUIRE(errc == mx::error_t::error);
318
320 REQUIRE(errc == mx::error_t::noerror);
321
323 REQUIRE(errc == mx::error_t::error);
324 }
325
326 SECTION("verbose::vv")
327 {
329 REQUIRE(errc == mx::error_t::noerror);
330
332 REQUIRE(errc == mx::error_t::error);
333
335 REQUIRE(errc == mx::error_t::noerror);
336
338 REQUIRE(errc == mx::error_t::error);
339 }
340
341 SECTION("verbose::vvv")
342 {
344 REQUIRE(errc == mx::error_t::noerror);
345
347 REQUIRE(errc == mx::error_t::error);
348
350 REQUIRE(errc == mx::error_t::noerror);
351
353 REQUIRE(errc == mx::error_t::error);
354 }
355}
356
357/// \cond
358mx::error_t mx_error_check_test_fxn(mx::error_t errc)
359{
360 return errc;
361}
362
363mx::error_t mx_error_check_test(mx::error_t errc)
364{
365 typedef mx::verbose::vv verboseT;
366
367 mx_error_check(mx_error_check_test_fxn(errc));
368
369 return mx::error_t::exception; //a sentinel
370}
371
372mx::error_t mx_error_check_code_test(mx::error_t errc)
373{
374 typedef mx::verbose::vv verboseT;
375
377
378 return mx::error_t::exception; //a sentinel
379}
380
381int mx_error_check_rv_test(mx::error_t errc)
382{
383 typedef mx::verbose::vv verboseT;
384
385 mx_error_check_rv(mx_error_check_test_fxn(errc), -1);
386
387 return 0;
388}
389
390int mx_error_check_code_rv_test(mx::error_t errc)
391{
392 typedef mx::verbose::vv verboseT;
393
394 mx_error_check_code_rv(mx_error_check_test_fxn(errc), -1);
395
396 return 0;
397}
398
399mx::error_t mx_error_return_test(mx::error_t errc)
400{
401 typedef mx::verbose::vv verboseT;
402
403 mx_error_return(mx_error_check_test_fxn(errc));
404
405}
406
407mx::error_t mx_error_return_code_test(mx::error_t errc)
408{
409 typedef mx::verbose::vv verboseT;
410
412
413}
414
415/// \endcond
416
417/// Error macros
418/**
419 * \ingroup error_error_unit_tests
420 */
421TEST_CASE( "Error macros", "[error::error]" )
422{
423 #if MXLIBTEST_DOXYGEN_REF //doxygen links
424 mx_error_check(errc);
426 mx_error_check_rv(errc);
428 mx_error_return(errc);
430 #endif
431
432 SECTION("mx_error_check")
433 {
434 //use a sentinel to test that noerror goes through the macro
435 REQUIRE(mx_error_check_test(mx::error_t::noerror) == mx::error_t::exception);
436 REQUIRE(mx_error_check_test(mx::error_t::error) == mx::error_t::error);
437 REQUIRE(mx_error_check_test(mx::error_t::eacces) == mx::error_t::eacces);
438 }
439
440 SECTION("mx_error_check_code")
441 {
442 //use a sentinel to test that noerror goes through the macro
443 REQUIRE(mx_error_check_code_test(mx::error_t::noerror) == mx::error_t::exception);
444 REQUIRE(mx_error_check_code_test(mx::error_t::error) == mx::error_t::error);
445 REQUIRE(mx_error_check_code_test(mx::error_t::eacces) == mx::error_t::eacces);
446 }
447
448 SECTION("mx_error_check_rv")
449 {
450 REQUIRE(mx_error_check_rv_test(mx::error_t::noerror) == 0);
451 REQUIRE(mx_error_check_rv_test(mx::error_t::error) == -1);
452 REQUIRE(mx_error_check_rv_test(mx::error_t::eacces) == -1);
453 }
454
455 SECTION("mx_error_check_code_rv")
456 {
457 REQUIRE(mx_error_check_code_rv_test(mx::error_t::noerror) == 0);
458 REQUIRE(mx_error_check_code_rv_test(mx::error_t::error) == -1);
459 REQUIRE(mx_error_check_code_rv_test(mx::error_t::eacces) == -1);
460 }
461
462 SECTION("mx_error_return")
463 {
464 REQUIRE(mx_error_return_test(mx::error_t::noerror) == mx::error_t::noerror);
465 REQUIRE(mx_error_return_test(mx::error_t::error) == mx::error_t::error);
466 REQUIRE(mx_error_return_test(mx::error_t::eacces) == mx::error_t::eacces);
467 }
468
469 SECTION("mx_error_return_code")
470 {
471 REQUIRE(mx_error_return_code_test(mx::error_t::noerror) == mx::error_t::noerror);
472 REQUIRE(mx_error_return_code_test(mx::error_t::error) == mx::error_t::error);
473 REQUIRE(mx_error_return_code_test(mx::error_t::eacces) == mx::error_t::eacces);
474 }
475}
476} // namespace errorTest
477} // namespace errorTest
478} // namespace unitTest
The mxlib error reporting system.
TEST_CASE("Test error_t boolean operators and functions", "[error::error]")
Test error_t boolean operators and functions.
error_t
The mxlib error codes.
Definition error_t.hpp:26
@ noerror
No error has occurred.
Definition error_t.hpp:27
@ exception
An exception was thrown.
Definition error_t.hpp:51
@ eacces
Permission denied (EACCES).
Definition error_t.hpp:71
@ error
A general error has occurred.
Definition error_t.hpp:28
#define mx_error_return_code(errc)
Perform an error check on an error_t code and return the result.
Definition error.hpp:712
bool operator!(const error_t &errc)
Check if an error_t code is an error.
Definition error.hpp:58
std::string error_message< mx::verbose::vvv >(const error_t &code, const std::string &expl, const std::source_location &loc)
Specialization of error_message for verbose::vvv.
bool operator==(const error_t &errc, bool tf)
Check if an error_t code is an error.
Definition error.hpp:82
std::string error_message< mx::verbose::v >(const error_t &code, const std::string &expl, const std::source_location &loc)
Specialization of error_message for verbose::v.
#define mx_error_check_code_rv(errc, rv)
Perform an error check on an error_t code.
Definition error.hpp:673
std::string error_message< mx::verbose::o >(const error_t &code, const std::string &expl, const std::source_location &loc)
Specialization of error_message for verbose::o.
#define mx_error_check(fxn)
Perform an error check on the output of a function.
Definition error.hpp:613
error_t error_report(const error_t &code, const std::string &expl, const std::source_location &loc=std::source_location::current())
Print a report to stderr given an mxlib error_t code and explanation and return the code.
Definition error.hpp:540
error_t error_report< mx::verbose::o >(const error_t &code, const std::string &expl, const std::source_location &loc)
Specialization of error_report for verbose::o.
std::string error_message(const error_t &code, const std::string &expl, const std::source_location &loc=std::source_location::current())
Format a report given an mxlib error_t code and explanation.
bool isError(const error_t &errc)
Check if an error_t code is an error.
Definition error.hpp:120
#define mx_error_check_code(errc)
Perform an error check on an error_t code.
Definition error.hpp:632
bool operator!=(const error_t &errc, bool tf)
Check if an error_t code is not an error.
Definition error.hpp:108
#define mx_error_check_rv(fxn, rv)
Perform an error check on the output of a function.
Definition error.hpp:652
#define mx_error_return(fxn)
Perform an error check on a function and return the result.
Definition error.hpp:692
std::string error_message< mx::verbose::vv >(const error_t &code, const std::string &expl, const std::source_location &loc)
Specialization of error_message for verbose::vv.
error_t mxlib_error_report(const error_t &code, const std::string &expl, const std::source_location &loc=std::source_location::current())
Print a report to stderr given an mxlib error_t code and explanation and return the code.
Definition error.hpp:331
error_t mxlib_error_report< mx::verbose::o >(const error_t &code, const std::string &expl, const std::source_location &loc)
Specialization of mxlib_error_report for verbose::o.
std::string mxlib_error_message< mx::verbose::vv >(const error_t &code, const std::string &expl, const std::source_location &loc)
Specialization of mxlib_error_message for verbose::vv.
std::string mxlib_error_message< mx::verbose::v >(const error_t &code, const std::string &expl, const std::source_location &loc)
Specialization of mxlib_error_message for verbose::v.
std::string mxlib_error_message< mx::verbose::o >(const error_t &code, const std::string &expl, const std::source_location &loc)
Specialization of mxlib_error_message for verbose::o.
std::string mxlib_error_message< mx::verbose::vvv >(const error_t &code, const std::string &expl, const std::source_location &loc)
Specialization of mxlib_error_message for verbose::vvv.
std::string mxlib_error_message(const error_t &code, const std::string &expl, const std::source_location &loc=std::source_location::current())
Format a report given an mxlib error_t code and explanation.
Verbosity level 2. Additional information is provided, including source file and line.
Definition error.hpp:173