Skip to content

Commit

Permalink
Validator: refactor number validators to core
Browse files Browse the repository at this point in the history
  • Loading branch information
fushar committed Oct 27, 2024
1 parent e4bbaa0 commit f1fb984
Show file tree
Hide file tree
Showing 15 changed files with 73 additions and 49 deletions.
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"
32 changes: 32 additions & 0 deletions include/tcframe/validator/core.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@

#include <string>
#include <type_traits>
#include <vector>

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

namespace tcframe {

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

template<typename T, typename = ValueType<T>>
struct ValueValidator {
private:
T val;

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

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

template<typename T, typename = ValueType<T>>
ValueValidator<T> valueOf(T val) {
return ValueValidator(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.

0 comments on commit f1fb984

Please sign in to comment.