Skip to content

Commit

Permalink
Added elementsAreNonAscending(), elementsAreDescending(), elementsAre…
Browse files Browse the repository at this point in the history
…Ascending(), and elementsAreUnique() validators (#198)

* added elementsAreNonDescending() validator function

* added elementsAreNonDescending() validator test

* added elementsAreNonAscending(), elementsAreDescending(), and elementsAreAscending() validators

* added elementsAreUnique() validator
  • Loading branch information
JoelGunawan authored Oct 22, 2024
1 parent 9b6b82c commit e4bbaa0
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
37 changes: 37 additions & 0 deletions include/tcframe/validator/vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,41 @@ 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>
bool elementsAreUnique(vector<T> v) {
sort(v.begin(), v.end());
std::size_t ns = unique(v.begin(), v.end()) - v.begin();
return ns == v.size();
}

}
32 changes: 32 additions & 0 deletions test/unit/tcframe/validator/VectorValidatorTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,36 @@ 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}));
}

TEST_F(VectorValidatorTests, elementsAreUnique) {
EXPECT_FALSE(elementsAreUnique(vector<int>{5, 1, 3, 4, 2, 1}));
EXPECT_FALSE(elementsAreUnique(vector<char>{'a', 'c', 'f', 'f', 'd'}));
EXPECT_TRUE(elementsAreUnique(vector<int>()));
EXPECT_TRUE(elementsAreUnique(vector<int>{5, 2, 4, 1, 9}));
EXPECT_TRUE(elementsAreUnique(vector<char>{'a', 'x', 'd', 'g', 'h'}));
}

}

0 comments on commit e4bbaa0

Please sign in to comment.