From 4e854516c1eab3d81bbabeb0ea29c6d0a43f9f29 Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Sun, 13 Dec 2020 11:41:41 +0100 Subject: [PATCH] Rename Assert macro to Ensure, clean up exception to be in line with THROW. --- src/Assert.hpp | 42 ++++++++++++++++------------------ src/Exception.hpp | 8 +++++-- src/Memory/__BorrowedPtr.hpp | 4 ++-- src/Memory/__ScopedPtr.hpp | 2 +- src/Memory/__UniquePtr.hpp | 4 ++-- src/Memory/__UniquePtrList.hpp | 10 ++++---- src/Random.hpp | 6 ++--- tests/AssertTests.cpp | 31 ------------------------- tests/EnsureTests.cpp | 28 +++++++++++++++++++++++ 9 files changed, 67 insertions(+), 68 deletions(-) delete mode 100644 tests/AssertTests.cpp create mode 100644 tests/EnsureTests.cpp diff --git a/src/Assert.hpp b/src/Assert.hpp index 3b40cd3..8fee4df 100644 --- a/src/Assert.hpp +++ b/src/Assert.hpp @@ -1,38 +1,36 @@ /** @file */ #include "Exception.hpp" -#define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__) +/// \defgroup Ensure Ensure +/// \brief A set of ensure macros. -/// \defgroup Asserts Asserts -/// \brief A set of assertion macros. - -#ifndef NO_ASSERT -/// @brief Asserts an expression is true. Throws an exception if the assertion fails. -/// @ingroup Asserts -#define Assert(expr) \ +#ifndef NO_ENSURE +/// @brief Ensures an expression is true. Throws an exception if the assertion fails. +/// @ingroup Ensure +#define Ensure(expr) \ if (!(expr)) { \ std::stringstream ss; \ - ss << "ASSERTION FAILED: [" << __FILENAME__ << " (" << __LINE__ << ")] \""; \ + ss << "[" << file_name(__FILE__) << ":" << __LINE__ << "] ENSURE FAILED: \""; \ ss << #expr << "\""; \ throw ArbUt::Exception(ss.str()); \ } -/// @brief Asserts an expression is true for a range. The value in the range can be retrieved using ``item``. -/// @ingroup Asserts -#define AssertForEach(iterator, assertion) \ +/// @brief Ensures an expression is true for a range. The value in the range can be retrieved using ``item``. +/// @ingroup Ensure +#define EnsureForEach(iterator, assertion) \ { \ for (auto item : (iterator)) \ - Assert(assertion) \ + Ensure(assertion) \ } #else -// Assert is empty if NO_ASSERT is defined. -#define Assert(expr) ; -#define AssertForEach(iterator, assertion) ; +// Ensure is empty if NO_ENSURE is defined. +#define Ensure(expr) ; +#define EnsureForEach(iterator, assertion) ; #endif -/// @brief Asserts a pointer is not null. -/// @ingroup Asserts -#define AssertNotNull(value) Assert((value) != nullptr) -/// @brief Asserts a range is not null. -/// @ingroup Asserts -#define AssertAllNotNull(iterator) AssertForEach(iterator, item != nullptr) +/// @brief Ensures a pointer is not null. +/// @ingroup Ensure +#define EnsureNotNull(value) Ensure((value) != nullptr) +/// @brief Ensures a range is not null. +/// @ingroup Ensure +#define EnsureAllNotNull(iterator) EnsureForEach(iterator, item != nullptr) diff --git a/src/Exception.hpp b/src/Exception.hpp index b36f04b..2881452 100644 --- a/src/Exception.hpp +++ b/src/Exception.hpp @@ -140,9 +140,13 @@ namespace ArbUt { }; } -static constexpr const char* file_name(const char* path) { +#if !__cpp_consteval +#define consteval constexpr +#endif + +static consteval const char* file_name(const char* path) { const char* file = path; - while (*path) { + while (*path != 0) { if (*path++ == '/') { file = path; } diff --git a/src/Memory/__BorrowedPtr.hpp b/src/Memory/__BorrowedPtr.hpp index e4d08e8..ee63693 100644 --- a/src/Memory/__BorrowedPtr.hpp +++ b/src/Memory/__BorrowedPtr.hpp @@ -16,7 +16,7 @@ namespace ArbUt { public: inline BorrowedPtr() {} /// @brief Initialise a BorrowedPtr with a specific raw pointer. - inline BorrowedPtr(T* ptr) : _raw(ptr) { AssertNotNull(ptr); }; + inline BorrowedPtr(T* ptr) : _raw(ptr) { EnsureNotNull(ptr); }; /// @brief Initialise a BorrowedPtr from a copy. inline BorrowedPtr(const BorrowedPtr& other) : _raw(other._raw){}; /// @brief Initialise a BorrowedPtr with a std unique_ptr. @@ -39,7 +39,7 @@ namespace ArbUt { if (_raw == rhs) { return *this; } - AssertNotNull(rhs); + EnsureNotNull(rhs); _raw = rhs; return *this; } diff --git a/src/Memory/__ScopedPtr.hpp b/src/Memory/__ScopedPtr.hpp index b03defe..4c34d8a 100644 --- a/src/Memory/__ScopedPtr.hpp +++ b/src/Memory/__ScopedPtr.hpp @@ -43,7 +43,7 @@ namespace ArbUt { /// @brief Operator for access into underlying pointer. /// @warning Note that this asserts that the underlying pointer is not null first, to prevent segfaults. inline T* operator->() const noexcept { - AssertNotNull(_raw); + EnsureNotNull(_raw); return _raw; } diff --git a/src/Memory/__UniquePtr.hpp b/src/Memory/__UniquePtr.hpp index 88e3e57..ea6ebdd 100644 --- a/src/Memory/__UniquePtr.hpp +++ b/src/Memory/__UniquePtr.hpp @@ -16,7 +16,7 @@ namespace ArbUt { public: inline UniquePtr() {} /// @brief Initialise a UniquePtr with a specific raw pointer. - inline UniquePtr(T* ptr) : _raw(ptr) { AssertNotNull(ptr); }; + inline UniquePtr(T* ptr) : _raw(ptr) { EnsureNotNull(ptr); }; /// @brief Initialise a UniquePtr from a copy. inline UniquePtr(const UniquePtr& other) : _raw(other._raw){}; /// @brief Initialise a UniquePtr with a std unique_ptr. @@ -42,7 +42,7 @@ namespace ArbUt { return *this; } delete _raw; - AssertNotNull(rhs); + EnsureNotNull(rhs); _raw = rhs; return *this; } diff --git a/src/Memory/__UniquePtrList.hpp b/src/Memory/__UniquePtrList.hpp index 344d208..515fd17 100644 --- a/src/Memory/__UniquePtrList.hpp +++ b/src/Memory/__UniquePtrList.hpp @@ -17,7 +17,7 @@ namespace ArbUt { inline UniquePtrList() noexcept : _vector() {} /// @brief Initialises a UniquePtrList from a std::vector of raw pointers. /// @param vec A std::vector of raw pointers. - inline UniquePtrList(const std::vector& vec) noexcept : _vector(vec) { AssertAllNotNull(vec); } + inline UniquePtrList(const std::vector& vec) noexcept : _vector(vec) { EnsureAllNotNull(vec); } /// @brief Initialises a UniquePtrList with a certain capacity reserved for use. This does not set immediate /// size. /// @param capacity The desired capacity. @@ -25,13 +25,13 @@ namespace ArbUt { /// @brief Initialises a UniquePtrList from a initialiser_list. /// @param l A initialiser_list inline UniquePtrList(const std::initializer_list& l) noexcept : _vector(l) { - AssertAllNotNull(_vector); + EnsureAllNotNull(_vector); } /// @brief Initialises a UniquePtrList from a raw pointer range. /// @param begin The raw pointer to the start of the list. /// @param end The raw pointer to the end of the list. inline UniquePtrList(ValueT* const* begin, ValueT* const* end) noexcept : _vector(begin, end) { - AssertAllNotNull(_vector); + EnsureAllNotNull(_vector); } UniquePtrList(const UniquePtrList&) = delete; @@ -80,7 +80,7 @@ namespace ArbUt { /// @param index The index to set the pointer to. /// @param ptr The pointer to store. void Set(size_t index, ValueT* ptr) { - AssertNotNull(ptr); + EnsureNotNull(ptr); delete _vector[index]; _vector[index] = ptr; } @@ -119,7 +119,7 @@ namespace ArbUt { /// @brief Append a pointer to the list. /// @param value The pointer to push to the list. inline void Append(ValueT* value) { - AssertNotNull(value); + EnsureNotNull(value); _vector.push_back(value); } /// @brief Borrow a pointer at a certain index. diff --git a/src/Random.hpp b/src/Random.hpp index 5af0500..966966e 100644 --- a/src/Random.hpp +++ b/src/Random.hpp @@ -43,7 +43,7 @@ namespace ArbUt { /// @brief Gets a random 32 bit integer between 0, and given max parameter. /// @param max The exclusive max value the random value should be. [[nodiscard]] inline int32_t Get(int32_t max) { - Assert(max > 0); + Ensure(max > 0); std::uniform_int_distribution distribution(0, max - 1); return distribution(_rng); } @@ -52,7 +52,7 @@ namespace ArbUt { /// @param min The inclusive min value the random value should be. /// @param max The exclusive max value the random value should be. [[nodiscard]] inline int32_t Get(int32_t min, int32_t max) { - Assert(max > min); + Ensure(max > min); std::uniform_int_distribution distribution(min, max - 1); return distribution(_rng); } @@ -71,7 +71,7 @@ namespace ArbUt { /// @param min The inclusive min value the random value should be. /// @param max The exclusive max value the random value should be. [[nodiscard]] inline uint32_t GetUnsigned(uint32_t min, uint32_t max) { - Assert(max > min); + Ensure(max > min); std::uniform_int_distribution distribution(min, max - 1); return distribution(_rng); } diff --git a/tests/AssertTests.cpp b/tests/AssertTests.cpp deleted file mode 100644 index 8196151..0000000 --- a/tests/AssertTests.cpp +++ /dev/null @@ -1,31 +0,0 @@ -#include "../extern/doctest.hpp" -#include "../src/Assert.hpp" -void TestWrapper(bool wrapperExpression) { Assert(wrapperExpression) } -void TestWrapperNotNull(void* value) { AssertNotNull(value) } - -TEST_CASE("Assert succeeds if true") { REQUIRE_NOTHROW(TestWrapper(true)); } - -TEST_CASE("Assert throws if false") { REQUIRE_THROWS(TestWrapper(false)); } - -TEST_CASE("Assert throws if false with message") { - try { - TestWrapper(false); - } catch (const ArbUt::Exception& e) { - REQUIRE(strcmp(e.what(), "ASSERTION FAILED: [AssertTests.cpp (3)] \"wrapperExpression\"") == 0); - return; - } - throw ArbUt::Exception("Didn't throw."); -} - -TEST_CASE("Multiple asserts") { - Assert(true) - Assert(true) - Assert(true) -} - -TEST_CASE("AssertNotNull throws if nullptr") { REQUIRE_THROWS(TestWrapperNotNull(nullptr)); } - -TEST_CASE("Assert for each") { - auto i = {10, 500, 2300, 454}; - AssertForEach(i, item > 0) -} diff --git a/tests/EnsureTests.cpp b/tests/EnsureTests.cpp new file mode 100644 index 0000000..2475d36 --- /dev/null +++ b/tests/EnsureTests.cpp @@ -0,0 +1,28 @@ +#include "../extern/doctest.hpp" +#include "../src/Assert.hpp" +void TestWrapper(bool wrapperExpression) { Ensure(wrapperExpression) } +void TestWrapperNotNull(void* value) {EnsureNotNull(value) } + +TEST_CASE("Ensure succeeds if true") { REQUIRE_NOTHROW(TestWrapper(true)); } + +TEST_CASE("Ensure throws if false") { REQUIRE_THROWS(TestWrapper(false)); } + +TEST_CASE("Ensure throws if false with message") { + try { + TestWrapper(false); + } catch (const ArbUt::Exception& e) { + REQUIRE(std::string(e.what()) == "[EnsureTests.cpp:3] ENSURE FAILED: \"wrapperExpression\""); + return; + } + throw ArbUt::Exception("Didn't throw."); +} + +TEST_CASE("Multiple asserts") {Ensure(true) Ensure(true) Ensure(true) +} + +TEST_CASE("EnsureNotNull throws if nullptr") { REQUIRE_THROWS(TestWrapperNotNull(nullptr)); } + +TEST_CASE("Ensure for each") { + auto i = {10, 500, 2300, 454}; + EnsureForEach(i, item > 0) +}