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 number validators to core #199

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

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

Expand Down
2 changes: 1 addition & 1 deletion include/tcframe/spec.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
#include "tcframe/spec/testcase.hpp"
#include "tcframe/spec/variable.hpp"
#include "tcframe/spec/verifier.hpp"

#include "tcframe/validator/core.hpp"
30 changes: 30 additions & 0 deletions include/tcframe/validator/core.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

#include <type_traits>

using std::enable_if_t;
using std::is_arithmetic_v;

namespace tcframe {

template<typename T>
using ScalarType = enable_if_t<is_arithmetic_v<T>>;

template<typename T, typename = ScalarType<T>>
struct ScalarValidator {
private:
T val;

public:
explicit ScalarValidator(T _val) : val(_val) {}

bool isBetween(T minVal, T maxVal) {
return minVal <= val && val <= maxVal;
}
};

template<typename T, typename = ScalarType<T>>
ScalarValidator<T> valueOf(T val) {
return ScalarValidator(val);
}

}
8 changes: 0 additions & 8 deletions include/tcframe/validator/number.hpp

This file was deleted.

2 changes: 1 addition & 1 deletion test/ete/resources/interactive/spec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class ProblemSpec : public BaseProblemSpec {
}

void Constraints() {
CONS(1 <= N && N <= 10);
CONS(valueOf(N).isBetween(1, 10));
}
};

Expand Down
6 changes: 3 additions & 3 deletions test/ete/resources/multi-no-output/spec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ class ProblemSpec : public BaseProblemSpec {
}

void MultipleTestCasesConstraints() {
CONS(1 <= T && T <= 5);
CONS(valueOf(T).isBetween(1, 5));
}

void Constraints() {
CONS(1 <= A && A <= 100);
CONS(1 <= B && B <= 100);
CONS(valueOf(A).isBetween(1, 100));
CONS(valueOf(B).isBetween(1, 100));
}
};

Expand Down
6 changes: 3 additions & 3 deletions test/ete/resources/multi-prefix/spec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ class ProblemSpec : public BaseProblemSpec {
}

void MultipleTestCasesConstraints() {
CONS(1 <= T && T <= 5);
CONS(valueOf(T).isBetween(1, 5));
}

void Constraints() {
CONS(1 <= A && A <= 100);
CONS(1 <= B && B <= 100);
CONS(valueOf(A).isBetween(1, 100));
CONS(valueOf(B).isBetween(1, 100));
}
};

Expand Down
4 changes: 2 additions & 2 deletions test/ete/resources/multi/spec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ class ProblemSpec : public BaseProblemSpec {
}

void Constraints() {
CONS(1 <= A && A <= 100);
CONS(1 <= B && B <= 100);
CONS(valueOf(A).isBetween(1, 100));
CONS(valueOf(B).isBetween(1, 100));
}
};

Expand Down
2 changes: 1 addition & 1 deletion test/ete/resources/normal-complex-formats/spec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class ProblemSpec : public BaseProblemSpec {
}

void Constraints() {
CONS(1 <= N && N <= 10);
CONS(valueOf(N).isBetween(1, 10));
}
};

Expand Down
4 changes: 2 additions & 2 deletions test/ete/resources/normal-custom-scorer/spec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ class ProblemSpec : public BaseProblemSpec {
}

void Constraints() {
CONS(1 <= A && A <= 10);
CONS(1 <= B && B <= 10);
CONS(valueOf(A).isBetween(1, 10));
CONS(valueOf(B).isBetween(1, 10));
}
};

Expand Down
2 changes: 1 addition & 1 deletion test/ete/resources/normal-lifecycle/spec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class ProblemSpec : public BaseProblemSpec {
}

void Constraints() {
CONS(1 <= N && N <= 10);
CONS(valueOf(N).isBetween(1, 10));
}
};

Expand Down
4 changes: 2 additions & 2 deletions test/ete/resources/normal-no-output/spec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ class ProblemSpec : public BaseProblemSpec {
}

void Constraints() {
CONS(1 <= A && A <= 10);
CONS(1 <= B && B <= 10);
CONS(valueOf(A).isBetween(1, 10));
CONS(valueOf(B).isBetween(1, 10));
}
};

Expand Down
4 changes: 2 additions & 2 deletions test/ete/resources/normal/spec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ class ProblemSpec : public BaseProblemSpec {
}

void Constraints() {
CONS(1 <= A && A <= 10);
CONS(1 <= B && B <= 10);
CONS(valueOf(A).isBetween(1, 10));
CONS(valueOf(B).isBetween(1, 10));
}
};

Expand Down
21 changes: 21 additions & 0 deletions test/unit/tcframe/validator/CoreValidatorTests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include "gmock/gmock.h"

#include "tcframe/validator/core.hpp"

using ::testing::Eq;
using ::testing::Test;

namespace tcframe {

class CoreValidatorTests : public Test {};

TEST_F(CoreValidatorTests, valueOf_isBetween) {
EXPECT_FALSE(valueOf(5).isBetween(1, 4));
EXPECT_FALSE(valueOf(5).isBetween(6, 10));
EXPECT_FALSE(valueOf(5).isBetween(100, -100));
EXPECT_TRUE(valueOf(5).isBetween(1, 5));
EXPECT_TRUE(valueOf(5).isBetween(5, 10));
EXPECT_TRUE(valueOf(5).isBetween(0, 10));
}

}
21 changes: 0 additions & 21 deletions test/unit/tcframe/validator/NumberValidatorTests.cpp

This file was deleted.

Loading