Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added elementsAreNonAscending(), elementsAreDescending(), elementsAreAscending(), and elementsAreUnique() validators #198

Merged
merged 5 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'}));
}

}
Loading