diff --git a/include/tcframe/validator/vector.hpp b/include/tcframe/validator/vector.hpp index 240a554..5819bc2 100644 --- a/include/tcframe/validator/vector.hpp +++ b/include/tcframe/validator/vector.hpp @@ -24,4 +24,41 @@ 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 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 612b8fd..956beb9 100644 --- a/test/unit/tcframe/validator/VectorValidatorTests.cpp +++ b/test/unit/tcframe/validator/VectorValidatorTests.cpp @@ -25,4 +25,36 @@ 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})); +} + +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'})); +} + }