forked from arc/libarc-validate
311 lines
5.9 KiB
C++
311 lines
5.9 KiB
C++
namespace arc::validate::asserts
|
|
{
|
|
|
|
template<typename T>
|
|
void
|
|
assert_true(T const& expr, std::source_location origin)
|
|
{
|
|
struct extras_t
|
|
{
|
|
std::source_location origin;
|
|
|
|
void
|
|
print(std::ostream& o) const
|
|
{
|
|
o << " assertion failed: " << origin.file_name() << ':' << origin.line() << '\n';
|
|
}
|
|
|
|
};
|
|
|
|
if (expr == true) {
|
|
return;
|
|
}
|
|
|
|
throw failure_t{origin, "assertion failed", extras_t{origin}};
|
|
}
|
|
|
|
template<typename T>
|
|
void
|
|
assert_false(T const& expr, std::source_location origin)
|
|
{
|
|
struct extras_t
|
|
{
|
|
std::source_location origin;
|
|
|
|
void
|
|
print(std::ostream& o) const
|
|
{
|
|
o << " assertion failed: " << origin.file_name() << ':' << origin.line() << '\n';
|
|
}
|
|
|
|
};
|
|
|
|
if (expr == false) {
|
|
return;
|
|
}
|
|
|
|
throw failure_t{origin, "assertion failed", extras_t{origin}};
|
|
}
|
|
|
|
template<typename T>
|
|
void
|
|
assert_nullptr(T const& expr, std::source_location origin)
|
|
{
|
|
struct extras_t
|
|
{
|
|
std::source_location origin;
|
|
|
|
void
|
|
print(std::ostream& o) const
|
|
{
|
|
o << " assertion failed: " << origin.file_name() << ':' << origin.line() << '\n';
|
|
}
|
|
|
|
};
|
|
|
|
if (expr == nullptr) {
|
|
return;
|
|
}
|
|
|
|
throw failure_t{origin, "assertion failed", extras_t{origin}};
|
|
}
|
|
|
|
template<typename T>
|
|
void
|
|
assert_not_nullptr(T const& expr, std::source_location origin)
|
|
{
|
|
struct extras_t
|
|
{
|
|
std::source_location origin;
|
|
|
|
void
|
|
print(std::ostream& o) const
|
|
{
|
|
o << " assertion failed: " << origin.file_name() << ':' << origin.line() << '\n';
|
|
}
|
|
|
|
};
|
|
|
|
if (expr != nullptr) {
|
|
return;
|
|
}
|
|
|
|
throw failure_t{origin, "assertion failed", extras_t{origin}};
|
|
}
|
|
|
|
template<typename L, typename R>
|
|
void
|
|
assert_equal(L const& left, R const& right, std::source_location origin)
|
|
{
|
|
struct extras_t
|
|
{
|
|
std::source_location origin;
|
|
|
|
void
|
|
print(std::ostream& o) const
|
|
{
|
|
o << " assertion failed: " << origin.file_name() << ':' << origin.line() << '\n';
|
|
}
|
|
|
|
};
|
|
|
|
if (left == right) {
|
|
return;
|
|
}
|
|
|
|
throw failure_t{origin, "assertion failed", extras_t{origin}};
|
|
}
|
|
|
|
template<typename L, typename R>
|
|
void
|
|
assert_not_equal(L const& left, R const& right, std::source_location origin)
|
|
{
|
|
struct extras_t
|
|
{
|
|
std::source_location origin;
|
|
|
|
void
|
|
print(std::ostream& o) const
|
|
{
|
|
o << " assertion failed: " << origin.file_name() << ':' << origin.line() << '\n';
|
|
}
|
|
|
|
};
|
|
|
|
if (left != right) {
|
|
return;
|
|
}
|
|
|
|
throw failure_t{origin, "assertion failed", extras_t{origin}};
|
|
}
|
|
|
|
template<typename L, typename R>
|
|
void
|
|
assert_less_than(L const& left, R const& right, std::source_location origin)
|
|
{
|
|
struct extras_t
|
|
{
|
|
std::source_location origin;
|
|
|
|
void
|
|
print(std::ostream& o) const
|
|
{
|
|
o << " assertion failed: " << origin.file_name() << ':' << origin.line() << '\n';
|
|
}
|
|
|
|
};
|
|
|
|
if (left < right) {
|
|
return;
|
|
}
|
|
|
|
throw failure_t{origin, "assertion failed", extras_t{origin}};
|
|
}
|
|
|
|
template<typename L, typename R>
|
|
void
|
|
assert_less_than_or_equal(L const& left, R const& right, std::source_location origin)
|
|
{
|
|
struct extras_t
|
|
{
|
|
std::source_location origin;
|
|
|
|
void
|
|
print(std::ostream& o) const
|
|
{
|
|
o << " assertion failed: " << origin.file_name() << ':' << origin.line() << '\n';
|
|
}
|
|
|
|
};
|
|
|
|
if (left <= right) {
|
|
return;
|
|
}
|
|
|
|
throw failure_t{origin, "assertion failed", extras_t{origin}};
|
|
}
|
|
|
|
template<typename L, typename R>
|
|
void
|
|
assert_greater_than(L const& left, R const& right, std::source_location origin)
|
|
{
|
|
struct extras_t
|
|
{
|
|
std::source_location origin;
|
|
|
|
void
|
|
print(std::ostream& o) const
|
|
{
|
|
o << " assertion failed: " << origin.file_name() << ':' << origin.line() << '\n';
|
|
}
|
|
|
|
};
|
|
|
|
if (left > right) {
|
|
return;
|
|
}
|
|
|
|
throw failure_t{origin, "assertion failed", extras_t{origin}};
|
|
}
|
|
|
|
template<typename L, typename R>
|
|
void
|
|
assert_greater_than_or_equal(L const& left, R const& right, std::source_location origin)
|
|
{
|
|
struct extras_t
|
|
{
|
|
std::source_location origin;
|
|
|
|
void
|
|
print(std::ostream& o) const
|
|
{
|
|
o << " assertion failed: " << origin.file_name() << ':' << origin.line() << '\n';
|
|
}
|
|
|
|
};
|
|
|
|
if (left >= right) {
|
|
return;
|
|
}
|
|
|
|
throw failure_t{origin, "assertion failed", extras_t{origin}};
|
|
}
|
|
|
|
template<typename E, typename F>
|
|
void
|
|
assert_throws(F const& function, std::source_location origin)
|
|
{
|
|
struct extras_t
|
|
{
|
|
std::source_location origin;
|
|
|
|
void
|
|
print(std::ostream& o) const
|
|
{
|
|
o << " assertion failed: " << origin.file_name() << ':' << origin.line() << '\n';
|
|
}
|
|
|
|
};
|
|
|
|
try {
|
|
function();
|
|
}
|
|
catch (E const&) {
|
|
return;
|
|
}
|
|
|
|
throw failure_t{origin, "assertion failed", extras_t{origin}};
|
|
}
|
|
|
|
template<typename F>
|
|
void
|
|
assert_throws_any(F const& function, std::source_location origin)
|
|
{
|
|
struct extras_t
|
|
{
|
|
std::source_location origin;
|
|
|
|
void
|
|
print(std::ostream& o) const
|
|
{
|
|
o << " assertion failed: " << origin.file_name() << ':' << origin.line() << '\n';
|
|
}
|
|
|
|
};
|
|
|
|
try {
|
|
function();
|
|
}
|
|
catch (...) {
|
|
return;
|
|
}
|
|
|
|
throw failure_t{origin, "assertion failed", extras_t{origin}};
|
|
}
|
|
|
|
template<typename F>
|
|
void
|
|
assert_does_not_throw(F const& function, std::source_location origin)
|
|
{
|
|
struct extras_t
|
|
{
|
|
std::source_location origin;
|
|
|
|
void
|
|
print(std::ostream& o) const
|
|
{
|
|
o << " assertion failed: " << origin.file_name() << ':' << origin.line() << '\n';
|
|
}
|
|
|
|
};
|
|
|
|
try {
|
|
function();
|
|
}
|
|
catch (...) {
|
|
throw failure_t{origin, "assertion failed", extras_t{origin}};
|
|
}
|
|
}
|
|
|
|
} // namespace arc::validate::asserts
|