From 84f0be73ca6cfa6f9f49ca41575fed7dd420ab66 Mon Sep 17 00:00:00 2001 From: Jürgen Knödlseder Date: Mon, 21 Jul 2014 21:31:59 +0200 Subject: [PATCH] Do not require setup in const operator[] access and add unit test for setup request in operator[] access. --- include/GNodeArray.hpp | 5 +++-- src/support/GNodeArray.cpp | 3 +++ test/test_GSupport.cpp | 20 +++++++++++++++++++- 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/include/GNodeArray.hpp b/include/GNodeArray.hpp index ff534ba..548589f 100644 --- a/include/GNodeArray.hpp +++ b/include/GNodeArray.hpp @@ -133,7 +133,9 @@ private: * @return Node value. * * Returns a reference to the node with the specified @p index. No range - * checking is performed on @p index. + * checking is performed on @p index. As this operator may change the + * values of the node array, the setup method needs to be called before + * doing the interpolation. ***************************************************************************/ inline double& GNodeArray::operator[](const int& index) @@ -155,7 +157,6 @@ double& GNodeArray::operator[](const int& index) inline const double& GNodeArray::operator[](const int& index) const { - m_need_setup = true; return (m_node[index]); } diff --git a/src/support/GNodeArray.cpp b/src/support/GNodeArray.cpp index c7a1e40..e54248e 100644 --- a/src/support/GNodeArray.cpp +++ b/src/support/GNodeArray.cpp @@ -550,6 +550,9 @@ double GNodeArray::interpolate(const double& value, * form (i.e. the nodes are equidistant), an analytic formula is used to * determine the boundary indices. If the nodes are not equidistant the * boundary indices are searched by bisection. + * + * Note that this method needs to be called after changing the node array + * to setup the corrected interpolation indices and weights. ***************************************************************************/ void GNodeArray::set_value(const double& value) const { diff --git a/test/test_GSupport.cpp b/test/test_GSupport.cpp index d34ba3e..80d3f37 100644 --- a/test/test_GSupport.cpp +++ b/test/test_GSupport.cpp @@ -1,7 +1,7 @@ /*************************************************************************** * test_GSupport.cpp - test support module * * ----------------------------------------------------------------------- * - * copyright (C) 2010-2013 by Juergen Knoedlseder * + * copyright (C) 2010-2014 by Juergen Knoedlseder * * ----------------------------------------------------------------------- * * * * This program is free software: you can redistribute it and/or modify * @@ -308,6 +308,24 @@ void TestGSupport::test_node_array(void) double array_nonlin[] = {-1.3, 0.0, 1.7}; test_node_array_interpolation(3, array_nonlin); + // Test node array change using access operator[] + GNodeArray nodes; + nodes.append(1.0); + nodes.append(2.0); + nodes.append(3.0); + nodes.set_value(2.5); + test_value(nodes.inx_left(), 1, "Expected node 1"); + test_value(nodes.inx_right(), 2, "Expected node 2"); + test_value(nodes.wgt_left(), 0.5, 1.0e-6, "Expected weight 0.5"); + test_value(nodes.wgt_right(), 0.5, 1.0e-6, "Expected weight 0.5"); + nodes[1] = 3.0; + nodes[2] = 4.0; + nodes.set_value(2.5); + test_value(nodes.inx_left(), 0, "Expected node 0"); + test_value(nodes.inx_right(), 1, "Expected node 1"); + test_value(nodes.wgt_left(), 0.25, 1.0e-6, "Expected weight 0.25"); + test_value(nodes.wgt_right(), 0.75, 1.0e-6, "Expected weight 0.75"); + // Return return; } -- libgit2 0.21.4