From 16cab14ec3bbbdcc274038b3cc17d48a3fa88233 Mon Sep 17 00:00:00 2001 From: Ashar Fuadi Date: Mon, 28 Oct 2024 09:19:09 +0700 Subject: [PATCH] Validator: refactor number validators to core (#199) --- CMakeLists.txt | 4 +-- include/tcframe/spec.hpp | 2 +- include/tcframe/validator/core.hpp | 30 +++++++++++++++++++ include/tcframe/validator/number.hpp | 8 ----- test/ete/resources/interactive/spec.cpp | 2 +- test/ete/resources/multi-no-output/spec.cpp | 6 ++-- test/ete/resources/multi-prefix/spec.cpp | 6 ++-- test/ete/resources/multi/spec.cpp | 4 +-- .../resources/normal-complex-formats/spec.cpp | 2 +- .../resources/normal-custom-scorer/spec.cpp | 4 +-- test/ete/resources/normal-lifecycle/spec.cpp | 2 +- test/ete/resources/normal-no-output/spec.cpp | 4 +-- test/ete/resources/normal/spec.cpp | 4 +-- .../tcframe/validator/CoreValidatorTests.cpp | 21 +++++++++++++ .../validator/NumberValidatorTests.cpp | 21 ------------- 15 files changed, 71 insertions(+), 49 deletions(-) create mode 100644 include/tcframe/validator/core.hpp delete mode 100644 include/tcframe/validator/number.hpp create mode 100644 test/unit/tcframe/validator/CoreValidatorTests.cpp delete mode 100644 test/unit/tcframe/validator/NumberValidatorTests.cpp 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..51eaf08c --- /dev/null +++ b/include/tcframe/validator/core.hpp @@ -0,0 +1,30 @@ + +#include + +using std::enable_if_t; +using std::is_arithmetic_v; + +namespace tcframe { + +template +using ScalarType = enable_if_t>; + +template> +struct ScalarValidator { +private: + T val; + +public: + explicit ScalarValidator(T _val) : val(_val) {} + + bool isBetween(T minVal, T maxVal) { + return minVal <= val && val <= maxVal; + } +}; + +template> +ScalarValidator valueOf(T val) { + return ScalarValidator(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)); -} - -}