Skip to content

Commit

Permalink
implement eachElementOf(v).satisfies(predicate)
Browse files Browse the repository at this point in the history
  • Loading branch information
JoelGunawan committed Nov 26, 2024
1 parent 5c9a744 commit c9cef3a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
20 changes: 20 additions & 0 deletions include/tcframe/validator/core.hpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#pragma once

#include <functional>
#include <string>
#include <type_traits>
#include <vector>

using std::enable_if_t;
using std::function;
using std::is_arithmetic_v;
using std::size_t;
using std::string;
Expand Down Expand Up @@ -49,6 +51,15 @@ struct VectorElementValidator {
}
return true;
}

bool satisfies(function<bool(T)> predicate) {
for(T v: val) {
if(!predicate(v)) {
return false;
}
}
return true;
}
};

template<typename T, typename = ScalarType<T>>
Expand Down Expand Up @@ -150,6 +161,15 @@ struct MatrixElementValidator {
}
return true;
}

bool satisfies(function<bool(T)> predicate) {
for(const vector<T>& v : val) {
if(!eachElementOf(v).satisfies(predicate)) {
return false;
}
}
return true;
}
};

template<typename T, typename = ScalarType<T>>
Expand Down
15 changes: 15 additions & 0 deletions test/unit/tcframe/validator/CoreValidatorTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,21 @@ TEST_F(CoreValidatorTests, eachElementOf_isBetween) {
vector<int>{3, 4}}).isBetween(1, 4));
}

TEST_F(CoreValidatorTests, eachElementOf_satisfies) {
EXPECT_FALSE(eachElementOf(vector<int>{3, 8, 5, 7, 9}).satisfies([](int a) { return a > 3; }));
EXPECT_FALSE(eachElementOf(vector<int>{3, 8, 5, 7, 9}).satisfies([](int a) { return (a % 2) == 0; }));
EXPECT_FALSE(eachElementOf(vector<int>{3, 8, 5, 7, 9}).satisfies([](int a) { return a <= 8; }));
EXPECT_TRUE(eachElementOf(vector<int>{3, 8, 5, 7, 9}).satisfies([](int a) { return a > 0; }));

EXPECT_TRUE(eachElementOf(vector<int>{}).satisfies([](int a) { return false; }));

EXPECT_FALSE(eachElementOf(vector<vector<int>>{vector<int>{3, 8, 5}, vector<int>{7, 9}}).satisfies([](int a) { return a > 3; }));
EXPECT_FALSE(eachElementOf(vector<vector<int>>{vector<int>{}, vector<int>{3, 8, 5, 7, 9}}).satisfies([](int a) { return (a % 2) == 0; }));
EXPECT_TRUE(eachElementOf(vector<vector<int>>{vector<int>{3, 8, 5, 7}, vector<int>{9}}).satisfies([](int a) { return a > 0; }));

EXPECT_TRUE(eachElementOf(vector<vector<int>>{}).satisfies([](int a) { return false; }));
}

TEST_F(CoreValidatorTests, elementsOf_areAscending) {
EXPECT_FALSE(elementsOf(vector<int>{1, 2, 3, 5, 3}).areAscending());
EXPECT_FALSE(elementsOf(vector<int>{2, 1, 1, 2, 5}).areAscending());
Expand Down

0 comments on commit c9cef3a

Please sign in to comment.