Skip to content

Commit

Permalink
added elementsAreUnique() validator
Browse files Browse the repository at this point in the history
  • Loading branch information
JoelGunawan committed Oct 17, 2024
1 parent c9aa252 commit b839ee6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
13 changes: 5 additions & 8 deletions include/tcframe/validator/vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,11 @@ bool elementsAreAscending(const vector<T> &v) {
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;
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();
}

}
8 changes: 8 additions & 0 deletions test/unit/tcframe/validator/VectorValidatorTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,12 @@ TEST_F(VectorValidatorTests, elementsAreAscending) {
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 b839ee6

Please sign in to comment.