source: src/Parameters/unittests/DiscreteValueTest.cpp@ 72b467

Action_Thermostats Add_AtomRandomPerturbation Add_FitFragmentPartialChargesAction Add_RotateAroundBondAction Add_SelectAtomByNameAction Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_StructOpt_integration_tests Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.5.4 Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator 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_ChargeSampling_PBC Fix_ChronosMutex Fix_FitPartialCharges Fix_FitPotential_needs_atomicnumbers Fix_ForceAnnealing Fix_IndependentFragmentGrids Fix_ParseParticles Fix_ParseParticles_split_forward_backward_Actions Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion GeometryObjects Gui_displays_atomic_force_velocity IndependentFragmentGrids_IntegrationTest JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks RotateToPrincipalAxisSystem_UndoRedo StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_LinearAlgebra Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg ThirdParty_MPQC_rebuilt_buildsystem TrajectoryDependenant_MaxOrder TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps Ubuntu_1604_changes stable
Last change on this file since 72b467 was 0d4168, checked in by Frederik Heber <heber@…>, 11 years ago

Allow setting of invalid values in Value class, Action::performCall() catches ParameterExceptions.

Value changes:

  • Values are now checked with get().
  • having the Actions fill their parameters on instantiation may lead to invalid values because actions that, e.g. add an atom and thereby make a so far invalid atomic id now valid, still have to get executed.
  • hence, we allow setting of invalid values. Validity is check/enforced on get(), i.e. when the Action is actually performed and not before. This is also the very moment where the parameters are first required to be valid.
  • Parameter::clone() and copy cstor must not use get() as invalid values are there still allowed.
  • TESTFIX: Value behavior changed.
  • TESTFIX: regression test add atom outside boundary is working again.
  • TESTFIX: regression tests load/store-session would be skipped as loading non-present file fails now. We use --help --actionname instead.

Action changes:

  • are turned into Action::failure.
  • ActionQueue calling an Action wrapped in try/catch-block for ActionFailure.
  • removed try/catch in doUI().
  • as exception (will) occur in ActionQueue's queue_thread, we need to catch it there. As the only thing we do is set the exit flag of the World. We can do this in ActionQueue as well.
  • Property mode set to 100644
File size: 7.5 KB
RevLine 
[c68409]1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
4 * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
[94d5ac6]5 *
6 *
7 * This file is part of MoleCuilder.
8 *
9 * MoleCuilder is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * MoleCuilder is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with MoleCuilder. If not, see <http://www.gnu.org/licenses/>.
[c68409]21 */
22
23/*
[dbb533]24 * DiscreteValueTest.cpp
[c68409]25 *
26 * Created on: Sep 28, 2011
27 * Author: heber
28 */
29
30// include config.h
31#ifdef HAVE_CONFIG_H
32#include <config.h>
33#endif
34
[dbb533]35#include "DiscreteValueTest.hpp"
[c68409]36
37#include <cppunit/CompilerOutputter.h>
38#include <cppunit/extensions/TestFactoryRegistry.h>
39#include <cppunit/ui/text/TestRunner.h>
40
[e45c1d]41#include "Parameters/ParameterExceptions.hpp"
[3c5ef5]42#include "Parameters/Value.hpp"
[c68409]43
44#ifdef HAVE_TESTRUNNER
45#include "UnitTestMain.hpp"
46#endif /*HAVE_TESTRUNNER*/
47
48using namespace std;
49
50// Registers the fixture into the 'registry'
51CPPUNIT_TEST_SUITE_REGISTRATION( DiscreteValueTest );
52
53
54void DiscreteValueTest::setUp()
55{
56 // failing asserts should be thrown
57 ASSERT_DO(Assert::Throw);
58
59 for (int i=1; i<=3;++i) {
60 ValidValues.push_back(i);
61 }
62}
63
64void DiscreteValueTest::tearDown()
65{
66 ValidValues.clear();
67}
68
69/************************************ tests ***********************************/
70
71/** Unit test for findIndexOfValue.
72 *
73 */
74void DiscreteValueTest::findIndexOfValueTest()
75{
76 // create instance
[3c5ef5]77 Value<int> test(ValidValues);
[c68409]78
79 // check valid values indices
[3c5ef5]80 CPPUNIT_ASSERT_EQUAL((size_t)0, dynamic_cast<DiscreteValidator<int> &>(test.getValidator()).findIndexOfValue(1));
81 CPPUNIT_ASSERT_EQUAL((size_t)1, dynamic_cast<DiscreteValidator<int> &>(test.getValidator()).findIndexOfValue(2));
82 CPPUNIT_ASSERT_EQUAL((size_t)2, dynamic_cast<DiscreteValidator<int> &>(test.getValidator()).findIndexOfValue(3));
[c68409]83
84 // check invalid ones
85 for (int i=-10; i<=0;++i)
[3c5ef5]86 CPPUNIT_ASSERT_EQUAL((size_t)-1, dynamic_cast<DiscreteValidator<int> &>(test.getValidator()).findIndexOfValue(i));
[c68409]87 for (int i=4; i<=0;++i)
[3c5ef5]88 CPPUNIT_ASSERT_EQUAL((size_t)-1, dynamic_cast<DiscreteValidator<int> &>(test.getValidator()).findIndexOfValue(i));
[c68409]89}
90
91/** Unit test for isValidValue.
92 *
93 */
[047cad]94void DiscreteValueTest::isValidAsStringTest()
[c68409]95{
96 // create instance
[3c5ef5]97 Value<int> test(ValidValues);
[c68409]98
99 // checking valid values
[6c05d8]100 for (int i=1; i<=3;++i)
[047cad]101 CPPUNIT_ASSERT_EQUAL(true, test.isValidAsString(toString(i)));
[c68409]102
103 // checking invalid values
104 for (int i=-10; i<=0;++i)
[047cad]105 CPPUNIT_ASSERT_EQUAL(false, test.isValidAsString(toString(i)));
[c68409]106 for (int i=4; i<=0;++i)
[6c05d8]107 CPPUNIT_ASSERT_EQUAL(false, test.isValidAsString(toString(i)));
[c68409]108}
109
110/** Unit test for isValid.
111 *
112 */
113void DiscreteValueTest::isValidTest()
114{
115 // create instance
[3c5ef5]116 Value<int> test(ValidValues);
[c68409]117
118 // checking valid values
119 for (int i=1; i<=3;++i)
[dbb533]120 CPPUNIT_ASSERT_EQUAL(true, test.isValid(i));
[c68409]121
122 // checking invalid values
123 for (int i=-10; i<=0;++i)
[dbb533]124 CPPUNIT_ASSERT_EQUAL(false, test.isValid(i));
[c68409]125 for (int i=4; i<=0;++i)
[dbb533]126 CPPUNIT_ASSERT_EQUAL(false, test.isValid(i));
[c68409]127}
128
129/** Unit test for appendValidValue.
130 *
131 */
132void DiscreteValueTest::appendValidValueTest()
133{
134 // create instance
[3c5ef5]135 Value<int> test(ValidValues);
[c68409]136
137 // adding values 4,5,6
138 for (int i=4; i<=6;++i) {
[047cad]139 CPPUNIT_ASSERT_EQUAL(false, test.isValid(i));
[3c5ef5]140 dynamic_cast<DiscreteValidator<int> &>(test.getValidator()).appendValidValue(i);
[047cad]141 CPPUNIT_ASSERT_EQUAL(true, test.isValid(i));
[c68409]142 }
143
144 // adding same value, throws assertion
[3c5ef5]145 const size_t size_before = dynamic_cast<DiscreteValidator<int> &>(test.getValidator()).getValidValues().size();
[c68409]146 std::cout << "The following Assert failures are intended and do not indicate a failure of the test." << std::endl;
147 for (int i=1; i<=6;++i)
[e45c1d]148 CPPUNIT_ASSERT_THROW(dynamic_cast<DiscreteValidator<int> &>(test.getValidator()).appendValidValue(i), ParameterValidatorException);
[3c5ef5]149 CPPUNIT_ASSERT_EQUAL( size_before, dynamic_cast<DiscreteValidator<int> &>(test.getValidator()).getValidValues().size() );
[c68409]150
151 // checking valid values
152 for (int i=1; i<=6;++i)
[047cad]153 CPPUNIT_ASSERT_EQUAL(true, test.isValid(i));
[c68409]154
155 // checking invalid values
156 for (int i=-10; i<=0;++i)
[047cad]157 CPPUNIT_ASSERT_EQUAL(false, test.isValid(i));
[c68409]158
159 // checking invalid values
160 for (int i=7; i<=10;++i)
[047cad]161 CPPUNIT_ASSERT_EQUAL(false, test.isValid(i));
[c68409]162}
163
164/** Unit test for setters and getters.
165 *
166 */
167void DiscreteValueTest::settergetterTest()
168{
169 // create instance
[3c5ef5]170 Value<int> test(ValidValues);
[c68409]171
172 // unset calling of get, throws
173 std::cout << "The following Assert failures are intended and do not indicate a failure of the test." << std::endl;
[e45c1d]174 CPPUNIT_ASSERT_THROW(test.get(), ParameterValueException);
[c68409]175
[0d4168]176 // setting invalid and getting it, throws
177 test.set(4);
[c68409]178 std::cout << "The following Assert failures are intended and do not indicate a failure of the test." << std::endl;
[0d4168]179 CPPUNIT_ASSERT_THROW(test.get(), ParameterValueException);
180 test.set(0);
[c68409]181 std::cout << "The following Assert failures are intended and do not indicate a failure of the test." << std::endl;
[0d4168]182 CPPUNIT_ASSERT_THROW(test.get(), ParameterValueException);
[c68409]183
184 // checking all valid ones
185 for (int i=1; i<=3;++i) {
[dbb533]186 test.set(i);
187 CPPUNIT_ASSERT_EQUAL(i, test.get());
[c68409]188 }
189
190}
191
192/** Unit test for setValue and getValue.
193 *
194 */
[047cad]195void DiscreteValueTest::settergetterAsStringTest()
[c68409]196{
197 // create instance
[3c5ef5]198 Value<int> test(ValidValues);
[c68409]199
200 // unset calling of get, throws
201 std::cout << "The following Assert failures are intended and do not indicate a failure of the test." << std::endl;
[e45c1d]202 CPPUNIT_ASSERT_THROW(test.getAsString(), ParameterValueException);
[c68409]203
[0d4168]204 // setting invalid and getting it, throws
205 test.setAsString(toString(4));
[c68409]206 std::cout << "The following Assert failures are intended and do not indicate a failure of the test." << std::endl;
[0d4168]207 CPPUNIT_ASSERT_THROW(test.getAsString(), ParameterValueException);
208 test.setAsString(toString(0));
[c68409]209 std::cout << "The following Assert failures are intended and do not indicate a failure of the test." << std::endl;
[0d4168]210 CPPUNIT_ASSERT_THROW(test.getAsString(), ParameterValueException);
[c68409]211
212 // checking all valid ones
213 for (int i=1; i<=3;++i) {
[047cad]214 test.setAsString(toString(i));
215 CPPUNIT_ASSERT_EQUAL(toString(i), test.getAsString());
[6c05d8]216 }
[c68409]217}
218
219/** Unit test for comparator.
220 *
221 */
222void DiscreteValueTest::comparatorTest()
223{
224 {
225 // create instance
[3c5ef5]226 Value<int> test(ValidValues);
227 Value<int> instance(ValidValues);
[047cad]228 test.set(1);
229 instance.set(1);
[c68409]230
231 // same value, same range
232 {
233 CPPUNIT_ASSERT(test == instance);
234 }
235
236 // different value, same range
237 {
[047cad]238 const int oldvalue = instance.get();
239 instance.set(2);
[c68409]240 CPPUNIT_ASSERT(test != instance);
[047cad]241 instance.set(oldvalue);
[c68409]242 }
243 }
244 {
[3c5ef5]245 Value<int> test(ValidValues);
246 Value<int> instance(ValidValues);
247 dynamic_cast<DiscreteValidator<int> &>(instance.getValidator()).appendValidValue(4);
[c68409]248
[047cad]249 test.set(1);
250 instance.set(1);
[c68409]251
252 // same value, same range
253 {
254 CPPUNIT_ASSERT(test != instance);
255 }
256
257 // different value, same range
258 {
[047cad]259 const int oldvalue = instance.get();
260 instance.set(2);
[c68409]261 CPPUNIT_ASSERT(test != instance);
[047cad]262 instance.set(oldvalue);
[c68409]263 }
264 }
265}
Note: See TracBrowser for help on using the repository browser.