From b839ee6f28f87a14edb37f85b84b399ded2d75db Mon Sep 17 00:00:00 2001 From: JoelGunawan Date: Thu, 17 Oct 2024 12:29:35 -0700 Subject: [PATCH] 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'})); +} + }