source: src/Parameters/unittests/DiscreteValueTest.cpp@ e45c1d

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
Last change on this file since e45c1d was e45c1d, checked in by Frederik Heber <heber@…>, 13 years ago

Added ParameterExceptions, is caught by Dialog::checkAll() for the moment.

  • Property mode set to 100644
File size: 6.8 KB
Line 
1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
4 * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
5 * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
6 */
7
8/*
9 * DiscreteValueTest.cpp
10 *
11 * Created on: Sep 28, 2011
12 * Author: heber
13 */
14
15// include config.h
16#ifdef HAVE_CONFIG_H
17#include <config.h>
18#endif
19
20#include "DiscreteValueTest.hpp"
21
22#include <cppunit/CompilerOutputter.h>
23#include <cppunit/extensions/TestFactoryRegistry.h>
24#include <cppunit/ui/text/TestRunner.h>
25
26#include "Parameters/ParameterExceptions.hpp"
27#include "Parameters/Value.hpp"
28
29#ifdef HAVE_TESTRUNNER
30#include "UnitTestMain.hpp"
31#endif /*HAVE_TESTRUNNER*/
32
33using namespace std;
34
35// Registers the fixture into the 'registry'
36CPPUNIT_TEST_SUITE_REGISTRATION( DiscreteValueTest );
37
38
39void DiscreteValueTest::setUp()
40{
41 // failing asserts should be thrown
42 ASSERT_DO(Assert::Throw);
43
44 for (int i=1; i<=3;++i) {
45 ValidValues.push_back(i);
46 }
47}
48
49void DiscreteValueTest::tearDown()
50{
51 ValidValues.clear();
52}
53
54/************************************ tests ***********************************/
55
56/** Unit test for findIndexOfValue.
57 *
58 */
59void DiscreteValueTest::findIndexOfValueTest()
60{
61 // create instance
62 Value<int> test(ValidValues);
63
64 // check valid values indices
65 CPPUNIT_ASSERT_EQUAL((size_t)0, dynamic_cast<DiscreteValidator<int> &>(test.getValidator()).findIndexOfValue(1));
66 CPPUNIT_ASSERT_EQUAL((size_t)1, dynamic_cast<DiscreteValidator<int> &>(test.getValidator()).findIndexOfValue(2));
67 CPPUNIT_ASSERT_EQUAL((size_t)2, dynamic_cast<DiscreteValidator<int> &>(test.getValidator()).findIndexOfValue(3));
68
69 // check invalid ones
70 for (int i=-10; i<=0;++i)
71 CPPUNIT_ASSERT_EQUAL((size_t)-1, dynamic_cast<DiscreteValidator<int> &>(test.getValidator()).findIndexOfValue(i));
72 for (int i=4; i<=0;++i)
73 CPPUNIT_ASSERT_EQUAL((size_t)-1, dynamic_cast<DiscreteValidator<int> &>(test.getValidator()).findIndexOfValue(i));
74}
75
76/** Unit test for isValidValue.
77 *
78 */
79void DiscreteValueTest::isValidAsStringTest()
80{
81 // create instance
82 Value<int> test(ValidValues);
83
84 // checking valid values
85 for (int i=1; i<=3;++i)
86 CPPUNIT_ASSERT_EQUAL(true, test.isValidAsString(toString(i)));
87
88 // checking invalid values
89 for (int i=-10; i<=0;++i)
90 CPPUNIT_ASSERT_EQUAL(false, test.isValidAsString(toString(i)));
91 for (int i=4; i<=0;++i)
92 CPPUNIT_ASSERT_EQUAL(false, test.isValidAsString(toString(i)));
93}
94
95/** Unit test for isValid.
96 *
97 */
98void DiscreteValueTest::isValidTest()
99{
100 // create instance
101 Value<int> test(ValidValues);
102
103 // checking valid values
104 for (int i=1; i<=3;++i)
105 CPPUNIT_ASSERT_EQUAL(true, test.isValid(i));
106
107 // checking invalid values
108 for (int i=-10; i<=0;++i)
109 CPPUNIT_ASSERT_EQUAL(false, test.isValid(i));
110 for (int i=4; i<=0;++i)
111 CPPUNIT_ASSERT_EQUAL(false, test.isValid(i));
112}
113
114/** Unit test for appendValidValue.
115 *
116 */
117void DiscreteValueTest::appendValidValueTest()
118{
119 // create instance
120 Value<int> test(ValidValues);
121
122 // adding values 4,5,6
123 for (int i=4; i<=6;++i) {
124 CPPUNIT_ASSERT_EQUAL(false, test.isValid(i));
125 dynamic_cast<DiscreteValidator<int> &>(test.getValidator()).appendValidValue(i);
126 CPPUNIT_ASSERT_EQUAL(true, test.isValid(i));
127 }
128
129 // adding same value, throws assertion
130 const size_t size_before = dynamic_cast<DiscreteValidator<int> &>(test.getValidator()).getValidValues().size();
131 std::cout << "The following Assert failures are intended and do not indicate a failure of the test." << std::endl;
132 for (int i=1; i<=6;++i)
133 CPPUNIT_ASSERT_THROW(dynamic_cast<DiscreteValidator<int> &>(test.getValidator()).appendValidValue(i), ParameterValidatorException);
134 CPPUNIT_ASSERT_EQUAL( size_before, dynamic_cast<DiscreteValidator<int> &>(test.getValidator()).getValidValues().size() );
135
136 // checking valid values
137 for (int i=1; i<=6;++i)
138 CPPUNIT_ASSERT_EQUAL(true, test.isValid(i));
139
140 // checking invalid values
141 for (int i=-10; i<=0;++i)
142 CPPUNIT_ASSERT_EQUAL(false, test.isValid(i));
143
144 // checking invalid values
145 for (int i=7; i<=10;++i)
146 CPPUNIT_ASSERT_EQUAL(false, test.isValid(i));
147}
148
149/** Unit test for setters and getters.
150 *
151 */
152void DiscreteValueTest::settergetterTest()
153{
154 // create instance
155 Value<int> test(ValidValues);
156
157 // unset calling of get, throws
158 std::cout << "The following Assert failures are intended and do not indicate a failure of the test." << std::endl;
159 CPPUNIT_ASSERT_THROW(test.get(), ParameterValueException);
160
161 // setting invalid, throws
162 std::cout << "The following Assert failures are intended and do not indicate a failure of the test." << std::endl;
163 CPPUNIT_ASSERT_THROW(test.set(4), ParameterValueException);
164 std::cout << "The following Assert failures are intended and do not indicate a failure of the test." << std::endl;
165 CPPUNIT_ASSERT_THROW(test.set(0), ParameterValueException);
166
167 // checking all valid ones
168 for (int i=1; i<=3;++i) {
169 test.set(i);
170 CPPUNIT_ASSERT_EQUAL(i, test.get());
171 }
172
173}
174
175/** Unit test for setValue and getValue.
176 *
177 */
178void DiscreteValueTest::settergetterAsStringTest()
179{
180 // create instance
181 Value<int> test(ValidValues);
182
183 // unset calling of get, throws
184 std::cout << "The following Assert failures are intended and do not indicate a failure of the test." << std::endl;
185 CPPUNIT_ASSERT_THROW(test.getAsString(), ParameterValueException);
186
187 // setting invalid, throws
188 std::cout << "The following Assert failures are intended and do not indicate a failure of the test." << std::endl;
189 CPPUNIT_ASSERT_THROW(test.setAsString(toString(4)), ParameterValueException);
190 std::cout << "The following Assert failures are intended and do not indicate a failure of the test." << std::endl;
191 CPPUNIT_ASSERT_THROW(test.setAsString(toString(0)), ParameterValueException);
192
193 // checking all valid ones
194 for (int i=1; i<=3;++i) {
195 test.setAsString(toString(i));
196 CPPUNIT_ASSERT_EQUAL(toString(i), test.getAsString());
197 }
198}
199
200/** Unit test for comparator.
201 *
202 */
203void DiscreteValueTest::comparatorTest()
204{
205 {
206 // create instance
207 Value<int> test(ValidValues);
208 Value<int> instance(ValidValues);
209 test.set(1);
210 instance.set(1);
211
212 // same value, same range
213 {
214 CPPUNIT_ASSERT(test == instance);
215 }
216
217 // different value, same range
218 {
219 const int oldvalue = instance.get();
220 instance.set(2);
221 CPPUNIT_ASSERT(test != instance);
222 instance.set(oldvalue);
223 }
224 }
225 {
226 Value<int> test(ValidValues);
227 Value<int> instance(ValidValues);
228 dynamic_cast<DiscreteValidator<int> &>(instance.getValidator()).appendValidValue(4);
229
230 test.set(1);
231 instance.set(1);
232
233 // same value, same range
234 {
235 CPPUNIT_ASSERT(test != instance);
236 }
237
238 // different value, same range
239 {
240 const int oldvalue = instance.get();
241 instance.set(2);
242 CPPUNIT_ASSERT(test != instance);
243 instance.set(oldvalue);
244 }
245 }
246}
Note: See TracBrowser for help on using the repository browser.