Skip to content

Commit

Permalink
Validator: support matrix for eachElementOf()
Browse files Browse the repository at this point in the history
  • Loading branch information
fushar committed Nov 2, 2024
1 parent 8b40821 commit 534d5f6
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
23 changes: 23 additions & 0 deletions include/tcframe/validator/core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,27 @@ inline StringElementValidator eachCharacterOf(const string& val) {
return StringElementValidator(val);
}

template<typename T, typename = ScalarType<T>>
struct MatrixElementValidator {
private:
const vector<vector<T>>& val;

public:
explicit MatrixElementValidator(const vector<vector<T>>& _val) : val(_val) {}

bool isBetween(T minVal, T maxVal) {
for (const vector<T>& v : val) {
if (!eachElementOf(v).isBetween(minVal, maxVal)) {
return false;
}
}
return true;
}
};

template<typename T, typename = ScalarType<T>>
inline MatrixElementValidator<T> eachElementOf(const vector<vector<T>>& val) {
return MatrixElementValidator(val);
}

}
12 changes: 12 additions & 0 deletions test/unit/tcframe/validator/CoreValidatorTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@ TEST_F(CoreValidatorTests, eachElementOf_isBetween) {
EXPECT_FALSE(eachElementOf(vector<int>{2, 3, 1, 5, 4}).isBetween(2, 4));
EXPECT_TRUE(eachElementOf(vector<int>{2, 3, 1, 5, 4}).isBetween(1, 5));
EXPECT_TRUE(eachElementOf(vector<int>{2, 3, 1, 5, 4}).isBetween(0, 6));

EXPECT_TRUE(eachElementOf(vector<int>{}).isBetween(0, 6));

EXPECT_FALSE(eachElementOf(vector<vector<int>>{
vector<int>{1, 2},
vector<int>{3, 4}}).isBetween(2, 4));
EXPECT_FALSE(eachElementOf(vector<vector<int>>{
vector<int>{1, 2},
vector<int>{3, 4}}).isBetween(1, 5));
EXPECT_TRUE(eachElementOf(vector<vector<int>>{
vector<int>{1, 2},
vector<int>{3, 4}}).isBetween(1, 4));
}

TEST_F(CoreValidatorTests, elementsOf_areAscending) {
Expand Down

0 comments on commit 534d5f6

Please sign in to comment.