Implement more assertions

Closes #2
This commit is contained in:
RYAN 2024-09-03 01:55:12 +02:00
parent aab07c0720
commit 55020f6b5c
Signed by: RYAN
GPG Key ID: 3BD93EABD1407B82
2 changed files with 311 additions and 3 deletions

View File

@ -20,6 +20,50 @@ namespace arc::validate::asserts
void void
assert_false(T const&, std::source_location = std::source_location::current()); assert_false(T const&, std::source_location = std::source_location::current());
template<typename T>
void
assert_nullptr(T const&, std::source_location = std::source_location::current());
template<typename T>
void
assert_not_nullptr(T const&, std::source_location = std::source_location::current());
template<typename L, typename R>
void
assert_equal(L const&, R const&, std::source_location = std::source_location::current());
template<typename L, typename R>
void
assert_not_equal(L const&, R const&, std::source_location = std::source_location::current());
template<typename L, typename R>
void
assert_less_than(L const&, R const&, std::source_location = std::source_location::current());
template<typename L, typename R>
void
assert_less_than_or_equal(L const&, R const&, std::source_location = std::source_location::current());
template<typename L, typename R>
void
assert_greater_than(L const&, R const&, std::source_location = std::source_location::current());
template<typename L, typename R>
void
assert_greater_than_or_equal(L const&, R const&, std::source_location = std::source_location::current());
template<typename E, typename F>
void
assert_throws(F const&, std::source_location = std::source_location::current());
template<typename F>
void
assert_throws_any(F const&, std::source_location = std::source_location::current());
template<typename F>
void
assert_does_not_throw(F const&, std::source_location = std::source_location::current());
} // namespace arc::validate::asserts } // namespace arc::validate::asserts
#include <arc/validate/asserts.txx> #include <arc/validate/asserts.txx>

View File

@ -17,9 +17,11 @@ namespace arc::validate::asserts
}; };
if (expr != true) { if (expr == true) {
throw failure_t{origin, "assertion failed", extras_t{origin}}; return;
} }
throw failure_t{origin, "assertion failed", extras_t{origin}};
} }
template<typename T> template<typename T>
@ -38,7 +40,269 @@ namespace arc::validate::asserts
}; };
if (expr != false) { 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}}; throw failure_t{origin, "assertion failed", extras_t{origin}};
} }
} }