From 4db6760b9da3b03ae30a424e607c8e8552a09a71 Mon Sep 17 00:00:00 2001 From: Andreas Lauser Date: Fri, 6 Sep 2013 13:23:18 +0200 Subject: [PATCH] fix the SparseVector and SparseTable unit tests also, throw std::logic_error in the OPM_ERROR_IF macro --- opm/core/utility/ErrorMacros.hpp | 2 +- opm/core/utility/SparseTable.hpp | 4 +++- opm/core/utility/SparseVector.hpp | 18 +++++++++++------- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/opm/core/utility/ErrorMacros.hpp b/opm/core/utility/ErrorMacros.hpp index 5351e0f5..aec19324 100644 --- a/opm/core/utility/ErrorMacros.hpp +++ b/opm/core/utility/ErrorMacros.hpp @@ -57,6 +57,6 @@ } while (false) // throw an exception if a condition is true -#define OPM_ERROR_IF(condition, message) do {if(condition){ OPM_THROW(std::runtime_error, message);}} while(false) +#define OPM_ERROR_IF(condition, message) do {if(condition){ OPM_THROW(std::logic_error, message);}} while(false) #endif // OPM_ERRORMACROS_HPP diff --git a/opm/core/utility/SparseTable.hpp b/opm/core/utility/SparseTable.hpp index fb18b4c9..190602a9 100644 --- a/opm/core/utility/SparseTable.hpp +++ b/opm/core/utility/SparseTable.hpp @@ -149,7 +149,9 @@ namespace Opm /// Returns the size of a table row. int rowSize(int row) const { - assert(row >= 0 && row < size()); +#ifndef NDEBUG + OPM_ERROR_IF(row < 0 || row >= size(), "Row index " << row << " is out of range"); +#endif return row_start_[row + 1] - row_start_[row]; } diff --git a/opm/core/utility/SparseVector.hpp b/opm/core/utility/SparseVector.hpp index 37892eaf..3b03a634 100644 --- a/opm/core/utility/SparseVector.hpp +++ b/opm/core/utility/SparseVector.hpp @@ -81,14 +81,14 @@ namespace Opm default_elem_() { #ifndef NDEBUG - assert(sz >= 0); - assert(indices_.size() == data_.size()); + OPM_ERROR_IF(sz < 0, "The size of a SparseVector must be non-negative"); + OPM_ERROR_IF(indices_.size() != data_.size(), "The number of indices of a SparseVector must equal to the number of entries"); int last_index = -1; int num_ind = indices_.size(); for (int i = 0; i < num_ind; ++i) { int index = indices_[i]; if (index <= last_index || index >= sz) { - OPM_THROW(std::runtime_error, "Error in SparseVector construction, index is nonincreasing or out of range."); + OPM_THROW(std::logic_error, "Error in SparseVector construction, index is nonincreasing or out of range."); } last_index = index; } @@ -146,8 +146,10 @@ namespace Opm /// the vector has the given index. const T& element(int index) const { - assert(index >= 0); - assert(index < size_); +#ifndef NDEBUG + OPM_ERROR_IF(index < 0, "The index of a SparseVector must be non-negative (is " << index << ")"); + OPM_ERROR_IF(index >= size_, "The index of a SparseVector must be smaller than the maximum value (is " << index << ", max value: " << size_ <<")"); +#endif std::vector::const_iterator lb = std::lower_bound(indices_.begin(), indices_.end(), index); if (lb != indices_.end() && *lb == index) { return data_[lb - indices_.begin()]; @@ -161,8 +163,10 @@ namespace Opm /// \return the nzindex'th nonzero element. const T& nonzeroElement(int nzindex) const { - assert(nzindex >= 0); - assert(nzindex < nonzeroSize()); +#ifndef NDEBUG + OPM_ERROR_IF(nzindex < 0, "The index of a SparseVector must be non-negative (is " << nzindex << ")"); + OPM_ERROR_IF(nzindex >= nonzeroSize(), "The index of a SparseVector must be smaller than the maximum value (is " << nzindex << ", max value: " << nonzeroSize() <<")"); +#endif return data_[nzindex]; }