Skip to content

Commit

Permalink
Add eachElementIsBetween() validator (#192)
Browse files Browse the repository at this point in the history
  • Loading branch information
fushar authored Oct 13, 2024
1 parent 23b2fe6 commit ec7c296
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ set(INCLUDE
include/tcframe/util.hpp
include/tcframe/util/StringUtils.hpp
include/tcframe/util/optional.hpp
include/tcframe/validator/vector.hpp
)

set(TEST_UNIT
Expand Down Expand Up @@ -258,6 +259,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/VectorValidatorTests.cpp
)

add_executable(test_unit ${INCLUDE} ${TEST_UNIT})
Expand Down
17 changes: 17 additions & 0 deletions include/tcframe/validator/vector.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <vector>

using std::vector;

namespace tcframe {

template<typename T>
bool eachElementIsBetween(const vector<T>& v, T minVal, T maxVal) {
for (T x : v) {
if (x < minVal || x > maxVal) {
return false;
}
}
return true;
}

}
20 changes: 20 additions & 0 deletions test/unit/tcframe/validator/VectorValidatorTests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include "gmock/gmock.h"

#include "tcframe/validator/vector.hpp"

using ::testing::Eq;
using ::testing::Test;

namespace tcframe {

class VectorValidatorTests : public Test {};

TEST_F(VectorValidatorTests, eachElementIsBetween) {
EXPECT_FALSE(eachElementIsBetween(vector<int>{2, 3, 1, 5, 4}, 1, 4));
EXPECT_FALSE(eachElementIsBetween(vector<int>{2, 3, 1, 5, 4}, 2, 5));
EXPECT_FALSE(eachElementIsBetween(vector<int>{2, 3, 1, 5, 4}, 2, 4));
EXPECT_TRUE(eachElementIsBetween(vector<int>{2, 3, 1, 5, 4}, 1, 5));
EXPECT_TRUE(eachElementIsBetween(vector<int>{2, 3, 1, 5, 4}, 0, 6));
}

}

0 comments on commit ec7c296

Please sign in to comment.