From fcb7ce575e2cdcab822365e9b593fb69956834ca Mon Sep 17 00:00:00 2001 From: Christian Zimmermann Date: Mon, 16 Jul 2018 14:44:55 +0200 Subject: [PATCH] first anonymous test works --- src/include/ranges/anonymous_range.h | 16 +++- src/include/ranges/rpack_num.h | 4 +- src/lib/ranges/anonymous_range.cc | 3 + src/tests/CMakeLists.txt | 5 + src/tests/ranges/anonymous_unit_test.cc | 122 ++++++++++++++++++++++++ 5 files changed, 144 insertions(+), 6 deletions(-) create mode 100644 src/tests/ranges/anonymous_unit_test.cc diff --git a/src/include/ranges/anonymous_range.h b/src/include/ranges/anonymous_range.h index 4801a00..de64fe1 100644 --- a/src/include/ranges/anonymous_range.h +++ b/src/include/ranges/anonymous_range.h @@ -38,11 +38,14 @@ namespace MultiArrayTools std::shared_ptr create(); private: + std::shared_ptr checkIfCreated(const std::vector >& pvec); static std::map,std::vector > mAleadyCreated; + + bool mProductCreated = false; }; - + class AnonymousRange : public RangeInterface { public: @@ -77,7 +80,7 @@ namespace MultiArrayTools protected: AnonymousRange() = delete; - AnonymousRange(const AnonymousRange& in) = delete; + AnonymousRange(const AnonymousRange& in) = default; template AnonymousRange(const std::tuple...>& origs); @@ -85,7 +88,7 @@ namespace MultiArrayTools template AnonymousRange(std::shared_ptr... origs); - size_t mSize; + size_t mSize = 0; std::vector > mOrig; }; @@ -118,7 +121,12 @@ namespace MultiArrayTools template void AnonymousRangeFactory::append(std::shared_ptr r) { + if(mProductCreated){ + mProd = std::shared_ptr( new AnonymousRange( *std::dynamic_pointer_cast(mProd) ) ); + mProductCreated = false; + } std::dynamic_pointer_cast(mProd)->mOrig.push_back(r); + std::dynamic_pointer_cast(mProd)->mSize *= r->size(); } /***************** @@ -175,7 +183,7 @@ namespace MultiArrayTools RangeInterface() { auto rst = std::make_tuple(origs...); - mOrig.resize(sizeof...(RangeTypes)); + //mOrig.resize(sizeof...(RangeTypes)); RPackNum::RangesToVec( rst, mOrig ); mSize = RPackNum::getSize( rst ); } diff --git a/src/include/ranges/rpack_num.h b/src/include/ranges/rpack_num.h index c5d6492..c876c36 100644 --- a/src/include/ranges/rpack_num.h +++ b/src/include/ranges/rpack_num.h @@ -224,7 +224,7 @@ namespace MultiArrayHelper template static inline void RangesToVec(const std::tuple...>& rst, - std::vector >& v) + std::vector >& v) { setRangeToVec(v, std::get(rst)); //v[N] = std::get(rst); @@ -420,7 +420,7 @@ namespace MultiArrayHelper static inline void RangesToVec(const std::tuple...>& rst, std::vector >& v) { - v[0] = std::get<0>(rst); + setRangeToVec(v, std::get<0>(rst)); } template diff --git a/src/lib/ranges/anonymous_range.cc b/src/lib/ranges/anonymous_range.cc index 5746191..d48167a 100644 --- a/src/lib/ranges/anonymous_range.cc +++ b/src/lib/ranges/anonymous_range.cc @@ -7,6 +7,8 @@ namespace MultiArrayTools * AnonymousRangeFactory * ******************************/ + std::map,std::vector > AnonymousRangeFactory::mAleadyCreated; + std::shared_ptr AnonymousRangeFactory::checkIfCreated(const std::vector >& pvec) { std::shared_ptr out; @@ -42,6 +44,7 @@ namespace MultiArrayTools { mProd = checkIfCreated(std::dynamic_pointer_cast(mProd)->mOrig); setSelf(); + mProductCreated = true; return mProd; } diff --git a/src/tests/CMakeLists.txt b/src/tests/CMakeLists.txt index 2ebab72..3a338da 100644 --- a/src/tests/CMakeLists.txt +++ b/src/tests/CMakeLists.txt @@ -5,6 +5,11 @@ add_dependencies(iutest multiarray) target_link_libraries(iutest ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} multiarray) add_test(NAME iutest COMMAND iutest) +add_executable(autest ranges/anonymous_unit_test.cc) +add_dependencies(autest multiarray) +target_link_libraries(autest ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} multiarray) +add_test(NAME autest COMMAND autest) + #add_executable(mautest src/tests/ma_unit_test.cc ${MA_CC_FILES}) add_executable(mautest ma_unit_test.cc) add_dependencies(mautest multiarray) diff --git a/src/tests/ranges/anonymous_unit_test.cc b/src/tests/ranges/anonymous_unit_test.cc new file mode 100644 index 0000000..1ec2d34 --- /dev/null +++ b/src/tests/ranges/anonymous_unit_test.cc @@ -0,0 +1,122 @@ +// -*- C++ -*- + +#include +#include "gtest/gtest.h" +#include + +#include "ranges/rheader.h" + +//#include "multi_array_header.h" + +namespace MAT = MultiArrayTools; + +namespace { + + using namespace MAT; + + template + void swapFactory(std::shared_ptr& fptr, std::initializer_list ilist) + { + std::vector tmp = ilist; + auto nptr = std::make_shared( tmp ); + fptr = nptr; + } + + template + void swapMFactory(std::shared_ptr& fptr, const Rs&... rs) + { + auto nptr = std::make_shared( rs... ); + fptr = nptr; + } + + template + auto mkt(Ts&&... ts) -> decltype(std::make_tuple(ts...)) + { + return std::make_tuple(ts...); + } + + class AnonymousIndexTest : public ::testing::Test + { + protected: + + typedef SingleRangeFactory SRF; + typedef SRF::oType SRange; + + typedef MultiRangeFactory M3RF; + typedef M3RF::oType M3Range; + + typedef MultiRangeFactory MasterRF; + typedef MasterRF::oType MasterRange; + + typedef ContainerRangeFactory CRF; + typedef CRF::oType CRange; + typedef ContainerIndex CIndex; + + AnonymousIndexTest() + { + swapFactory(rfbptr, { 'e', 'b', 'n' } ); + sr1ptr = std::dynamic_pointer_cast( rfbptr->create() ); + + swapFactory(rfbptr, { 'x', 'y', 'l', 'f' } ); + sr2ptr = std::dynamic_pointer_cast( rfbptr->create() ); + + swapFactory(rfbptr, { 'a', 'b' } ); + std::shared_ptr temp1 = std::dynamic_pointer_cast( rfbptr->create() ); + swapFactory(rfbptr, { '1' } ); + std::shared_ptr temp2 = std::dynamic_pointer_cast( rfbptr->create() ); + swapFactory(rfbptr, { '0', '7' } ); + std::shared_ptr temp3 = std::dynamic_pointer_cast( rfbptr->create() ); + + swapMFactory(rfbptr, temp1, temp2, temp3 ); + m3rptr = std::dynamic_pointer_cast( rfbptr->create() ); + + swapMFactory(rfbptr, sr1ptr, m3rptr, sr2ptr); + mstrptr = std::dynamic_pointer_cast( rfbptr->create() ); + + swapMFactory(rfbptr, m3rptr, sr2ptr); + cr1ptr = std::dynamic_pointer_cast( rfbptr->create() ); + + swapMFactory(rfbptr, m3rptr, sr1ptr); + cr2ptr = std::dynamic_pointer_cast( rfbptr->create() ); + } + + std::shared_ptr rfbptr; + std::shared_ptr sr1ptr; + std::shared_ptr sr2ptr; + std::shared_ptr m3rptr; + std::shared_ptr mstrptr; + std::shared_ptr cr1ptr; + std::shared_ptr cr2ptr; + }; + + TEST_F(AnonymousIndexTest, Cast1) + { + AnonymousRangeFactory arf1(sr1ptr,m3rptr); + auto ar1a = std::dynamic_pointer_cast( arf1.create() ); + + arf1.append(sr2ptr); + + auto ar1b = std::dynamic_pointer_cast( arf1.create() ); + + AnonymousRangeFactory arf2(sr1ptr,m3rptr,sr2ptr); + auto ar2 = std::dynamic_pointer_cast( arf2.create() ); + + EXPECT_EQ(ar1b.get(), ar2.get()); + EXPECT_EQ(ar1a->size(), sr1ptr->size() * m3rptr->size()); + EXPECT_EQ(ar2->size(), sr1ptr->size() * m3rptr->size() * sr2ptr->size()); + + EXPECT_EQ(ar1a->sub(0)->size(), sr1ptr->size()); + EXPECT_EQ(ar1a->sub(1)->size(), m3rptr->size()); + + EXPECT_EQ(ar2->sub(0)->size(), sr1ptr->size()); + EXPECT_EQ(ar2->sub(1)->size(), m3rptr->size()); + EXPECT_EQ(ar2->sub(2)->size(), sr2ptr->size()); + } + +} // end namespace + +int main(int argc, char** argv) +{ + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +}