From 8ca415355a1f65710f065db7e4f0b482b96c6c9d Mon Sep 17 00:00:00 2001 From: Christian Zimmermann Date: Wed, 23 Sep 2020 14:14:47 +0200 Subject: [PATCH] dynamic/anonymous range: replace asserts by MA_ASSERTs (throws errors instead of aborting) --- src/include/ma_assert.h | 15 +++++++++++++++ src/lib/ranges/anonymous_range.cc | 13 ++++++++++--- src/lib/ranges/dynamic_range.cc | 22 ++++++++++++++-------- 3 files changed, 39 insertions(+), 11 deletions(-) create mode 100644 src/include/ma_assert.h diff --git a/src/include/ma_assert.h b/src/include/ma_assert.h new file mode 100644 index 0000000..3ee5e0b --- /dev/null +++ b/src/include/ma_assert.h @@ -0,0 +1,15 @@ + +#include +#include + +#define MA_ERRTAG __FILE__ << '@' << __LINE__ << '(' << __func__ << "): error" +#define MA_WARNTAG __FILE__ << '@' << __LINE__ << ": warning" +#define MA_ERROR(errmsg) {\ + std::stringstream ss;\ + ss << MA_ERRTAG << ": " << errmsg << std::flush;\ + throw std::runtime_error(ss.str()); } + +#define MA_WARNING(errmsg) {\ + std::cerr << MA_WARNTAG << ": " << errmsg << std::endl; } + +#define MA_ASSERT(statement, errmsg) if(not (statement)) { MA_ERROR(errmsg); } diff --git a/src/lib/ranges/anonymous_range.cc b/src/lib/ranges/anonymous_range.cc index 4d43d15..c895763 100644 --- a/src/lib/ranges/anonymous_range.cc +++ b/src/lib/ranges/anonymous_range.cc @@ -1,6 +1,7 @@ #include "ranges/anonymous_range.h" #include "ranges/ranges_header.cc.h" +#include "ma_assert.h" namespace MultiArrayTools { @@ -199,7 +200,9 @@ namespace MultiArrayTools std::shared_ptr AnonymousRange::sreplace(const std::shared_ptr in, size_t num) const { - assert(mOrig[num]->size() == in->size()); + MA_ASSERT(mOrig[num]->size() == in->size(), + std::string("replaced range has different size than given range (") + +std::to_string(mOrig[num]->size())+" vs "+std::to_string(in->size())+")"); auto tmp = mOrig; tmp[num] = in; AnonymousRangeFactory arf(tmp); @@ -212,7 +215,9 @@ namespace MultiArrayTools for(auto& x: in){ nsize *= x->size(); } - assert(mOrig[num]->size() == nsize); + MA_ASSERT(mOrig[num]->size() == nsize, + std::string("replaced range has different size than given range (") + +std::to_string(mOrig[num]->size())+" vs "+std::to_string(nsize)+")"); auto norig = mOrig; norig.resize(mOrig.size() + in.size() - 1); for(size_t i = 0; i != num; ++i){ @@ -240,7 +245,9 @@ namespace MultiArrayTools assert(cnt++ == x); rep_size *= mOrig[x]->size(); } - assert(rep_size == in->size()); + MA_ASSERT(rep_size == in->size(), + std::string("replaced range has different size than given range (") + +std::to_string(rep_size)+" vs "+std::to_string(in->size())+")"); vector> norig; norig.reserve(mOrig.size()-num.size()+1); norig.insert(norig.end(),mOrig.begin(),mOrig.begin()+num[0]); diff --git a/src/lib/ranges/dynamic_range.cc b/src/lib/ranges/dynamic_range.cc index b8c846b..0c7ac2a 100644 --- a/src/lib/ranges/dynamic_range.cc +++ b/src/lib/ranges/dynamic_range.cc @@ -1,6 +1,7 @@ #include "ranges/dynamic_range.h" #include "ranges/ranges_header.cc.h" +#include "ma_assert.h" namespace MultiArrayTools { @@ -139,7 +140,7 @@ namespace MultiArrayTools DynamicIndex& DynamicIndex::sync() { - assert(mIvecInit); + MA_ASSERT(mIvecInit, "ivec not initialized"); size_t sv = 1; IB::mPos = 0; for(size_t i = 0; i != mIVec.size(); ++i){ @@ -163,7 +164,7 @@ namespace MultiArrayTools DynamicIndex& DynamicIndex::operator()(const vector>& ivec) { mIvecInit = true; - assert(mIVec.size() == ivec.size()); + MA_ASSERT(mIVec.size() == ivec.size(), std::string("require ")+std::to_string(mIVec.size())+" indices"); for(size_t i = 0; i != mIVec.size(); ++i){ mIVec[i].first = ivec[i]; } @@ -174,19 +175,22 @@ namespace MultiArrayTools DynamicIndex& DynamicIndex::operator()(const vector& inames) { mIvecInit = true; - assert(mIVec.size() == inames.size()); + MA_ASSERT(mIVec.size() == inames.size(), std::string("require ")+std::to_string(mIVec.size())+" indices"); for(size_t i = 0; i != mIVec.size(); ++i){ const std::string& iname = inames[i]; const std::string smeta = (iname.find_first_of("=") != std::string::npos) ? iname.substr(iname.find_first_of("=")+1) : ""; if(sIMap.count(iname) != 0){ - assert(this->range()->sub(i) == sIMap.at(iname)->range()); + MA_ASSERT(this->range()->sub(i) == sIMap.at(iname)->range(), + std::string("range of index at position ")+std::to_string(i)+ + " is different from range of index with name "+iname); } else { sIMap[iname] = this->range()->sub(i)->aindex(); } if(smeta != ""){ sIMap.at(iname)->at(smeta); - assert(sIMap.at(iname)->pos() != sIMap.at(iname)->max()); + MA_ASSERT(sIMap.at(iname)->pos() != sIMap.at(iname)->max(), + smeta+" is not part of range of index with name "+iname); } mIVec[i].first = sIMap.at(iname); } @@ -266,13 +270,13 @@ namespace MultiArrayTools const IndexW& DynamicIndex::get(size_t n) const { - assert(mIvecInit); + MA_ASSERT(mIvecInit, "ivec not initialized"); return *mIVec[n].first; } const std::shared_ptr& DynamicIndex::getP(size_t n) const { - assert(mIvecInit); + MA_ASSERT(mIvecInit, "ivec not initialized"); return mIVec[n].first; } @@ -459,7 +463,9 @@ namespace MultiArrayTools void DynamicRange::sreplace(const std::shared_ptr in, size_t num) { - assert(mOrig[num]->size() == in->size()); + MA_ASSERT(mOrig[num]->size() == in->size(), + std::string("replaced range has different size than given range (") + +std::to_string(mOrig[num]->size())+" vs "+std::to_string(in->size())+")"); mOrig[num] = in; }