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 */
3#include "../../catch2/catch.hpp"
4
5#include "../../../include/error/error.hpp"
6
7namespace unitTest
8{
9namespace errorTest
10{
11namespace errorTest
12{
13/// Test error_t boolean operators and functions
14/**
15 * \ingroup error_error_unit_tests
16 */
17TEST_CASE( "Test error_t boolean operators and functions", "[error::error]" )
18{
19 #if MXLIBTEST_DOXYGEN_REF //doxygen links
23 #endif
24
25 SECTION( "operator!" )
26 {
28
29 REQUIRE(!errc == true);
30 REQUIRE(!!errc == false);
31
32 errc = mx::error_t::error;
33 REQUIRE(!errc == false);
34 REQUIRE(!!errc == true);
35 }
36
37 SECTION( "operator==" )
38 {
40
41 REQUIRE((errc == false));
42 REQUIRE(!(errc == true));
43
44 errc = mx::error_t::error;
45 REQUIRE((errc == true));
46 REQUIRE(!(errc == false));
47 }
48
49 SECTION( "operator!=" )
50 {
52
53 REQUIRE(!(errc != false));
54 REQUIRE((errc != true));
55
56 errc = mx::error_t::error;
57 REQUIRE(!(errc != true));
58 REQUIRE((errc != false));
59 }
60
61 SECTION( "isError" )
62 {
64
65 REQUIRE(!mx::isError(errc));
66
67 errc = mx::error_t::error;
68 REQUIRE(mx::isError(errc));
69 }
70}
71
72/// Internal error messages
73/**
74 * \ingroup error_error_unit_tests
75 */
76TEST_CASE( "Internal error messages", "[error::error]" )
77{
78 #ifdef MXLIBTEST_DOXYGEN_REF //doxygen link
80 #endif
81
82 SECTION("verbose::o")
83 {
85 REQUIRE(msg == "");
86
88 REQUIRE(msg == "");
89
91 REQUIRE(msg == "");
92
94 REQUIRE(msg == "");
95 }
96
97 SECTION("verbose::v")
98 {
100 REQUIRE(msg != "");
101
103 REQUIRE(msg != "");
104
106 REQUIRE(msg != "");
107
109 REQUIRE(msg != "");
110 }
111
112 SECTION("verbose::vv")
113 {
115 REQUIRE(msg != "");
116
118 REQUIRE(msg != "");
119
121 REQUIRE(msg != "");
122
124 REQUIRE(msg != "");
125 }
126
127 SECTION("verbose::vvv")
128 {
130 REQUIRE(msg != "");
131
133 REQUIRE(msg != "");
134
136 REQUIRE(msg != "");
137
139 REQUIRE(msg != "");
140 }
141}
142
143/// Internal error reports
144/**
145 * \ingroup error_error_unit_tests
146 */
147TEST_CASE( "Internal error reports", "[error::error]" )
148{
149 #ifdef MXLIBTEST_DOXYGEN_REF //doxygen link
151 #endif
152
153 SECTION("verbose::o")
154 {
156 REQUIRE(errc == mx::error_t::noerror);
157
159 REQUIRE(errc == mx::error_t::error);
160
162 REQUIRE(errc == mx::error_t::noerror);
163
165 REQUIRE(errc == mx::error_t::error);
166 }
167
168 SECTION("verbose::v")
169 {
170 mx::error_t errc = mx::internal::mxlib_error_report<mx::verbose::v>(mx::error_t::noerror, "test");
171 REQUIRE(errc == mx::error_t::noerror);
172
173 errc = mx::internal::mxlib_error_report<mx::verbose::v>(mx::error_t::error, "test");
174 REQUIRE(errc == mx::error_t::error);
175
176 errc = mx::internal::mxlib_error_report<mx::verbose::v>(mx::error_t::noerror);
177 REQUIRE(errc == mx::error_t::noerror);
178
179 errc = mx::internal::mxlib_error_report<mx::verbose::v>(mx::error_t::error);
180 REQUIRE(errc == mx::error_t::error);
181 }
182
183 SECTION("verbose::vv")
184 {
185 mx::error_t errc = mx::internal::mxlib_error_report<mx::verbose::vv>(mx::error_t::noerror, "test");
186 REQUIRE(errc == mx::error_t::noerror);
187
188 errc = mx::internal::mxlib_error_report<mx::verbose::vv>(mx::error_t::error, "test");
189 REQUIRE(errc == mx::error_t::error);
190
191 errc = mx::internal::mxlib_error_report<mx::verbose::vv>(mx::error_t::noerror);
192 REQUIRE(errc == mx::error_t::noerror);
193
194 errc = mx::internal::mxlib_error_report<mx::verbose::vv>(mx::error_t::error);
195 REQUIRE(errc == mx::error_t::error);
196 }
197
198 SECTION("verbose::vvv")
199 {
200 mx::error_t errc = mx::internal::mxlib_error_report<mx::verbose::vvv>(mx::error_t::noerror, "test");
201 REQUIRE(errc == mx::error_t::noerror);
202
203 errc = mx::internal::mxlib_error_report<mx::verbose::vvv>(mx::error_t::error, "test");
204 REQUIRE(errc == mx::error_t::error);
205
206 errc = mx::internal::mxlib_error_report<mx::verbose::vvv>(mx::error_t::noerror);
207 REQUIRE(errc == mx::error_t::noerror);
208
209 errc = mx::internal::mxlib_error_report<mx::verbose::vvv>(mx::error_t::error);
210 REQUIRE(errc == mx::error_t::error);
211 }
212}
213
214/// Error messages
215/**
216 * \ingroup error_error_unit_tests
217 */
218TEST_CASE( "Error messages", "[error::error]" )
219{
220 #ifdef MXLIBTEST_DOXYGEN_REF //doxygen link
222 #endif
223
224 SECTION("verbose::o")
225 {
227 REQUIRE(msg == "");
228
230 REQUIRE(msg == "");
231
233 REQUIRE(msg == "");
234
236 REQUIRE(msg == "");
237 }
238
239 SECTION("verbose::v")
240 {
242 REQUIRE(msg != "");
243
245 REQUIRE(msg != "");
246
248 REQUIRE(msg != "");
249
251 REQUIRE(msg != "");
252 }
253
254 SECTION("verbose::vv")
255 {
257 REQUIRE(msg != "");
258
260 REQUIRE(msg != "");
261
263 REQUIRE(msg != "");
264
266 REQUIRE(msg != "");
267 }
268
269 SECTION("verbose::vvv")
270 {
272 REQUIRE(msg != "");
273
275 REQUIRE(msg != "");
276
278 REQUIRE(msg != "");
279
281 REQUIRE(msg != "");
282 }
283}
284
285/// Error reports
286/**
287 * \ingroup error_error_unit_tests
288 */
289TEST_CASE( "Error reports", "[error::error]" )
290{
291 #ifdef MXLIBTEST_DOXYGEN_REF //doxygen link
293 #endif
294
295 SECTION("verbose::o")
296 {
298 REQUIRE(errc == mx::error_t::noerror);
299
301 REQUIRE(errc == mx::error_t::error);
302
304 REQUIRE(errc == mx::error_t::noerror);
305
307 REQUIRE(errc == mx::error_t::error);
308 }
309
310 SECTION("verbose::v")
311 {
312 mx::error_t errc = mx::error_report<mx::verbose::v>(mx::error_t::noerror, "test");
313 REQUIRE(errc == mx::error_t::noerror);
314
315 errc = mx::error_report<mx::verbose::v>(mx::error_t::error, "test");
316 REQUIRE(errc == mx::error_t::error);
317
318 errc = mx::error_report<mx::verbose::v>(mx::error_t::noerror);
319 REQUIRE(errc == mx::error_t::noerror);
320
321 errc = mx::error_report<mx::verbose::v>(mx::error_t::error);
322 REQUIRE(errc == mx::error_t::error);
323 }
324
325 SECTION("verbose::vv")
326 {
327 mx::error_t errc = mx::error_report<mx::verbose::vv>(mx::error_t::noerror, "test");
328 REQUIRE(errc == mx::error_t::noerror);
329
330 errc = mx::error_report<mx::verbose::vv>(mx::error_t::error, "test");
331 REQUIRE(errc == mx::error_t::error);
332
333 errc = mx::error_report<mx::verbose::vv>(mx::error_t::noerror);
334 REQUIRE(errc == mx::error_t::noerror);
335
336 errc = mx::error_report<mx::verbose::vv>(mx::error_t::error);
337 REQUIRE(errc == mx::error_t::error);
338 }
339
340 SECTION("verbose::vvv")
341 {
342 mx::error_t errc = mx::error_report<mx::verbose::vvv>(mx::error_t::noerror, "test");
343 REQUIRE(errc == mx::error_t::noerror);
344
345 errc = mx::error_report<mx::verbose::vvv>(mx::error_t::error, "test");
346 REQUIRE(errc == mx::error_t::error);
347
348 errc = mx::error_report<mx::verbose::vvv>(mx::error_t::noerror);
349 REQUIRE(errc == mx::error_t::noerror);
350
351 errc = mx::error_report<mx::verbose::vvv>(mx::error_t::error);
352 REQUIRE(errc == mx::error_t::error);
353 }
354}
355
356/// \cond
357mx::error_t mx_error_check_test_fxn(mx::error_t errc)
358{
359 return errc;
360}
361
362mx::error_t mx_error_check_test(mx::error_t errc)
363{
364 typedef mx::verbose::vv verboseT;
365
366 mx_error_check(mx_error_check_test_fxn(errc));
367
368 return mx::error_t::exception; //a sentinel
369}
370
371mx::error_t mx_error_check_code_test(mx::error_t errc)
372{
373 typedef mx::verbose::vv verboseT;
374
376
377 return mx::error_t::exception; //a sentinel
378}
379
380int mx_error_check_rv_test(mx::error_t errc)
381{
382 typedef mx::verbose::vv verboseT;
383
384 mx_error_check_rv(mx_error_check_test_fxn(errc), -1);
385
386 return 0;
387}
388
389int mx_error_check_code_rv_test(mx::error_t errc)
390{
391 typedef mx::verbose::vv verboseT;
392
393 mx_error_check_code_rv(mx_error_check_test_fxn(errc), -1);
394
395 return 0;
396}
397
398mx::error_t mx_error_return_test(mx::error_t errc)
399{
400 typedef mx::verbose::vv verboseT;
401
402 mx_error_return(mx_error_check_test_fxn(errc));
403
404}
405
406mx::error_t mx_error_return_code_test(mx::error_t errc)
407{
408 typedef mx::verbose::vv verboseT;
409
411
412}
413
414/// \endcond
415
416/// Error macros
417/**
418 * \ingroup error_error_unit_tests
419 */
420TEST_CASE( "Error macros", "[error::error]" )
421{
422 #if MXLIBTEST_DOXYGEN_REF //doxygen links
423 mx_error_check(errc);
425 mx_error_check_rv(errc);
427 mx_error_return(errc);
429 #endif
430
431 SECTION("mx_error_check")
432 {
433 //use a sentinel to test that noerror goes through the macro
434 REQUIRE(mx_error_check_test(mx::error_t::noerror) == mx::error_t::exception);
435 REQUIRE(mx_error_check_test(mx::error_t::error) == mx::error_t::error);
436 REQUIRE(mx_error_check_test(mx::error_t::eacces) == mx::error_t::eacces);
437 }
438
439 SECTION("mx_error_check_code")
440 {
441 //use a sentinel to test that noerror goes through the macro
442 REQUIRE(mx_error_check_code_test(mx::error_t::noerror) == mx::error_t::exception);
443 REQUIRE(mx_error_check_code_test(mx::error_t::error) == mx::error_t::error);
444 REQUIRE(mx_error_check_code_test(mx::error_t::eacces) == mx::error_t::eacces);
445 }
446
447 SECTION("mx_error_check_rv")
448 {
449 REQUIRE(mx_error_check_rv_test(mx::error_t::noerror) == 0);
450 REQUIRE(mx_error_check_rv_test(mx::error_t::error) == -1);
451 REQUIRE(mx_error_check_rv_test(mx::error_t::eacces) == -1);
452 }
453
454 SECTION("mx_error_check_code_rv")
455 {
456 REQUIRE(mx_error_check_code_rv_test(mx::error_t::noerror) == 0);
457 REQUIRE(mx_error_check_code_rv_test(mx::error_t::error) == -1);
458 REQUIRE(mx_error_check_code_rv_test(mx::error_t::eacces) == -1);
459 }
460
461 SECTION("mx_error_return")
462 {
463 REQUIRE(mx_error_return_test(mx::error_t::noerror) == mx::error_t::noerror);
464 REQUIRE(mx_error_return_test(mx::error_t::error) == mx::error_t::error);
465 REQUIRE(mx_error_return_test(mx::error_t::eacces) == mx::error_t::eacces);
466 }
467
468 SECTION("mx_error_return_code")
469 {
470 REQUIRE(mx_error_return_code_test(mx::error_t::noerror) == mx::error_t::noerror);
471 REQUIRE(mx_error_return_code_test(mx::error_t::error) == mx::error_t::error);
472 REQUIRE(mx_error_return_code_test(mx::error_t::eacces) == mx::error_t::eacces);
473 }
474}
475} // namespace errorTest
476} // namespace errorTest
477} // namespace unitTest
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.
@ exception
An exception was thrown.
@ eacces
Permission denied (EACCES)
@ error
A general error has occurred.
#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