cnorxz/src/op_unit_test.cc

84 lines
2.1 KiB
C++
Raw Normal View History

2017-08-11 11:30:27 +02:00
// -*- C++ -*-
#include <cstdlib>
#include "gtest/gtest.h"
#include <iostream>
#include "multi_array_header.h"
namespace MAT = MultiArrayTools;
namespace {
using namespace MAT;
template <class Factory, typename T>
void swapFactory(std::shared_ptr<RangeFactoryBase>& fptr, std::initializer_list<T> ilist)
{
std::vector<T> tmp = ilist;
auto nptr = std::make_shared<Factory>( tmp );
fptr = nptr;
}
template <class Factory, class... Rs>
void swapMFactory(std::shared_ptr<RangeFactoryBase>& fptr, const Rs&... rs)
{
auto nptr = std::make_shared<Factory>( rs... );
fptr = nptr;
}
template <typename... Ts>
auto mkt(Ts&&... ts) -> decltype(std::make_tuple(ts...))
{
return std::make_tuple(ts...);
}
2017-08-11 15:26:10 +02:00
2017-08-11 11:30:27 +02:00
class OpTest_1Dim : public ::testing::Test
{
protected:
typedef SingleRangeFactory<char,RangeType::ANY> SRF;
typedef SRF::oType SRange;
typedef ContainerRangeFactory<SRange> CRF;
typedef CRF::oType CRange;
OpTest_1Dim()
{
swapFactory<SRF>(rfbptr, {'a', 'l', 'f', 'g'} );
srptr = std::dynamic_pointer_cast<SRange>( rfbptr->create() );
2017-08-11 15:26:10 +02:00
swapMFactory<CRF>(rfbptr, srptr);
2017-08-11 11:30:27 +02:00
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> v1 = { 2.917, 9.436, 0.373, 7.192 };
std::vector<double> v2 = { 8.870, 4.790, 8.215, 5.063 };
};
TEST_F(OpTest_1Dim, ExecOp)
{
MultiArray<double,CRange> ma1(crptr, v1);
MultiArray<double,CRange> ma2(crptr, v2);
MultiArray<double,CRange> res(crptr);
2017-08-11 15:26:10 +02:00
auto i = std::dynamic_pointer_cast<SRange::IndexType>( srptr->index() );
res.operator()<SRange>(i) = ma1.operator()<SRange>(i) + ma2.operator()<SRange>(i);
EXPECT_EQ( fabs( res.at('a') - (2.917+8.870) ) < 0.0001, true);
EXPECT_EQ( fabs( res.at('l') - (9.436+4.790) ) < 0.0001, true );
EXPECT_EQ( fabs( res.at('f') - (0.373+8.215) ) < 0.0001, true );
EXPECT_EQ( fabs( res.at('g') - (7.192+5.063) ) < 0.0001, true );
2017-08-11 11:30:27 +02:00
}
} // anonymous namspace
int main(int argc, char** argv)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}