source: src/unittests/vectorunittest.cpp@ 9879f6

Action_Thermostats Add_AtomRandomPerturbation Add_FitFragmentPartialChargesAction Add_RotateAroundBondAction Add_SelectAtomByNameAction Added_ParseSaveFragmentResults AddingActions_SaveParseParticleParameters Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_ParticleName_to_Atom Adding_StructOpt_integration_tests AtomFragments Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.5.4 Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator CombiningParticlePotentialParsing Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_BoundInBox_CenterInBox_MoleculeActions Fix_ChargeSampling_PBC Fix_ChronosMutex Fix_FitPartialCharges Fix_FitPotential_needs_atomicnumbers Fix_ForceAnnealing Fix_IndependentFragmentGrids Fix_ParseParticles Fix_ParseParticles_split_forward_backward_Actions Fix_PopActions Fix_QtFragmentList_sorted_selection Fix_Restrictedkeyset_FragmentMolecule Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns Fix_fitting_potentials Fixes ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion FragmentAction_writes_AtomFragments FragmentMolecule_checks_bonddegrees GeometryObjects Gui_Fixes Gui_displays_atomic_force_velocity ImplicitCharges IndependentFragmentGrids IndependentFragmentGrids_IndividualZeroInstances IndependentFragmentGrids_IntegrationTest IndependentFragmentGrids_Sole_NN_Calculation JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix MoreRobust_FragmentAutomation ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PdbParser_setsAtomName PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks Rewrite_FitPartialCharges RotateToPrincipalAxisSystem_UndoRedo SaturateAtoms_findBestMatching SaturateAtoms_singleDegree StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_LinearAlgebra Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg Switchable_LogView ThirdParty_MPQC_rebuilt_buildsystem TrajectoryDependenant_MaxOrder TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps TremoloParser_setsAtomName Ubuntu_1604_changes stable
Last change on this file since 9879f6 was e6fdbe, checked in by Frederik Heber <heber@…>, 15 years ago

MEMLEAK: In many tests World, MemoryUsageObserver, logger or errorLogger being singletons were instantiated but not destroyed.

  • in some cases respective includes had to be added.
  • Property mode set to 100644
File size: 10.0 KB
Line 
1/*
2 * unittest.cpp
3 *
4 * Created on: Aug 17, 2009
5 * Author: heber
6 */
7
8
9using namespace std;
10
11#include <cppunit/CompilerOutputter.h>
12#include <cppunit/extensions/TestFactoryRegistry.h>
13#include <cppunit/ui/text/TestRunner.h>
14
15#include "defs.hpp"
16#include "log.hpp"
17#include "memoryusageobserver.hpp"
18#include "vector.hpp"
19#include "vectorunittest.hpp"
20
21#ifdef HAVE_TESTRUNNER
22#include "UnitTestMain.hpp"
23#endif /*HAVE_TESTRUNNER*/
24
25/********************************************** Test classes **************************************/
26
27// Registers the fixture into the 'registry'
28CPPUNIT_TEST_SUITE_REGISTRATION( VectorTest );
29
30
31void VectorTest::setUp()
32{
33 zero.Init(0.,0.,0.);
34 unit.Init(1.,0.,0.);
35 otherunit.Init(0.,1.,0.);
36 notunit.Init(0.,1.,1.);
37 two.Init(2.,1.,0.);
38};
39
40
41void VectorTest::tearDown()
42{
43 MemoryUsageObserver::purgeInstance();
44 logger::purgeInstance();
45 errorLogger::purgeInstance();
46};
47
48/** UnitTest for Constructors and Vector::IsZero() and Vector::IsOne().
49 */
50void VectorTest::UnityTest()
51{
52 // unity and zero tests
53 CPPUNIT_ASSERT_EQUAL( true, zero.IsZero() );
54 CPPUNIT_ASSERT_EQUAL( false, zero.IsOne() );
55 CPPUNIT_ASSERT_EQUAL( false, unit.IsZero() );
56 CPPUNIT_ASSERT_EQUAL( true, unit.IsOne() );
57 CPPUNIT_ASSERT_EQUAL( false, notunit.IsOne() );
58 CPPUNIT_ASSERT_EQUAL( true, otherunit.IsOne() );
59 CPPUNIT_ASSERT_EQUAL( false, otherunit.IsZero() );
60};
61
62/** UnitTest for Vector::CopyVector(), Vector::AddVector, Vector::SubtractVector() and Vector::Scale()/
63 */
64void VectorTest::SimpleAlgebraTest()
65{
66 double factor;
67 // copy vector
68 fixture.Init(2.,3.,4.);
69 CPPUNIT_ASSERT_EQUAL( Vector(2.,3.,4.), fixture );
70 // summation and scaling
71 fixture.CopyVector(&zero);
72 fixture.AddVector(&unit);
73 CPPUNIT_ASSERT_EQUAL( true, fixture.IsOne() );
74 fixture.CopyVector(&zero);
75 fixture.SubtractVector(&unit);
76 CPPUNIT_ASSERT_EQUAL( true, fixture.IsOne() );
77 CPPUNIT_ASSERT_EQUAL( false, fixture.IsZero() );
78 fixture.CopyVector(&zero);
79 fixture.AddVector(&zero);
80 CPPUNIT_ASSERT_EQUAL( true, fixture.IsZero() );
81 fixture.CopyVector(&notunit);
82 fixture.SubtractVector(&otherunit);
83 CPPUNIT_ASSERT_EQUAL( true, fixture.IsOne() );
84 fixture.CopyVector(&unit);
85 fixture.AddVector(&otherunit);
86 CPPUNIT_ASSERT_EQUAL( false, fixture.IsOne() );
87 fixture.CopyVector(&notunit);
88 fixture.SubtractVector(&unit);
89 fixture.SubtractVector(&otherunit);
90 CPPUNIT_ASSERT_EQUAL( false, fixture.IsZero() );
91 fixture.CopyVector(&unit);
92 fixture.Scale(0.98);
93 CPPUNIT_ASSERT_EQUAL( false, fixture.IsOne() );
94 fixture.CopyVector(&unit);
95 fixture.Scale(1.);
96 CPPUNIT_ASSERT_EQUAL( true, fixture.IsOne() );
97 fixture.CopyVector(&unit);
98 factor = 0.98;
99 fixture.Scale(factor);
100 CPPUNIT_ASSERT_EQUAL( false, fixture.IsOne() );
101 fixture.CopyVector(&unit);
102 factor = 1.;
103 fixture.Scale(factor);
104 CPPUNIT_ASSERT_EQUAL( true, fixture.IsOne() );
105};
106
107
108/** UnitTest for operator versions of Vector::CopyVector(), Vector::AddVector, Vector::SubtractVector() and Vector::Scale().
109 */
110void VectorTest::OperatorAlgebraTest()
111{
112 // summation and scaling
113 CPPUNIT_ASSERT_EQUAL( true, (zero+unit).IsOne() );
114 CPPUNIT_ASSERT_EQUAL( true, (zero+unit).IsOne() );
115 CPPUNIT_ASSERT_EQUAL( true, (zero-unit).IsOne() );
116 CPPUNIT_ASSERT_EQUAL( false, (zero-unit).IsZero() );
117 CPPUNIT_ASSERT_EQUAL( true, (zero+zero).IsZero() );
118 CPPUNIT_ASSERT_EQUAL( true, (notunit-otherunit).IsOne() );
119 CPPUNIT_ASSERT_EQUAL( false, (unit+otherunit).IsOne() );
120 CPPUNIT_ASSERT_EQUAL( false, (notunit-unit-otherunit).IsZero() );
121 CPPUNIT_ASSERT_EQUAL( false, (unit*0.98).IsOne() );
122 CPPUNIT_ASSERT_EQUAL( true, (unit*1.).IsOne() );
123
124 CPPUNIT_ASSERT_EQUAL( unit, (zero+unit) );
125 CPPUNIT_ASSERT_EQUAL( Vector(0.,0.,1.), (notunit-otherunit) );
126 CPPUNIT_ASSERT_EQUAL( Vector(-1, 0., 1.), (notunit-unit-otherunit) );
127};
128
129/** UnitTest for scalar products.
130 */
131void VectorTest::EuclidianScalarProductTest()
132{
133 CPPUNIT_ASSERT_EQUAL( 0., zero.ScalarProduct(&zero) );
134 CPPUNIT_ASSERT_EQUAL( 0., zero.ScalarProduct(&unit) );
135 CPPUNIT_ASSERT_EQUAL( 0., zero.ScalarProduct(&otherunit) );
136 CPPUNIT_ASSERT_EQUAL( 0., zero.ScalarProduct(&notunit) );
137 CPPUNIT_ASSERT_EQUAL( 1., unit.ScalarProduct(&unit) );
138 CPPUNIT_ASSERT_EQUAL( 0., otherunit.ScalarProduct(&unit) );
139 CPPUNIT_ASSERT_EQUAL( 0., otherunit.ScalarProduct(&unit) );
140 CPPUNIT_ASSERT_EQUAL( 1., otherunit.ScalarProduct(&notunit) );
141 CPPUNIT_ASSERT_EQUAL( 2., two.ScalarProduct(&unit) );
142 CPPUNIT_ASSERT_EQUAL( 1., two.ScalarProduct(&otherunit) );
143 CPPUNIT_ASSERT_EQUAL( 1., two.ScalarProduct(&notunit) );
144}
145
146/** UnitTest for norms.
147 */
148void VectorTest::EuclidianNormTest()
149{
150 CPPUNIT_ASSERT_EQUAL( 0., zero.Norm() );
151 CPPUNIT_ASSERT_EQUAL( 0., zero.NormSquared() );
152 CPPUNIT_ASSERT_EQUAL( 1., unit.Norm() );
153 CPPUNIT_ASSERT_EQUAL( 1., unit.NormSquared() );
154 CPPUNIT_ASSERT_EQUAL( 1., otherunit.Norm() );
155 CPPUNIT_ASSERT_EQUAL( 1., otherunit.NormSquared() );
156 CPPUNIT_ASSERT_EQUAL( 2., notunit.NormSquared() );
157 CPPUNIT_ASSERT_EQUAL( sqrt(2.), notunit.Norm() );
158}
159
160/** UnitTest for distances.
161 */
162void VectorTest::EuclidianDistancesTest()
163{
164 CPPUNIT_ASSERT_EQUAL( 1., zero.Distance(&unit) );
165 CPPUNIT_ASSERT_EQUAL( sqrt(2.), otherunit.Distance(&unit) );
166 CPPUNIT_ASSERT_EQUAL( sqrt(2.), zero.Distance(&notunit) );
167 CPPUNIT_ASSERT_EQUAL( 1., otherunit.Distance(&notunit) );
168 CPPUNIT_ASSERT_EQUAL( sqrt(5.), two.Distance(&notunit) );
169}
170
171/** UnitTest for angles.
172 */
173void VectorTest::EuclidianAnglesTest()
174{
175 CPPUNIT_ASSERT_EQUAL( M_PI, zero.Angle(&unit) );
176 CPPUNIT_ASSERT_EQUAL( 0., unit.Angle(&unit) );
177 CPPUNIT_ASSERT_EQUAL( true, fabs(M_PI/2. - otherunit.Angle(&unit)) < MYEPSILON );
178 CPPUNIT_ASSERT_EQUAL( true, fabs(M_PI/2. - unit.Angle(&notunit)) < MYEPSILON );
179 CPPUNIT_ASSERT_EQUAL( true, fabs(M_PI/4. - otherunit.Angle(&notunit)) < MYEPSILON );
180};
181
182/** UnitTest for projections.
183 */
184void VectorTest::ProjectionTest()
185{
186 CPPUNIT_ASSERT_EQUAL( zero, zero.Projection(&unit) );
187 CPPUNIT_ASSERT_EQUAL( zero, otherunit.Projection(&unit) );
188 CPPUNIT_ASSERT_EQUAL( Vector(0.4,0.2,0.), otherunit.Projection(&two) );
189 CPPUNIT_ASSERT_EQUAL( Vector(0.,1.,0.), two.Projection(&otherunit) );
190};
191
192/** UnitTest for line intersections.
193 */
194void VectorTest::LineIntersectionTest()
195{
196 // plane at (0,0,0) normal to (1,0,0) cuts line from (0,0,0) to (2,1,0) at ???
197 CPPUNIT_ASSERT_EQUAL( true, fixture.GetIntersectionWithPlane(&unit, &zero, &zero, &two) );
198 CPPUNIT_ASSERT_EQUAL( zero, fixture );
199
200 // plane at (2,1,0) normal to (0,1,0) cuts line from (1,0,0) to (0,1,1) at ???
201 CPPUNIT_ASSERT_EQUAL( true, fixture.GetIntersectionWithPlane(&otherunit, &two, &unit, &notunit) );
202 CPPUNIT_ASSERT_EQUAL( Vector(0., 1., 1.), fixture );
203
204 // four vectors equal to zero
205 CPPUNIT_ASSERT_EQUAL( false, fixture.GetIntersectionOfTwoLinesOnPlane(&zero, &zero, &zero, &zero, NULL) );
206 CPPUNIT_ASSERT_EQUAL( zero, fixture );
207
208 // four vectors equal to unit
209 CPPUNIT_ASSERT_EQUAL( false, fixture.GetIntersectionOfTwoLinesOnPlane(&unit, &unit, &unit, &unit, NULL) );
210 CPPUNIT_ASSERT_EQUAL( zero, fixture );
211
212 // two equal lines
213 CPPUNIT_ASSERT_EQUAL( true, fixture.GetIntersectionOfTwoLinesOnPlane(&unit, &two, &unit, &two, NULL) );
214 CPPUNIT_ASSERT_EQUAL( unit, fixture );
215
216 // line from (1,0,0) to (2,1,0) cuts line from (1,0,0) to (0,1,0) at ???
217 CPPUNIT_ASSERT_EQUAL( true, fixture.GetIntersectionOfTwoLinesOnPlane(&unit, &two, &unit, &otherunit, NULL) );
218 CPPUNIT_ASSERT_EQUAL( unit, fixture );
219
220 // line from (1,0,0) to (0,0,0) cuts line from (0,0,0) to (2,1,0) at ???
221 CPPUNIT_ASSERT_EQUAL( true, fixture.GetIntersectionOfTwoLinesOnPlane(&unit, &zero, &zero, &two, NULL) );
222 CPPUNIT_ASSERT_EQUAL( zero, fixture );
223
224 // line from (1,0,0) to (2,1,0) cuts line from (0,0,0) to (0,1,0) at ???
225 CPPUNIT_ASSERT_EQUAL( true, fixture.GetIntersectionOfTwoLinesOnPlane(&unit, &two, &zero, &otherunit, NULL) );
226 CPPUNIT_ASSERT_EQUAL( Vector(0., -1., 0.), fixture );
227};
228
229/** UnitTest for vector rotations.
230 */
231void VectorTest::VectorRotationTest()
232{
233 fixture.Init(-1.,0.,0.);
234
235 // zero vector does not change
236 fixture.CopyVector(&zero);
237 fixture.RotateVector(&unit, 1.);
238 CPPUNIT_ASSERT_EQUAL( zero, fixture );
239
240 fixture.RotateVector(&two, 1.);
241 CPPUNIT_ASSERT_EQUAL( zero, fixture);
242
243 // vector on axis does not change
244 fixture.CopyVector(&unit);
245 fixture.RotateVector(&unit, 1.);
246 CPPUNIT_ASSERT_EQUAL( unit, fixture );
247
248 fixture.RotateVector(&unit, 1.);
249 CPPUNIT_ASSERT_EQUAL( unit, fixture );
250
251 // rotations
252 fixture.CopyVector(&otherunit);
253 fixture.RotateVector(&unit, M_PI);
254 CPPUNIT_ASSERT_EQUAL( Vector(0.,-1.,0.), fixture );
255
256 fixture.CopyVector(&otherunit);
257 fixture.RotateVector(&unit, 2. * M_PI);
258 CPPUNIT_ASSERT_EQUAL( otherunit, fixture );
259
260 fixture.CopyVector(&otherunit);
261 fixture.RotateVector(&unit, 0);
262 CPPUNIT_ASSERT_EQUAL( otherunit, fixture );
263
264 fixture.Init(0.,0.,1.);
265 fixture.RotateVector(&notunit, M_PI);
266 CPPUNIT_ASSERT_EQUAL( otherunit, fixture );
267}
268
269/**
270 * UnitTest for Vector::IsInParallelepiped().
271 */
272void VectorTest::IsInParallelepipedTest()
273{
274 double parallelepiped[NDIM*NDIM];
275 parallelepiped[0] = 1;
276 parallelepiped[1] = 0;
277 parallelepiped[2] = 0;
278 parallelepiped[3] = 0;
279 parallelepiped[4] = 1;
280 parallelepiped[5] = 0;
281 parallelepiped[6] = 0;
282 parallelepiped[7] = 0;
283 parallelepiped[8] = 1;
284
285 fixture.CopyVector(zero);
286 CPPUNIT_ASSERT_EQUAL( false, fixture.IsInParallelepiped(Vector(2.,2.,2.), parallelepiped) );
287 fixture.Init(2.5,2.5,2.5);
288 CPPUNIT_ASSERT_EQUAL( true, fixture.IsInParallelepiped(Vector(2.,2.,2.), parallelepiped) );
289 fixture.Init(1.,1.,1.);
290 CPPUNIT_ASSERT_EQUAL( false, fixture.IsInParallelepiped(Vector(2.,2.,2.), parallelepiped) );
291 fixture.Init(3.5,3.5,3.5);
292 CPPUNIT_ASSERT_EQUAL( false, fixture.IsInParallelepiped(Vector(2.,2.,2.), parallelepiped) );
293 fixture.Init(2.,2.,2.);
294 CPPUNIT_ASSERT_EQUAL( true, fixture.IsInParallelepiped(Vector(2.,2.,2.), parallelepiped) );
295 fixture.Init(2.,3.,2.);
296 CPPUNIT_ASSERT_EQUAL( true, fixture.IsInParallelepiped(Vector(2.,2.,2.), parallelepiped) );
297 fixture.Init(-2.,2.,-1.);
298 CPPUNIT_ASSERT_EQUAL( false, fixture.IsInParallelepiped(Vector(2.,2.,2.), parallelepiped) );
299}
300
Note: See TracBrowser for help on using the repository browser.