From 37dc3bf8182c748fe51fafdea94f2166d4809ad7 Mon Sep 17 00:00:00 2001 From: Christian Zimmermann Date: Wed, 28 Sep 2022 23:28:07 +0200 Subject: [PATCH] Pos_Test Multi works; change some things in near future --- src/include/ranges/xfor/pos_type.cc.h | 32 +++++-- src/include/ranges/xfor/pos_type.h | 30 ++++++- src/include/ranges/xfor/vpos_type.cc.h | 14 +-- src/tests/range_unit_test.cc | 6 +- src/tests/xfor_unit_test.cc | 116 +++++++++++++++++++++---- 5 files changed, 164 insertions(+), 34 deletions(-) diff --git a/src/include/ranges/xfor/pos_type.cc.h b/src/include/ranges/xfor/pos_type.cc.h index 517f0f9..6d3a562 100644 --- a/src/include/ranges/xfor/pos_type.cc.h +++ b/src/include/ranges/xfor/pos_type.cc.h @@ -186,7 +186,7 @@ namespace CNORXZ template inline DPos::DPos(const CPosInterface& a) : - ObjHandle( std::make_unique(a) ) + ObjHandle( std::make_unique>(a) ) {} inline SizeT DPos::size() const @@ -199,6 +199,11 @@ namespace CNORXZ return mC->vval(); } + inline DPosRef DPos::first() const + { + return DPosRef(mC->vget()); + } + inline DPosRef DPos::next() const { return DPosRef(mC->vnext()); @@ -207,13 +212,15 @@ namespace CNORXZ template inline DPos DPos::operator+(const CPosInterface& a) const { - return DPos(mC->vplus(VPosRef(&a))); // check memory safety!!! + VPosRef r(&a); + return DPos(mC->vplus(&r)); // check memory safety!!! } template inline DPos DPos::operator*(const CPosInterface& a) const { - return DPos(mC->vtimes(VPosRef(&a))); // check memory safety!!! + VPosRef r(&a); + return DPos(mC->vtimes(&r)); // check memory safety!!! } template @@ -240,6 +247,11 @@ namespace CNORXZ return mC->vval(); } + inline DPosRef DPosRef::first() const + { + return DPosRef(mC->vget()); + } + inline DPosRef DPosRef::next() const { return DPosRef(mC->vnext()); @@ -248,13 +260,15 @@ namespace CNORXZ template inline DPos DPosRef::operator+(const CPosInterface& a) const { - return DPos(mC->vplus(VPosRef(&a))); // check memory safety!!! + VPosRef r(&a); + return DPos(mC->vplus(&r)); // check memory safety!!! } template inline DPos DPosRef::operator*(const CPosInterface& a) const { - return DPos(mC->vtimes(VPosRef(&a))); // check memory safety!!! + VPosRef r(&a); + return DPos(mC->vtimes(&r)); // check memory safety!!! } template @@ -271,7 +285,7 @@ namespace CNORXZ template constexpr MPos::MPos(const CPosInterface& first, const CPosInterface& next) : - mFirst(first), mNext(next) + mFirst(first.THIS()), mNext(next.THIS()) {} template @@ -311,9 +325,9 @@ namespace CNORXZ template constexpr auto MPos::operator*(const CPosInterface& a) const { - typedef decltype(first()*a) PosT5; - typedef decltype(next()*a) PosT6; - return MPos(first()*a, next()*a); + typedef decltype(first()*a.THIS()) PosT5; + typedef decltype(next()*a.THIS()) PosT6; + return MPos(first()*a.THIS(), next()*a.THIS()); } template diff --git a/src/include/ranges/xfor/pos_type.h b/src/include/ranges/xfor/pos_type.h index 9b1fa5a..5365bc6 100644 --- a/src/include/ranges/xfor/pos_type.h +++ b/src/include/ranges/xfor/pos_type.h @@ -11,6 +11,8 @@ namespace CNORXZ class CPosInterface { public: + static constexpr bool MULTI = PosT::MULTI; + DEFAULT_MEMBERS(CPosInterface); PosT& THIS() { return static_cast(*this); } @@ -18,11 +20,20 @@ namespace CNORXZ inline SizeT size() const { return THIS().size(); } inline auto val() const { return THIS().val(); } + // template!!! inline CPosInterface operator+(const CPosInterface& a) const { return THIS() + a.THIS(); } + // template!!! inline CPosInterface operator*(const CPosInterface& a) const { return THIS() * a.THIS(); } - + // template!!! + // inline CPosInterface execute(const CPosInterface& a, + // const CPosInterface& b, const CPosInterface& c) const + // => a+b*c; only this executes also the FPos/SFPos-Map!!! + + // for each class implement +/* for each argument type EXPLICITLY (NO templates, except for MPos) + // *: only UPos/SPos as Arguments, for DPos only UPos as Args + template inline auto extend(const CPosInterface

& a) const { return THIS().extend(a); } }; @@ -31,6 +42,8 @@ namespace CNORXZ class SPos : public CPosInterface> { public: + static constexpr bool MULTI = false; + constexpr SPos() = default; constexpr SizeT size() const; @@ -53,6 +66,7 @@ namespace CNORXZ private: SizeT mExt = 0; public: + static constexpr bool MULTI = false; DEFAULT_MEMBERS(UPos); @@ -79,6 +93,8 @@ namespace CNORXZ const SizeT* mMap = nullptr; public: + static constexpr bool MULTI = false; + DEFAULT_MEMBERS(FPos); inline FPos(SizeT ext, const SizeT* map); @@ -100,6 +116,8 @@ namespace CNORXZ class SFPos : public CPosInterface> { public: + static constexpr bool MULTI = false; + constexpr SFPos() = default; constexpr SizeT size() const; @@ -121,6 +139,8 @@ namespace CNORXZ public CPosInterface { public: + static constexpr bool MULTI = true; + DEFAULT_MEMBERS(DPos); inline DPos(Uptr&& a); @@ -129,6 +149,7 @@ namespace CNORXZ inline SizeT size() const; inline SizeT val() const; + inline DPosRef first() const; inline DPosRef next() const; template @@ -147,11 +168,14 @@ namespace CNORXZ private: const VPosBase* mC; public: + static constexpr bool MULTI = true; + DEFAULT_MEMBERS(DPosRef); inline DPosRef(const VPosBase* c); inline SizeT size() const; inline SizeT val() const; + inline DPosRef first() const; inline DPosRef next() const; template @@ -164,6 +188,8 @@ namespace CNORXZ inline DPos extend(const CPosInterface& a) const; }; + // go to original pattern (-> LINEAR template chain) + // first: just cast by constructor template class MPos : public CPosInterface> { @@ -173,6 +199,8 @@ namespace CNORXZ PosT2 mNext; public: + static constexpr bool MULTI = true; + DEFAULT_MEMBERS(MPos); constexpr MPos(const CPosInterface& first, diff --git a/src/include/ranges/xfor/vpos_type.cc.h b/src/include/ranges/xfor/vpos_type.cc.h index 56c8c7e..d0d2815 100644 --- a/src/include/ranges/xfor/vpos_type.cc.h +++ b/src/include/ranges/xfor/vpos_type.cc.h @@ -28,7 +28,7 @@ namespace CNORXZ template Uptr VPos::copy() const { - return std::make_unique(*this); + return std::make_unique>(*this); } template @@ -99,13 +99,13 @@ namespace CNORXZ template VPos>::VPos(const CPosInterface>& a) : MPos(a.THIS()), - mFRef(&MPosT::mFirst), mNRef(&MPosT::mNext) + mFRef(&this->first()), mNRef(&this->next()) {} template Uptr VPos>::copy() const { - return std::make_unique>(*this); + return std::make_unique>>(*this); } template @@ -123,13 +123,13 @@ namespace CNORXZ template const VPosBase* VPos>::vget() const { - return mFRef; + return &mFRef; } template const VPosBase* VPos>::vnext() const { - return mNRef; + return &mNRef; } template @@ -174,7 +174,7 @@ namespace CNORXZ template VPosRef::VPosRef(const CPosInterface* c) : - mC(c) + mC(&c->THIS()) {} template @@ -255,7 +255,7 @@ namespace CNORXZ template Uptr VPosRef>::copy() const { - return std::make_unique>(*mFRef,*mNRef); + return std::make_unique>>(*mFRef,*mNRef); } template diff --git a/src/tests/range_unit_test.cc b/src/tests/range_unit_test.cc index e5ca76b..f1f1935 100644 --- a/src/tests/range_unit_test.cc +++ b/src/tests/range_unit_test.cc @@ -32,11 +32,13 @@ namespace EXPECT_EQ(cr->size(), mSize); EXPECT_EQ(crx->size(), mSize); - EXPECT_EQ(crx->begin() != crx->end(), true); + EXPECT_TRUE(crx->begin() != crx->end()); + EXPECT_FALSE(crx->begin() == crx->end()); EXPECT_EQ(crx->begin().pos(), 0u); EXPECT_EQ(crx->end().pos(), mSize); - EXPECT_EQ(cr->begin() != cr->end(), true); + EXPECT_TRUE(cr->begin() != cr->end()); + EXPECT_FALSE(cr->begin() == cr->end()); EXPECT_EQ(cr->begin().pos(), 0u); EXPECT_EQ(cr->end().pos(), mSize); diff --git a/src/tests/xfor_unit_test.cc b/src/tests/xfor_unit_test.cc index 4704993..426f5af 100644 --- a/src/tests/xfor_unit_test.cc +++ b/src/tests/xfor_unit_test.cc @@ -7,19 +7,31 @@ namespace { using namespace CNORXZ; + template + constexpr auto mkMPos(const CPosInterface& a, const CPosInterface& b) + { + return MPos(a,b); + } + class Pos_Test : public ::testing::Test { protected: + + static constexpr SizeT s1 = 7; + static constexpr SizeT s2 = 3; + static constexpr SizeT ss1 = 4; + static constexpr SizeT ss2 = 2; + Pos_Test() { - mUp1 = UPos(7); - mUp2 = UPos(3); + mUp1 = UPos(s1); + mUp2 = UPos(s2); } UPos mUp1; UPos mUp2; - SPos<4> mS4p; - SPos<2> mS2p; + SPos mS4p; + SPos mS2p; }; TEST_F(Pos_Test, Basics) @@ -29,10 +41,10 @@ namespace EXPECT_EQ( mS4p.size(), 1 ); EXPECT_EQ( mS2p.size(), 1 ); - EXPECT_EQ( mUp1.val(), 7 ); - EXPECT_EQ( mUp2.val(), 3 ); - EXPECT_EQ( mS4p.val(), 4 ); - EXPECT_EQ( mS2p.val(), 2 ); + EXPECT_EQ( mUp1.val(), s1 ); + EXPECT_EQ( mUp2.val(), s2 ); + EXPECT_EQ( mS4p.val(), ss1 ); + EXPECT_EQ( mS2p.val(), ss2 ); auto s6p = mS4p + mS2p; auto s8p = mS4p * mS2p; @@ -48,13 +60,87 @@ namespace EXPECT_EQ( up5.size(), 1 ); EXPECT_EQ( up6.size(), 1 ); - EXPECT_EQ( s6p.val(), 6 ); - EXPECT_EQ( s8p.val(), 8 ); - EXPECT_EQ( up3.val(), 4*7 ); - EXPECT_EQ( up4.val(), 7*2 ); - EXPECT_EQ( up5.val(), 4+7 ); - EXPECT_EQ( up6.val(), 7+2 ); - + EXPECT_EQ( s6p.val(), ss1+ss2 ); + EXPECT_EQ( s8p.val(), ss1*ss2 ); + EXPECT_EQ( up3.val(), ss1*s1 ); + EXPECT_EQ( up4.val(), s1*ss2 ); + EXPECT_EQ( up5.val(), ss1+s1 ); + EXPECT_EQ( up6.val(), s1+ss2 ); } + + TEST_F(Pos_Test, Multi) + { + auto mp1 = mkMPos(mS2p, mUp1); + auto mp2 = mkMPos(mUp2, mS4p); + auto mp3a = mp1 + mp2; + auto mp3b = mp2 + mp1; + auto mp4 = mp1 * mS2p; + auto mp5 = mp2 * mUp1; + + EXPECT_EQ(mp1.size(), 2); + EXPECT_EQ(mp2.size(), 2); + EXPECT_EQ(mp3a.size(), 2); + EXPECT_EQ(mp3b.size(), 2); + EXPECT_EQ(mp4.size(), 2); + EXPECT_EQ(mp5.size(), 2); + + EXPECT_EQ(mp1.first().val(), mS2p.val()); + EXPECT_EQ(mp1.next().val(), mUp1.val()); + EXPECT_EQ(mp2.first().val(), mUp2.val()); + EXPECT_EQ(mp2.next().val(), mS4p.val()); + EXPECT_EQ(mp3a.first().val(), mS2p.val() + mUp2.val()); + EXPECT_EQ(mp3a.next().val(), mUp1.val() + mS4p.val()); + EXPECT_EQ(mp3b.first().val(), mS2p.val() + mUp2.val()); + EXPECT_EQ(mp3b.next().val(), mUp1.val() + mS4p.val()); + EXPECT_EQ(mp4.first().val(), mS2p.val() * mS2p.val()); + EXPECT_EQ(mp4.next().val(), mUp1.val() * mS2p.val()); + EXPECT_EQ(mp5.first().val(), mUp2.val() * mUp1.val()); + EXPECT_EQ(mp5.next().val(), mS4p.val() * mUp1.val()); + } + /* + TEST_F(Pos_Test, Dyn) + { + DPos dp01(mS2p); + DPos dp02(mUp1); + DPos dp1(mkMPos(mS2p, mUp1)); + DPos dp2(mkMPos(mUp2, mS4p)); + auto dp3a = dp1 + dp2; + auto dp3b = dp2 + dp1; + auto dp4 = dp1 * dp01; + auto dp5 = dp2 * dp02; + + EXPECT_EQ(dp01.size(), 1); + EXPECT_EQ(dp02.size(), 1); + + EXPECT_EQ(dp01.val(), mS2p.val()); + EXPECT_EQ(dp02.val(), mUp1.val()); + + EXPECT_EQ(dp1.size(), 2); + EXPECT_EQ(dp2.size(), 2); + + EXPECT_EQ(dp3a.size(), 2); + EXPECT_EQ(dp3b.size(), 2); + EXPECT_EQ(dp4.size(), 2); + EXPECT_EQ(dp5.size(), 2); + + EXPECT_EQ(dp1.first().val(), mS2p.val()); + EXPECT_EQ(dp1.next().val(), mUp1.val()); + EXPECT_EQ(dp2.first().val(), mUp2.val()); + EXPECT_EQ(dp2.next().val(), mS4p.val()); + EXPECT_EQ(dp3a.first().val(), mS2p.val() + mUp2.val()); + EXPECT_EQ(dp3a.next().val(), mUp1.val() + mS4p.val()); + EXPECT_EQ(dp3b.first().val(), mS2p.val() + mUp2.val()); + EXPECT_EQ(dp3b.next().val(), mUp1.val() + mS4p.val()); + EXPECT_EQ(dp4.first().val(), mS2p.val() * mS2p.val()); + EXPECT_EQ(dp4.next().val(), mUp1.val() * mS2p.val()); + EXPECT_EQ(dp5.first().val(), mUp2.val() * mUp1.val()); + EXPECT_EQ(dp5.next().val(), mS4p.val() * mUp1.val()); + } + */ } +int main(int argc, char** argv) +{ + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +}