diff --git a/CMakeLists.txt b/CMakeLists.txt index 66040d0e..3f2a71f7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 ) @@ -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 ) diff --git a/include/tcframe/spec.hpp b/include/tcframe/spec.hpp index 63be5740..27dc9b4e 100644 --- a/include/tcframe/spec.hpp +++ b/include/tcframe/spec.hpp @@ -7,4 +7,4 @@ #include "tcframe/spec/testcase.hpp" #include "tcframe/spec/variable.hpp" #include "tcframe/spec/verifier.hpp" - +#include "tcframe/validator/core.hpp" diff --git a/include/tcframe/validator/core.hpp b/include/tcframe/validator/core.hpp new file mode 100644 index 00000000..d6d53526 --- /dev/null +++ b/include/tcframe/validator/core.hpp @@ -0,0 +1,32 @@ + +#include +#include +#include + +using std::enable_if_t; +using std::is_arithmetic_v; + +namespace tcframe { + +template +using ValueType = enable_if_t>; + +template> +struct ValueValidator { +private: + T val; + +public: + explicit ValueValidator(T _val) : val(_val) {} + + bool isBetween(T minVal, T maxVal) { + return minVal <= val && val <= maxVal; + } +}; + +template> +ValueValidator valueOf(T val) { + return ValueValidator(val); +} + +} diff --git a/include/tcframe/validator/number.hpp b/include/tcframe/validator/number.hpp deleted file mode 100644 index f26f7fa4..00000000 --- a/include/tcframe/validator/number.hpp +++ /dev/null @@ -1,8 +0,0 @@ -namespace tcframe { - -template -bool isBetween(T N, T mn, T mx) { - return N >= mn && N <= mx; -} - -} diff --git a/test/ete/resources/interactive/spec.cpp b/test/ete/resources/interactive/spec.cpp index b77061b5..24fc870c 100644 --- a/test/ete/resources/interactive/spec.cpp +++ b/test/ete/resources/interactive/spec.cpp @@ -20,7 +20,7 @@ class ProblemSpec : public BaseProblemSpec { } void Constraints() { - CONS(1 <= N && N <= 10); + CONS(valueOf(N).isBetween(1, 10)); } }; diff --git a/test/ete/resources/multi-no-output/spec.cpp b/test/ete/resources/multi-no-output/spec.cpp index 4ba5cf99..90f77a25 100644 --- a/test/ete/resources/multi-no-output/spec.cpp +++ b/test/ete/resources/multi-no-output/spec.cpp @@ -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)); } }; diff --git a/test/ete/resources/multi-prefix/spec.cpp b/test/ete/resources/multi-prefix/spec.cpp index 00a4ba76..3f51fa9d 100644 --- a/test/ete/resources/multi-prefix/spec.cpp +++ b/test/ete/resources/multi-prefix/spec.cpp @@ -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)); } }; diff --git a/test/ete/resources/multi/spec.cpp b/test/ete/resources/multi/spec.cpp index bf4af81a..af3b567d 100644 --- a/test/ete/resources/multi/spec.cpp +++ b/test/ete/resources/multi/spec.cpp @@ -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)); } }; diff --git a/test/ete/resources/normal-complex-formats/spec.cpp b/test/ete/resources/normal-complex-formats/spec.cpp index f98c7675..f913a1d4 100644 --- a/test/ete/resources/normal-complex-formats/spec.cpp +++ b/test/ete/resources/normal-complex-formats/spec.cpp @@ -39,7 +39,7 @@ class ProblemSpec : public BaseProblemSpec { } void Constraints() { - CONS(1 <= N && N <= 10); + CONS(valueOf(N).isBetween(1, 10)); } }; diff --git a/test/ete/resources/normal-custom-scorer/spec.cpp b/test/ete/resources/normal-custom-scorer/spec.cpp index 538ce35b..5a61f502 100644 --- a/test/ete/resources/normal-custom-scorer/spec.cpp +++ b/test/ete/resources/normal-custom-scorer/spec.cpp @@ -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)); } }; diff --git a/test/ete/resources/normal-lifecycle/spec.cpp b/test/ete/resources/normal-lifecycle/spec.cpp index c5526592..55a3e48c 100644 --- a/test/ete/resources/normal-lifecycle/spec.cpp +++ b/test/ete/resources/normal-lifecycle/spec.cpp @@ -31,7 +31,7 @@ class ProblemSpec : public BaseProblemSpec { } void Constraints() { - CONS(1 <= N && N <= 10); + CONS(valueOf(N).isBetween(1, 10)); } }; diff --git a/test/ete/resources/normal-no-output/spec.cpp b/test/ete/resources/normal-no-output/spec.cpp index aed13700..90601a3f 100644 --- a/test/ete/resources/normal-no-output/spec.cpp +++ b/test/ete/resources/normal-no-output/spec.cpp @@ -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)); } }; diff --git a/test/ete/resources/normal/spec.cpp b/test/ete/resources/normal/spec.cpp index 5c832295..84190499 100644 --- a/test/ete/resources/normal/spec.cpp +++ b/test/ete/resources/normal/spec.cpp @@ -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)); } }; diff --git a/test/unit/tcframe/validator/CoreValidatorTests.cpp b/test/unit/tcframe/validator/CoreValidatorTests.cpp new file mode 100644 index 00000000..5d38541f --- /dev/null +++ b/test/unit/tcframe/validator/CoreValidatorTests.cpp @@ -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)); +} + +} diff --git a/test/unit/tcframe/validator/NumberValidatorTests.cpp b/test/unit/tcframe/validator/NumberValidatorTests.cpp deleted file mode 100644 index 9d9bc914..00000000 --- a/test/unit/tcframe/validator/NumberValidatorTests.cpp +++ /dev/null @@ -1,21 +0,0 @@ -#include "gmock/gmock.h" - -#include "tcframe/validator/number.hpp" - -using ::testing::Eq; -using ::testing::Test; - -namespace tcframe { - -class NumberValidatorTests : public Test {}; - -TEST_F(NumberValidatorTests, isBetween) { - EXPECT_FALSE(isBetween(5, 1, 4)); - EXPECT_FALSE(isBetween(5, 6, 10)); - EXPECT_FALSE(isBetween(5, 100, -100)); - EXPECT_TRUE(isBetween(5, 1, 5)); - EXPECT_TRUE(isBetween(5, 5, 10)); - EXPECT_TRUE(isBetween(5, 0, 10)); -} - -}