MultiArray.format + corresp unit test

This commit is contained in:
Christian Zimmermann 2017-08-09 11:29:41 +02:00
parent 1652e122ea
commit d5bf63ac82
3 changed files with 88 additions and 2 deletions

View file

@ -59,17 +59,95 @@ namespace {
std::vector<double> vv = { 3.141, 2.718, 1.618, 0.693, 0.577 }; std::vector<double> vv = { 3.141, 2.718, 1.618, 0.693, 0.577 };
}; };
/*
class MATest_1Dim : public ::testing::Test
{
protected:
typedef SingleRangeFactory<char,RangeType::ANY> SRF;
typedef SRF::oType SRange;
typedef ContainerRangeFactory<SRange> CRF;
typedef CRF::oType CRange;
MATest_1Dim()
{
swapFactory<SRF>(rfbptr, {'x', 'y', 'l', 'f', 'g'} );
srptr = std::dynamic_pointer_cast<SRange>( rfbptr->create() );
swapMFactory<CRF>(rfbptr, srptr);
crptr = std::dynamic_pointer_cast<CRange>( rfbptr->create() );
}
std::shared_ptr<RangeFactoryBase> rfbptr;
std::shared_ptr<SRange> srptr;
std::shared_ptr<CRange> crptr;
std::vector<double> vv = { 3.141, 2.718, 1.618, 0.693, 0.577 };
};*/
TEST_F(MATest_1Dim, SimpleCall) TEST_F(MATest_1Dim, SimpleCall)
{ {
MultiArray<double,MATest_1Dim::CRange> ma(crptr, vv); MultiArray<double,MATest_1Dim::CRange> ma(crptr, vv);
EXPECT_EQ( ma.size(), 5);
EXPECT_EQ( ma.isConst(), false);
EXPECT_EQ( ma.isSlice(), false);
auto i = ma.beginIndex(); auto i = ma.beginIndex();
EXPECT_EQ( ma[ i.at('x') ], 3.141); EXPECT_EQ( ma[ i.at('x') ], 3.141);
EXPECT_EQ( ma[ i.at('y') ], 2.718); EXPECT_EQ( ma[ i.at('y') ], 2.718);
EXPECT_EQ( ma[ i.at('l') ], 1.618); EXPECT_EQ( ma[ i.at('l') ], 1.618);
EXPECT_EQ( ma[ i.at('f') ], 0.693); EXPECT_EQ( ma[ i.at('f') ], 0.693);
EXPECT_EQ( ma[ i.at('g') ], 0.577); EXPECT_EQ( ma[ i.at('g') ], 0.577);
} }
TEST_F(MATest_1Dim, ForLoop)
{
std::vector<double> v2 = { 0.693 , 2.718, 3.141, 1.618, 9.98 };
MultiArray<double,MATest_1Dim::CRange> 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<SRF>( rfbptr, { 'a', 'c', 'e', 'g', 'i' } );
std::shared_ptr<SRange> sr2 = std::dynamic_pointer_cast<SRange>( rfbptr->create() );
swapMFactory<CRF>( rfbptr, sr2 );
std::shared_ptr<CRange> cr2 = std::dynamic_pointer_cast<CRange>( rfbptr->create() );
MultiArray<double,MATest_1Dim::CRange> 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 } // end namespace
int main(int argc, char** argv) int main(int argc, char** argv)

View file

@ -531,6 +531,14 @@ namespace MultiArrayTools
return false; return false;
} }
template <typename T, class CRange>
template <class Range2>
MultiArray<T,Range2> MultiArray<T,CRange>::format(const std::shared_ptr<Range2>& nr)
{
assert(MAB::mRange->size() == nr->size());
return MultiArray<T,Range2>( nr, std::move(mCont) );
}
template <typename T, class CRange> template <typename T, class CRange>
const T* MultiArray<T,CRange>::data() const const T* MultiArray<T,CRange>::data() const
{ {

View file

@ -225,8 +225,8 @@ namespace MultiArrayTools
virtual bool isSlice() const override; virtual bool isSlice() const override;
template <class Range2> template <class Range2>
MultiArray& operator()(const std::shared_ptr<Range2>& nr); // reformat array using 'nr' which in MultiArray<T,Range2> format(const std::shared_ptr<Range2>& nr); // reformat array using 'nr' which in
// total must have the same size as mRange // total must have the same size as mRange
const T* data() const override; const T* data() const override;
T* data() override; T* data() override;