source: src/Parameters/unittests/ContinuousValueTest.cpp@ 3e6b93

Last change on this file since 3e6b93 was 3e6b93, checked in by Frederik Heber <heber@…>, 11 years ago

Changed checking of Parameter::isValid() on set(), not on get().

  • this would fix problems with ActionQueue::OutputAs...() commands that need to get() parameter values after usage by the Action. If files were forced to be non-present before, then written by the Action, the Validator will then fail.
  • Property mode set to 100644
File size: 14.0 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 *
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/>.
21 */
22
23/*
24 * ContinuousValueTest.cpp
25 *
26 * Created on: Sep 29, 2011
27 * Author: heber
28 */
29
30// include config.h
31#ifdef HAVE_CONFIG_H
32#include <config.h>
33#endif
34
35#include "ContinuousValueTest.hpp"
36
37#include <cppunit/CompilerOutputter.h>
38#include <cppunit/extensions/TestFactoryRegistry.h>
39#include <cppunit/ui/text/TestRunner.h>
40
41#include "Parameters/Value.hpp"
42
43#ifdef HAVE_TESTRUNNER
44#include "UnitTestMain.hpp"
45#endif /*HAVE_TESTRUNNER*/
46
47// Registers the fixture into the 'registry'
48CPPUNIT_TEST_SUITE_REGISTRATION( ContinuousValueTest );
49
50
51void ContinuousValueTest::setUp()
52{
53 // failing asserts should be thrown
54 ASSERT_DO(Assert::Throw);
55
56 ValidIntRange = new range<int>(1,4);
57 ValidVectorRange = new range<Vector>(Vector(0,1,2), Vector(10,11,12));
58}
59
60void ContinuousValueTest::tearDown()
61{
62 delete ValidIntRange;
63 delete ValidVectorRange;
64}
65
66/************************************ tests ***********************************/
67
68/** Unit test for isValid.
69 *
70 */
71void ContinuousValueTest::isValidIntAsStringTest()
72{
73 // create instance
74 Value<int> test(*ValidIntRange);
75
76 // checking valid values
77 for (int i=1; i<=4;++i)
78 CPPUNIT_ASSERT_EQUAL(true, test.isValidAsString(toString(i)));
79
80 // checking invalid values
81 for (int i=-10; i<=0;++i)
82 CPPUNIT_ASSERT_EQUAL(false, test.isValidAsString(toString(i)));
83 for (int i=5; i<=0;++i)
84 CPPUNIT_ASSERT_EQUAL(false, test.isValidAsString(toString(i)));
85}
86
87/** Unit test for isValid.
88 *
89 */
90void ContinuousValueTest::isValidIntTest()
91{
92 // create instance
93 Value<int> test(*ValidIntRange);
94
95 // checking valid values
96 for (int i=1; i<=4;++i)
97 CPPUNIT_ASSERT_EQUAL(true, test.isValid(i));
98
99 // checking invalid values
100 for (int i=-10; i<=0;++i)
101 CPPUNIT_ASSERT_EQUAL(false, test.isValid(i));
102 for (int i=5; i<=0;++i)
103 CPPUNIT_ASSERT_EQUAL(false, test.isValid(i));
104}
105
106/** Unit test for setting/getting valid range.
107 *
108 */
109void ContinuousValueTest::setgetValidIntRangeTest()
110{
111 {
112 // create instance
113 Value<int> test(*ValidIntRange);
114
115 // extending range and checking
116 for (int i=5; i<=6;++i)
117 CPPUNIT_ASSERT_EQUAL(false, test.isValid(i));
118 test.setValidRange(range<int>(1,6));
119 for (int i=5; i<=6;++i)
120 CPPUNIT_ASSERT_EQUAL(true, test.isValid(i));
121 }
122
123 {
124 // create instance
125 Value<int> test(*ValidIntRange);
126
127 // lowering range with set value
128 test.set(4);
129 CPPUNIT_ASSERT_EQUAL(true, test.ValueSet);
130 std::cout << "The following Assert failures are intended and do not indicate a failure of the test." << std::endl;
131 CPPUNIT_ASSERT_THROW(test.setValidRange(range<int>(1,3)), ParameterValueException);
132
133 // no value is not set
134 std::cout << "The following Assert failures are intended and do not indicate a failure of the test." << std::endl;
135 CPPUNIT_ASSERT_THROW(test.get(), ParameterValueException);
136
137 // value gets invalidated in either case
138 CPPUNIT_ASSERT_EQUAL(false, test.ValueSet);
139 }
140}
141
142/** Unit test for setValue and getValue.
143 *
144 */
145void ContinuousValueTest::settergetterIntAsStringTest()
146{
147 // unset calling of get, throws ParameterValueException
148 {
149 Value<int> test(*ValidIntRange);
150 std::cout << "The following Assert failures are intended and do not indicate a failure of the test." << std::endl;
151 CPPUNIT_ASSERT_THROW(test.getAsString(), ParameterValueException);
152 }
153
154 // setting invalid and getting it, throws ParameterValueException
155 {
156 Value<int> test(*ValidIntRange);
157 std::cout << "The following Assert failures are intended and do not indicate a failure of the test." << std::endl;
158 CPPUNIT_ASSERT_THROW(test.setAsString(toString(5)), ParameterValueException);
159 std::cout << "The following Assert failures are intended and do not indicate a failure of the test." << std::endl;
160 CPPUNIT_ASSERT_THROW(test.setAsString(toString(0)), ParameterValueException);
161 }
162
163 // checking all valid ones
164 {
165 Value<int> test(*ValidIntRange);
166 CPPUNIT_ASSERT_EQUAL(false, test.ValueSet);
167 for (int i=1; i<=4;++i) {
168 test.setAsString(toString(i));
169 CPPUNIT_ASSERT_EQUAL(true, test.ValueSet);
170 CPPUNIT_ASSERT_EQUAL(toString(i), test.getAsString());
171 }
172 }
173}
174
175/** Unit test for setters and getters.
176 *
177 */
178void ContinuousValueTest::settergetterIntTest()
179{
180 // create instance
181 Value<int> test(*ValidIntRange);
182
183 // unset calling of get, throws ParameterValueException
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.get(), ParameterValueException);
186
187 // setting invalid and getting it, throws ParameterValueException
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.set(5), 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.set(0), ParameterValueException);
192
193 // checking all valid ones
194 for (int i=1; i<=4;++i) {
195 test.set(i);
196 CPPUNIT_ASSERT_EQUAL(i, test.get());
197 }
198}
199
200/** Unit test for comparator.
201 *
202 */
203void ContinuousValueTest::comparatorIntTest()
204{
205 {
206 // create instance
207 Value<int> test(*ValidIntRange);
208 Value<int> instance(*ValidIntRange);
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(*ValidIntRange);
227 range<int> OtherValidIntRange(1,5);
228 Value<int> instance(OtherValidIntRange);
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}
247
248
249
250/***************************** vector tests ***********************************/
251
252/** Unit test for isValid.
253 *
254 */
255void ContinuousValueTest::isValidVectorAsStringTest()
256{
257 // create instance
258 /*ContinuousValue<Vector> test(*ValidVectorRange);
259
260 // checking valid values
261 CPPUNIT_ASSERT_EQUAL(true, test.isValidValue(Vector(0,1,2)));
262 CPPUNIT_ASSERT_EQUAL(true, test.isValidValue(Vector(9.9,10.9,11.9)));
263 CPPUNIT_ASSERT_EQUAL(true, test.isValidValue(Vector(5,5,5)));
264
265 // checking invalid values
266 CPPUNIT_ASSERT_EQUAL(false, test.isValidValue(Vector(-0.1,0.9,1.9)));
267 CPPUNIT_ASSERT_EQUAL(false, test.isValidValue(Vector(10.1,11.1,12.1)));
268 CPPUNIT_ASSERT_EQUAL(false, test.isValidValue(Vector(5,5,-1)));
269 CPPUNIT_ASSERT_EQUAL(false, test.isValidValue(Vector(5,-1,5)));
270 CPPUNIT_ASSERT_EQUAL(false, test.isValidValue(Vector(-1,5,5)));
271 CPPUNIT_ASSERT_EQUAL(false, test.isValidValue(Vector(5,5,20)));
272 CPPUNIT_ASSERT_EQUAL(false, test.isValidValue(Vector(5,20,5)));
273 CPPUNIT_ASSERT_EQUAL(false, test.isValidValue(Vector(20,5,5)));
274 CPPUNIT_ASSERT_EQUAL(false, test.isValidValue(Vector(0,0,0)));
275 CPPUNIT_ASSERT_EQUAL(false, test.isValidValue(Vector(5,-1,-1)));
276 CPPUNIT_ASSERT_EQUAL(false, test.isValidValue(Vector(-1,5,-1)));
277 CPPUNIT_ASSERT_EQUAL(false, test.isValidValue(Vector(-1,-1,5)));
278 CPPUNIT_ASSERT_EQUAL(false, test.isValidValue(Vector(5,20,20)));
279 CPPUNIT_ASSERT_EQUAL(false, test.isValidValue(Vector(20,5,20)));
280 CPPUNIT_ASSERT_EQUAL(false, test.isValidValue(Vector(20,20,5)));*/
281}
282
283/** Unit test for isValid.
284 *
285 */
286void ContinuousValueTest::isValidVectorTest()
287{
288 // create instance
289 Value<Vector> test(*ValidVectorRange);
290
291 // checking valid values
292 CPPUNIT_ASSERT_EQUAL(true, test.isValid(Vector(0,1,2)));
293 CPPUNIT_ASSERT_EQUAL(true, test.isValid(Vector(9.9,10.9,11.9)));
294 CPPUNIT_ASSERT_EQUAL(true, test.isValid(Vector(5,5,5)));
295
296 // checking invalid values
297 CPPUNIT_ASSERT_EQUAL(false, test.isValid(Vector(-0.1,0.9,1.9)));
298 CPPUNIT_ASSERT_EQUAL(false, test.isValid(Vector(10.1,11.1,12.1)));
299 CPPUNIT_ASSERT_EQUAL(false, test.isValid(Vector(5,5,-1)));
300 CPPUNIT_ASSERT_EQUAL(false, test.isValid(Vector(5,-1,5)));
301 CPPUNIT_ASSERT_EQUAL(false, test.isValid(Vector(-1,5,5)));
302 CPPUNIT_ASSERT_EQUAL(false, test.isValid(Vector(5,5,20)));
303 CPPUNIT_ASSERT_EQUAL(false, test.isValid(Vector(5,20,5)));
304 CPPUNIT_ASSERT_EQUAL(false, test.isValid(Vector(20,5,5)));
305 CPPUNIT_ASSERT_EQUAL(false, test.isValid(Vector(0,0,0)));
306 CPPUNIT_ASSERT_EQUAL(false, test.isValid(Vector(5,-1,-1)));
307 CPPUNIT_ASSERT_EQUAL(false, test.isValid(Vector(-1,5,-1)));
308 CPPUNIT_ASSERT_EQUAL(false, test.isValid(Vector(-1,-1,5)));
309 CPPUNIT_ASSERT_EQUAL(false, test.isValid(Vector(5,20,20)));
310 CPPUNIT_ASSERT_EQUAL(false, test.isValid(Vector(20,5,20)));
311 CPPUNIT_ASSERT_EQUAL(false, test.isValid(Vector(20,20,5)));
312}
313
314/** Unit test for setting/getting valid range.
315 *
316 */
317void ContinuousValueTest::setgetValidVectorRangeTest()
318{
319 {
320 // create instance
321 Value<Vector> test(*ValidVectorRange);
322
323 // extending range and checking
324 for (int i=15; i<=16;++i)
325 CPPUNIT_ASSERT_EQUAL(false, test.isValid(Vector(i,5,5)));
326 test.setValidRange(range<Vector>(Vector(0,1,2),Vector(20,11,12)));
327 for (int i=15; i<=16;++i)
328 CPPUNIT_ASSERT_EQUAL(true, test.isValid(Vector(i,5,5)));
329 }
330
331 {
332 // create instance
333 Value<Vector> test(*ValidVectorRange);
334
335 // lowering range with set value
336 test.set(Vector(4,4,4));
337 CPPUNIT_ASSERT_EQUAL(true, test.ValueSet);
338 std::cout << "The following Assert failures are intended and do not indicate a failure of the test." << std::endl;
339 CPPUNIT_ASSERT_THROW(test.setValidRange(range<Vector>(Vector(1,1,1),Vector(3,3,3))), ParameterValueException);
340
341 // no value is not set
342 std::cout << "The following Assert failures are intended and do not indicate a failure of the test." << std::endl;
343 CPPUNIT_ASSERT_THROW(test.get(), ParameterException);
344
345 // value gets invalidated in either case
346 CPPUNIT_ASSERT_EQUAL(false, test.ValueSet);
347 }
348}
349
350/** Unit test for setValue and getValue.
351 *
352 */
353void ContinuousValueTest::settergetterVectorAsStringTest()
354{
355 // create instance
356 /*Value<Vector> test(*ValidVectorRange);
357
358 // unset calling of get, throws
359#ifndef NDEBUG
360 std::cout << "The following Assert failures are intended and do not indicate a failure of the test." << std::endl;
361 CPPUNIT_ASSERT_THROW(test.getValue(), Assert::AssertionFailure);
362#endif
363
364 // setting invalid, throws
365#ifndef NDEBUG
366 std::cout << "The following Assert failures are intended and do not indicate a failure of the test." << std::endl;
367 CPPUNIT_ASSERT_THROW(test.setValue(Vector(5,0,0)), Assert::AssertionFailure);
368#endif
369#ifndef NDEBUG
370 std::cout << "The following Assert failures are intended and do not indicate a failure of the test." << std::endl;
371 CPPUNIT_ASSERT_THROW(test.setValue(Vector(5,20,5)), Assert::AssertionFailure);
372#endif
373
374 CPPUNIT_ASSERT_EQUAL(false, test.ValueSet);
375 // checking some valid ones
376 for (int i=1; i<=4;++i) {
377 Vector v(i,5,5);
378 test.setValue(v);
379 CPPUNIT_ASSERT_EQUAL(true, test.ValueSet);
380 CPPUNIT_ASSERT_EQUAL(v, test.getValue());
381 }*/
382}
383
384/** Unit test for setters and getters.
385 *
386 */
387void ContinuousValueTest::settergetterVectorTest()
388{
389 // unset calling of get, throws
390 {
391 Value<Vector> test(*ValidVectorRange);
392 std::cout << "The following Assert failures are intended and do not indicate a failure of the test." << std::endl;
393 CPPUNIT_ASSERT_THROW(test.get(), ParameterValueException);
394 }
395
396 // setting invalid and getting it, throws
397 {
398 Value<Vector> test(*ValidVectorRange);
399 std::cout << "The following Assert failures are intended and do not indicate a failure of the test." << std::endl;
400 CPPUNIT_ASSERT_THROW(test.set(Vector(5,0,0)), ParameterValueException);
401 std::cout << "The following Assert failures are intended and do not indicate a failure of the test." << std::endl;
402 CPPUNIT_ASSERT_THROW(test.set(Vector(5,20,5)), ParameterValueException);
403 }
404
405 // checking some valid ones
406 {
407 Value<Vector> test(*ValidVectorRange);
408 CPPUNIT_ASSERT_EQUAL(false, test.ValueSet);
409 for (int i=1; i<=4;++i) {
410 Vector v(i,5,5);
411 test.set(v);
412 CPPUNIT_ASSERT_EQUAL(true, test.ValueSet);
413 CPPUNIT_ASSERT_EQUAL(v, test.get());
414 }
415 }
416}
417
418/** Unit test for comparator.
419 *
420 */
421void ContinuousValueTest::comparatorVectorTest()
422{
423 {
424 // create instance
425 Value<Vector> test(*ValidVectorRange);
426 Value<Vector> instance(*ValidVectorRange);
427 test.set(Vector(5,6,7));
428 instance.set(Vector(5,6,7));
429
430 // same value, same range
431 {
432 CPPUNIT_ASSERT(test == instance);
433 }
434
435 // different value, same range
436 {
437 const Vector oldvalue = instance.get();
438 instance.set(Vector(2,3,4));
439 CPPUNIT_ASSERT(test != instance);
440 instance.set(oldvalue);
441 }
442 }
443 {
444 Value<Vector> test(*ValidVectorRange);
445 range<Vector> OtherValidVectorRange(Vector(0,1,2), Vector(20,21,22));
446 Value<Vector> instance(OtherValidVectorRange);
447
448 test.set(Vector(1,2,3));
449 instance.set(Vector(1,2,3));
450
451 // same value, same range
452 {
453 CPPUNIT_ASSERT(test != instance);
454 }
455
456 // different value, same range
457 {
458 const Vector oldvalue = instance.get();
459 instance.set(Vector(2,3,4));
460 CPPUNIT_ASSERT(test != instance);
461 instance.set(oldvalue);
462 }
463 }
464}
Note: See TracBrowser for help on using the repository browser.