Changeset a7d753


Ignore:
Timestamp:
Oct 17, 2011, 2:36:47 PM (13 years ago)
Author:
Frederik Heber <heber@…>
Branches:
Action_Thermostats, Add_AtomRandomPerturbation, Add_FitFragmentPartialChargesAction, Add_RotateAroundBondAction, Add_SelectAtomByNameAction, Added_ParseSaveFragmentResults, AddingActions_SaveParseParticleParameters, Adding_Graph_to_ChangeBondActions, Adding_MD_integration_tests, Adding_ParticleName_to_Atom, Adding_StructOpt_integration_tests, AtomFragments, Automaking_mpqc_open, AutomationFragmentation_failures, Candidate_v1.5.4, Candidate_v1.6.0, Candidate_v1.6.1, ChangeBugEmailaddress, ChangingTestPorts, ChemicalSpaceEvaluator, CombiningParticlePotentialParsing, Combining_Subpackages, Debian_Package_split, Debian_package_split_molecuildergui_only, Disabling_MemDebug, Docu_Python_wait, EmpiricalPotential_contain_HomologyGraph, EmpiricalPotential_contain_HomologyGraph_documentation, Enable_parallel_make_install, Enhance_userguide, Enhanced_StructuralOptimization, Enhanced_StructuralOptimization_continued, Example_ManyWaysToTranslateAtom, Exclude_Hydrogens_annealWithBondGraph, FitPartialCharges_GlobalError, Fix_BoundInBox_CenterInBox_MoleculeActions, Fix_ChargeSampling_PBC, Fix_ChronosMutex, Fix_FitPartialCharges, Fix_FitPotential_needs_atomicnumbers, Fix_ForceAnnealing, Fix_IndependentFragmentGrids, Fix_ParseParticles, Fix_ParseParticles_split_forward_backward_Actions, Fix_PopActions, Fix_QtFragmentList_sorted_selection, Fix_Restrictedkeyset_FragmentMolecule, Fix_StatusMsg, Fix_StepWorldTime_single_argument, Fix_Verbose_Codepatterns, Fix_fitting_potentials, Fixes, ForceAnnealing_goodresults, ForceAnnealing_oldresults, ForceAnnealing_tocheck, ForceAnnealing_with_BondGraph, ForceAnnealing_with_BondGraph_continued, ForceAnnealing_with_BondGraph_continued_betteresults, ForceAnnealing_with_BondGraph_contraction-expansion, FragmentAction_writes_AtomFragments, FragmentMolecule_checks_bonddegrees, GeometryObjects, Gui_Fixes, Gui_displays_atomic_force_velocity, ImplicitCharges, IndependentFragmentGrids, IndependentFragmentGrids_IndividualZeroInstances, IndependentFragmentGrids_IntegrationTest, IndependentFragmentGrids_Sole_NN_Calculation, JobMarket_RobustOnKillsSegFaults, JobMarket_StableWorkerPool, JobMarket_unresolvable_hostname_fix, MoreRobust_FragmentAutomation, ODR_violation_mpqc_open, PartialCharges_OrthogonalSummation, PdbParser_setsAtomName, PythonUI_with_named_parameters, QtGui_reactivate_TimeChanged_changes, Recreated_GuiChecks, Rewrite_FitPartialCharges, RotateToPrincipalAxisSystem_UndoRedo, SaturateAtoms_findBestMatching, SaturateAtoms_singleDegree, StoppableMakroAction, Subpackage_CodePatterns, Subpackage_JobMarket, Subpackage_LinearAlgebra, Subpackage_levmar, Subpackage_mpqc_open, Subpackage_vmg, Switchable_LogView, ThirdParty_MPQC_rebuilt_buildsystem, TrajectoryDependenant_MaxOrder, TremoloParser_IncreasedPrecision, TremoloParser_MultipleTimesteps, TremoloParser_setsAtomName, Ubuntu_1604_changes, stable
Children:
84e752
Parents:
3308b6
git-author:
Frederik Heber <heber@…> (09/30/11 09:32:43)
git-committer:
Frederik Heber <heber@…> (10/17/11 14:36:47)
Message:

Extended DiscreteValue to ContinuousValue and added ValueInterface for both.

Location:
src/Parser
Files:
5 added
5 edited

Legend:

Unmodified
Added
Removed
  • src/Parser/DiscreteValue.hpp

    r3308b6 ra7d753  
    1717#include <vector>
    1818
     19#include "CodePatterns/toString.hpp"
     20
     21#include "ValueInterface.hpp"
     22
     23class DiscreteValueTest;
     24
    1925/** This class represents a discrete value.
    2026 *
    2127 */
    2228template <class T>
    23 class DiscreteValue
     29class DiscreteValue : public ValueInterface
    2430{
    2531  //!> unit test needs to have access to internal values
     
    3137
    3238  // functions for ValueInterface
    33   bool isValid(const T &_value) const;
     39  bool isValid(const std::string _value) const;
     40  const std::string get() const;
     41  void set(const std::string _value);
    3442
    35   // init functions
     43  // setter/getter for valid values
    3644  void appendValidValue(const T &_value);
    3745  const std::vector<T> &getValidValues() const;
    3846
    39   // getter and setter
    40   void set(const T &_value);
    41 
    42   const T & get() const;
    43 
     47  // internal getter and setter
     48  bool isValidValue(const T &_value) const;
     49  void setValue(const T &_value);
     50  const T & getValue() const;
    4451  const size_t getIndexOfValue() const { return value; }
    4552
    4653private:
    4754  const size_t findIndexOfValue(const T &_value) const;
     55
     56private:
     57  //!> Internal converter from string to internal type
     58  static ConvertTo<T> Converter;
    4859
    4960  //!> Typedef for the vector of valid values.
  • src/Parser/DiscreteValue_impl.hpp

    r3308b6 ra7d753  
    1414#endif
    1515
     16#include <algorithm>
    1617#include <vector>
    1718
     19#include <boost/any.hpp>
     20
    1821#include "CodePatterns/Assert.hpp"
     22
     23#include "CodePatterns/Log.hpp"
     24
     25// static member
     26template <class T> ConvertTo<T> DiscreteValue<T>::Converter;
    1927
    2028/** Constructor of class DiscreteValue.
     
    2533{}
    2634
    27 /** Constructor of class DiscreteValue.
     35/** Constructor of class DiscreteValue with set of valid values.
     36 *
     37 * @param _ValidValues vector with all valid values
    2838 */
    2939template <class T>
     
    4454 */
    4555template <class T>
    46 bool DiscreteValue<T>::isValid(const T &_value) const
     56bool DiscreteValue<T>::isValid(const std::string _value) const
    4757{
    48   const size_t index = findIndexOfValue(_value);
    49   if (index == (size_t)-1)
    50     return false;
    51   else
    52     return true;
     58  const T castvalue = Converter(_value);
     59  return isValidValue(castvalue);
    5360}
     61
     62/** Getter of value, returning string.
     63 *
     64 * @return string value
     65 */
     66template <class T>
     67const std::string DiscreteValue<T>::get() const
     68{
     69  ASSERT(ValueSet,
     70      "DiscreteValue<T>::get() - requesting unset value.");
     71  return toString(getValue());
     72}
     73
     74/** Setter of value for string
     75 *
     76 * @param _value string containing new value
     77 */
     78template <class T>
     79void DiscreteValue<T>::set(const std::string _value)
     80{
     81  const T castvalue = Converter(_value);
     82  setValue(castvalue);
     83}
     84
    5485
    5586/** Internal function for finding the index of a desired value.
     
    85116void DiscreteValue<T>::appendValidValue(const T &_value)
    86117{
    87   ASSERT(!isValid(_value),
     118  ASSERT(!isValidValue(_value),
    88119      "DiscreteValue<>::appendValidValue() - value "+toString(_value)+" is already among the valid");
    89120  ValidValues.push_back(_value);
     
    107138 */
    108139template <class T>
    109 void DiscreteValue<T>::set(const T &_value)
     140void DiscreteValue<T>::setValue(const T &_value)
    110141{
    111142  const size_t index = findIndexOfValue(_value);
     
    117148}
    118149
    119 /** Getter fot the set value.
     150/** Getter for the set value.
    120151 *
    121152 * We check whether it has been set, otherwise we throw an Assert::AssertionFailure.
     
    124155 */
    125156template <class T>
    126 const T & DiscreteValue<T>::get() const
     157const T & DiscreteValue<T>::getValue() const
    127158{
    128159  ASSERT(ValueSet,
     
    131162}
    132163
     164/** Checks whether \a _value is a valid value.
     165 * \param _value value to check for validity.
     166 * \return true - \a _value is valid, false - is not
     167 */
     168template <class T>
     169bool DiscreteValue<T>::isValidValue(const T &_value) const
     170{
     171  typename ValidRange::const_iterator iter = std::find(ValidValues.begin(), ValidValues.end(), _value);
     172  if (iter != ValidValues.end()) {
     173    //std::cout << "Found " << _value << ":" << *iter << std::endl;
     174    return true;
     175  } else {
     176    //std::cout << "Did not find " << _value << "." << std::endl;
     177    return false;
     178  }
     179}
    133180
    134181#endif /* DISCRETEVALUE_IMPL_HPP_ */
  • src/Parser/unittests/DiscreteValueUnitTest.cpp

    r3308b6 ra7d753  
    2626#include "Parser/DiscreteValue.hpp"
    2727
     28#include "CodePatterns/toString.hpp"
     29
    2830#ifdef HAVE_TESTRUNNER
    2931#include "UnitTestMain.hpp"
     
    7375}
    7476
    75 /** Unit test for isValid.
    76  *
    77  */
    78 void DiscreteValueTest::isValidTest()
     77/** Unit test for isValidValue.
     78 *
     79 */
     80void DiscreteValueTest::isValidValueTest()
    7981{
    8082  // create instance
     
    8385  // checking valid values
    8486  for (int i=1; i<=3;++i)
    85     CPPUNIT_ASSERT_EQUAL(true, test.isValid(i));
    86 
    87   // checking invalid values
    88   for (int i=-10; i<=0;++i)
    89     CPPUNIT_ASSERT_EQUAL(false, test.isValid(i));
     87    CPPUNIT_ASSERT_EQUAL(true, test.isValidValue(i));
     88
     89  // checking invalid values
     90  for (int i=-10; i<=0;++i)
     91    CPPUNIT_ASSERT_EQUAL(false, test.isValidValue(i));
    9092  for (int i=4; i<=0;++i)
    91     CPPUNIT_ASSERT_EQUAL(false, test.isValid(i));
     93    CPPUNIT_ASSERT_EQUAL(false, test.isValidValue(i));
     94}
     95
     96/** Unit test for isValid.
     97 *
     98 */
     99void DiscreteValueTest::isValidTest()
     100{
     101  // create instance
     102  DiscreteValue<int> test(ValidValues);
     103
     104  // checking valid values
     105  for (int i=1; i<=3;++i)
     106    CPPUNIT_ASSERT_EQUAL(true, test.isValid(toString(i)));
     107
     108  // checking invalid values
     109  for (int i=-10; i<=0;++i)
     110    CPPUNIT_ASSERT_EQUAL(false, test.isValid(toString(i)));
     111  for (int i=4; i<=0;++i)
     112    CPPUNIT_ASSERT_EQUAL(false, test.isValid(toString(i)));
    92113}
    93114
     
    102123  // adding values 4,5,6
    103124  for (int i=4; i<=6;++i) {
    104     CPPUNIT_ASSERT_EQUAL(false, test.isValid(i));
     125    CPPUNIT_ASSERT_EQUAL(false, test.isValidValue(i));
    105126    test.appendValidValue(i);
    106     CPPUNIT_ASSERT_EQUAL(true, test.isValid(i));
     127    CPPUNIT_ASSERT_EQUAL(true, test.isValidValue(i));
    107128  }
    108129
    109130  // adding same value, throws assertion
     131  const size_t size_before = test.ValidValues.size();
    110132#ifndef NDEBUG
    111133  std::cout << "The following Assert failures are intended and do not indicate a failure of the test." << std::endl;
     
    113135    CPPUNIT_ASSERT_THROW(test.appendValidValue(i), Assert::AssertionFailure);
    114136#endif
     137  CPPUNIT_ASSERT_EQUAL( size_before, test.ValidValues.size() );
    115138
    116139  // checking valid values
    117140  for (int i=1; i<=6;++i)
    118     CPPUNIT_ASSERT_EQUAL(true, test.isValid(i));
    119 
    120   // checking invalid values
    121   for (int i=-10; i<=0;++i)
    122     CPPUNIT_ASSERT_EQUAL(false, test.isValid(i));
     141    CPPUNIT_ASSERT_EQUAL(true, test.isValidValue(i));
     142
     143  // checking invalid values
     144  for (int i=-10; i<=0;++i)
     145    CPPUNIT_ASSERT_EQUAL(false, test.isValidValue(i));
    123146
    124147  // checking invalid values
    125148  for (int i=7; i<=10;++i)
    126     CPPUNIT_ASSERT_EQUAL(false, test.isValid(i));
     149    CPPUNIT_ASSERT_EQUAL(false, test.isValidValue(i));
    127150}
    128151
     
    144167#ifndef NDEBUG
    145168  std::cout << "The following Assert failures are intended and do not indicate a failure of the test." << std::endl;
    146   CPPUNIT_ASSERT_THROW(test.set(4), Assert::AssertionFailure);
    147 #endif
    148 #ifndef NDEBUG
    149   std::cout << "The following Assert failures are intended and do not indicate a failure of the test." << std::endl;
    150   CPPUNIT_ASSERT_THROW(test.set(0), Assert::AssertionFailure);
     169  CPPUNIT_ASSERT_THROW(test.set(toString(4)), Assert::AssertionFailure);
     170#endif
     171#ifndef NDEBUG
     172  std::cout << "The following Assert failures are intended and do not indicate a failure of the test." << std::endl;
     173  CPPUNIT_ASSERT_THROW(test.set(toString(0)), Assert::AssertionFailure);
    151174#endif
    152175
    153176  // checking all valid ones
    154177  for (int i=1; i<=3;++i) {
    155     test.set(i);
    156     CPPUNIT_ASSERT_EQUAL(i, test.get());
    157   }
    158 }
    159 
    160 
    161 
     178    test.set(toString(i));
     179    CPPUNIT_ASSERT_EQUAL(toString(i), test.get());
     180  }
     181
     182}
     183
     184/** Unit test for setValue and getValue.
     185 *
     186 */
     187void DiscreteValueTest::settergetterValueTest()
     188{
     189  // create instance
     190  DiscreteValue<int> test(ValidValues);
     191
     192  // unset calling of get, throws
     193#ifndef NDEBUG
     194  std::cout << "The following Assert failures are intended and do not indicate a failure of the test." << std::endl;
     195  CPPUNIT_ASSERT_THROW(test.getValue(), Assert::AssertionFailure);
     196#endif
     197
     198  // setting invalid, throws
     199#ifndef NDEBUG
     200  std::cout << "The following Assert failures are intended and do not indicate a failure of the test." << std::endl;
     201  CPPUNIT_ASSERT_THROW(test.setValue(4), Assert::AssertionFailure);
     202#endif
     203#ifndef NDEBUG
     204  std::cout << "The following Assert failures are intended and do not indicate a failure of the test." << std::endl;
     205  CPPUNIT_ASSERT_THROW(test.setValue(0), Assert::AssertionFailure);
     206#endif
     207
     208  // checking all valid ones
     209  for (int i=1; i<=3;++i) {
     210    test.setValue(i);
     211    CPPUNIT_ASSERT_EQUAL(i, test.getValue());
     212  }
     213}
  • src/Parser/unittests/DiscreteValueUnitTest.hpp

    r3308b6 ra7d753  
    1414
    1515#include <cppunit/extensions/HelperMacros.h>
    16 #include <string>
     16
     17#include <vector>
    1718
    1819class DiscreteValueTest :  public CppUnit::TestFixture
     
    2021  CPPUNIT_TEST_SUITE( DiscreteValueTest ) ;
    2122  CPPUNIT_TEST ( findIndexOfValueTest );
     23  CPPUNIT_TEST ( isValidValueTest );
    2224  CPPUNIT_TEST ( isValidTest );
    2325  CPPUNIT_TEST ( appendValidValueTest );
    2426  CPPUNIT_TEST ( settergetterTest );
     27  CPPUNIT_TEST ( settergetterValueTest );
    2528  CPPUNIT_TEST_SUITE_END();
    2629
     
    3033
    3134  void findIndexOfValueTest();
     35  void isValidValueTest();
    3236  void isValidTest();
    3337  void appendValidValueTest();
    3438  void settergetterTest();
     39  void settergetterValueTest();
    3540
    3641private:
  • src/Parser/unittests/Makefile.am

    r3308b6 ra7d753  
    44
    55PARSERTESTSSOURCES = \
     6  ../Parser/unittests/ContinuousValueUnitTest.cpp \
    67  ../Parser/unittests/DiscreteValueUnitTest.cpp \
    78        ../Parser/unittests/ParserMpqcUnitTest.cpp \
     
    1213
    1314PARSERTESTSHEADERS = \
     15  ../Parser/unittests/ContinuousValueUnitTest.hpp \
    1416  ../Parser/unittests/DiscreteValueUnitTest.hpp \
    1517        ../Parser/unittests/ParserMpqcUnitTest.hpp \
     
    2022
    2123PARSERTESTS = \
     24        ContinuousValueUnitTest \
    2225  DiscreteValueUnitTest \
    2326  ParserMpqcUnitTest \
     
    3841
    3942
     43ContinuousValueUnitTest_SOURCES = $(top_srcdir)/src/unittests/UnitTestMain.cpp \
     44  ../Parser/unittests/ContinuousValueUnitTest.cpp \
     45  ../Parser/unittests/ContinuousValueUnitTest.hpp \
     46  ../Parser/ContinuousValue.hpp \
     47  ../Parser/ContinuousValue_impl.hpp
     48#ContinuousValueUnitTest_LDADD
     49 
    4050DiscreteValueUnitTest_SOURCES = $(top_srcdir)/src/unittests/UnitTestMain.cpp \
    4151  ../Parser/unittests/DiscreteValueUnitTest.cpp \
Note: See TracChangeset for help on using the changeset viewer.