From 55020f6b5cb77b110ab99f6fea3bc4afb9cec822 Mon Sep 17 00:00:00 2001 From: Ryan Date: Tue, 3 Sep 2024 01:55:12 +0200 Subject: [PATCH] Implement more assertions Closes #2 --- arc/validate/asserts.hxx | 44 +++++++ arc/validate/asserts.txx | 270 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 311 insertions(+), 3 deletions(-) diff --git a/arc/validate/asserts.hxx b/arc/validate/asserts.hxx index 12232ac..204395c 100644 --- a/arc/validate/asserts.hxx +++ b/arc/validate/asserts.hxx @@ -20,6 +20,50 @@ namespace arc::validate::asserts void assert_false(T const&, std::source_location = std::source_location::current()); + template + void + assert_nullptr(T const&, std::source_location = std::source_location::current()); + + template + void + assert_not_nullptr(T const&, std::source_location = std::source_location::current()); + + template + void + assert_equal(L const&, R const&, std::source_location = std::source_location::current()); + + template + void + assert_not_equal(L const&, R const&, std::source_location = std::source_location::current()); + + template + void + assert_less_than(L const&, R const&, std::source_location = std::source_location::current()); + + template + void + assert_less_than_or_equal(L const&, R const&, std::source_location = std::source_location::current()); + + template + void + assert_greater_than(L const&, R const&, std::source_location = std::source_location::current()); + + template + void + assert_greater_than_or_equal(L const&, R const&, std::source_location = std::source_location::current()); + + template + void + assert_throws(F const&, std::source_location = std::source_location::current()); + + template + void + assert_throws_any(F const&, std::source_location = std::source_location::current()); + + template + void + assert_does_not_throw(F const&, std::source_location = std::source_location::current()); + } // namespace arc::validate::asserts #include diff --git a/arc/validate/asserts.txx b/arc/validate/asserts.txx index 6300153..fc484c1 100644 --- a/arc/validate/asserts.txx +++ b/arc/validate/asserts.txx @@ -17,9 +17,11 @@ namespace arc::validate::asserts }; - if (expr != true) { - throw failure_t{origin, "assertion failed", extras_t{origin}}; + if (expr == true) { + return; } + + throw failure_t{origin, "assertion failed", extras_t{origin}}; } template @@ -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 + 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 + 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 + 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 + 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 + 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 + 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 + 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 + 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 + 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 + 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 + 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}}; } }