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

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 e5d7970 was 6c05d8, checked in by Michael Ankele <ankele@…>, 13 years ago

FIX: Value<string>, empty validators

  • conversion string -> string only returned the part up to the first blank
  • replaced NULL validators with DummyValidator
  • unit tests corrected (because Parameter.clone() now works with unset values)
  • Property mode set to 100644
File size: 6.9 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/Value.hpp"
27
28#ifdef HAVE_TESTRUNNER
29#include "UnitTestMain.hpp"
30#endif /*HAVE_TESTRUNNER*/
31
32using namespace std;
33
34// Registers the fixture into the 'registry'
35CPPUNIT_TEST_SUITE_REGISTRATION( DiscreteValueTest );
36
37
38void DiscreteValueTest::setUp()
39{
40 // failing asserts should be thrown
41 ASSERT_DO(Assert::Throw);
42
43 for (int i=1; i<=3;++i) {
44 ValidValues.push_back(i);
45 }
46}
47
48void DiscreteValueTest::tearDown()
49{
50 ValidValues.clear();
51}
52
53/************************************ tests ***********************************/
54
55/** Unit test for findIndexOfValue.
56 *
57 */
58void DiscreteValueTest::findIndexOfValueTest()
59{
60 // create instance
61 Value<int> test(ValidValues);
62
63 // check valid values indices
64 CPPUNIT_ASSERT_EQUAL((size_t)0, dynamic_cast<DiscreteValidator<int> &>(test.getValidator()).findIndexOfValue(1));
65 CPPUNIT_ASSERT_EQUAL((size_t)1, dynamic_cast<DiscreteValidator<int> &>(test.getValidator()).findIndexOfValue(2));
66 CPPUNIT_ASSERT_EQUAL((size_t)2, dynamic_cast<DiscreteValidator<int> &>(test.getValidator()).findIndexOfValue(3));
67
68 // check invalid ones
69 for (int i=-10; i<=0;++i)
70 CPPUNIT_ASSERT_EQUAL((size_t)-1, dynamic_cast<DiscreteValidator<int> &>(test.getValidator()).findIndexOfValue(i));
71 for (int i=4; i<=0;++i)
72 CPPUNIT_ASSERT_EQUAL((size_t)-1, dynamic_cast<DiscreteValidator<int> &>(test.getValidator()).findIndexOfValue(i));
73}
74
75/** Unit test for isValidValue.
76 *
77 */
78void DiscreteValueTest::isValidAsStringTest()
79{
80 // create instance
81 Value<int> test(ValidValues);
82
83 // checking valid values
84 for (int i=1; i<=3;++i)
85 CPPUNIT_ASSERT_EQUAL(true, test.isValidAsString(toString(i)));
86
87 // checking invalid values
88 for (int i=-10; i<=0;++i)
89 CPPUNIT_ASSERT_EQUAL(false, test.isValidAsString(toString(i)));
90 for (int i=4; i<=0;++i)
91 CPPUNIT_ASSERT_EQUAL(false, test.isValidAsString(toString(i)));
92}
93
94/** Unit test for isValid.
95 *
96 */
97void DiscreteValueTest::isValidTest()
98{
99 // create instance
100 Value<int> test(ValidValues);
101
102 // checking valid values
103 for (int i=1; i<=3;++i)
104 CPPUNIT_ASSERT_EQUAL(true, test.isValid(i));
105
106 // checking invalid values
107 for (int i=-10; i<=0;++i)
108 CPPUNIT_ASSERT_EQUAL(false, test.isValid(i));
109 for (int i=4; i<=0;++i)
110 CPPUNIT_ASSERT_EQUAL(false, test.isValid(i));
111}
112
113/** Unit test for appendValidValue.
114 *
115 */
116void DiscreteValueTest::appendValidValueTest()
117{
118 // create instance
119 Value<int> test(ValidValues);
120
121 // adding values 4,5,6
122 for (int i=4; i<=6;++i) {
123 CPPUNIT_ASSERT_EQUAL(false, test.isValid(i));
124 dynamic_cast<DiscreteValidator<int> &>(test.getValidator()).appendValidValue(i);
125 CPPUNIT_ASSERT_EQUAL(true, test.isValid(i));
126 }
127
128 // adding same value, throws assertion
129 const size_t size_before = dynamic_cast<DiscreteValidator<int> &>(test.getValidator()).getValidValues().size();
130#ifndef NDEBUG
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), Assert::AssertionFailure);
134#endif
135 CPPUNIT_ASSERT_EQUAL( size_before, dynamic_cast<DiscreteValidator<int> &>(test.getValidator()).getValidValues().size() );
136
137 // checking valid values
138 for (int i=1; i<=6;++i)
139 CPPUNIT_ASSERT_EQUAL(true, test.isValid(i));
140
141 // checking invalid values
142 for (int i=-10; i<=0;++i)
143 CPPUNIT_ASSERT_EQUAL(false, test.isValid(i));
144
145 // checking invalid values
146 for (int i=7; i<=10;++i)
147 CPPUNIT_ASSERT_EQUAL(false, test.isValid(i));
148}
149
150/** Unit test for setters and getters.
151 *
152 */
153void DiscreteValueTest::settergetterTest()
154{
155 // create instance
156 Value<int> test(ValidValues);
157
158 // unset calling of get, throws
159#ifndef NDEBUG
160 std::cout << "The following Assert failures are intended and do not indicate a failure of the test." << std::endl;
161 CPPUNIT_ASSERT_THROW(test.get(), Assert::AssertionFailure);
162#endif
163
164 // setting invalid, throws
165#ifndef NDEBUG
166 std::cout << "The following Assert failures are intended and do not indicate a failure of the test." << std::endl;
167 CPPUNIT_ASSERT_THROW(test.set(4), Assert::AssertionFailure);
168#endif
169#ifndef NDEBUG
170 std::cout << "The following Assert failures are intended and do not indicate a failure of the test." << std::endl;
171 CPPUNIT_ASSERT_THROW(test.set(0), Assert::AssertionFailure);
172#endif
173
174 // checking all valid ones
175 for (int i=1; i<=3;++i) {
176 test.set(i);
177 CPPUNIT_ASSERT_EQUAL(i, test.get());
178 }
179
180}
181
182/** Unit test for setValue and getValue.
183 *
184 */
185void DiscreteValueTest::settergetterAsStringTest()
186{
187 // create instance
188 Value<int> test(ValidValues);
189
190 // unset calling of get, throws
191#ifndef NDEBUG
192 std::cout << "The following Assert failures are intended and do not indicate a failure of the test." << std::endl;
193 CPPUNIT_ASSERT_THROW(test.getAsString(), Assert::AssertionFailure);
194#endif
195
196 // setting invalid, throws
197#ifndef NDEBUG
198 std::cout << "The following Assert failures are intended and do not indicate a failure of the test." << std::endl;
199 CPPUNIT_ASSERT_THROW(test.setAsString(toString(4)), Assert::AssertionFailure);
200#endif
201#ifndef NDEBUG
202 std::cout << "The following Assert failures are intended and do not indicate a failure of the test." << std::endl;
203 CPPUNIT_ASSERT_THROW(test.setAsString(toString(0)), Assert::AssertionFailure);
204#endif
205
206 // checking all valid ones
207 for (int i=1; i<=3;++i) {
208 test.setAsString(toString(i));
209 CPPUNIT_ASSERT_EQUAL(toString(i), test.getAsString());
210 }
211}
212
213/** Unit test for comparator.
214 *
215 */
216void DiscreteValueTest::comparatorTest()
217{
218 {
219 // create instance
220 Value<int> test(ValidValues);
221 Value<int> instance(ValidValues);
222 test.set(1);
223 instance.set(1);
224
225 // same value, same range
226 {
227 CPPUNIT_ASSERT(test == instance);
228 }
229
230 // different value, same range
231 {
232 const int oldvalue = instance.get();
233 instance.set(2);
234 CPPUNIT_ASSERT(test != instance);
235 instance.set(oldvalue);
236 }
237 }
238 {
239 Value<int> test(ValidValues);
240 Value<int> instance(ValidValues);
241 dynamic_cast<DiscreteValidator<int> &>(instance.getValidator()).appendValidValue(4);
242
243 test.set(1);
244 instance.set(1);
245
246 // same value, same range
247 {
248 CPPUNIT_ASSERT(test != instance);
249 }
250
251 // different value, same range
252 {
253 const int oldvalue = instance.get();
254 instance.set(2);
255 CPPUNIT_ASSERT(test != instance);
256 instance.set(oldvalue);
257 }
258 }
259}
Note: See TracBrowser for help on using the repository browser.