Skip to content

Commit

Permalink
added elementsAreNonAscending(), elementsAreDescending(), and element…
Browse files Browse the repository at this point in the history
…sAreAscending() validators
  • Loading branch information
JoelGunawan committed Oct 17, 2024
1 parent 0ab2d1c commit c9aa252
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
40 changes: 40 additions & 0 deletions include/tcframe/validator/vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,44 @@ bool elementsAreNonDescending(const vector<T>& v) {
return true;
}

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

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

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

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

}
24 changes: 24 additions & 0 deletions test/unit/tcframe/validator/VectorValidatorTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,28 @@ TEST_F(VectorValidatorTests, elementsAreNonDescending) {
EXPECT_TRUE(elementsAreNonDescending(vector<int>{1, 1, 2, 3, 3, 7}));
}

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

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

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

}

0 comments on commit c9aa252

Please sign in to comment.