dynamic/anonymous range: replace asserts by MA_ASSERTs (throws errors instead of aborting)

This commit is contained in:
Christian Zimmermann 2020-09-23 14:14:47 +02:00
parent 6dac4957c5
commit 8ca415355a
3 changed files with 39 additions and 11 deletions

15
src/include/ma_assert.h Normal file
View file

@ -0,0 +1,15 @@
#include <iostream>
#include <sstream>
#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); }

View file

@ -1,6 +1,7 @@
#include "ranges/anonymous_range.h" #include "ranges/anonymous_range.h"
#include "ranges/ranges_header.cc.h" #include "ranges/ranges_header.cc.h"
#include "ma_assert.h"
namespace MultiArrayTools namespace MultiArrayTools
{ {
@ -199,7 +200,9 @@ namespace MultiArrayTools
std::shared_ptr<AnonymousRange> AnonymousRange::sreplace(const std::shared_ptr<RangeBase> in, size_t num) const std::shared_ptr<AnonymousRange> AnonymousRange::sreplace(const std::shared_ptr<RangeBase> 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; auto tmp = mOrig;
tmp[num] = in; tmp[num] = in;
AnonymousRangeFactory arf(tmp); AnonymousRangeFactory arf(tmp);
@ -212,7 +215,9 @@ namespace MultiArrayTools
for(auto& x: in){ for(auto& x: in){
nsize *= x->size(); 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; auto norig = mOrig;
norig.resize(mOrig.size() + in.size() - 1); norig.resize(mOrig.size() + in.size() - 1);
for(size_t i = 0; i != num; ++i){ for(size_t i = 0; i != num; ++i){
@ -240,7 +245,9 @@ namespace MultiArrayTools
assert(cnt++ == x); assert(cnt++ == x);
rep_size *= mOrig[x]->size(); 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<std::shared_ptr<RangeBase>> norig; vector<std::shared_ptr<RangeBase>> norig;
norig.reserve(mOrig.size()-num.size()+1); norig.reserve(mOrig.size()-num.size()+1);
norig.insert(norig.end(),mOrig.begin(),mOrig.begin()+num[0]); norig.insert(norig.end(),mOrig.begin(),mOrig.begin()+num[0]);

View file

@ -1,6 +1,7 @@
#include "ranges/dynamic_range.h" #include "ranges/dynamic_range.h"
#include "ranges/ranges_header.cc.h" #include "ranges/ranges_header.cc.h"
#include "ma_assert.h"
namespace MultiArrayTools namespace MultiArrayTools
{ {
@ -139,7 +140,7 @@ namespace MultiArrayTools
DynamicIndex& DynamicIndex::sync() DynamicIndex& DynamicIndex::sync()
{ {
assert(mIvecInit); MA_ASSERT(mIvecInit, "ivec not initialized");
size_t sv = 1; size_t sv = 1;
IB::mPos = 0; IB::mPos = 0;
for(size_t i = 0; i != mIVec.size(); ++i){ for(size_t i = 0; i != mIVec.size(); ++i){
@ -163,7 +164,7 @@ namespace MultiArrayTools
DynamicIndex& DynamicIndex::operator()(const vector<std::shared_ptr<IndexW>>& ivec) DynamicIndex& DynamicIndex::operator()(const vector<std::shared_ptr<IndexW>>& ivec)
{ {
mIvecInit = true; 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){ for(size_t i = 0; i != mIVec.size(); ++i){
mIVec[i].first = ivec[i]; mIVec[i].first = ivec[i];
} }
@ -174,19 +175,22 @@ namespace MultiArrayTools
DynamicIndex& DynamicIndex::operator()(const vector<std::string>& inames) DynamicIndex& DynamicIndex::operator()(const vector<std::string>& inames)
{ {
mIvecInit = true; 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){ for(size_t i = 0; i != mIVec.size(); ++i){
const std::string& iname = inames[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) : ""; const std::string smeta = (iname.find_first_of("=") != std::string::npos) ? iname.substr(iname.find_first_of("=")+1) : "";
if(sIMap.count(iname) != 0){ 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 { else {
sIMap[iname] = this->range()->sub(i)->aindex(); sIMap[iname] = this->range()->sub(i)->aindex();
} }
if(smeta != ""){ if(smeta != ""){
sIMap.at(iname)->at(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); mIVec[i].first = sIMap.at(iname);
} }
@ -266,13 +270,13 @@ namespace MultiArrayTools
const IndexW& DynamicIndex::get(size_t n) const const IndexW& DynamicIndex::get(size_t n) const
{ {
assert(mIvecInit); MA_ASSERT(mIvecInit, "ivec not initialized");
return *mIVec[n].first; return *mIVec[n].first;
} }
const std::shared_ptr<IndexW>& DynamicIndex::getP(size_t n) const const std::shared_ptr<IndexW>& DynamicIndex::getP(size_t n) const
{ {
assert(mIvecInit); MA_ASSERT(mIvecInit, "ivec not initialized");
return mIVec[n].first; return mIVec[n].first;
} }
@ -459,7 +463,9 @@ namespace MultiArrayTools
void DynamicRange::sreplace(const std::shared_ptr<RangeBase> in, size_t num) void DynamicRange::sreplace(const std::shared_ptr<RangeBase> 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; mOrig[num] = in;
} }