From 80ac6040890672bf74f38240ae4bd1d3bbd62fb0 Mon Sep 17 00:00:00 2001 From: Christian Zimmermann Date: Tue, 28 Feb 2023 14:15:16 +0100 Subject: [PATCH] cxz cereal: more tests --- CMakeLists.txt | 2 +- src/include/ranges/mrange.cc.h | 4 +- src/include/ranges/range_base.cc.h | 3 + src/include/ranges/range_base.h | 4 +- src/lib/ranges/range_base.cc | 1 + src/lib/ranges/yrange.cc | 14 +-- src/opt/cereal/include/cer_array.cc.h | 3 +- src/opt/cereal/include/cer_ranges.cc.h | 4 + src/opt/cereal/include/range_save_load.cc.h | 5 +- src/opt/cereal/include/range_save_load.h | 4 +- src/opt/cereal/tests/cereal_unit_test.cc | 123 ++++++++++++++++++++ 11 files changed, 152 insertions(+), 15 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f0f4e0e..e5e9ee7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,7 +3,7 @@ cmake_minimum_required(VERSION 3.0) project(cnorxz) execute_process(COMMAND bash "-c" "git rev-parse HEAD" OUTPUT_VARIABLE GIT_HASH OUTPUT_STRIP_TRAILING_WHITESPACE) -execute_process(COMMAND bash "-c" "git tag -l --sort=refname v* | tail -n1" OUTPUT_VARIABLE GIT_TAG OUTPUT_STRIP_TRAILING_WHITESPACE) +execute_process(COMMAND bash "-c" "git tag -l --sort=refname 'v*' | tail -n1" OUTPUT_VARIABLE GIT_TAG OUTPUT_STRIP_TRAILING_WHITESPACE) execute_process(COMMAND bash "-c" "git rev-list -n 1 ${GIT_TAG}" OUTPUT_VARIABLE GIT_TAG_HASH OUTPUT_STRIP_TRAILING_WHITESPACE) string(SUBSTRING ${GIT_TAG} 1 -1 VERSION) if(NOT (${GIT_HASH} EQUAL ${GIT_TAG_HASH})) diff --git a/src/include/ranges/mrange.cc.h b/src/include/ranges/mrange.cc.h index 64ac367..875b06d 100644 --- a/src/include/ranges/mrange.cc.h +++ b/src/include/ranges/mrange.cc.h @@ -624,10 +624,10 @@ namespace CNORXZ template Vector MRange::key() const { - Vector key = iter<0,sizeof...(Ranges)> + Vector k = iter<0,sizeof...(Ranges)> ( [&](auto i) { return std::get( mRs ); }, [](const auto&... e) { return Vector { e->id()... }; } ); - return key; + return k; } /************************ diff --git a/src/include/ranges/range_base.cc.h b/src/include/ranges/range_base.cc.h index 8c80a4f..23fe348 100644 --- a/src/include/ranges/range_base.cc.h +++ b/src/include/ranges/range_base.cc.h @@ -6,8 +6,11 @@ #include "dindex.h" #include "mrange.h" +//#include "crange.h" + namespace CNORXZ { + template decltype(auto) RangeInterface::begin() const { diff --git a/src/include/ranges/range_base.h b/src/include/ranges/range_base.h index 4d986dc..1b91c86 100644 --- a/src/include/ranges/range_base.h +++ b/src/include/ranges/range_base.h @@ -68,6 +68,8 @@ namespace CNORXZ DIndex end() const; RangePtr orig() const; + //virtual Vector key2() const { return Vector(); } + friend RangeFactoryBase; protected: @@ -75,8 +77,8 @@ namespace CNORXZ RangeBase(); RangeBase(const RangePtr& rel); - virtual Vector key() const = 0; + virtual Vector key() const = 0; // delete copy/move??? Uuid mId = {0,0}; Wptr mThis; diff --git a/src/lib/ranges/range_base.cc b/src/lib/ranges/range_base.cc index 52091fe..b17abfe 100644 --- a/src/lib/ranges/range_base.cc +++ b/src/lib/ranges/range_base.cc @@ -37,6 +37,7 @@ namespace CNORXZ RangePtr RangeFactoryBase::getRegistered(const TypeInfo& info, const RangePtr& r) { + CXZ_ASSERT(r != nullptr, "got range ptr to null"); auto& rx = sCreated[info.hash_code()][r->key()]; if(rx == nullptr){ rx = r; diff --git a/src/lib/ranges/yrange.cc b/src/lib/ranges/yrange.cc index 04d7536..9b041b8 100644 --- a/src/lib/ranges/yrange.cc +++ b/src/lib/ranges/yrange.cc @@ -425,14 +425,14 @@ namespace CNORXZ void YRangeFactory::make() { - Vector key(mRVec.size()); - std::transform(mRVec.begin(), mRVec.end(), key.begin(), + Vector k(mRVec.size()); + std::transform(mRVec.begin(), mRVec.end(), k.begin(), [&](const RangePtr& r) { return r->id(); } ); - mProd = this->fromCreated(typeid(YRange), key); + mProd = this->fromCreated(typeid(YRange), k); if(mProd == nullptr){ mProd = std::shared_ptr ( new YRange( std::move(mRVec) ) ); - this->addToCreated(typeid(YRange), key, mProd); + this->addToCreated(typeid(YRange), k, mProd); } } @@ -507,10 +507,10 @@ namespace CNORXZ Vector YRange::key() const { - Vector key(mRVec.size()); - std::transform(mRVec.begin(), mRVec.end(), key.begin(), + Vector k(mRVec.size()); + std::transform(mRVec.begin(), mRVec.end(), k.begin(), [&](const RangePtr& r) { return r->id(); } ); - return key; + return k; } YRange::YRange(const Vector& rvec) : mRVec(rvec) {} diff --git a/src/opt/cereal/include/cer_array.cc.h b/src/opt/cereal/include/cer_array.cc.h index 35299d5..bbbd01d 100644 --- a/src/opt/cereal/include/cer_array.cc.h +++ b/src/opt/cereal/include/cer_array.cc.h @@ -20,7 +20,8 @@ namespace CNORXZ template void MArray::load(Archive& ar) { - ar(cereal::make_nvp("range", AB::mRange)); + save_load("range", AB::mRange); + //ar(cereal::make_nvp("range", AB::mRange)); ar(cereal::make_nvp("data", mCont)); } } diff --git a/src/opt/cereal/include/cer_ranges.cc.h b/src/opt/cereal/include/cer_ranges.cc.h index d0ce264..b0bda43 100644 --- a/src/opt/cereal/include/cer_ranges.cc.h +++ b/src/opt/cereal/include/cer_ranges.cc.h @@ -85,6 +85,10 @@ namespace CNORXZ ar(cereal::make_nvp("this", RB::mThis)); ar(cereal::make_nvp("sub", mRVec)); CXZ_ASSERT(RangePtr(RB::mThis).get() == this, "got corrupted range data"); + // register range: + for(auto& r: mRVec){ + r = RangeFactoryBase::getRegistered(r->type(), r); + } } } diff --git a/src/opt/cereal/include/range_save_load.cc.h b/src/opt/cereal/include/range_save_load.cc.h index acde3c5..db6917c 100644 --- a/src/opt/cereal/include/range_save_load.cc.h +++ b/src/opt/cereal/include/range_save_load.cc.h @@ -4,15 +4,16 @@ #include "ranges/range_base.h" #include "range_save_load.h" +#include namespace CNORXZ { namespace cer { template - RangePtr save_load(Archive& ar, RangePtr& r) + RangePtr save_load(Archive& ar, const String& name, RangePtr& r) { - ar(r); + ar(cereal::make_nvp(name, r)); return r = RangeFactoryBase::getRegistered(r->type(), r); } } diff --git a/src/opt/cereal/include/range_save_load.h b/src/opt/cereal/include/range_save_load.h index 7518379..a60f0f6 100644 --- a/src/opt/cereal/include/range_save_load.h +++ b/src/opt/cereal/include/range_save_load.h @@ -8,8 +8,10 @@ namespace CNORXZ { namespace cer { + // use this function to register the range in the map of the range factories + // (otherwise there might be more than one instance of the same range) template - RangePtr save_load(Archive& ar, RangePtr& r); + RangePtr save_load(Archive& ar, const String& name, RangePtr& r); } } diff --git a/src/opt/cereal/tests/cereal_unit_test.cc b/src/opt/cereal/tests/cereal_unit_test.cc index 9d31ad6..edc5226 100644 --- a/src/opt/cereal/tests/cereal_unit_test.cc +++ b/src/opt/cereal/tests/cereal_unit_test.cc @@ -28,6 +28,43 @@ namespace std::stringstream mS; }; + class CerURange_Test : public ::testing::Test + { + protected: + + CerURange_Test() + { + mMeta = { "some", "random", "words", "with", "no", "meaning" }; + mR = URangeFactory(mMeta).create(); + } + + Vector mMeta; + RangePtr mR; + std::stringstream mS; + }; + + class CerYRange_Test : public ::testing::Test + { + protected: + + CerYRange_Test() + { + mSize = 7; + mMeta = { "some", "random", "words", "with", "no", "meaning" }; + mR1 = CRangeFactory(mSize).create(); + mR2 = URangeFactory(mMeta).create(); + mR = YRangeFactory({mR1,mR2}).create(); + } + + SizeT mSize; + Vector mMeta; + RangePtr mR1; + RangePtr mR2; + RangePtr mR; + std::stringstream mS; + std::stringstream mS2; + }; + TEST_F(CerCRange_Test, Serialize) { { @@ -50,6 +87,92 @@ namespace EXPECT_TRUE(*i1 == *i2); } } + + TEST_F(CerURange_Test, Serialize) + { + { + cereal::JSONOutputArchive ar(mS); + ar(mR); + } + std::cout << mS.str() << std::endl; + RangePtr r = nullptr; + { + cereal::JSONInputArchive ar(mS); + ar(r); + } + EXPECT_EQ(r->size(), mR->size()); + EXPECT_EQ(r->dim(), mR->dim()); + auto i1 = r->begin(); + auto i2 = mR->begin(); + auto i1e = r->end(); + auto i2e = mR->end(); + for(; i1 != i1e; ++i1, ++i2){ + EXPECT_TRUE(*i1 == *i2); + } + } + + TEST_F(CerYRange_Test, Serialize) + { + { + cereal::JSONOutputArchive ar(mS); + ar(cereal::make_nvp("mR",mR)); + //ar(mR); + } + std::cout << mS.str() << std::endl; + RangePtr r = nullptr; + { + cereal::JSONInputArchive ar(mS); + cer::save_load(ar, "mR", r); + } + EXPECT_EQ(r->size(), mR->size()); + EXPECT_EQ(r->dim(), mR->dim()); + auto i1 = r->begin(); + auto i2 = mR->begin(); + auto i1e = r->end(); + auto i2e = mR->end(); + for(; i1 != i1e; ++i1, ++i2){ + EXPECT_TRUE(*i1 == *i2); + } + } + + TEST_F(CerYRange_Test, NoDuplicates) + { + { + cereal::JSONOutputArchive ar(mS); + ar(cereal::make_nvp("mR",mR)); + } + { + cereal::JSONOutputArchive ar(mS2); + ar(cereal::make_nvp("mR",mR)); + } + std::cout << mS.str() << std::endl; + RangePtr r = nullptr; + { + cereal::JSONInputArchive ar(mS); + //ar(r); + cer::save_load(ar, "mR", r); + } + RangePtr r2 = nullptr; + { + cereal::JSONInputArchive ar(mS2); + //ar(r2); + cer::save_load(ar, "mR", r2); + } + + EXPECT_TRUE(r == r2); + EXPECT_EQ(r->size(), r2->size()); + EXPECT_EQ(r->dim(), r2->dim()); + for(SizeT i = 0; i != r->dim(); ++i){ + EXPECT_TRUE(r->sub(i) == r2->sub(i)); + } + auto i1 = r->begin(); + auto i2 = r2->begin(); + auto i1e = r->end(); + auto i2e = r2->end(); + for(; i1 != i1e; ++i1, ++i2){ + EXPECT_TRUE(*i1 == *i2); + } + } } int main(int argc, char** argv)