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 |
|
---|
32 | using namespace std;
|
---|
33 |
|
---|
34 | // Registers the fixture into the 'registry'
|
---|
35 | CPPUNIT_TEST_SUITE_REGISTRATION( DiscreteValueTest );
|
---|
36 |
|
---|
37 |
|
---|
38 | void 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 |
|
---|
48 | void DiscreteValueTest::tearDown()
|
---|
49 | {
|
---|
50 | ValidValues.clear();
|
---|
51 | }
|
---|
52 |
|
---|
53 | /************************************ tests ***********************************/
|
---|
54 |
|
---|
55 | /** Unit test for findIndexOfValue.
|
---|
56 | *
|
---|
57 | */
|
---|
58 | void 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 | */
|
---|
78 | void 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 | */
|
---|
97 | void 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 | */
|
---|
116 | void 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 | */
|
---|
153 | void 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 | */
|
---|
185 | void 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 | */
|
---|
216 | void 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 | }
|
---|