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

Validator: refactor vector validators to core #201

Merged
merged 1 commit into from
Oct 29, 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
2 changes: 0 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,6 @@ set(INCLUDE
include/tcframe/util/StringUtils.hpp
include/tcframe/util/optional.hpp
include/tcframe/validator/core.hpp
include/tcframe/validator/vector.hpp
)

set(TEST_UNIT
Expand Down Expand Up @@ -261,7 +260,6 @@ set(TEST_UNIT
test/unit/tcframe/util/StringUtilsTests.cpp
test/unit/tcframe/util/TestUtils.hpp
test/unit/tcframe/validator/CoreValidatorTests.cpp
test/unit/tcframe/validator/VectorValidatorTests.cpp
)

add_executable(test_unit ${INCLUDE} ${TEST_UNIT})
Expand Down
87 changes: 87 additions & 0 deletions include/tcframe/validator/core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

using std::enable_if_t;
using std::is_arithmetic_v;
using std::size_t;
using std::vector;

namespace tcframe {

Expand All @@ -27,4 +29,89 @@ ScalarValidator<T> valueOf(T val) {
return ScalarValidator(val);
}

template<typename T, typename = ScalarType<T>>
struct VectorElementValidator {
private:
const vector<T>& val;

public:
explicit VectorElementValidator(const vector<T>& _val) : val(_val) {}

bool isBetween(T minVal, T maxVal) {
for (T v : val) {
if (!valueOf(v).isBetween(minVal, maxVal)) {
return false;
}
}
return true;
}
};

template<typename T, typename = ScalarType<T>>
VectorElementValidator<T> eachElementOf(const vector<T>& val) {
return VectorElementValidator(val);
}

template<typename T, typename = ScalarType<T>>
VectorElementValidator<T> eachElementOf(vector<T>&& val) {
return VectorElementValidator(val);
}

template<typename T, typename = ScalarType<T>>
struct VectorElementsValidator {
private:
const vector<T>& val;

public:
explicit VectorElementsValidator(const vector<T>& _val) : val(_val) {}

bool areAscending() {
for (size_t i = 1; i < val.size(); ++i) {
if (val[i - 1] >= val[i]) {
return false;
}
}
return true;
}

bool areDescending() {
for (size_t i = 1; i < val.size(); ++i) {
if (val[i - 1] <= val[i]) {
return false;
}
}
return true;
}

bool areNonAscending() {
for (size_t i = 1; i < val.size(); ++i) {
if (val[i - 1] < val[i]) {
return false;
}
}
return true;
}

bool areNonDescending() {
for (size_t i = 1; i < val.size(); ++i) {
if (val[i - 1] > val[i]) {
return false;
}
}
return true;
}

bool areUnique() {
vector<T> v = val;
sort(v.begin(), v.end());
size_t ns = unique(v.begin(), v.end()) - v.begin();
return ns == v.size();
}
};

template<typename T, typename = ScalarType<T>>
VectorElementsValidator<T> elementsOf(const vector<T>& val) {
return VectorElementsValidator(val);
}

}
64 changes: 0 additions & 64 deletions include/tcframe/validator/vector.hpp

This file was deleted.

48 changes: 48 additions & 0 deletions test/unit/tcframe/validator/CoreValidatorTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,52 @@ TEST_F(CoreValidatorTests, valueOf_isBetween) {
EXPECT_TRUE(valueOf(5).isBetween(0, 10));
}

TEST_F(CoreValidatorTests, eachElementOf_isBetween) {
EXPECT_FALSE(eachElementOf(vector<int>{2, 3, 1, 5, 4}).isBetween(1, 4));
EXPECT_FALSE(eachElementOf(vector<int>{2, 3, 1, 5, 4}).isBetween(2, 5));
EXPECT_FALSE(eachElementOf(vector<int>{2, 3, 1, 5, 4}).isBetween(2, 4));
EXPECT_TRUE(eachElementOf(vector<int>{2, 3, 1, 5, 4}).isBetween(1, 5));
EXPECT_TRUE(eachElementOf(vector<int>{2, 3, 1, 5, 4}).isBetween(0, 6));
}

TEST_F(CoreValidatorTests, elementsOf_areAscending) {
EXPECT_FALSE(elementsOf(vector<int>{1, 2, 3, 5, 3}).areAscending());
EXPECT_FALSE(elementsOf(vector<int>{2, 1, 1, 2, 5}).areAscending());
EXPECT_FALSE(elementsOf(vector<int>{1, 1, 2, 3, 3, 7}).areAscending());
EXPECT_TRUE(elementsOf(vector<int>()).areAscending());
EXPECT_TRUE(elementsOf(vector<int>{1, 2, 3, 4, 5}).areAscending());
}

TEST_F(CoreValidatorTests, elementsOf_areDescending) {
EXPECT_FALSE(elementsOf(vector<int>{3, 5, 3, 2, 1}).areDescending());
EXPECT_FALSE(elementsOf(vector<int>{5, 2, 1, 1, 2}).areDescending());
EXPECT_FALSE(elementsOf(vector<int>{7, 3, 3, 2, 1, 1}).areDescending());
EXPECT_TRUE(elementsOf(vector<int>()).areDescending());
EXPECT_TRUE(elementsOf(vector<int>{5, 4, 3, 2, 1}).areDescending());
}

TEST_F(CoreValidatorTests, elementsOf_areNonAscending) {
EXPECT_FALSE(elementsOf(vector<int>{3, 5, 3, 2, 1}).areNonAscending());
EXPECT_FALSE(elementsOf(vector<int>{5, 2, 1, 1, 2}).areNonAscending());
EXPECT_TRUE(elementsOf(vector<int>()).areNonAscending());
EXPECT_TRUE(elementsOf(vector<int>{5, 4, 3, 2, 1}).areNonAscending());
EXPECT_TRUE(elementsOf(vector<int>{7, 3, 3, 2, 1, 1}).areNonAscending());
}

TEST_F(CoreValidatorTests, elementsOf_areNonDescending) {
EXPECT_FALSE(elementsOf(vector<int>{1, 2, 3, 5, 3}).areNonDescending());
EXPECT_FALSE(elementsOf(vector<int>{2, 1, 1, 2, 5}).areNonDescending());
EXPECT_TRUE(elementsOf(vector<int>()).areNonDescending());
EXPECT_TRUE(elementsOf(vector<int>{1, 2, 3, 4, 5}).areNonDescending());
EXPECT_TRUE(elementsOf(vector<int>{1, 1, 2, 3, 3, 7}).areNonDescending());
}

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

}
60 changes: 0 additions & 60 deletions test/unit/tcframe/validator/VectorValidatorTests.cpp

This file was deleted.

Loading