From fe03f45f767790885408efbe86a4c2665ab394b8 Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Sat, 25 Jul 2015 20:55:52 +0200 Subject: [PATCH] Add an example.cpp unit test to demonstrate usage. Shows what it looks like when you mix vectors of different type vs using vectorwrapper. --- include/vectorwrapper/vectorwrapper.inl | 20 +++---- test/unit/CMakeLists.txt | 1 + test/unit/example.cpp | 77 +++++++++++++++++++++++++ test/unit/test_conversions.cpp | 22 +++---- 4 files changed, 100 insertions(+), 20 deletions(-) create mode 100644 test/unit/example.cpp diff --git a/include/vectorwrapper/vectorwrapper.inl b/include/vectorwrapper/vectorwrapper.inl index 861a92e..81b6363 100644 --- a/include/vectorwrapper/vectorwrapper.inl +++ b/include/vectorwrapper/vectorwrapper.inl @@ -98,7 +98,7 @@ namespace vwr { template template VecBase& VecBase::operator+= (const VecBase& parOther) { - static_assert(VectorWrapperInfo::dimensions == VectorWrapperInfo::dimensions, "Dimensions mismatch"); + static_assert(static_cast(VectorWrapperInfo::dimensions) == static_cast(VectorWrapperInfo::dimensions), "Dimensions mismatch"); for (int z = 0; z < VectorWrapperInfo::dimensions; ++z) { (*this)[z] += parOther[z]; } @@ -107,7 +107,7 @@ namespace vwr { template template VecBase& VecBase::operator-= (const VecBase& parOther) { - static_assert(VectorWrapperInfo::dimensions == VectorWrapperInfo::dimensions, "Dimensions mismatch"); + static_assert(static_cast(VectorWrapperInfo::dimensions) == static_cast(VectorWrapperInfo::dimensions), "Dimensions mismatch"); for (int z = 0; z < VectorWrapperInfo::dimensions; ++z) { (*this)[z] -= parOther[z]; } @@ -116,7 +116,7 @@ namespace vwr { template template VecBase& VecBase::operator*= (const VecBase& parOther) { - static_assert(VectorWrapperInfo::dimensions == VectorWrapperInfo::dimensions, "Dimensions mismatch"); + static_assert(static_cast(VectorWrapperInfo::dimensions) == static_cast(VectorWrapperInfo::dimensions), "Dimensions mismatch"); for (int z = 0; z < VectorWrapperInfo::dimensions; ++z) { (*this)[z] *= parOther[z]; } @@ -125,7 +125,7 @@ namespace vwr { template template VecBase& VecBase::operator/= (const VecBase& parOther) { - static_assert(VectorWrapperInfo::dimensions == VectorWrapperInfo::dimensions, "Dimensions mismatch"); + static_assert(static_cast(VectorWrapperInfo::dimensions) == static_cast(VectorWrapperInfo::dimensions), "Dimensions mismatch"); for (int z = 0; z < VectorWrapperInfo::dimensions; ++z) { (*this)[z] /= parOther[z]; } @@ -300,7 +300,7 @@ namespace vwr { template inline bool operator== (const Vec& parLeft, const Vec& parRight) { - static_assert(VectorWrapperInfo::dimensions == VectorWrapperInfo::dimensions, "Dimensions mismatch"); + static_assert(static_cast(VectorWrapperInfo::dimensions) == static_cast(VectorWrapperInfo::dimensions), "Dimensions mismatch"); bool retval = true; for (int z = 0; z < VectorWrapperInfo::dimensions; ++z) { retval &= (parLeft[z] == parRight[z]); @@ -309,7 +309,7 @@ namespace vwr { } template inline bool operator< (const Vec& parLeft, const Vec& parRight) { - static_assert(VectorWrapperInfo::dimensions == VectorWrapperInfo::dimensions, "Dimensions mismatch"); + static_assert(static_cast(VectorWrapperInfo::dimensions) == static_cast(VectorWrapperInfo::dimensions), "Dimensions mismatch"); bool retval = true; for (int z = 0; z < VectorWrapperInfo::dimensions; ++z) { retval &= (parLeft[z] < parRight[z]); @@ -353,7 +353,7 @@ namespace vwr { template inline Vec::type> operator+ (const Vec& parLeft, const Vec& parRight) { - static_assert(VectorWrapperInfo::dimensions == VectorWrapperInfo::dimensions, "Dimensions mismatch"); + static_assert(static_cast(VectorWrapperInfo::dimensions) == static_cast(VectorWrapperInfo::dimensions), "Dimensions mismatch"); Vec::type> retval; for (int z = 0; z < VectorWrapperInfo::dimensions; ++z) { retval[z] = parLeft[z] + parRight[z]; @@ -362,7 +362,7 @@ namespace vwr { } template inline Vec::type> operator- (const Vec& parLeft, const Vec& parRight) { - static_assert(VectorWrapperInfo::dimensions == VectorWrapperInfo::dimensions, "Dimensions mismatch"); + static_assert(static_cast(VectorWrapperInfo::dimensions) == static_cast(VectorWrapperInfo::dimensions), "Dimensions mismatch"); Vec::type> retval; for (int z = 0; z < VectorWrapperInfo::dimensions; ++z) { retval[z] = parLeft[z] - parRight[z]; @@ -371,7 +371,7 @@ namespace vwr { } template inline Vec::type> operator* (const Vec& parLeft, const Vec& parRight) { - static_assert(VectorWrapperInfo::dimensions == VectorWrapperInfo::dimensions, "Dimensions mismatch"); + static_assert(static_cast(VectorWrapperInfo::dimensions) == static_cast(VectorWrapperInfo::dimensions), "Dimensions mismatch"); Vec::type> retval; for (int z = 0; z < VectorWrapperInfo::dimensions; ++z) { retval[z] = parLeft[z] * parRight[z]; @@ -380,7 +380,7 @@ namespace vwr { } template inline Vec::type> operator/ (const Vec& parLeft, const Vec& parRight) { - static_assert(VectorWrapperInfo::dimensions == VectorWrapperInfo::dimensions, "Dimensions mismatch"); + static_assert(static_cast(VectorWrapperInfo::dimensions) == static_cast(VectorWrapperInfo::dimensions), "Dimensions mismatch"); Vec::type> retval; for (int z = 0; z < VectorWrapperInfo::dimensions; ++z) { retval[z] = parLeft[z] / parRight[z]; diff --git a/test/unit/CMakeLists.txt b/test/unit/CMakeLists.txt index 19f39b9..bfcf2a7 100644 --- a/test/unit/CMakeLists.txt +++ b/test/unit/CMakeLists.txt @@ -4,6 +4,7 @@ add_executable(${PROJECT_NAME} ${GTEST_MAIN_CPP} test_conversions.cpp test_ops.cpp + example.cpp ) target_link_libraries(${PROJECT_NAME} diff --git a/test/unit/example.cpp b/test/unit/example.cpp new file mode 100644 index 0000000..faa7d13 --- /dev/null +++ b/test/unit/example.cpp @@ -0,0 +1,77 @@ +#include "sample_vectors.hpp" +#include + +namespace { + void simplevec_double (vwr::SimpleVector3& parVec, float parX, float parY, float parZ) { + EXPECT_EQ(parVec.x, parX); + EXPECT_EQ(parVec.y, parY); + EXPECT_EQ(parVec.z, parZ); + + parVec.x *= 2.0f; + parVec.y *= 2.0f; + parVec.z *= 2.0f; + } +} //unnamed namespace + +TEST(vwr, example) { + using namespace vwr; + + //Using the bare vectors + { + //Imagine you get this vector from a function's return value. + auto vec = TrailingDataVector3{2.0f, 5.5f, 3.8f, 1, 2}; + + //Now you want to pass it on to a function from a different library. + //To be safe, create a temporary of the correct type. + //Was it lower case x, upper case, with or without paretheses? + auto vec_b = SimpleVector3{vec.X, vec.Y, vec.Z}; + simplevec_double(vec_b, vec_b.x, vec_b.y, vec_b.z); + + //The above call changed the input vector, so re-assign it (vec_b is + //just a converted copy, remember?) + vec.X = vec_b.x; + vec.Y = vec_b.y; + vec.Z = vec_b.z; + + //Can't compare with vec == vec_b, so unroll it manually + EXPECT_EQ(vec.X, vec_b.x); + EXPECT_EQ(vec.Y, vec_b.y); + EXPECT_EQ(vec.Z, vec_b.z); + } + + //Using vectorwrapper + { + auto vec_in = TrailingDataVector3{2.0f, 5.5f, 3.8f, 1, 2}; + + //In reality, I just wrote TrailingDataVector3's definition myself, but + //I'm already spending time to fix the build as I call vec.x instead of + //vec.X. Do you think it's a remote case? Think twice. + EXPECT_EQ(vec_in.X, 2.0f); + EXPECT_EQ(vec_in.Y, 5.5f); + EXPECT_EQ(vec_in.Z, 3.8f); + EXPECT_EQ(vec_in.a, 1); + EXPECT_EQ(vec_in.b, 2); + + tvec3 vec(vec_in); + EXPECT_EQ(vec.x(), 2.0f); + EXPECT_EQ(vec.y(), 5.5f); + EXPECT_EQ(vec.z(), 3.8f); + //If you initialize vec from an object of the wrapped type, extra data + //also gets initialized correctly. But I think in most situations you + //won't need this. + EXPECT_EQ(vec.data().a, 1); + EXPECT_EQ(vec.data().b, 2); + + //Suppose TrailingDataVector3 is a third-party struct and I'm not + //guaranteed it will never change. Sure I can reinterpret_cast it, as + //much as I could reinterpret_cast a short int to a TrailingDataVector3. + //The following line won't compile if casting is not deemed appropriate. + simplevec_double(vec.cast().data(), vec.x(), vec.y(), vec.z()); + + //Well that's it. + + //If what you wanted is indeed a copy of type SimpleVector3, here we go: + svec3 vec_b(vec); + EXPECT_EQ(vec_b, vec); + } +} diff --git a/test/unit/test_conversions.cpp b/test/unit/test_conversions.cpp index 374bbe0..5bc9df0 100644 --- a/test/unit/test_conversions.cpp +++ b/test/unit/test_conversions.cpp @@ -1,17 +1,19 @@ #include "sample_vectors.hpp" #include -void test_svec3 (const vwr::svec3& parVec, float parX, float parY, float parZ) { - EXPECT_EQ(parVec.x(), parX); - EXPECT_EQ(parVec.y(), parY); - EXPECT_EQ(parVec.z(), parZ); -} +namespace { + void test_svec3 (const vwr::svec3& parVec, float parX, float parY, float parZ) { + EXPECT_EQ(parVec.x(), parX); + EXPECT_EQ(parVec.y(), parY); + EXPECT_EQ(parVec.z(), parZ); + } -void test_tvec3 (const vwr::tvec3& parVec, float parX, float parY, float parZ) { - EXPECT_EQ(parVec.x(), parX); - EXPECT_EQ(parVec.y(), parY); - EXPECT_EQ(parVec.z(), parZ); -} + void test_tvec3 (const vwr::tvec3& parVec, float parX, float parY, float parZ) { + EXPECT_EQ(parVec.x(), parX); + EXPECT_EQ(parVec.y(), parY); + EXPECT_EQ(parVec.z(), parZ); + } +} //unnamed namespace TEST(vwr, conversion) { using namespace vwr;