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 | * VectorUnitTest.cpp
|
---|
25 | *
|
---|
26 | * Created on: Aug 17, 2009
|
---|
27 | * Author: heber
|
---|
28 | */
|
---|
29 |
|
---|
30 | // include config.h
|
---|
31 | #ifdef HAVE_CONFIG_H
|
---|
32 | #include <config.h>
|
---|
33 | #endif
|
---|
34 |
|
---|
35 | using namespace std;
|
---|
36 |
|
---|
37 | #include <cppunit/CompilerOutputter.h>
|
---|
38 | #include <cppunit/extensions/TestFactoryRegistry.h>
|
---|
39 | #include <cppunit/ui/text/TestRunner.h>
|
---|
40 |
|
---|
41 | #include <limits>
|
---|
42 |
|
---|
43 | #include "CodePatterns/Log.hpp"
|
---|
44 | #include "defs.hpp"
|
---|
45 | #include "Plane.hpp"
|
---|
46 | #include "RealSpaceMatrix.hpp"
|
---|
47 | #include "Vector.hpp"
|
---|
48 | #include "vector_ops.hpp"
|
---|
49 |
|
---|
50 | #include "VectorUnitTest.hpp"
|
---|
51 |
|
---|
52 | #ifdef HAVE_TESTRUNNER
|
---|
53 | #include "UnitTestMain.hpp"
|
---|
54 | #endif /*HAVE_TESTRUNNER*/
|
---|
55 |
|
---|
56 | #include <iostream>
|
---|
57 |
|
---|
58 | /********************************************** Test classes **************************************/
|
---|
59 |
|
---|
60 | // Registers the fixture into the 'registry'
|
---|
61 | CPPUNIT_TEST_SUITE_REGISTRATION( VectorTest );
|
---|
62 |
|
---|
63 |
|
---|
64 | void VectorTest::setUp()
|
---|
65 | {
|
---|
66 | zero = Vector(0.,0.,0.);
|
---|
67 | unit = Vector(1.,0.,0.);
|
---|
68 | otherunit = Vector(0.,1.,0.);
|
---|
69 | notunit = Vector(0.,1.,1.);
|
---|
70 | two = Vector(2.,1.,0.);
|
---|
71 | three = Vector(1,2,3);
|
---|
72 | };
|
---|
73 |
|
---|
74 |
|
---|
75 | void VectorTest::tearDown()
|
---|
76 | {
|
---|
77 | logger::purgeInstance();
|
---|
78 | errorLogger::purgeInstance();
|
---|
79 | };
|
---|
80 |
|
---|
81 | /** UnitTest for Constructors and Vector::IsZero() and Vector::IsOne().
|
---|
82 | */
|
---|
83 | void VectorTest::AssignmentTest()
|
---|
84 | {
|
---|
85 | // test with zero
|
---|
86 | zero.at(0) = 0;
|
---|
87 | zero.at(1) = 0;
|
---|
88 | zero.at(2) = 0;
|
---|
89 | double zero_array[3] = {0., 0., 0.};
|
---|
90 |
|
---|
91 | CPPUNIT_ASSERT_EQUAL( zero, Vector(0,0,0));
|
---|
92 | CPPUNIT_ASSERT_EQUAL( zero, Vector(0.,0.,0.));
|
---|
93 | CPPUNIT_ASSERT_EQUAL( zero, Vector(zero_array[0], zero_array[1], zero_array[2]));
|
---|
94 | CPPUNIT_ASSERT_EQUAL( zero, Vector(zero_array));
|
---|
95 |
|
---|
96 | // test with unit
|
---|
97 | unit.at(0) = 1;
|
---|
98 | unit.at(1) = 0;
|
---|
99 | unit.at(2) = 0;
|
---|
100 | double unit_array[3] = {1., 0., 0.};
|
---|
101 |
|
---|
102 | CPPUNIT_ASSERT_EQUAL( unit, Vector(1,0,0));
|
---|
103 | CPPUNIT_ASSERT_EQUAL( unit, Vector(1.,0.,0.));
|
---|
104 | CPPUNIT_ASSERT_EQUAL( unit, Vector(unit_array[0], unit_array[1], unit_array[2]));
|
---|
105 | CPPUNIT_ASSERT_EQUAL( unit, Vector(unit_array));
|
---|
106 |
|
---|
107 | // test with two
|
---|
108 | two.at(0) = 2;
|
---|
109 | two.at(1) = 1;
|
---|
110 | two.at(2) = 0;
|
---|
111 | double two_array[3] = {2., 1., 0.};
|
---|
112 |
|
---|
113 | CPPUNIT_ASSERT_EQUAL( two, Vector(2,1,0));
|
---|
114 | CPPUNIT_ASSERT_EQUAL( two, Vector(2.,1.,0.));
|
---|
115 | CPPUNIT_ASSERT_EQUAL( two, Vector(two_array[0], two_array[1], two_array[2]));
|
---|
116 | CPPUNIT_ASSERT_EQUAL( two, Vector(two_array));
|
---|
117 |
|
---|
118 | // test with three
|
---|
119 | three.at(0) = 1;
|
---|
120 | three.at(1) = 2;
|
---|
121 | three.at(2) = 3;
|
---|
122 | double three_array[3] = {1., 2., 3.};
|
---|
123 |
|
---|
124 | CPPUNIT_ASSERT_EQUAL( three, Vector(1,2,3));
|
---|
125 | CPPUNIT_ASSERT_EQUAL( three, Vector(1.,2.,3.));
|
---|
126 | CPPUNIT_ASSERT_EQUAL( three, Vector(three_array[0], three_array[1], three_array[2]));
|
---|
127 | CPPUNIT_ASSERT_EQUAL( three, Vector(three_array));
|
---|
128 | }
|
---|
129 |
|
---|
130 | /** UnitTest for Constructors and Vector::IsZero() and Vector::IsOne().
|
---|
131 | */
|
---|
132 | void VectorTest::UnityTest()
|
---|
133 | {
|
---|
134 | // unity and zero tests
|
---|
135 | CPPUNIT_ASSERT_EQUAL( true, zero.IsZero() );
|
---|
136 | CPPUNIT_ASSERT_EQUAL( false, zero.IsOne() );
|
---|
137 | CPPUNIT_ASSERT_EQUAL( false, unit.IsZero() );
|
---|
138 | CPPUNIT_ASSERT_EQUAL( true, unit.IsOne() );
|
---|
139 | CPPUNIT_ASSERT_EQUAL( false, notunit.IsOne() );
|
---|
140 | CPPUNIT_ASSERT_EQUAL( true, otherunit.IsOne() );
|
---|
141 | CPPUNIT_ASSERT_EQUAL( false, otherunit.IsZero() );
|
---|
142 | };
|
---|
143 |
|
---|
144 | /** UnitTest for Vector::CopyVector(), Vector::AddVector, Vector::SubtractVector() and Vector::Scale()/
|
---|
145 | */
|
---|
146 | void VectorTest::SimpleAlgebraTest()
|
---|
147 | {
|
---|
148 | double factor;
|
---|
149 | // copy vector
|
---|
150 | fixture = Vector(2.,3.,4.);
|
---|
151 | CPPUNIT_ASSERT_EQUAL( Vector(2.,3.,4.), fixture );
|
---|
152 | // summation and scaling
|
---|
153 | fixture = zero + unit;
|
---|
154 | CPPUNIT_ASSERT_EQUAL( true, fixture.IsOne() );
|
---|
155 | fixture = zero - unit;
|
---|
156 | CPPUNIT_ASSERT_EQUAL( true, fixture.IsOne() );
|
---|
157 | CPPUNIT_ASSERT_EQUAL( false, fixture.IsZero() );
|
---|
158 | fixture = zero + zero;
|
---|
159 | CPPUNIT_ASSERT_EQUAL( true, fixture.IsZero() );
|
---|
160 | fixture = notunit - otherunit;
|
---|
161 | CPPUNIT_ASSERT_EQUAL( true, fixture.IsOne() );
|
---|
162 | fixture = unit - otherunit;
|
---|
163 | CPPUNIT_ASSERT_EQUAL( false, fixture.IsOne() );
|
---|
164 | fixture = notunit - unit - otherunit;
|
---|
165 | CPPUNIT_ASSERT_EQUAL( false, fixture.IsZero() );
|
---|
166 | fixture = 0.98 * unit;
|
---|
167 | CPPUNIT_ASSERT_EQUAL( false, fixture.IsOne() );
|
---|
168 | fixture = 1. * unit;
|
---|
169 | CPPUNIT_ASSERT_EQUAL( true, fixture.IsOne() );
|
---|
170 | factor = 0.98;
|
---|
171 | fixture = factor * unit;
|
---|
172 | CPPUNIT_ASSERT_EQUAL( false, fixture.IsOne() );
|
---|
173 | factor = 1.;
|
---|
174 | fixture = factor * unit;
|
---|
175 | CPPUNIT_ASSERT_EQUAL( true, fixture.IsOne() );
|
---|
176 | };
|
---|
177 |
|
---|
178 |
|
---|
179 | /** UnitTest for operator versions of Vector::CopyVector(), Vector::AddVector, Vector::SubtractVector() and Vector::Scale().
|
---|
180 | */
|
---|
181 | void VectorTest::OperatorAlgebraTest()
|
---|
182 | {
|
---|
183 | // summation and scaling
|
---|
184 | CPPUNIT_ASSERT_EQUAL( true, (zero+unit).IsOne() );
|
---|
185 | CPPUNIT_ASSERT_EQUAL( true, (zero+unit).IsOne() );
|
---|
186 | CPPUNIT_ASSERT_EQUAL( true, (zero-unit).IsOne() );
|
---|
187 | CPPUNIT_ASSERT_EQUAL( false, (zero-unit).IsZero() );
|
---|
188 | CPPUNIT_ASSERT_EQUAL( true, (zero+zero).IsZero() );
|
---|
189 | CPPUNIT_ASSERT_EQUAL( true, (notunit-otherunit).IsOne() );
|
---|
190 | CPPUNIT_ASSERT_EQUAL( false, (unit+otherunit).IsOne() );
|
---|
191 | CPPUNIT_ASSERT_EQUAL( false, (notunit-unit-otherunit).IsZero() );
|
---|
192 | CPPUNIT_ASSERT_EQUAL( false, (unit*0.98).IsOne() );
|
---|
193 | CPPUNIT_ASSERT_EQUAL( true, (unit*1.).IsOne() );
|
---|
194 |
|
---|
195 | CPPUNIT_ASSERT_EQUAL( unit, (zero+unit) );
|
---|
196 | CPPUNIT_ASSERT_EQUAL( Vector(0.,0.,1.), (notunit-otherunit) );
|
---|
197 | CPPUNIT_ASSERT_EQUAL( Vector(-1, 0., 1.), (notunit-unit-otherunit) );
|
---|
198 | };
|
---|
199 |
|
---|
200 | /** UnitTest for scalar products.
|
---|
201 | */
|
---|
202 | void VectorTest::EuclidianScalarProductTest()
|
---|
203 | {
|
---|
204 | CPPUNIT_ASSERT_EQUAL( 0., zero.ScalarProduct(zero) );
|
---|
205 | CPPUNIT_ASSERT_EQUAL( 0., zero.ScalarProduct(unit) );
|
---|
206 | CPPUNIT_ASSERT_EQUAL( 0., zero.ScalarProduct(otherunit) );
|
---|
207 | CPPUNIT_ASSERT_EQUAL( 0., zero.ScalarProduct(notunit) );
|
---|
208 | CPPUNIT_ASSERT_EQUAL( 1., unit.ScalarProduct(unit) );
|
---|
209 | CPPUNIT_ASSERT_EQUAL( 0., otherunit.ScalarProduct(unit) );
|
---|
210 | CPPUNIT_ASSERT_EQUAL( 0., otherunit.ScalarProduct(unit) );
|
---|
211 | CPPUNIT_ASSERT_EQUAL( 1., otherunit.ScalarProduct(notunit) );
|
---|
212 | CPPUNIT_ASSERT_EQUAL( 2., two.ScalarProduct(unit) );
|
---|
213 | CPPUNIT_ASSERT_EQUAL( 1., two.ScalarProduct(otherunit) );
|
---|
214 | CPPUNIT_ASSERT_EQUAL( 1., two.ScalarProduct(notunit) );
|
---|
215 | }
|
---|
216 |
|
---|
217 | /** UnitTest for norms.
|
---|
218 | */
|
---|
219 | void VectorTest::EuclidianNormTest()
|
---|
220 | {
|
---|
221 | CPPUNIT_ASSERT_EQUAL( 0., zero.Norm() );
|
---|
222 | CPPUNIT_ASSERT_EQUAL( 0., zero.NormSquared() );
|
---|
223 | CPPUNIT_ASSERT_EQUAL( 1., unit.Norm() );
|
---|
224 | CPPUNIT_ASSERT_EQUAL( 1., unit.NormSquared() );
|
---|
225 | CPPUNIT_ASSERT_EQUAL( 1., otherunit.Norm() );
|
---|
226 | CPPUNIT_ASSERT_EQUAL( 1., otherunit.NormSquared() );
|
---|
227 | CPPUNIT_ASSERT_EQUAL( 2., notunit.NormSquared() );
|
---|
228 | CPPUNIT_ASSERT_EQUAL( sqrt(2.), notunit.Norm() );
|
---|
229 | }
|
---|
230 |
|
---|
231 | /** UnitTest for distances.
|
---|
232 | */
|
---|
233 | void VectorTest::EuclidianDistancesTest()
|
---|
234 | {
|
---|
235 | CPPUNIT_ASSERT_EQUAL( 1., zero.distance(unit) );
|
---|
236 | CPPUNIT_ASSERT_EQUAL( sqrt(2.), otherunit.distance(unit) );
|
---|
237 | CPPUNIT_ASSERT_EQUAL( sqrt(2.), zero.distance(notunit) );
|
---|
238 | CPPUNIT_ASSERT_EQUAL( 1., otherunit.distance(notunit) );
|
---|
239 | CPPUNIT_ASSERT_EQUAL( sqrt(5.), two.distance(notunit) );
|
---|
240 |
|
---|
241 | // check operator
|
---|
242 | CPPUNIT_ASSERT( !(zero < zero) );
|
---|
243 | CPPUNIT_ASSERT( !(unit < otherunit) );
|
---|
244 | CPPUNIT_ASSERT( zero < unit );
|
---|
245 | CPPUNIT_ASSERT( unit < two );
|
---|
246 | }
|
---|
247 |
|
---|
248 | /** UnitTest for angles.
|
---|
249 | */
|
---|
250 | void VectorTest::EuclidianAnglesTest()
|
---|
251 | {
|
---|
252 | CPPUNIT_ASSERT_EQUAL( M_PI, zero.Angle(unit) );
|
---|
253 | CPPUNIT_ASSERT_EQUAL( 0., unit.Angle(unit) );
|
---|
254 | CPPUNIT_ASSERT_EQUAL( true, fabs(M_PI/2. - otherunit.Angle(unit)) <= LINALG_MYEPSILON() );
|
---|
255 | CPPUNIT_ASSERT_EQUAL( true, fabs(M_PI/2. - unit.Angle(notunit)) <= LINALG_MYEPSILON() );
|
---|
256 | CPPUNIT_ASSERT_EQUAL( true, fabs(M_PI/4. - otherunit.Angle(notunit)) <= LINALG_MYEPSILON() );
|
---|
257 | };
|
---|
258 |
|
---|
259 | /** UnitTest for projections.
|
---|
260 | */
|
---|
261 | void VectorTest::ProjectionTest()
|
---|
262 | {
|
---|
263 | CPPUNIT_ASSERT_EQUAL( zero, zero.Projection(unit) );
|
---|
264 | CPPUNIT_ASSERT_EQUAL( zero, otherunit.Projection(unit) );
|
---|
265 | CPPUNIT_ASSERT_EQUAL( Vector(0.4,0.2,0.), otherunit.Projection(two) );
|
---|
266 | CPPUNIT_ASSERT_EQUAL( Vector(0.,1.,0.), two.Projection(otherunit) );
|
---|
267 | };
|
---|
268 |
|
---|
269 | /**
|
---|
270 | * Unittest for operation with normals
|
---|
271 | */
|
---|
272 | void VectorTest::NormalsTest(){
|
---|
273 | Vector testVector;
|
---|
274 | // the zero Vector should produce an error
|
---|
275 | CPPUNIT_ASSERT(!testVector.GetOneNormalVector(zero));
|
---|
276 |
|
---|
277 | // first one-component system
|
---|
278 | CPPUNIT_ASSERT(testVector.GetOneNormalVector(unit));
|
---|
279 | CPPUNIT_ASSERT(testVector.ScalarProduct(unit) <= LINALG_MYEPSILON());
|
---|
280 |
|
---|
281 | // second one-component system
|
---|
282 | CPPUNIT_ASSERT(testVector.GetOneNormalVector(otherunit));
|
---|
283 | CPPUNIT_ASSERT(testVector.ScalarProduct(otherunit) <= LINALG_MYEPSILON());
|
---|
284 |
|
---|
285 | // first two-component system
|
---|
286 | CPPUNIT_ASSERT(testVector.GetOneNormalVector(notunit));
|
---|
287 | CPPUNIT_ASSERT(testVector.ScalarProduct(notunit) <= LINALG_MYEPSILON());
|
---|
288 |
|
---|
289 | // second two-component system
|
---|
290 | CPPUNIT_ASSERT(testVector.GetOneNormalVector(two));
|
---|
291 | CPPUNIT_ASSERT(testVector.ScalarProduct(two) <= LINALG_MYEPSILON());
|
---|
292 |
|
---|
293 | // three component system
|
---|
294 | CPPUNIT_ASSERT(testVector.GetOneNormalVector(three));
|
---|
295 | CPPUNIT_ASSERT(testVector.ScalarProduct(three) <= LINALG_MYEPSILON());
|
---|
296 | }
|
---|