fix the SparseVector and SparseTable unit tests

also, throw std::logic_error in the OPM_ERROR_IF macro
This commit is contained in:
Andreas Lauser 2013-09-06 13:23:18 +02:00
parent 5d435d396c
commit 4db6760b9d
3 changed files with 15 additions and 9 deletions

View File

@ -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

View File

@ -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];
}

View File

@ -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<int>::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];
}