From 9b6b82c25b6b7d889372a3909c415b64121b2047 Mon Sep 17 00:00:00 2001 From: JoelGunawan <39652408+JoelGunawan@users.noreply.github.com> Date: Thu, 17 Oct 2024 20:40:35 +0700 Subject: [PATCH] Add isBetween() validator (#197) --- CMakeLists.txt | 2 ++ include/tcframe/validator/number.hpp | 8 +++++++ .../validator/NumberValidatorTests.cpp | 21 +++++++++++++++++++ 3 files changed, 31 insertions(+) create mode 100644 include/tcframe/validator/number.hpp create mode 100644 test/unit/tcframe/validator/NumberValidatorTests.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 4beef06..66040d0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -164,6 +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/vector.hpp ) @@ -259,6 +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/VectorValidatorTests.cpp ) diff --git a/include/tcframe/validator/number.hpp b/include/tcframe/validator/number.hpp new file mode 100644 index 0000000..f26f7fa --- /dev/null +++ b/include/tcframe/validator/number.hpp @@ -0,0 +1,8 @@ +namespace tcframe { + +template +bool isBetween(T N, T mn, T mx) { + return N >= mn && N <= mx; +} + +} diff --git a/test/unit/tcframe/validator/NumberValidatorTests.cpp b/test/unit/tcframe/validator/NumberValidatorTests.cpp new file mode 100644 index 0000000..9d9bc91 --- /dev/null +++ b/test/unit/tcframe/validator/NumberValidatorTests.cpp @@ -0,0 +1,21 @@ +#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)); +} + +}