From 2161c64365d083f167e19b26429dac187f2df010 Mon Sep 17 00:00:00 2001 From: JoelGunawan <39652408+JoelGunawan@users.noreply.github.com> Date: Sun, 13 Oct 2024 11:25:16 +0700 Subject: [PATCH 1/4] added elementsAreNonDescending() validator function --- include/tcframe/validator/vector.hpp | 10 ++++++++++ 1 file changed, 10 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; +} + } From 0ab2d1cc21e003689f519b0ddae2cedca24a5785 Mon Sep 17 00:00:00 2001 From: JoelGunawan Date: Sat, 12 Oct 2024 23:18:05 -0700 Subject: [PATCH 2/4] added elementsAreNonDescending() validator test --- test/unit/tcframe/validator/VectorValidatorTests.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) 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 c9aa2529a0c7e2250d7f2c325626048404108b91 Mon Sep 17 00:00:00 2001 From: JoelGunawan Date: Wed, 16 Oct 2024 22:04:48 -0700 Subject: [PATCH 3/4] added elementsAreNonAscending(), elementsAreDescending(), and elementsAreAscending() validators --- include/tcframe/validator/vector.hpp | 40 +++++++++++++++++++ .../validator/VectorValidatorTests.cpp | 24 +++++++++++ 2 files changed, 64 insertions(+) diff --git a/include/tcframe/validator/vector.hpp b/include/tcframe/validator/vector.hpp index 240a554..21838bc 100644 --- a/include/tcframe/validator/vector.hpp +++ b/include/tcframe/validator/vector.hpp @@ -24,4 +24,44 @@ bool elementsAreNonDescending(const vector& v) { return true; } +template +bool elementsAreNonAscending(const vector &v) { + for (std::size_t i = 1; i < v.size(); ++i) { + if (v[i - 1] < v[i]) { + return false; + } + } + return true; +} + +template +bool elementsAreDescending(const vector& v) { + for (std::size_t i = 1; i < v.size(); ++i) { + if (v[i - 1] <= v[i]) { + return false; + } + } + return true; +} + +template +bool elementsAreAscending(const vector &v) { + for (std::size_t i = 1; i < v.size(); ++i) { + if (v[i - 1] >= v[i]) { + return false; + } + } + return true; +} + +template +bool elementsAreOrdered(const vector &v, Compare comp) { + for (std::size_t i = 1; i < v.size(); ++i) { + if(!comp(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 612b8fd..28f91f5 100644 --- a/test/unit/tcframe/validator/VectorValidatorTests.cpp +++ b/test/unit/tcframe/validator/VectorValidatorTests.cpp @@ -25,4 +25,28 @@ TEST_F(VectorValidatorTests, elementsAreNonDescending) { EXPECT_TRUE(elementsAreNonDescending(vector{1, 1, 2, 3, 3, 7})); } +TEST_F(VectorValidatorTests, elementsAreNonAscending) { + EXPECT_FALSE(elementsAreNonAscending(vector{3, 5, 3, 2, 1})); + EXPECT_FALSE(elementsAreNonAscending(vector{5, 2, 1, 1, 2})); + EXPECT_TRUE(elementsAreNonAscending(vector())); + EXPECT_TRUE(elementsAreNonAscending(vector{5, 4, 3, 2, 1})); + EXPECT_TRUE(elementsAreNonAscending(vector{7, 3, 3, 2, 1, 1})); +} + +TEST_F(VectorValidatorTests, elementsAreDescending) { + EXPECT_FALSE(elementsAreDescending(vector{3, 5, 3, 2, 1})); + EXPECT_FALSE(elementsAreDescending(vector{5, 2, 1, 1, 2})); + EXPECT_FALSE(elementsAreDescending(vector{7, 3, 3, 2, 1, 1})); + EXPECT_TRUE(elementsAreDescending(vector())); + EXPECT_TRUE(elementsAreDescending(vector{5, 4, 3, 2, 1})); +} + +TEST_F(VectorValidatorTests, elementsAreAscending) { + EXPECT_FALSE(elementsAreAscending(vector{1, 2, 3, 5, 3})); + EXPECT_FALSE(elementsAreAscending(vector{2, 1, 1, 2, 5})); + EXPECT_FALSE(elementsAreAscending(vector{1, 1, 2, 3, 3, 7})); + EXPECT_TRUE(elementsAreAscending(vector())); + EXPECT_TRUE(elementsAreAscending(vector{1, 2, 3, 4, 5})); +} + } From b839ee6f28f87a14edb37f85b84b399ded2d75db Mon Sep 17 00:00:00 2001 From: JoelGunawan Date: Thu, 17 Oct 2024 12:29:35 -0700 Subject: [PATCH 4/4] added elementsAreUnique() validator --- include/tcframe/validator/vector.hpp | 13 +++++-------- .../unit/tcframe/validator/VectorValidatorTests.cpp | 8 ++++++++ 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/include/tcframe/validator/vector.hpp b/include/tcframe/validator/vector.hpp index 21838bc..5819bc2 100644 --- a/include/tcframe/validator/vector.hpp +++ b/include/tcframe/validator/vector.hpp @@ -54,14 +54,11 @@ bool elementsAreAscending(const vector &v) { return true; } -template -bool elementsAreOrdered(const vector &v, Compare comp) { - for (std::size_t i = 1; i < v.size(); ++i) { - if(!comp(v[i - 1], v[i])) { - return false; - } - } - return true; +template +bool elementsAreUnique(vector v) { + sort(v.begin(), v.end()); + std::size_t ns = unique(v.begin(), v.end()) - v.begin(); + return ns == v.size(); } } diff --git a/test/unit/tcframe/validator/VectorValidatorTests.cpp b/test/unit/tcframe/validator/VectorValidatorTests.cpp index 28f91f5..956beb9 100644 --- a/test/unit/tcframe/validator/VectorValidatorTests.cpp +++ b/test/unit/tcframe/validator/VectorValidatorTests.cpp @@ -49,4 +49,12 @@ TEST_F(VectorValidatorTests, elementsAreAscending) { EXPECT_TRUE(elementsAreAscending(vector{1, 2, 3, 4, 5})); } +TEST_F(VectorValidatorTests, elementsAreUnique) { + EXPECT_FALSE(elementsAreUnique(vector{5, 1, 3, 4, 2, 1})); + EXPECT_FALSE(elementsAreUnique(vector{'a', 'c', 'f', 'f', 'd'})); + EXPECT_TRUE(elementsAreUnique(vector())); + EXPECT_TRUE(elementsAreUnique(vector{5, 2, 4, 1, 9})); + EXPECT_TRUE(elementsAreUnique(vector{'a', 'x', 'd', 'g', 'h'})); +} + }