From 99eb72bb761b5090d3e604e85cc6acfc46efe8c8 Mon Sep 17 00:00:00 2001 From: Christian Zimmermann Date: Thu, 17 Nov 2022 19:00:58 +0100 Subject: [PATCH] fix bug in DType + further yrange test --- src/include/base/dtype.cc.h | 10 +++++----- src/include/base/dtype.h | 20 +++++++++---------- src/lib/ranges/yrange.cc | 3 ++- src/tests/range_unit_test.cc | 37 ++++++++++++++++++++++++++---------- 4 files changed, 44 insertions(+), 26 deletions(-) diff --git a/src/include/base/dtype.cc.h b/src/include/base/dtype.cc.h index 9a34486..4b333da 100644 --- a/src/include/base/dtype.cc.h +++ b/src/include/base/dtype.cc.h @@ -30,21 +30,21 @@ namespace CNORXZ template void DType::_mkToStr() { - mToStr = [&](){ - return toString(std::any_cast(mD)); + mToStr = [](const std::any& d){ + return toString(std::any_cast(d)); }; } template void DType::_mkComp() { - mComp = [&](const std::any& a){ - if(mD.type() != a.type()){ + mComp = [](const std::any& d, const std::any& a){ + if(d.type() != a.type()){ return 2; } else { auto& at = std::any_cast(a); - auto& dt = std::any_cast(mD); + auto& dt = std::any_cast(d); if(std::equal_to{}(dt,at)){ return 0; } diff --git a/src/include/base/dtype.h b/src/include/base/dtype.h index fb94791..316d2f7 100644 --- a/src/include/base/dtype.h +++ b/src/include/base/dtype.h @@ -17,8 +17,8 @@ namespace CNORXZ { private: std::any mD; - std::function mToStr; - std::function mComp; + std::function mToStr; + std::function mComp; template void _mkToStr(); @@ -28,22 +28,22 @@ namespace CNORXZ public: DEFAULT_MEMBERS(DType); - + template DType(const T& d); template DType& operator=(const T& d); - String str() const { return mToStr(); } + String str() const { return mToStr(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(a.mD) != 0; } - bool operator<(const DType& a) const { return mComp(a.mD) == -1; } - bool operator>(const DType& a) const { return mComp(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(a.mD); return c == 1 or c == 0; } + bool operator==(const DType& a) const { return mComp(mD,a.mD) == 0; } + bool operator!=(const DType& a) const { return mComp(mD,a.mD) != 0; } + bool operator<(const DType& a) const { return mComp(mD,a.mD) == -1; } + bool operator>(const DType& a) const { return mComp(mD,a.mD) == 1; } + bool operator<=(const DType& a) const { auto c = mComp(mD,a.mD); return c <= 0; } + bool operator>=(const DType& a) const { auto c = mComp(mD,a.mD); return c == 1 or c == 0; } }; } // namespace CNORXZ diff --git a/src/lib/ranges/yrange.cc b/src/lib/ranges/yrange.cc index 3f7137a..d505051 100644 --- a/src/lib/ranges/yrange.cc +++ b/src/lib/ranges/yrange.cc @@ -260,7 +260,8 @@ namespace CNORXZ DType YIndex::meta() const { Vector 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); } diff --git a/src/tests/range_unit_test.cc b/src/tests/range_unit_test.cc index a655a03..c8008d5 100644 --- a/src/tests/range_unit_test.cc +++ b/src/tests/range_unit_test.cc @@ -19,9 +19,11 @@ namespace CR_Test() { mSize = 7; + cr = CRangeFactory(mSize).create(); } SizeT mSize; + RangePtr cr; }; class UR_Test : public ::testing::Test @@ -32,9 +34,11 @@ namespace { mMeta = { "These", "are", "test", "strings", "foo", "bar", "baz" }; std::sort(mMeta.begin(), mMeta.end(), std::less()); + ur = URangeFactory(mMeta).create(); } Vector mMeta; + RangePtr ur; }; class MR_Test : public ::testing::Test @@ -46,10 +50,16 @@ namespace mMeta = { "test", "strings", "foo" }; std::sort(mMeta.begin(), mMeta.end(), std::less()); mSize = 7; + auto cr = CRangeFactory(mSize).create(); + auto crx = std::dynamic_pointer_cast(cr); + auto ur = URangeFactory(mMeta).create(); + auto urx = std::dynamic_pointer_cast>(ur); + mr = mrange(crx,urx); } Vector mMeta; SizeT mSize; + RangePtr mr; }; class YR_Test : public ::testing::Test @@ -61,15 +71,18 @@ namespace mMeta = { "test", "strings", "foo" }; std::sort(mMeta.begin(), mMeta.end(), std::less()); mSize = 7; + auto cr = CRangeFactory(mSize).create(); + auto ur = URangeFactory(mMeta).create(); + yr = cr * ur; } Vector mMeta; SizeT mSize; + RangePtr yr; }; TEST_F(CR_Test, Basics) { - auto cr = CRangeFactory(mSize).create(); auto crx = std::dynamic_pointer_cast(cr); EXPECT_EQ(cr->size(), mSize); EXPECT_EQ(crx->size(), mSize); @@ -103,7 +116,6 @@ namespace TEST_F(UR_Test, Basics) { - auto ur = URangeFactory(mMeta).create(); auto urx = std::dynamic_pointer_cast>(ur); EXPECT_EQ(ur->size(), mMeta.size()); EXPECT_EQ(urx->size(), mMeta.size()); @@ -135,11 +147,6 @@ namespace TEST_F(MR_Test, Basics) { - auto cr = CRangeFactory(mSize).create(); - auto crx = std::dynamic_pointer_cast(cr); - auto ur = URangeFactory(mMeta).create(); - auto urx = std::dynamic_pointer_cast>(ur); - auto mr = mrange(crx,urx); auto mrx = std::dynamic_pointer_cast>>(mr); EXPECT_EQ(mr->size(), mMeta.size()*mSize); @@ -193,9 +200,6 @@ namespace TEST_F(YR_Test, Basics) { - auto cr = CRangeFactory(mSize).create(); - auto ur = URangeFactory(mMeta).create(); - auto yr = cr * ur; EXPECT_EQ(yr->size(), mMeta.size()*mSize); EXPECT_EQ(yr->dim(), 2u); @@ -204,6 +208,19 @@ namespace EXPECT_FALSE(yr->begin() == yr->end()); EXPECT_EQ(yr->begin().pos(), 0u); EXPECT_EQ(yr->end().pos(), yr->size()); + + const SizeT mmsize = mMeta.size(); + auto mkm = [&](SizeT i) { return Vector({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