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 | * DiscreteValueTest.cpp
|
---|
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 |
|
---|
35 | #include "DiscreteValueTest.hpp"
|
---|
36 |
|
---|
37 | #include <cppunit/CompilerOutputter.h>
|
---|
38 | #include <cppunit/extensions/TestFactoryRegistry.h>
|
---|
39 | #include <cppunit/ui/text/TestRunner.h>
|
---|
40 |
|
---|
41 | #include "Parameters/ParameterExceptions.hpp"
|
---|
42 | #include "Parameters/Value.hpp"
|
---|
43 |
|
---|
44 | #ifdef HAVE_TESTRUNNER
|
---|
45 | #include "UnitTestMain.hpp"
|
---|
46 | #endif /*HAVE_TESTRUNNER*/
|
---|
47 |
|
---|
48 | using namespace std;
|
---|
49 |
|
---|
50 | // Registers the fixture into the 'registry'
|
---|
51 | CPPUNIT_TEST_SUITE_REGISTRATION( DiscreteValueTest );
|
---|
52 |
|
---|
53 |
|
---|
54 | void 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 |
|
---|
64 | void DiscreteValueTest::tearDown()
|
---|
65 | {
|
---|
66 | ValidValues.clear();
|
---|
67 | }
|
---|
68 |
|
---|
69 | /************************************ tests ***********************************/
|
---|
70 |
|
---|
71 | /** Unit test for findIndexOfValue.
|
---|
72 | *
|
---|
73 | */
|
---|
74 | void DiscreteValueTest::findIndexOfValueTest()
|
---|
75 | {
|
---|
76 | // create instance
|
---|
77 | Value<int> test(ValidValues);
|
---|
78 |
|
---|
79 | // check valid values indices
|
---|
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));
|
---|
83 |
|
---|
84 | // check invalid ones
|
---|
85 | for (int i=-10; i<=0;++i)
|
---|
86 | CPPUNIT_ASSERT_EQUAL((size_t)-1, dynamic_cast<DiscreteValidator<int> &>(test.getValidator()).findIndexOfValue(i));
|
---|
87 | for (int i=4; i<=0;++i)
|
---|
88 | CPPUNIT_ASSERT_EQUAL((size_t)-1, dynamic_cast<DiscreteValidator<int> &>(test.getValidator()).findIndexOfValue(i));
|
---|
89 | }
|
---|
90 |
|
---|
91 | /** Unit test for isValidValue.
|
---|
92 | *
|
---|
93 | */
|
---|
94 | void DiscreteValueTest::isValidAsStringTest()
|
---|
95 | {
|
---|
96 | // create instance
|
---|
97 | Value<int> test(ValidValues);
|
---|
98 |
|
---|
99 | // checking valid values
|
---|
100 | for (int i=1; i<=3;++i)
|
---|
101 | CPPUNIT_ASSERT_EQUAL(true, test.isValidAsString(toString(i)));
|
---|
102 |
|
---|
103 | // checking invalid values
|
---|
104 | for (int i=-10; i<=0;++i)
|
---|
105 | CPPUNIT_ASSERT_EQUAL(false, test.isValidAsString(toString(i)));
|
---|
106 | for (int i=4; i<=0;++i)
|
---|
107 | CPPUNIT_ASSERT_EQUAL(false, test.isValidAsString(toString(i)));
|
---|
108 | }
|
---|
109 |
|
---|
110 | /** Unit test for isValid.
|
---|
111 | *
|
---|
112 | */
|
---|
113 | void DiscreteValueTest::isValidTest()
|
---|
114 | {
|
---|
115 | // create instance
|
---|
116 | Value<int> test(ValidValues);
|
---|
117 |
|
---|
118 | // checking valid values
|
---|
119 | for (int i=1; i<=3;++i)
|
---|
120 | CPPUNIT_ASSERT_EQUAL(true, test.isValid(i));
|
---|
121 |
|
---|
122 | // checking invalid values
|
---|
123 | for (int i=-10; i<=0;++i)
|
---|
124 | CPPUNIT_ASSERT_EQUAL(false, test.isValid(i));
|
---|
125 | for (int i=4; i<=0;++i)
|
---|
126 | CPPUNIT_ASSERT_EQUAL(false, test.isValid(i));
|
---|
127 | }
|
---|
128 |
|
---|
129 | /** Unit test for appendValidValue.
|
---|
130 | *
|
---|
131 | */
|
---|
132 | void DiscreteValueTest::appendValidValueTest()
|
---|
133 | {
|
---|
134 | // create instance
|
---|
135 | Value<int> test(ValidValues);
|
---|
136 |
|
---|
137 | // adding values 4,5,6
|
---|
138 | for (int i=4; i<=6;++i) {
|
---|
139 | CPPUNIT_ASSERT_EQUAL(false, test.isValid(i));
|
---|
140 | dynamic_cast<DiscreteValidator<int> &>(test.getValidator()).appendValidValue(i);
|
---|
141 | CPPUNIT_ASSERT_EQUAL(true, test.isValid(i));
|
---|
142 | }
|
---|
143 |
|
---|
144 | // adding same value, throws assertion
|
---|
145 | const size_t size_before = dynamic_cast<DiscreteValidator<int> &>(test.getValidator()).getValidValues().size();
|
---|
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)
|
---|
148 | CPPUNIT_ASSERT_THROW(dynamic_cast<DiscreteValidator<int> &>(test.getValidator()).appendValidValue(i), ParameterValidatorException);
|
---|
149 | CPPUNIT_ASSERT_EQUAL( size_before, dynamic_cast<DiscreteValidator<int> &>(test.getValidator()).getValidValues().size() );
|
---|
150 |
|
---|
151 | // checking valid values
|
---|
152 | for (int i=1; i<=6;++i)
|
---|
153 | CPPUNIT_ASSERT_EQUAL(true, test.isValid(i));
|
---|
154 |
|
---|
155 | // checking invalid values
|
---|
156 | for (int i=-10; i<=0;++i)
|
---|
157 | CPPUNIT_ASSERT_EQUAL(false, test.isValid(i));
|
---|
158 |
|
---|
159 | // checking invalid values
|
---|
160 | for (int i=7; i<=10;++i)
|
---|
161 | CPPUNIT_ASSERT_EQUAL(false, test.isValid(i));
|
---|
162 | }
|
---|
163 |
|
---|
164 | /** Unit test for setters and getters.
|
---|
165 | *
|
---|
166 | */
|
---|
167 | void DiscreteValueTest::settergetterTest()
|
---|
168 | {
|
---|
169 | // create instance
|
---|
170 | Value<int> test(ValidValues);
|
---|
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;
|
---|
174 | CPPUNIT_ASSERT_THROW(test.get(), ParameterValueException);
|
---|
175 |
|
---|
176 | // setting invalid and getting it, throws
|
---|
177 | test.set(4);
|
---|
178 | std::cout << "The following Assert failures are intended and do not indicate a failure of the test." << std::endl;
|
---|
179 | CPPUNIT_ASSERT_THROW(test.get(), ParameterValueException);
|
---|
180 | test.set(0);
|
---|
181 | std::cout << "The following Assert failures are intended and do not indicate a failure of the test." << std::endl;
|
---|
182 | CPPUNIT_ASSERT_THROW(test.get(), ParameterValueException);
|
---|
183 |
|
---|
184 | // checking all valid ones
|
---|
185 | for (int i=1; i<=3;++i) {
|
---|
186 | test.set(i);
|
---|
187 | CPPUNIT_ASSERT_EQUAL(i, test.get());
|
---|
188 | }
|
---|
189 |
|
---|
190 | }
|
---|
191 |
|
---|
192 | /** Unit test for setValue and getValue.
|
---|
193 | *
|
---|
194 | */
|
---|
195 | void DiscreteValueTest::settergetterAsStringTest()
|
---|
196 | {
|
---|
197 | // create instance
|
---|
198 | Value<int> test(ValidValues);
|
---|
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;
|
---|
202 | CPPUNIT_ASSERT_THROW(test.getAsString(), ParameterValueException);
|
---|
203 |
|
---|
204 | // setting invalid and getting it, throws
|
---|
205 | test.setAsString(toString(4));
|
---|
206 | std::cout << "The following Assert failures are intended and do not indicate a failure of the test." << std::endl;
|
---|
207 | CPPUNIT_ASSERT_THROW(test.getAsString(), ParameterValueException);
|
---|
208 | test.setAsString(toString(0));
|
---|
209 | std::cout << "The following Assert failures are intended and do not indicate a failure of the test." << std::endl;
|
---|
210 | CPPUNIT_ASSERT_THROW(test.getAsString(), ParameterValueException);
|
---|
211 |
|
---|
212 | // checking all valid ones
|
---|
213 | for (int i=1; i<=3;++i) {
|
---|
214 | test.setAsString(toString(i));
|
---|
215 | CPPUNIT_ASSERT_EQUAL(toString(i), test.getAsString());
|
---|
216 | }
|
---|
217 | }
|
---|
218 |
|
---|
219 | /** Unit test for comparator.
|
---|
220 | *
|
---|
221 | */
|
---|
222 | void DiscreteValueTest::comparatorTest()
|
---|
223 | {
|
---|
224 | {
|
---|
225 | // create instance
|
---|
226 | Value<int> test(ValidValues);
|
---|
227 | Value<int> instance(ValidValues);
|
---|
228 | test.set(1);
|
---|
229 | instance.set(1);
|
---|
230 |
|
---|
231 | // same value, same range
|
---|
232 | {
|
---|
233 | CPPUNIT_ASSERT(test == instance);
|
---|
234 | }
|
---|
235 |
|
---|
236 | // different value, same range
|
---|
237 | {
|
---|
238 | const int oldvalue = instance.get();
|
---|
239 | instance.set(2);
|
---|
240 | CPPUNIT_ASSERT(test != instance);
|
---|
241 | instance.set(oldvalue);
|
---|
242 | }
|
---|
243 | }
|
---|
244 | {
|
---|
245 | Value<int> test(ValidValues);
|
---|
246 | Value<int> instance(ValidValues);
|
---|
247 | dynamic_cast<DiscreteValidator<int> &>(instance.getValidator()).appendValidValue(4);
|
---|
248 |
|
---|
249 | test.set(1);
|
---|
250 | instance.set(1);
|
---|
251 |
|
---|
252 | // same value, same range
|
---|
253 | {
|
---|
254 | CPPUNIT_ASSERT(test != instance);
|
---|
255 | }
|
---|
256 |
|
---|
257 | // different value, same range
|
---|
258 | {
|
---|
259 | const int oldvalue = instance.get();
|
---|
260 | instance.set(2);
|
---|
261 | CPPUNIT_ASSERT(test != instance);
|
---|
262 | instance.set(oldvalue);
|
---|
263 | }
|
---|
264 | }
|
---|
265 | }
|
---|