From 195043d4c5ecbb1cf042cac3ac0b9d2ddafee2c6 Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Wed, 22 Apr 2020 12:44:50 +0200 Subject: [PATCH] Implements easier assertions for iterators --- src/Assert.hpp | 6 +++--- tests/AssertTests.cpp | 5 +++++ 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/Assert.hpp b/src/Assert.hpp index 503b353..c8a3c76 100644 --- a/src/Assert.hpp +++ b/src/Assert.hpp @@ -12,12 +12,12 @@ ss << #expr << "\""; \ throw std::logic_error(ss.str()); \ } +#define AssertForEach(iterator, assertion) { for (auto item: iterator) Assert(assertion)} #else // Assert is empty if NO_ASSERT is defined. #define Assert(expr) ; +#define AssertForEach(iterator, assertion) ; #endif #define AssertNotNull(value) Assert(value != nullptr) -#define UnpackWeakPtr(weakPtr, sharedPtr) \ - Assert(!weakPtr.expired()); \ - auto sharedPtr = weakPtr.lock(); +#define AssertAllNotNull(iterator) AssertForEach(iterator, item != nullptr) diff --git a/tests/AssertTests.cpp b/tests/AssertTests.cpp index dc79505..7d627ab 100644 --- a/tests/AssertTests.cpp +++ b/tests/AssertTests.cpp @@ -19,3 +19,8 @@ TEST_CASE("Multiple asserts", "[Utilities]") { } TEST_CASE("AssertNotNull throws if nullptr", "[Utilities]") { REQUIRE_THROWS(TestWrapperNotNull(nullptr)); } + +TEST_CASE("Assert for each", "[Utilities]") { + auto i = {10, 500, 2300, 454}; + AssertForEach(i, item > 0) +}