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})); +} + }