From 971cd546881cbee14e9cd9056044fee7b3af5295 Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Thu, 30 Jul 2015 01:48:29 +0200 Subject: [PATCH] Fix vector comparison and add unit test. --- include/doorkeeper/implem/vector.inl | 20 ++++++----------- test/unit/CMakeLists.txt | 1 + test/unit/vector.cpp | 32 ++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 14 deletions(-) create mode 100644 test/unit/vector.cpp diff --git a/include/doorkeeper/implem/vector.inl b/include/doorkeeper/implem/vector.inl index 1bc1179..7660411 100644 --- a/include/doorkeeper/implem/vector.inl +++ b/include/doorkeeper/implem/vector.inl @@ -199,35 +199,27 @@ namespace cloonel { ///-------------------------------------------------------------------------- template inline bool operator< (const Vector& parA, const Vector& parB) { - bool retVal = false; + bool retVal = true; for (uint32_t z = 0; z < S; ++z) { - retVal |= static_cast(parA[z] < parB[z]); + retVal &= static_cast(parA[z] < parB[z]); } return retVal; } template inline bool operator> (const Vector& parA, const Vector& parB) { - bool retVal = false; - for (uint32_t z = 0; z < S; ++z) { - retVal |= static_cast(parA[z] > parB[z]); - } - return retVal; + return not (parA <= parB); } template inline bool operator<= (const Vector& parA, const Vector& parB) { - bool retVal = false; + bool retVal = true; for (uint32_t z = 0; z < S; ++z) { - retVal |= static_cast(parA[z] <= parB[z]); + retVal &= static_cast(parA[z] <= parB[z]); } return retVal; } template inline bool operator>= (const Vector& parA, const Vector& parB) { - bool retVal = false; - for (uint32_t z = 0; z < S; ++z) { - retVal |= static_cast(parA[z] >= parB[z]); - } - return retVal; + return not (parA < parB); } template inline bool operator== (const Vector& parA, const Vector& parB) { diff --git a/test/unit/CMakeLists.txt b/test/unit/CMakeLists.txt index 27ded9d..f85ee11 100644 --- a/test/unit/CMakeLists.txt +++ b/test/unit/CMakeLists.txt @@ -11,6 +11,7 @@ include_directories(SYSTEM add_executable(${PROJECT_NAME} "${CMAKE_CURRENT_SOURCE_DIR}/../gtest-1.7.0/src/gtest_main.cc" tilecoords.cpp + vector.cpp ) target_link_libraries(${PROJECT_NAME} diff --git a/test/unit/vector.cpp b/test/unit/vector.cpp new file mode 100644 index 0000000..5afe2ff --- /dev/null +++ b/test/unit/vector.cpp @@ -0,0 +1,32 @@ +#include +#include "doorkeeper/primitivetypes.hpp" + + +TEST(misc, vector) { + { + typedef dk::Vector<2> coords2; + + const coords2 small(5, 6); + EXPECT_EQ(small.x(), 5); + EXPECT_EQ(small.y(), 6); + + const coords2 med(1200, 4000); + EXPECT_LT(small, med); + EXPECT_LE(small, med); + EXPECT_GT(med, small); + EXPECT_GE(med, small); + + const coords2 big(10000, 12345); + EXPECT_GT(big, med); + EXPECT_GT(big, small); + EXPECT_LT(small, big); + + const coords2 med2_a(1200, 4001); + EXPECT_LE(med, med2_a); + EXPECT_GE(med2_a, med); + + EXPECT_FALSE(med2_a < med); + EXPECT_FALSE(med2_a == med); + EXPECT_FALSE(med > med2_a); + } +}