Skip to content

Commit

Permalink
Add elementsAreNonDecreasing() validator (#193)
Browse files Browse the repository at this point in the history
  • Loading branch information
JoelGunawan authored Oct 14, 2024
1 parent ec7c296 commit 0a0c7bf
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
10 changes: 10 additions & 0 deletions include/tcframe/validator/vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,14 @@ bool eachElementIsBetween(const vector<T>& v, T minVal, T maxVal) {
return true;
}

template<typename T>
bool elementsAreNonDescending(const vector<T>& v) {
for (std::size_t i = 1; i < v.size(); ++i) {
if (v[i - 1] > v[i]) {
return false;
}
}
return true;
}

}
8 changes: 8 additions & 0 deletions test/unit/tcframe/validator/VectorValidatorTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,12 @@ TEST_F(VectorValidatorTests, eachElementIsBetween) {
EXPECT_TRUE(eachElementIsBetween(vector<int>{2, 3, 1, 5, 4}, 0, 6));
}

TEST_F(VectorValidatorTests, elementsAreNonDescending) {
EXPECT_FALSE(elementsAreNonDescending(vector<int>{1, 2, 3, 5, 3}));
EXPECT_FALSE(elementsAreNonDescending(vector<int>{2, 1, 1, 2, 5}));
EXPECT_TRUE(elementsAreNonDescending(vector<int>()));
EXPECT_TRUE(elementsAreNonDescending(vector<int>{1, 2, 3, 4, 5}));
EXPECT_TRUE(elementsAreNonDescending(vector<int>{1, 1, 2, 3, 3, 7}));
}

}

0 comments on commit 0a0c7bf

Please sign in to comment.