From 0a0c7bf60560b59a3ce9bcdfee3eeda8daf5135e Mon Sep 17 00:00:00 2001 From: JoelGunawan <39652408+JoelGunawan@users.noreply.github.com> Date: Mon, 14 Oct 2024 20:56:44 +0700 Subject: [PATCH 1/2] Add elementsAreNonDecreasing() validator (#193) --- include/tcframe/validator/vector.hpp | 10 ++++++++++ test/unit/tcframe/validator/VectorValidatorTests.cpp | 8 ++++++++ 2 files changed, 18 insertions(+) diff --git a/include/tcframe/validator/vector.hpp b/include/tcframe/validator/vector.hpp index 25d5671..240a554 100644 --- a/include/tcframe/validator/vector.hpp +++ b/include/tcframe/validator/vector.hpp @@ -14,4 +14,14 @@ bool eachElementIsBetween(const vector& v, T minVal, T maxVal) { return true; } +template +bool elementsAreNonDescending(const vector& v) { + for (std::size_t i = 1; i < v.size(); ++i) { + if (v[i - 1] > v[i]) { + return false; + } + } + return true; +} + } diff --git a/test/unit/tcframe/validator/VectorValidatorTests.cpp b/test/unit/tcframe/validator/VectorValidatorTests.cpp index fbe8da5..612b8fd 100644 --- a/test/unit/tcframe/validator/VectorValidatorTests.cpp +++ b/test/unit/tcframe/validator/VectorValidatorTests.cpp @@ -17,4 +17,12 @@ TEST_F(VectorValidatorTests, eachElementIsBetween) { EXPECT_TRUE(eachElementIsBetween(vector{2, 3, 1, 5, 4}, 0, 6)); } +TEST_F(VectorValidatorTests, elementsAreNonDescending) { + EXPECT_FALSE(elementsAreNonDescending(vector{1, 2, 3, 5, 3})); + EXPECT_FALSE(elementsAreNonDescending(vector{2, 1, 1, 2, 5})); + EXPECT_TRUE(elementsAreNonDescending(vector())); + EXPECT_TRUE(elementsAreNonDescending(vector{1, 2, 3, 4, 5})); + EXPECT_TRUE(elementsAreNonDescending(vector{1, 1, 2, 3, 3, 7})); +} + } 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 2/2] 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)); +} + +}