Changeset a7d753
- Timestamp:
- Oct 17, 2011, 2:36:47 PM (13 years ago)
- 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)
- Location:
- src/Parser
- Files:
-
- 5 added
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
src/Parser/DiscreteValue.hpp
r3308b6 ra7d753 17 17 #include <vector> 18 18 19 #include "CodePatterns/toString.hpp" 20 21 #include "ValueInterface.hpp" 22 23 class DiscreteValueTest; 24 19 25 /** This class represents a discrete value. 20 26 * 21 27 */ 22 28 template <class T> 23 class DiscreteValue 29 class DiscreteValue : public ValueInterface 24 30 { 25 31 //!> unit test needs to have access to internal values … … 31 37 32 38 // 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); 34 42 35 // init functions43 // setter/getter for valid values 36 44 void appendValidValue(const T &_value); 37 45 const std::vector<T> &getValidValues() const; 38 46 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; 44 51 const size_t getIndexOfValue() const { return value; } 45 52 46 53 private: 47 54 const size_t findIndexOfValue(const T &_value) const; 55 56 private: 57 //!> Internal converter from string to internal type 58 static ConvertTo<T> Converter; 48 59 49 60 //!> Typedef for the vector of valid values. -
src/Parser/DiscreteValue_impl.hpp
r3308b6 ra7d753 14 14 #endif 15 15 16 #include <algorithm> 16 17 #include <vector> 17 18 19 #include <boost/any.hpp> 20 18 21 #include "CodePatterns/Assert.hpp" 22 23 #include "CodePatterns/Log.hpp" 24 25 // static member 26 template <class T> ConvertTo<T> DiscreteValue<T>::Converter; 19 27 20 28 /** Constructor of class DiscreteValue. … … 25 33 {} 26 34 27 /** Constructor of class DiscreteValue. 35 /** Constructor of class DiscreteValue with set of valid values. 36 * 37 * @param _ValidValues vector with all valid values 28 38 */ 29 39 template <class T> … … 44 54 */ 45 55 template <class T> 46 bool DiscreteValue<T>::isValid(const T &_value) const56 bool DiscreteValue<T>::isValid(const std::string _value) const 47 57 { 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); 53 60 } 61 62 /** Getter of value, returning string. 63 * 64 * @return string value 65 */ 66 template <class T> 67 const 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 */ 78 template <class T> 79 void DiscreteValue<T>::set(const std::string _value) 80 { 81 const T castvalue = Converter(_value); 82 setValue(castvalue); 83 } 84 54 85 55 86 /** Internal function for finding the index of a desired value. … … 85 116 void DiscreteValue<T>::appendValidValue(const T &_value) 86 117 { 87 ASSERT(!isValid (_value),118 ASSERT(!isValidValue(_value), 88 119 "DiscreteValue<>::appendValidValue() - value "+toString(_value)+" is already among the valid"); 89 120 ValidValues.push_back(_value); … … 107 138 */ 108 139 template <class T> 109 void DiscreteValue<T>::set (const T &_value)140 void DiscreteValue<T>::setValue(const T &_value) 110 141 { 111 142 const size_t index = findIndexOfValue(_value); … … 117 148 } 118 149 119 /** Getter fo tthe set value.150 /** Getter for the set value. 120 151 * 121 152 * We check whether it has been set, otherwise we throw an Assert::AssertionFailure. … … 124 155 */ 125 156 template <class T> 126 const T & DiscreteValue<T>::get () const157 const T & DiscreteValue<T>::getValue() const 127 158 { 128 159 ASSERT(ValueSet, … … 131 162 } 132 163 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 */ 168 template <class T> 169 bool 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 } 133 180 134 181 #endif /* DISCRETEVALUE_IMPL_HPP_ */ -
src/Parser/unittests/DiscreteValueUnitTest.cpp
r3308b6 ra7d753 26 26 #include "Parser/DiscreteValue.hpp" 27 27 28 #include "CodePatterns/toString.hpp" 29 28 30 #ifdef HAVE_TESTRUNNER 29 31 #include "UnitTestMain.hpp" … … 73 75 } 74 76 75 /** Unit test for isValid .76 * 77 */ 78 void DiscreteValueTest::isValid Test()77 /** Unit test for isValidValue. 78 * 79 */ 80 void DiscreteValueTest::isValidValueTest() 79 81 { 80 82 // create instance … … 83 85 // checking valid values 84 86 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)); 90 92 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 */ 99 void 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))); 92 113 } 93 114 … … 102 123 // adding values 4,5,6 103 124 for (int i=4; i<=6;++i) { 104 CPPUNIT_ASSERT_EQUAL(false, test.isValid (i));125 CPPUNIT_ASSERT_EQUAL(false, test.isValidValue(i)); 105 126 test.appendValidValue(i); 106 CPPUNIT_ASSERT_EQUAL(true, test.isValid (i));127 CPPUNIT_ASSERT_EQUAL(true, test.isValidValue(i)); 107 128 } 108 129 109 130 // adding same value, throws assertion 131 const size_t size_before = test.ValidValues.size(); 110 132 #ifndef NDEBUG 111 133 std::cout << "The following Assert failures are intended and do not indicate a failure of the test." << std::endl; … … 113 135 CPPUNIT_ASSERT_THROW(test.appendValidValue(i), Assert::AssertionFailure); 114 136 #endif 137 CPPUNIT_ASSERT_EQUAL( size_before, test.ValidValues.size() ); 115 138 116 139 // checking valid values 117 140 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)); 123 146 124 147 // checking invalid values 125 148 for (int i=7; i<=10;++i) 126 CPPUNIT_ASSERT_EQUAL(false, test.isValid (i));149 CPPUNIT_ASSERT_EQUAL(false, test.isValidValue(i)); 127 150 } 128 151 … … 144 167 #ifndef NDEBUG 145 168 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); 151 174 #endif 152 175 153 176 // checking all valid ones 154 177 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 */ 187 void 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 14 14 15 15 #include <cppunit/extensions/HelperMacros.h> 16 #include <string> 16 17 #include <vector> 17 18 18 19 class DiscreteValueTest : public CppUnit::TestFixture … … 20 21 CPPUNIT_TEST_SUITE( DiscreteValueTest ) ; 21 22 CPPUNIT_TEST ( findIndexOfValueTest ); 23 CPPUNIT_TEST ( isValidValueTest ); 22 24 CPPUNIT_TEST ( isValidTest ); 23 25 CPPUNIT_TEST ( appendValidValueTest ); 24 26 CPPUNIT_TEST ( settergetterTest ); 27 CPPUNIT_TEST ( settergetterValueTest ); 25 28 CPPUNIT_TEST_SUITE_END(); 26 29 … … 30 33 31 34 void findIndexOfValueTest(); 35 void isValidValueTest(); 32 36 void isValidTest(); 33 37 void appendValidValueTest(); 34 38 void settergetterTest(); 39 void settergetterValueTest(); 35 40 36 41 private: -
src/Parser/unittests/Makefile.am
r3308b6 ra7d753 4 4 5 5 PARSERTESTSSOURCES = \ 6 ../Parser/unittests/ContinuousValueUnitTest.cpp \ 6 7 ../Parser/unittests/DiscreteValueUnitTest.cpp \ 7 8 ../Parser/unittests/ParserMpqcUnitTest.cpp \ … … 12 13 13 14 PARSERTESTSHEADERS = \ 15 ../Parser/unittests/ContinuousValueUnitTest.hpp \ 14 16 ../Parser/unittests/DiscreteValueUnitTest.hpp \ 15 17 ../Parser/unittests/ParserMpqcUnitTest.hpp \ … … 20 22 21 23 PARSERTESTS = \ 24 ContinuousValueUnitTest \ 22 25 DiscreteValueUnitTest \ 23 26 ParserMpqcUnitTest \ … … 38 41 39 42 43 ContinuousValueUnitTest_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 40 50 DiscreteValueUnitTest_SOURCES = $(top_srcdir)/src/unittests/UnitTestMain.cpp \ 41 51 ../Parser/unittests/DiscreteValueUnitTest.cpp \
Note:
See TracChangeset
for help on using the changeset viewer.