source: src/unittests/vectorunittest.cpp@ 72e7fa

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 72e7fa was 0a4f7f, checked in by Tillmann Crueger <crueger@…>, 15 years ago

Made data internal data-structure of vector class private

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