From c9cef3ad91b029d9f231d106306eefe9dcd23acc Mon Sep 17 00:00:00 2001 From: JoelGunawan Date: Tue, 26 Nov 2024 00:11:04 -0800 Subject: [PATCH] implement eachElementOf(v).satisfies(predicate) --- include/tcframe/validator/core.hpp | 20 +++++++++++++++++++ .../tcframe/validator/CoreValidatorTests.cpp | 15 ++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/include/tcframe/validator/core.hpp b/include/tcframe/validator/core.hpp index 3050cfb..1875fcb 100644 --- a/include/tcframe/validator/core.hpp +++ b/include/tcframe/validator/core.hpp @@ -1,10 +1,12 @@ #pragma once +#include #include #include #include using std::enable_if_t; +using std::function; using std::is_arithmetic_v; using std::size_t; using std::string; @@ -49,6 +51,15 @@ struct VectorElementValidator { } return true; } + + bool satisfies(function predicate) { + for(T v: val) { + if(!predicate(v)) { + return false; + } + } + return true; + } }; template> @@ -150,6 +161,15 @@ struct MatrixElementValidator { } return true; } + + bool satisfies(function predicate) { + for(const vector& v : val) { + if(!eachElementOf(v).satisfies(predicate)) { + return false; + } + } + return true; + } }; template> diff --git a/test/unit/tcframe/validator/CoreValidatorTests.cpp b/test/unit/tcframe/validator/CoreValidatorTests.cpp index 40ffaf8..cd76a09 100644 --- a/test/unit/tcframe/validator/CoreValidatorTests.cpp +++ b/test/unit/tcframe/validator/CoreValidatorTests.cpp @@ -38,6 +38,21 @@ TEST_F(CoreValidatorTests, eachElementOf_isBetween) { vector{3, 4}}).isBetween(1, 4)); } +TEST_F(CoreValidatorTests, eachElementOf_satisfies) { + EXPECT_FALSE(eachElementOf(vector{3, 8, 5, 7, 9}).satisfies([](int a) { return a > 3; })); + EXPECT_FALSE(eachElementOf(vector{3, 8, 5, 7, 9}).satisfies([](int a) { return (a % 2) == 0; })); + EXPECT_FALSE(eachElementOf(vector{3, 8, 5, 7, 9}).satisfies([](int a) { return a <= 8; })); + EXPECT_TRUE(eachElementOf(vector{3, 8, 5, 7, 9}).satisfies([](int a) { return a > 0; })); + + EXPECT_TRUE(eachElementOf(vector{}).satisfies([](int a) { return false; })); + + EXPECT_FALSE(eachElementOf(vector>{vector{3, 8, 5}, vector{7, 9}}).satisfies([](int a) { return a > 3; })); + EXPECT_FALSE(eachElementOf(vector>{vector{}, vector{3, 8, 5, 7, 9}}).satisfies([](int a) { return (a % 2) == 0; })); + EXPECT_TRUE(eachElementOf(vector>{vector{3, 8, 5, 7}, vector{9}}).satisfies([](int a) { return a > 0; })); + + EXPECT_TRUE(eachElementOf(vector>{}).satisfies([](int a) { return false; })); +} + TEST_F(CoreValidatorTests, elementsOf_areAscending) { EXPECT_FALSE(elementsOf(vector{1, 2, 3, 5, 3}).areAscending()); EXPECT_FALSE(elementsOf(vector{2, 1, 1, 2, 5}).areAscending());