Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make IndexError consistent #1851

Merged
merged 1 commit into from
Feb 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion include/cantera/base/Solution.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@
//! Get the name of an adjacent phase by index
string adjacentName(size_t i) const {
if (i >= m_adjacent.size()) {
throw IndexError("Solution::adjacentName", "m_adjacent", i, m_adjacent.size()-1);
throw IndexError("Solution::adjacentName", "m_adjacent",
i, m_adjacent.size());

Check warning on line 120 in include/cantera/base/Solution.h

View check run for this annotation

Codecov / codecov/patch

include/cantera/base/Solution.h#L120

Added line #L120 was not covered by tests
}
return m_adjacent.at(i)->name();
}
Expand Down
18 changes: 8 additions & 10 deletions include/cantera/base/ctexceptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -175,16 +175,13 @@ class IndexError : public CanteraError
/*!
* This class indicates an out-of-bounds array index.
*
* @param func String name for the function within which the error was
* generated.
* @param arrayName name of the corresponding array
* @param m This is the value of the out-of-bounds index.
* @param mmax This is the maximum allowed value of the index. The
* minimum allowed value is assumed to be 0. The special
* value npos indicates that the array is empty.
* @param func String name for the function within which the error was generated.
* @param arrayName Name of the corresponding array or empty string @c "".
* @param m Value of the out-of-bounds index.
* @param arrSize Size of the array.
*/
IndexError(const string& func, const string& arrayName, size_t m, size_t mmax) :
CanteraError(func), arrayName_(arrayName), m_(m), mmax_(mmax) {}
IndexError(const string& func, const string& arrayName, size_t m, size_t arrSize) :
CanteraError(func), arrayName_(arrayName), m_(m), m_size(arrSize) {}

~IndexError() throw() override {};
string getMessage() const override;
Expand All @@ -194,7 +191,8 @@ class IndexError : public CanteraError

private:
string arrayName_;
size_t m_, mmax_;
size_t m_;
size_t m_size;
};

//! An error indicating that an unimplemented function has been called
Expand Down
4 changes: 2 additions & 2 deletions include/cantera/oneD/Domain1D.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@
//! Throws an exception if n is greater than nComponents()-1
void checkComponentIndex(size_t n) const {
if (n >= m_nv) {
throw IndexError("Domain1D::checkComponentIndex", "points", n, m_nv-1);
throw IndexError("Domain1D::checkComponentIndex", "points", n, m_nv);
}
}

Expand All @@ -175,7 +175,7 @@
//! Throws an exception if n is greater than nPoints()-1
void checkPointIndex(size_t n) const {
if (n >= m_points) {
throw IndexError("Domain1D::checkPointIndex", "points", n, m_points-1);
throw IndexError("Domain1D::checkPointIndex", "points", n, m_points);

Check warning on line 178 in include/cantera/oneD/Domain1D.h

View check run for this annotation

Codecov / codecov/patch

include/cantera/oneD/Domain1D.h#L178

Added line #L178 was not covered by tests
}
}

Expand Down
3 changes: 1 addition & 2 deletions include/cantera/oneD/OneDim.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,7 @@
//! Throws an exception if n is greater than nDomains()-1
void checkDomainIndex(size_t n) const {
if (n >= m_dom.size()) {
throw IndexError("OneDim::checkDomainIndex", "domains", n,
m_dom.size()-1);
throw IndexError("OneDim::checkDomainIndex", "domains", n, m_dom.size());

Check warning on line 92 in include/cantera/oneD/OneDim.h

View check run for this annotation

Codecov / codecov/patch

include/cantera/oneD/OneDim.h#L92

Added line #L92 was not covered by tests
}
}

Expand Down
2 changes: 1 addition & 1 deletion interfaces/sourcegen/sourcegen/clib/templates.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ clib-accessor: |-
{% if checks %}
## accessor uses index checker (see: sol3_adjacent)
if ({{ c_args[1] }} < 0 || {{ c_args[1] }} >= {{ base }}Cabinet::at({{ handle }})->{{ checks[0] }}()) {
throw IndexError("{{ c_func }}", "", {{ c_args[1] }}, {{ base }}Cabinet::at({{ handle }})->{{ checks[0] }}() - 1);
throw IndexError("{{ c_func }}", "", {{ c_args[1] }}, {{ base }}Cabinet::at({{ handle }})->{{ checks[0] }}());
}
{% endif %}{# checks #}
{% if shared %}
Expand Down
2 changes: 1 addition & 1 deletion src/base/SolutionArray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -781,7 +781,7 @@
} else if (static_cast<size_t>(m_active[loc_]) == m_loc) {
return;
} else if (loc_ >= m_size) {
throw IndexError("SolutionArray::setLoc", "indices", loc_, m_size - 1);
throw IndexError("SolutionArray::setLoc", "indices", loc_, m_size);

Check warning on line 784 in src/base/SolutionArray.cpp

View check run for this annotation

Codecov / codecov/patch

src/base/SolutionArray.cpp#L784

Added line #L784 was not covered by tests
}
m_loc = static_cast<size_t>(m_active[loc_]);
if (restore) {
Expand Down
7 changes: 4 additions & 3 deletions src/base/ctexceptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,16 @@

string IndexError::getMessage() const
{
if (mmax_ == npos) {
if (m_size == 0) {
return fmt::format("IndexError: index {} given, but array{} is empty.",
m_, arrayName_.empty() ? arrayName_ : " "+arrayName_);
}
if (arrayName_ == "") {
return fmt::format("IndexError: {} outside valid range of 0 to {}.", m_, mmax_);
return fmt::format("IndexError: {} outside valid range of 0 to {}.",
m_, m_size - 1);

Check warning on line 81 in src/base/ctexceptions.cpp

View check run for this annotation

Codecov / codecov/patch

src/base/ctexceptions.cpp#L81

Added line #L81 was not covered by tests
}
return fmt::format("IndexError: {}[{}] outside valid range of 0 to {}.",
arrayName_, m_, mmax_);
arrayName_, m_, m_size - 1);
}

} // namespace Cantera
6 changes: 3 additions & 3 deletions src/equil/MultiPhase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@
void MultiPhase::checkPhaseIndex(size_t m) const
{
if (m >= nPhases()) {
throw IndexError("MultiPhase::checkPhaseIndex", "phase", m, nPhases()-1);
throw IndexError("MultiPhase::checkPhaseIndex", "phase", m, nPhases());

Check warning on line 180 in src/equil/MultiPhase.cpp

View check run for this annotation

Codecov / codecov/patch

src/equil/MultiPhase.cpp#L180

Added line #L180 was not covered by tests
}
}

Expand Down Expand Up @@ -720,7 +720,7 @@
void MultiPhase::checkElementIndex(size_t m) const
{
if (m >= m_nel) {
throw IndexError("MultiPhase::checkElementIndex", "elements", m, m_nel-1);
throw IndexError("MultiPhase::checkElementIndex", "elements", m, m_nel);

Check warning on line 723 in src/equil/MultiPhase.cpp

View check run for this annotation

Codecov / codecov/patch

src/equil/MultiPhase.cpp#L723

Added line #L723 was not covered by tests
}
}

Expand Down Expand Up @@ -749,7 +749,7 @@
void MultiPhase::checkSpeciesIndex(size_t k) const
{
if (k >= m_nsp) {
throw IndexError("MultiPhase::checkSpeciesIndex", "species", k, m_nsp-1);
throw IndexError("MultiPhase::checkSpeciesIndex", "species", k, m_nsp);

Check warning on line 752 in src/equil/MultiPhase.cpp

View check run for this annotation

Codecov / codecov/patch

src/equil/MultiPhase.cpp#L752

Added line #L752 was not covered by tests
}
}

Expand Down
7 changes: 3 additions & 4 deletions src/kinetics/Kinetics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@
void Kinetics::checkReactionIndex(size_t i) const
{
if (i >= nReactions()) {
throw IndexError("Kinetics::checkReactionIndex", "reactions", i,
nReactions()-1);
throw IndexError("Kinetics::checkReactionIndex", "reactions", i, nReactions());
}
}

Expand Down Expand Up @@ -62,7 +61,7 @@
void Kinetics::checkPhaseIndex(size_t m) const
{
if (m >= nPhases()) {
throw IndexError("Kinetics::checkPhaseIndex", "phase", m, nPhases()-1);
throw IndexError("Kinetics::checkPhaseIndex", "phase", m, nPhases());

Check warning on line 64 in src/kinetics/Kinetics.cpp

View check run for this annotation

Codecov / codecov/patch

src/kinetics/Kinetics.cpp#L64

Added line #L64 was not covered by tests
}
}

Expand All @@ -81,7 +80,7 @@
void Kinetics::checkSpeciesIndex(size_t k) const
{
if (k >= m_kk) {
throw IndexError("Kinetics::checkSpeciesIndex", "species", k, m_kk-1);
throw IndexError("Kinetics::checkSpeciesIndex", "species", k, m_kk);

Check warning on line 83 in src/kinetics/Kinetics.cpp

View check run for this annotation

Codecov / codecov/patch

src/kinetics/Kinetics.cpp#L83

Added line #L83 was not covered by tests
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/thermo/Phase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
void Phase::checkElementIndex(size_t m) const
{
if (m >= m_mm) {
throw IndexError("Phase::checkElementIndex", "elements", m, m_mm-1);
throw IndexError("Phase::checkElementIndex", "elements", m, m_mm);

Check warning on line 38 in src/thermo/Phase.cpp

View check run for this annotation

Codecov / codecov/patch

src/thermo/Phase.cpp#L38

Added line #L38 was not covered by tests
}
}

Expand Down Expand Up @@ -153,7 +153,7 @@
void Phase::checkSpeciesIndex(size_t k) const
{
if (k >= m_kk) {
throw IndexError("Phase::checkSpeciesIndex", "species", k, m_kk-1);
throw IndexError("Phase::checkSpeciesIndex", "species", k, m_kk);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/transport/Transport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace Cantera
void Transport::checkSpeciesIndex(size_t k) const
{
if (k >= m_nsp) {
throw IndexError("Transport::checkSpeciesIndex", "species", k, m_nsp-1);
throw IndexError("Transport::checkSpeciesIndex", "species", k, m_nsp);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/zeroD/ReactorNet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@
}
if (p >= m_sens_params.size()) {
throw IndexError("ReactorNet::sensitivity",
"m_sens_params", p, m_sens_params.size()-1);
"m_sens_params", p, m_sens_params.size());

Check warning on line 406 in src/zeroD/ReactorNet.cpp

View check run for this annotation

Codecov / codecov/patch

src/zeroD/ReactorNet.cpp#L406

Added line #L406 was not covered by tests
}
double denom = m_integ->solution(k);
if (denom == 0.0) {
Expand Down
Loading