From 74017bd0731af4be2b4b58179abbf79009db5c3e Mon Sep 17 00:00:00 2001 From: JoelGunawan Date: Tue, 26 Nov 2024 00:11:04 -0800 Subject: [PATCH 1/2] 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()); From 595ef8626858ecece017eef983df525fe9f05465 Mon Sep 17 00:00:00 2001 From: Ashar Fuadi Date: Mon, 2 Dec 2024 21:16:31 +0700 Subject: [PATCH 2/2] Apply suggestions from code review --- include/tcframe/validator/core.hpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/include/tcframe/validator/core.hpp b/include/tcframe/validator/core.hpp index 1875fcb..640c452 100644 --- a/include/tcframe/validator/core.hpp +++ b/include/tcframe/validator/core.hpp @@ -53,8 +53,8 @@ struct VectorElementValidator { } bool satisfies(function predicate) { - for(T v: val) { - if(!predicate(v)) { + for (T v : val) { + if (!predicate(v)) { return false; } } @@ -163,8 +163,8 @@ struct MatrixElementValidator { } bool satisfies(function predicate) { - for(const vector& v : val) { - if(!eachElementOf(v).satisfies(predicate)) { + for (const vector& v : val) { + if (!eachElementOf(v).satisfies(predicate)) { return false; } }