fix bug in DType + further yrange test

This commit is contained in:
Christian Zimmermann 2022-11-17 19:00:58 +01:00
parent da4bf39c83
commit 99eb72bb76
4 changed files with 44 additions and 26 deletions

View file

@ -30,21 +30,21 @@ namespace CNORXZ
template <typename T> template <typename T>
void DType::_mkToStr() void DType::_mkToStr()
{ {
mToStr = [&](){ mToStr = [](const std::any& d){
return toString(std::any_cast<T>(mD)); return toString(std::any_cast<T>(d));
}; };
} }
template <typename T> template <typename T>
void DType::_mkComp() void DType::_mkComp()
{ {
mComp = [&](const std::any& a){ mComp = [](const std::any& d, const std::any& a){
if(mD.type() != a.type()){ if(d.type() != a.type()){
return 2; return 2;
} }
else { else {
auto& at = std::any_cast<const T&>(a); auto& at = std::any_cast<const T&>(a);
auto& dt = std::any_cast<const T&>(mD); auto& dt = std::any_cast<const T&>(d);
if(std::equal_to<T>{}(dt,at)){ if(std::equal_to<T>{}(dt,at)){
return 0; return 0;
} }

View file

@ -17,8 +17,8 @@ namespace CNORXZ
{ {
private: private:
std::any mD; std::any mD;
std::function<String()> mToStr; std::function<String(const std::any&)> mToStr;
std::function<Int(std::any)> mComp; std::function<Int(const std::any&,const std::any&)> mComp;
template <typename T> template <typename T>
void _mkToStr(); void _mkToStr();
@ -35,15 +35,15 @@ namespace CNORXZ
template <typename T> template <typename T>
DType& operator=(const T& d); DType& operator=(const T& d);
String str() const { return mToStr(); } String str() const { return mToStr(mD); }
const std::any& get() const { return mD; } const std::any& get() const { return mD; }
bool operator==(const DType& a) const { return mComp(a.mD) == 0; } bool operator==(const DType& a) const { return mComp(mD,a.mD) == 0; }
bool operator!=(const DType& a) const { return mComp(a.mD) != 0; } bool operator!=(const DType& a) const { return mComp(mD,a.mD) != 0; }
bool operator<(const DType& a) const { return mComp(a.mD) == -1; } bool operator<(const DType& a) const { return mComp(mD,a.mD) == -1; }
bool operator>(const DType& a) const { return mComp(a.mD) == 1; } bool operator>(const DType& a) const { return mComp(mD,a.mD) == 1; }
bool operator<=(const DType& a) const { auto c = mComp(a.mD); return c <= 0; } bool operator<=(const DType& a) const { auto c = mComp(mD,a.mD); return c <= 0; }
bool operator>=(const DType& a) const { auto c = mComp(a.mD); return c == 1 or c == 0; } bool operator>=(const DType& a) const { auto c = mComp(mD,a.mD); return c == 1 or c == 0; }
}; };
} // namespace CNORXZ } // namespace CNORXZ

View file

@ -260,7 +260,8 @@ namespace CNORXZ
DType YIndex::meta() const DType YIndex::meta() const
{ {
Vector<DType> v(mIs.size()); Vector<DType> v(mIs.size());
std::transform(mIs.begin(), mIs.end(), v.begin(), [](const auto& x) { return x->meta(); }); std::transform(mIs.begin(), mIs.end(), v.begin(),
[](const auto& x) { return x->meta(); });
return DType(v); return DType(v);
} }

View file

@ -19,9 +19,11 @@ namespace
CR_Test() CR_Test()
{ {
mSize = 7; mSize = 7;
cr = CRangeFactory(mSize).create();
} }
SizeT mSize; SizeT mSize;
RangePtr cr;
}; };
class UR_Test : public ::testing::Test class UR_Test : public ::testing::Test
@ -32,9 +34,11 @@ namespace
{ {
mMeta = { "These", "are", "test", "strings", "foo", "bar", "baz" }; mMeta = { "These", "are", "test", "strings", "foo", "bar", "baz" };
std::sort(mMeta.begin(), mMeta.end(), std::less<String>()); std::sort(mMeta.begin(), mMeta.end(), std::less<String>());
ur = URangeFactory<String>(mMeta).create();
} }
Vector<String> mMeta; Vector<String> mMeta;
RangePtr ur;
}; };
class MR_Test : public ::testing::Test class MR_Test : public ::testing::Test
@ -46,10 +50,16 @@ namespace
mMeta = { "test", "strings", "foo" }; mMeta = { "test", "strings", "foo" };
std::sort(mMeta.begin(), mMeta.end(), std::less<String>()); std::sort(mMeta.begin(), mMeta.end(), std::less<String>());
mSize = 7; mSize = 7;
auto cr = CRangeFactory(mSize).create();
auto crx = std::dynamic_pointer_cast<CRange>(cr);
auto ur = URangeFactory<String>(mMeta).create();
auto urx = std::dynamic_pointer_cast<URange<String>>(ur);
mr = mrange(crx,urx);
} }
Vector<String> mMeta; Vector<String> mMeta;
SizeT mSize; SizeT mSize;
RangePtr mr;
}; };
class YR_Test : public ::testing::Test class YR_Test : public ::testing::Test
@ -61,15 +71,18 @@ namespace
mMeta = { "test", "strings", "foo" }; mMeta = { "test", "strings", "foo" };
std::sort(mMeta.begin(), mMeta.end(), std::less<String>()); std::sort(mMeta.begin(), mMeta.end(), std::less<String>());
mSize = 7; mSize = 7;
auto cr = CRangeFactory(mSize).create();
auto ur = URangeFactory<String>(mMeta).create();
yr = cr * ur;
} }
Vector<String> mMeta; Vector<String> mMeta;
SizeT mSize; SizeT mSize;
RangePtr yr;
}; };
TEST_F(CR_Test, Basics) TEST_F(CR_Test, Basics)
{ {
auto cr = CRangeFactory(mSize).create();
auto crx = std::dynamic_pointer_cast<CRange>(cr); auto crx = std::dynamic_pointer_cast<CRange>(cr);
EXPECT_EQ(cr->size(), mSize); EXPECT_EQ(cr->size(), mSize);
EXPECT_EQ(crx->size(), mSize); EXPECT_EQ(crx->size(), mSize);
@ -103,7 +116,6 @@ namespace
TEST_F(UR_Test, Basics) TEST_F(UR_Test, Basics)
{ {
auto ur = URangeFactory<String>(mMeta).create();
auto urx = std::dynamic_pointer_cast<URange<String>>(ur); auto urx = std::dynamic_pointer_cast<URange<String>>(ur);
EXPECT_EQ(ur->size(), mMeta.size()); EXPECT_EQ(ur->size(), mMeta.size());
EXPECT_EQ(urx->size(), mMeta.size()); EXPECT_EQ(urx->size(), mMeta.size());
@ -135,11 +147,6 @@ namespace
TEST_F(MR_Test, Basics) TEST_F(MR_Test, Basics)
{ {
auto cr = CRangeFactory(mSize).create();
auto crx = std::dynamic_pointer_cast<CRange>(cr);
auto ur = URangeFactory<String>(mMeta).create();
auto urx = std::dynamic_pointer_cast<URange<String>>(ur);
auto mr = mrange(crx,urx);
auto mrx = std::dynamic_pointer_cast<MRange<CRange,URange<String>>>(mr); auto mrx = std::dynamic_pointer_cast<MRange<CRange,URange<String>>>(mr);
EXPECT_EQ(mr->size(), mMeta.size()*mSize); EXPECT_EQ(mr->size(), mMeta.size()*mSize);
@ -193,9 +200,6 @@ namespace
TEST_F(YR_Test, Basics) TEST_F(YR_Test, Basics)
{ {
auto cr = CRangeFactory(mSize).create();
auto ur = URangeFactory<String>(mMeta).create();
auto yr = cr * ur;
EXPECT_EQ(yr->size(), mMeta.size()*mSize); EXPECT_EQ(yr->size(), mMeta.size()*mSize);
EXPECT_EQ(yr->dim(), 2u); EXPECT_EQ(yr->dim(), 2u);
@ -205,6 +209,19 @@ namespace
EXPECT_EQ(yr->begin().pos(), 0u); EXPECT_EQ(yr->begin().pos(), 0u);
EXPECT_EQ(yr->end().pos(), yr->size()); EXPECT_EQ(yr->end().pos(), yr->size());
const SizeT mmsize = mMeta.size();
auto mkm = [&](SizeT i) { return Vector<DType>({DType(i/mmsize),DType(mMeta[i % mmsize])}); };
SizeT cnt = 0;
auto endxi = yr->end();
for(auto xi = yr->begin(); xi != endxi; ++xi){
EXPECT_EQ(xi.pos(), cnt);
auto meta = mkm(cnt);
EXPECT_TRUE(*xi == DType(meta));
EXPECT_EQ((*xi).str(), toString(meta));
++cnt;
}
} }
// RCast_Test // RCast_Test
} }