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 | * ContinuousValueTest.cpp
|
---|
10 | *
|
---|
11 | * Created on: Sep 29, 2011
|
---|
12 | * Author: heber
|
---|
13 | */
|
---|
14 |
|
---|
15 | // include config.h
|
---|
16 | #ifdef HAVE_CONFIG_H
|
---|
17 | #include <config.h>
|
---|
18 | #endif
|
---|
19 |
|
---|
20 | #include "ContinuousValueTest.hpp"
|
---|
21 |
|
---|
22 | #include <cppunit/CompilerOutputter.h>
|
---|
23 | #include <cppunit/extensions/TestFactoryRegistry.h>
|
---|
24 | #include <cppunit/ui/text/TestRunner.h>
|
---|
25 |
|
---|
26 | #include "Parameters/ContinuousValue.hpp"
|
---|
27 |
|
---|
28 | #ifdef HAVE_TESTRUNNER
|
---|
29 | #include "UnitTestMain.hpp"
|
---|
30 | #endif /*HAVE_TESTRUNNER*/
|
---|
31 |
|
---|
32 | // Registers the fixture into the 'registry'
|
---|
33 | CPPUNIT_TEST_SUITE_REGISTRATION( ContinuousValueTest );
|
---|
34 |
|
---|
35 |
|
---|
36 | void ContinuousValueTest::setUp()
|
---|
37 | {
|
---|
38 | // failing asserts should be thrown
|
---|
39 | ASSERT_DO(Assert::Throw);
|
---|
40 |
|
---|
41 | ValidRange = new range<int>(1,4);
|
---|
42 | }
|
---|
43 |
|
---|
44 | void ContinuousValueTest::tearDown()
|
---|
45 | {
|
---|
46 | delete ValidRange;
|
---|
47 | }
|
---|
48 |
|
---|
49 | /************************************ tests ***********************************/
|
---|
50 |
|
---|
51 | /** Unit test for isValid.
|
---|
52 | *
|
---|
53 | */
|
---|
54 | void ContinuousValueTest::isValidValueTest()
|
---|
55 | {
|
---|
56 | // create instance
|
---|
57 | ContinuousValue<int> test(*ValidRange);
|
---|
58 |
|
---|
59 | // checking valid values
|
---|
60 | for (int i=1; i<=4;++i)
|
---|
61 | CPPUNIT_ASSERT_EQUAL(true, test.isValidValue(i));
|
---|
62 |
|
---|
63 | // checking invalid values
|
---|
64 | for (int i=-10; i<=0;++i)
|
---|
65 | CPPUNIT_ASSERT_EQUAL(false, test.isValidValue(i));
|
---|
66 | for (int i=5; i<=0;++i)
|
---|
67 | CPPUNIT_ASSERT_EQUAL(false, test.isValidValue(i));
|
---|
68 | }
|
---|
69 |
|
---|
70 | /** Unit test for isValid.
|
---|
71 | *
|
---|
72 | */
|
---|
73 | void ContinuousValueTest::isValidTest()
|
---|
74 | {
|
---|
75 | // create instance
|
---|
76 | ContinuousValue<int> test(*ValidRange);
|
---|
77 |
|
---|
78 | // checking valid values
|
---|
79 | for (int i=1; i<=4;++i)
|
---|
80 | CPPUNIT_ASSERT_EQUAL(true, test.isValid(i));
|
---|
81 |
|
---|
82 | // checking invalid values
|
---|
83 | for (int i=-10; i<=0;++i)
|
---|
84 | CPPUNIT_ASSERT_EQUAL(false, test.isValid(i));
|
---|
85 | for (int i=5; i<=0;++i)
|
---|
86 | CPPUNIT_ASSERT_EQUAL(false, test.isValid(i));
|
---|
87 | }
|
---|
88 |
|
---|
89 | /** Unit test for setting/getting valid range.
|
---|
90 | *
|
---|
91 | */
|
---|
92 | void ContinuousValueTest::setgetValidRangeTest()
|
---|
93 | {
|
---|
94 | {
|
---|
95 | // create instance
|
---|
96 | ContinuousValue<int> test(*ValidRange);
|
---|
97 |
|
---|
98 | // extending range and checking
|
---|
99 | for (int i=5; i<=6;++i)
|
---|
100 | CPPUNIT_ASSERT_EQUAL(false, test.isValidValue(i));
|
---|
101 | test.setValidRange(range<int>(1,6));
|
---|
102 | for (int i=5; i<=6;++i)
|
---|
103 | CPPUNIT_ASSERT_EQUAL(true, test.isValidValue(i));
|
---|
104 | }
|
---|
105 |
|
---|
106 | {
|
---|
107 | // create instance
|
---|
108 | ContinuousValue<int> test(*ValidRange);
|
---|
109 |
|
---|
110 | // lowering range with set value
|
---|
111 | test.setValue(4);
|
---|
112 | CPPUNIT_ASSERT_EQUAL(true, test.ValueSet);
|
---|
113 | #ifndef NDEBUG
|
---|
114 | std::cout << "The following Assert failures are intended and do not indicate a failure of the test." << std::endl;
|
---|
115 | CPPUNIT_ASSERT_THROW(test.setValidRange(range<int>(1,3)), Assert::AssertionFailure);
|
---|
116 | #else
|
---|
117 | test.setValidRange(range<int>(1,3));
|
---|
118 | #endif
|
---|
119 | #ifndef NDEBUG
|
---|
120 | // no value is not set
|
---|
121 | std::cout << "The following Assert failures are intended and do not indicate a failure of the test." << std::endl;
|
---|
122 | CPPUNIT_ASSERT_THROW(test.get(), Assert::AssertionFailure);
|
---|
123 | #endif
|
---|
124 | // value gets invalidated in either case
|
---|
125 | CPPUNIT_ASSERT_EQUAL(false, test.ValueSet);
|
---|
126 | }
|
---|
127 | }
|
---|
128 |
|
---|
129 | /** Unit test for setValue and getValue.
|
---|
130 | *
|
---|
131 | */
|
---|
132 | void ContinuousValueTest::settergetterValueTest()
|
---|
133 | {
|
---|
134 | // create instance
|
---|
135 | ContinuousValue<int> test(*ValidRange);
|
---|
136 |
|
---|
137 | // unset calling of get, throws
|
---|
138 | #ifndef NDEBUG
|
---|
139 | std::cout << "The following Assert failures are intended and do not indicate a failure of the test." << std::endl;
|
---|
140 | CPPUNIT_ASSERT_THROW(test.getValue(), Assert::AssertionFailure);
|
---|
141 | #endif
|
---|
142 |
|
---|
143 | // setting invalid, throws
|
---|
144 | #ifndef NDEBUG
|
---|
145 | std::cout << "The following Assert failures are intended and do not indicate a failure of the test." << std::endl;
|
---|
146 | CPPUNIT_ASSERT_THROW(test.setValue(5), Assert::AssertionFailure);
|
---|
147 | #endif
|
---|
148 | #ifndef NDEBUG
|
---|
149 | std::cout << "The following Assert failures are intended and do not indicate a failure of the test." << std::endl;
|
---|
150 | CPPUNIT_ASSERT_THROW(test.setValue(0), Assert::AssertionFailure);
|
---|
151 | #endif
|
---|
152 |
|
---|
153 | CPPUNIT_ASSERT_EQUAL(false, test.ValueSet);
|
---|
154 | // checking all valid ones
|
---|
155 | for (int i=1; i<=4;++i) {
|
---|
156 | test.setValue(i);
|
---|
157 | CPPUNIT_ASSERT_EQUAL(true, test.ValueSet);
|
---|
158 | CPPUNIT_ASSERT_EQUAL(i, test.getValue());
|
---|
159 | }
|
---|
160 | }
|
---|
161 |
|
---|
162 | /** Unit test for setters and getters.
|
---|
163 | *
|
---|
164 | */
|
---|
165 | void ContinuousValueTest::settergetterTest()
|
---|
166 | {
|
---|
167 | // create instance
|
---|
168 | ContinuousValue<int> test(*ValidRange);
|
---|
169 |
|
---|
170 | // unset calling of get, throws
|
---|
171 | #ifndef NDEBUG
|
---|
172 | std::cout << "The following Assert failures are intended and do not indicate a failure of the test." << std::endl;
|
---|
173 | CPPUNIT_ASSERT_THROW(test.get(), Assert::AssertionFailure);
|
---|
174 | #endif
|
---|
175 |
|
---|
176 | // setting invalid, throws
|
---|
177 | #ifndef NDEBUG
|
---|
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.set(5), Assert::AssertionFailure);
|
---|
180 | #endif
|
---|
181 | #ifndef NDEBUG
|
---|
182 | std::cout << "The following Assert failures are intended and do not indicate a failure of the test." << std::endl;
|
---|
183 | CPPUNIT_ASSERT_THROW(test.set(0), Assert::AssertionFailure);
|
---|
184 | #endif
|
---|
185 |
|
---|
186 | // checking all valid ones
|
---|
187 | for (int i=1; i<=4;++i) {
|
---|
188 | test.set(i);
|
---|
189 | CPPUNIT_ASSERT_EQUAL(i, test.get());
|
---|
190 | }
|
---|
191 | }
|
---|
192 |
|
---|
193 | /** Unit test for comparator.
|
---|
194 | *
|
---|
195 | */
|
---|
196 | void ContinuousValueTest::comparatorTest()
|
---|
197 | {
|
---|
198 | {
|
---|
199 | // create instance
|
---|
200 | ContinuousValue<int> test(*ValidRange);
|
---|
201 | ContinuousValue<int> instance(*ValidRange);
|
---|
202 | test.setValue(1);
|
---|
203 | instance.setValue(1);
|
---|
204 |
|
---|
205 | // same value, same range
|
---|
206 | {
|
---|
207 | CPPUNIT_ASSERT(test == instance);
|
---|
208 | }
|
---|
209 |
|
---|
210 | // different value, same range
|
---|
211 | {
|
---|
212 | const int oldvalue = instance.getValue();
|
---|
213 | instance.setValue(2);
|
---|
214 | CPPUNIT_ASSERT(test != instance);
|
---|
215 | instance.setValue(oldvalue);
|
---|
216 | }
|
---|
217 | }
|
---|
218 | {
|
---|
219 | ContinuousValue<int> test(*ValidRange);
|
---|
220 | range<int> OtherValidRange(1,5);
|
---|
221 | ContinuousValue<int> instance(OtherValidRange);
|
---|
222 |
|
---|
223 | test.setValue(1);
|
---|
224 | instance.setValue(1);
|
---|
225 |
|
---|
226 | // same value, same range
|
---|
227 | {
|
---|
228 | CPPUNIT_ASSERT(test != instance);
|
---|
229 | }
|
---|
230 |
|
---|
231 | // different value, same range
|
---|
232 | {
|
---|
233 | const int oldvalue = instance.getValue();
|
---|
234 | instance.setValue(2);
|
---|
235 | CPPUNIT_ASSERT(test != instance);
|
---|
236 | instance.setValue(oldvalue);
|
---|
237 | }
|
---|
238 | }
|
---|
239 | }
|
---|