Commit 84f0be73ca6cfa6f9f49ca41575fed7dd420ab66
1 parent
29221874
Do not require setup in const operator[] access and add unit test for setup requ…
…est in operator[] access.
Showing
3 changed files
with
25 additions
and
3 deletions
include/GNodeArray.hpp
| ... | ... | @@ -133,7 +133,9 @@ private: |
| 133 | 133 | * @return Node value. |
| 134 | 134 | * |
| 135 | 135 | * Returns a reference to the node with the specified @p index. No range |
| 136 | - * checking is performed on @p index. | |
| 136 | + * checking is performed on @p index. As this operator may change the | |
| 137 | + * values of the node array, the setup method needs to be called before | |
| 138 | + * doing the interpolation. | |
| 137 | 139 | ***************************************************************************/ |
| 138 | 140 | inline |
| 139 | 141 | double& GNodeArray::operator[](const int& index) |
| ... | ... | @@ -155,7 +157,6 @@ double& GNodeArray::operator[](const int& index) |
| 155 | 157 | inline |
| 156 | 158 | const double& GNodeArray::operator[](const int& index) const |
| 157 | 159 | { |
| 158 | - m_need_setup = true; | |
| 159 | 160 | return (m_node[index]); |
| 160 | 161 | } |
| 161 | 162 | ... | ... |
src/support/GNodeArray.cpp
| ... | ... | @@ -550,6 +550,9 @@ double GNodeArray::interpolate(const double& value, |
| 550 | 550 | * form (i.e. the nodes are equidistant), an analytic formula is used to |
| 551 | 551 | * determine the boundary indices. If the nodes are not equidistant the |
| 552 | 552 | * boundary indices are searched by bisection. |
| 553 | + * | |
| 554 | + * Note that this method needs to be called after changing the node array | |
| 555 | + * to setup the corrected interpolation indices and weights. | |
| 553 | 556 | ***************************************************************************/ |
| 554 | 557 | void GNodeArray::set_value(const double& value) const |
| 555 | 558 | { | ... | ... |
test/test_GSupport.cpp
| 1 | 1 | /*************************************************************************** |
| 2 | 2 | * test_GSupport.cpp - test support module * |
| 3 | 3 | * ----------------------------------------------------------------------- * |
| 4 | - * copyright (C) 2010-2013 by Juergen Knoedlseder * | |
| 4 | + * copyright (C) 2010-2014 by Juergen Knoedlseder * | |
| 5 | 5 | * ----------------------------------------------------------------------- * |
| 6 | 6 | * * |
| 7 | 7 | * This program is free software: you can redistribute it and/or modify * |
| ... | ... | @@ -308,6 +308,24 @@ void TestGSupport::test_node_array(void) |
| 308 | 308 | double array_nonlin[] = {-1.3, 0.0, 1.7}; |
| 309 | 309 | test_node_array_interpolation(3, array_nonlin); |
| 310 | 310 | |
| 311 | + // Test node array change using access operator[] | |
| 312 | + GNodeArray nodes; | |
| 313 | + nodes.append(1.0); | |
| 314 | + nodes.append(2.0); | |
| 315 | + nodes.append(3.0); | |
| 316 | + nodes.set_value(2.5); | |
| 317 | + test_value(nodes.inx_left(), 1, "Expected node 1"); | |
| 318 | + test_value(nodes.inx_right(), 2, "Expected node 2"); | |
| 319 | + test_value(nodes.wgt_left(), 0.5, 1.0e-6, "Expected weight 0.5"); | |
| 320 | + test_value(nodes.wgt_right(), 0.5, 1.0e-6, "Expected weight 0.5"); | |
| 321 | + nodes[1] = 3.0; | |
| 322 | + nodes[2] = 4.0; | |
| 323 | + nodes.set_value(2.5); | |
| 324 | + test_value(nodes.inx_left(), 0, "Expected node 0"); | |
| 325 | + test_value(nodes.inx_right(), 1, "Expected node 1"); | |
| 326 | + test_value(nodes.wgt_left(), 0.25, 1.0e-6, "Expected weight 0.25"); | |
| 327 | + test_value(nodes.wgt_right(), 0.75, 1.0e-6, "Expected weight 0.75"); | |
| 328 | + | |
| 311 | 329 | // Return |
| 312 | 330 | return; |
| 313 | 331 | } | ... | ... |