jaulib v1.3.6
Jau Support Library (C++, Java, ..)
Loading...
Searching...
No Matches
test_exceptions01.cpp
Go to the documentation of this file.
1/*
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright (c) 2024 Gothel Software e.K.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24#include <cstring>
25#include <exception>
26#include <stdexcept>
27#include <system_error>
28
29#include <jau/test/catch2_ext.hpp>
30
31#include <jau/basic_types.hpp>
33#include <jau/mp/big_int.hpp>
34
35using namespace jau;
36
37static void throwOutOfMemoryError() {
39}
40static void throwRuntimeException() {
42}
43static void throwLogicError() {
44 throw jau::LogicError("test", E_FILE_LINE);
45}
54}
56 throw jau::RuntimeSystemException(std::error_code(), "test", E_FILE_LINE);
57}
58static void throwIOError() {
59 throw jau::IOError("test", E_FILE_LINE);
60}
61static void throwInternalError() {
62 throw jau::InternalError("test", E_FILE_LINE);
63}
73
74TEST_CASE( "Exception 00", "[exceptions][error]" ) {
75 static_assert( true == std::is_base_of_v<jau::ExceptionBase, jau::OutOfMemoryError>);
76 static_assert( true == std::is_base_of_v<std::bad_alloc, jau::OutOfMemoryError>);
77 static_assert( true == std::is_base_of_v<std::exception, jau::OutOfMemoryError>);
78 REQUIRE_THROWS_MATCHES( throwOutOfMemoryError(), jau::OutOfMemoryError, Catch::Matchers::ContainsSubstring("OutOfMemoryError") );
79 REQUIRE_THROWS_AS( throwOutOfMemoryError(), jau::ExceptionBase);
80 REQUIRE_THROWS_AS( throwOutOfMemoryError(), std::bad_alloc);
81 REQUIRE_THROWS_AS( throwOutOfMemoryError(), std::exception);
82
83 static_assert( true == std::is_base_of_v<jau::ExceptionBase, jau::RuntimeExceptionBase>);
84 static_assert( true == std::is_base_of_v<jau::RuntimeExceptionBase, jau::RuntimeException>);
85 static_assert( true == std::is_base_of_v<std::runtime_error, jau::RuntimeException>);
86 static_assert( true == std::is_base_of_v<std::exception, jau::RuntimeException>);
87 REQUIRE_THROWS_MATCHES( throwRuntimeException(), jau::RuntimeException, Catch::Matchers::ContainsSubstring("RuntimeException") );
89 REQUIRE_THROWS_AS( throwRuntimeException(), std::runtime_error);
90 REQUIRE_THROWS_AS( throwRuntimeException(), std::exception);
91
92 static_assert( true == std::is_base_of_v<jau::ExceptionBase, jau::LogicErrorBase>);
93 static_assert( true == std::is_base_of_v<jau::LogicErrorBase, jau::LogicError>);
94 static_assert( true == std::is_base_of_v<std::logic_error, jau::LogicError>);
95 static_assert( true == std::is_base_of_v<std::exception, jau::LogicError>);
96 REQUIRE_THROWS_MATCHES( throwLogicError(), jau::LogicError, Catch::Matchers::ContainsSubstring("LogicError") );
97 REQUIRE_THROWS_AS( throwLogicError(), jau::LogicErrorBase);
98 REQUIRE_THROWS_AS( throwLogicError(), std::logic_error);
99 REQUIRE_THROWS_AS( throwLogicError(), std::exception);
100
101 static_assert( true == std::is_base_of_v<std::out_of_range, jau::IndexOutOfBoundsError>);
102 static_assert( true == std::is_base_of_v<jau::LogicErrorBase, jau::IndexOutOfBoundsError>);
103 static_assert( true == std::is_base_of_v<std::logic_error, jau::IndexOutOfBoundsError>);
104 REQUIRE_THROWS_MATCHES( throwIndexOutOfBoundsError(), jau::IndexOutOfBoundsError, Catch::Matchers::ContainsSubstring("IndexOutOfBoundsError") );
105 REQUIRE_THROWS_AS( throwIndexOutOfBoundsError(), std::out_of_range);
106 REQUIRE_THROWS_MATCHES( throwIndexOutOfBoundsError(), jau::LogicErrorBase, Catch::Matchers::ContainsSubstring("IndexOutOfBoundsError") );
107 REQUIRE_THROWS_AS( throwIndexOutOfBoundsError(), std::logic_error);
108 REQUIRE_THROWS_AS( throwIndexOutOfBoundsError(), std::exception);
109
110 static_assert( true == std::is_base_of_v<std::invalid_argument, jau::IllegalArgumentError>);
111 static_assert( true == std::is_base_of_v<jau::LogicErrorBase, jau::IllegalArgumentError>);
112 static_assert( true == std::is_base_of_v<std::logic_error, jau::IllegalArgumentError>);
113 REQUIRE_THROWS_MATCHES( throwIllegalArgumentError(), jau::IllegalArgumentError, Catch::Matchers::ContainsSubstring("IllegalArgumentError") );
114 REQUIRE_THROWS_AS( throwIllegalArgumentError(), std::invalid_argument);
115 REQUIRE_THROWS_MATCHES( throwIllegalArgumentError(), jau::LogicErrorBase, Catch::Matchers::ContainsSubstring("IllegalArgumentError") );
116 REQUIRE_THROWS_AS( throwIllegalArgumentError(), std::logic_error);
117 REQUIRE_THROWS_AS( throwIllegalArgumentError(), std::exception);
118
119 static_assert( true == std::is_base_of_v<std::domain_error, jau::IllegalStateError>);
120 static_assert( true == std::is_base_of_v<jau::LogicErrorBase, jau::IllegalStateError>);
121 static_assert( true == std::is_base_of_v<std::logic_error, jau::IllegalStateError>);
122 REQUIRE_THROWS_MATCHES( throwIllegalStateError(), jau::IllegalStateError, Catch::Matchers::ContainsSubstring("IllegalStateError") );
123 REQUIRE_THROWS_AS( throwIllegalStateError(), std::domain_error);
124 REQUIRE_THROWS_MATCHES( throwIllegalStateError(), jau::LogicErrorBase, Catch::Matchers::ContainsSubstring("IllegalStateError") );
125 REQUIRE_THROWS_AS( throwIllegalStateError(), std::logic_error);
126 REQUIRE_THROWS_AS( throwIllegalStateError(), std::exception);
127
128 static_assert( true == std::is_base_of_v<jau::RuntimeExceptionBase, jau::RuntimeSystemExceptionBase>);
129 static_assert( true == std::is_base_of_v<jau::ExceptionBase, jau::RuntimeSystemExceptionBase>);
130 static_assert( true == std::is_base_of_v<jau::RuntimeSystemExceptionBase, jau::RuntimeSystemException>);
131 static_assert( true == std::is_base_of_v<std::system_error, jau::RuntimeSystemException>);
132 static_assert( true == std::is_base_of_v<std::runtime_error, jau::RuntimeSystemException>);
133 static_assert( true == std::is_base_of_v<std::exception, jau::RuntimeSystemException>);
134 REQUIRE_THROWS_MATCHES( throwRuntimeSystemException(), jau::RuntimeSystemExceptionBase, Catch::Matchers::ContainsSubstring("RuntimeSystemException") );
135 REQUIRE_THROWS_AS( throwRuntimeSystemException(), std::system_error);
136 REQUIRE_THROWS_MATCHES( throwRuntimeSystemException(), jau::RuntimeExceptionBase, Catch::Matchers::ContainsSubstring("RuntimeSystemException") );
137 REQUIRE_THROWS_AS( throwRuntimeSystemException(), std::runtime_error);
138 REQUIRE_THROWS_AS( throwRuntimeSystemException(), std::exception);
139
140 static_assert( true == std::is_base_of_v<std::ios_base::failure, jau::IOError>);
141 static_assert( true == std::is_base_of_v<jau::RuntimeSystemExceptionBase, jau::IOError>);
142 static_assert( true == std::is_base_of_v<std::system_error, jau::IOError>);
143 static_assert( true == std::is_base_of_v<jau::RuntimeExceptionBase, jau::IOError>);
144 static_assert( true == std::is_base_of_v<std::runtime_error, jau::IOError>);
145 REQUIRE_THROWS_MATCHES( throwIOError(), jau::IOError, Catch::Matchers::ContainsSubstring("IOError") );
146 REQUIRE_THROWS_AS( throwIOError(), std::ios_base::failure );
147 REQUIRE_THROWS_MATCHES( throwIOError(), jau::RuntimeSystemExceptionBase, Catch::Matchers::ContainsSubstring("IOError") );
148 REQUIRE_THROWS_AS( throwIOError(), std::system_error);
149 REQUIRE_THROWS_MATCHES( throwIOError(), jau::RuntimeExceptionBase, Catch::Matchers::ContainsSubstring("IOError") );
150 REQUIRE_THROWS_AS( throwIOError(), std::runtime_error );
151 REQUIRE_THROWS_AS( throwIOError(), std::exception );
152
153 REQUIRE_THROWS_MATCHES( throwInternalError(), jau::RuntimeExceptionBase, Catch::Matchers::ContainsSubstring("InternalError") );
154 REQUIRE_THROWS_AS( throwInternalError(), std::runtime_error );
155 REQUIRE_THROWS_MATCHES( throwInternalError(), jau::InternalError, Catch::Matchers::ContainsSubstring("InternalError") );
156 REQUIRE_THROWS_AS( throwInternalError(), std::exception );
157
158 REQUIRE_THROWS_MATCHES( throwNotImplementedException(), jau::RuntimeExceptionBase, Catch::Matchers::ContainsSubstring("NotImplementedException") );
159 REQUIRE_THROWS_AS( throwNotImplementedException(), std::runtime_error );
160 REQUIRE_THROWS_MATCHES( throwNotImplementedException(), jau::NotImplementedException, Catch::Matchers::ContainsSubstring("NotImplementedException") );
161 REQUIRE_THROWS_AS( throwNotImplementedException(), std::exception );
162
163 REQUIRE_THROWS_MATCHES( throwNullPointerException(), jau::RuntimeExceptionBase, Catch::Matchers::ContainsSubstring("NullPointerException") );
164 REQUIRE_THROWS_AS( throwNullPointerException(), std::runtime_error );
165 REQUIRE_THROWS_MATCHES( throwNullPointerException(), jau::NullPointerException, Catch::Matchers::ContainsSubstring("NullPointerException") );
166 REQUIRE_THROWS_AS( throwNullPointerException(), std::exception );
167
168 REQUIRE_THROWS_MATCHES( throwUnsupportedOperationException(), jau::RuntimeExceptionBase, Catch::Matchers::ContainsSubstring("UnsupportedOperationException") );
169 REQUIRE_THROWS_AS( throwUnsupportedOperationException(), std::runtime_error );
170 REQUIRE_THROWS_MATCHES( throwUnsupportedOperationException(), jau::UnsupportedOperationException, Catch::Matchers::ContainsSubstring("UnsupportedOperationException") );
171 REQUIRE_THROWS_AS( throwUnsupportedOperationException(), std::exception );
172}
173
179}
180static void throwMathDomainError() {
182}
188}
192
193TEST_CASE( "Exception 10 Math", "[big_int_t][exceptions][error][math]" ) {
194 // MathError
195 static_assert( true == std::is_base_of_v<jau::math::MathErrorBase, jau::math::MathError>);
196 static_assert( true == std::is_base_of_v<std::exception, jau::math::MathError>);
197 REQUIRE_THROWS_MATCHES( throwMathError(), jau::math::MathErrorBase, Catch::Matchers::ContainsSubstring("MathError(undefined)") );
198 REQUIRE_THROWS_AS( throwMathError(), std::exception);
199
200 static_assert( true == std::is_base_of_v<jau::math::MathRuntimeErrorBase, jau::math::MathInexactError>);
201 static_assert( true == std::is_base_of_v<jau::math::MathErrorBase, jau::math::MathInexactError>);
202 static_assert( true == std::is_base_of_v<std::runtime_error, jau::math::MathInexactError>);
203 static_assert( true == std::is_base_of_v<std::exception, jau::math::MathInexactError>);
204 REQUIRE_THROWS_MATCHES( throwMathInexactError(), jau::math::MathInexactError, Catch::Matchers::ContainsSubstring("MathError(inexact)") );
206 REQUIRE_THROWS_AS( throwMathInexactError(), jau::math::MathErrorBase);
207 REQUIRE_THROWS_AS( throwMathInexactError(), std::runtime_error);
208 REQUIRE_THROWS_AS( throwMathInexactError(), std::exception);
209
210 static_assert( true == std::is_base_of_v<jau::math::MathRuntimeErrorBase, jau::math::MathOverflowError>);
211 static_assert( true == std::is_base_of_v<jau::math::MathErrorBase, jau::math::MathOverflowError>);
212 static_assert( true == std::is_base_of_v<std::overflow_error, jau::math::MathOverflowError>);
213 static_assert( true == std::is_base_of_v<std::runtime_error, jau::math::MathOverflowError>);
214 static_assert( true == std::is_base_of_v<std::exception, jau::math::MathOverflowError>);
215 REQUIRE_THROWS_MATCHES( throwMathOverflowError(), jau::math::MathOverflowError, Catch::Matchers::ContainsSubstring("MathError(overflow)") );
218 REQUIRE_THROWS_AS( throwMathOverflowError(), std::overflow_error);
219 REQUIRE_THROWS_AS( throwMathOverflowError(), std::runtime_error);
220 REQUIRE_THROWS_AS( throwMathOverflowError(), std::exception);
221
222 static_assert( true == std::is_base_of_v<jau::math::MathRuntimeErrorBase, jau::math::MathUnderflowError>);
223 static_assert( true == std::is_base_of_v<jau::math::MathErrorBase, jau::math::MathUnderflowError>);
224 static_assert( true == std::is_base_of_v<std::underflow_error, jau::math::MathUnderflowError>);
225 static_assert( true == std::is_base_of_v<std::runtime_error, jau::math::MathUnderflowError>);
226 static_assert( true == std::is_base_of_v<std::exception, jau::math::MathUnderflowError>);
227 REQUIRE_THROWS_MATCHES( throwMathUnderflowError(), jau::math::MathUnderflowError, Catch::Matchers::ContainsSubstring("MathError(underflow)") );
230 REQUIRE_THROWS_AS( throwMathUnderflowError(), std::underflow_error);
231 REQUIRE_THROWS_AS( throwMathUnderflowError(), std::runtime_error);
232 REQUIRE_THROWS_AS( throwMathUnderflowError(), std::exception);
233
234 static_assert( true == std::is_base_of_v<jau::math::MathErrorBase, jau::math::MathDomainError>);
235 static_assert( true == std::is_base_of_v<std::domain_error, jau::math::MathDomainError>);
236 static_assert( true == std::is_base_of_v<std::exception, jau::math::MathDomainError>);
237 REQUIRE_THROWS_MATCHES( throwMathDomainError(), jau::math::MathDomainError, Catch::Matchers::ContainsSubstring("MathError(invalid)") );
238 REQUIRE_THROWS_AS( throwMathDomainError(), jau::math::MathErrorBase);
239 REQUIRE_THROWS_AS( throwMathDomainError(), std::domain_error);
240 REQUIRE_THROWS_AS( throwMathDomainError(), std::exception);
241
242 static_assert( true == std::is_base_of_v<jau::math::MathErrorBase, jau::math::MathDivByZeroError>);
243 static_assert( true == std::is_base_of_v<jau::math::MathDomainError, jau::math::MathDivByZeroError>);
244 static_assert( true == std::is_base_of_v<std::domain_error, jau::math::MathDivByZeroError>);
245 static_assert( true == std::is_base_of_v<std::exception, jau::math::MathDivByZeroError>);
246 REQUIRE_THROWS_MATCHES( throwMathDivByZeroError(), jau::math::MathDivByZeroError, Catch::Matchers::ContainsSubstring("MathError(div_by_zero)") );
249 REQUIRE_THROWS_AS( throwMathDivByZeroError(), std::domain_error);
250 REQUIRE_THROWS_AS( throwMathDivByZeroError(), std::exception);
251 REQUIRE_THROWS_AS( throwMathDivByZeroError(), std::exception);
252}
253
254TEST_CASE( "Exception 11 Math", "[big_int_t][exceptions][error][arithmetic][math]" ) {
255 {
256 jau::mp::BigInt a = 1, b = 0, r;
257 REQUIRE_THROWS_MATCHES( r = a / b, jau::math::MathDivByZeroError, Catch::Matchers::ContainsSubstring("div_by_zero") );
258 REQUIRE_THROWS_MATCHES( r = a % b, jau::math::MathDivByZeroError, Catch::Matchers::ContainsSubstring("div_by_zero") );
259 }
260 {
262 REQUIRE_THROWS_MATCHES( r = a % b, jau::math::MathDomainError, Catch::Matchers::ContainsSubstring("invalid") );
263 }
264}
#define E_FILE_LINE
math_error_t::div_by_zero, i.e.
math_error_t::invalid
math_error_t::inexact
math_error_t::overflow
math_error_t::underflow
Arbitrary precision integer type.
Definition big_int.hpp:43
static BigInt from_s32(int32_t n)
Create big_int from a signed 32 bit integer.
Definition big_int.hpp:96
@ undefined
undefined math error
__pack(...): Produces MSVC, clang and gcc compatible lead-in and -out macros.
Definition backtrace.hpp:32
TEST_CASE("Exception 00", "[exceptions][error]")
static void throwIllegalArgumentError()
static void throwMathError()
static void throwLogicError()
static void throwMathUnderflowError()
static void throwUnsupportedOperationException()
static void throwOutOfMemoryError()
static void throwMathDivByZeroError()
static void throwRuntimeSystemException()
static void throwRuntimeException()
static void throwMathDomainError()
static void throwNotImplementedException()
static void throwIndexOutOfBoundsError()
static void throwMathInexactError()
static void throwIllegalStateError()
static void throwInternalError()
static void throwNullPointerException()
static void throwIOError()
static void throwMathOverflowError()