diff --git a/src/ma_unit_test.cc b/src/ma_unit_test.cc index 25c1485..e08d4bc 100644 --- a/src/ma_unit_test.cc +++ b/src/ma_unit_test.cc @@ -59,16 +59,94 @@ namespace { std::vector vv = { 3.141, 2.718, 1.618, 0.693, 0.577 }; }; + /* + class MATest_1Dim : public ::testing::Test + { + protected: + + typedef SingleRangeFactory SRF; + typedef SRF::oType SRange; + + typedef ContainerRangeFactory CRF; + typedef CRF::oType CRange; + + MATest_1Dim() + { + swapFactory(rfbptr, {'x', 'y', 'l', 'f', 'g'} ); + srptr = std::dynamic_pointer_cast( rfbptr->create() ); + + swapMFactory(rfbptr, srptr); + crptr = std::dynamic_pointer_cast( rfbptr->create() ); + } + + std::shared_ptr rfbptr; + std::shared_ptr srptr; + std::shared_ptr crptr; + std::vector vv = { 3.141, 2.718, 1.618, 0.693, 0.577 }; + };*/ + TEST_F(MATest_1Dim, SimpleCall) { MultiArray ma(crptr, vv); + EXPECT_EQ( ma.size(), 5); + EXPECT_EQ( ma.isConst(), false); + EXPECT_EQ( ma.isSlice(), false); + auto i = ma.beginIndex(); EXPECT_EQ( ma[ i.at('x') ], 3.141); EXPECT_EQ( ma[ i.at('y') ], 2.718); EXPECT_EQ( ma[ i.at('l') ], 1.618); EXPECT_EQ( ma[ i.at('f') ], 0.693); EXPECT_EQ( ma[ i.at('g') ], 0.577); + } + + TEST_F(MATest_1Dim, ForLoop) + { + std::vector v2 = { 0.693 , 2.718, 3.141, 1.618, 9.98 }; + MultiArray ma(crptr, std::move( v2 ) ); + size_t cnt = 0; + for(auto el: ma){ + + if(cnt == 0){ + EXPECT_EQ(el, 0.693); + } else if(cnt == 1){ + EXPECT_EQ(el, 2.718); + } else if(cnt == 2){ + EXPECT_EQ(el, 3.141); + } else if(cnt == 3){ + EXPECT_EQ(el, 1.618); + } else if(cnt == 4){ + EXPECT_EQ(el, 9.98); + } else { + EXPECT_EQ(cnt, 0); + } + ++cnt; + } + EXPECT_EQ(cnt, ma.size()); + } + + TEST_F(MATest_1Dim, ReFormat) + { + swapFactory( rfbptr, { 'a', 'c', 'e', 'g', 'i' } ); + std::shared_ptr sr2 = std::dynamic_pointer_cast( rfbptr->create() ); + swapMFactory( rfbptr, sr2 ); + std::shared_ptr cr2 = std::dynamic_pointer_cast( rfbptr->create() ); + + MultiArray ma(crptr, vv); + auto i = ma.beginIndex(); + EXPECT_EQ( ma[ i.at('x') ], 3.141); + + auto ma2 = ma.format( cr2 ); + auto j = ma2.beginIndex(); + + EXPECT_EQ( ma[ j.at('a') ], 3.141); + EXPECT_EQ( ma[ j.at('c') ], 2.718); + EXPECT_EQ( ma[ j.at('e') ], 1.618); + EXPECT_EQ( ma[ j.at('g') ], 0.693); + EXPECT_EQ( ma[ j.at('i') ], 0.577); + } + } // end namespace diff --git a/src/multi_array.cc b/src/multi_array.cc index 1d950fb..7fae5de 100644 --- a/src/multi_array.cc +++ b/src/multi_array.cc @@ -531,6 +531,14 @@ namespace MultiArrayTools return false; } + template + template + MultiArray MultiArray::format(const std::shared_ptr& nr) + { + assert(MAB::mRange->size() == nr->size()); + return MultiArray( nr, std::move(mCont) ); + } + template const T* MultiArray::data() const { diff --git a/src/multi_array.h b/src/multi_array.h index 12ae6da..2180c62 100644 --- a/src/multi_array.h +++ b/src/multi_array.h @@ -225,8 +225,8 @@ namespace MultiArrayTools virtual bool isSlice() const override; template - MultiArray& operator()(const std::shared_ptr& nr); // reformat array using 'nr' which in - // total must have the same size as mRange + MultiArray format(const std::shared_ptr& nr); // reformat array using 'nr' which in + // total must have the same size as mRange const T* data() const override; T* data() override;