source: src/unittests/vectorunittest.cpp@ 273382

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

Prepared interface of Vector Class for transition to VectorComposites

  • Property mode set to 100644
File size: 9.5 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 = zero + unit;
75 CPPUNIT_ASSERT_EQUAL( true, fixture.IsOne() );
76 fixture = zero - unit;
77 CPPUNIT_ASSERT_EQUAL( true, fixture.IsOne() );
78 CPPUNIT_ASSERT_EQUAL( false, fixture.IsZero() );
79 fixture = zero + zero;
80 CPPUNIT_ASSERT_EQUAL( true, fixture.IsZero() );
81 fixture = notunit - otherunit;
82 CPPUNIT_ASSERT_EQUAL( true, fixture.IsOne() );
83 fixture = unit - otherunit;
84 CPPUNIT_ASSERT_EQUAL( false, fixture.IsOne() );
85 fixture = notunit - unit - otherunit;
86 CPPUNIT_ASSERT_EQUAL( false, fixture.IsZero() );
87 fixture = 0.98 * unit;
88 CPPUNIT_ASSERT_EQUAL( false, fixture.IsOne() );
89 fixture = 1. * unit;
90 CPPUNIT_ASSERT_EQUAL( true, fixture.IsOne() );
91 factor = 0.98;
92 fixture = factor * unit;
93 CPPUNIT_ASSERT_EQUAL( false, fixture.IsOne() );
94 factor = 1.;
95 fixture = factor * unit;
96 CPPUNIT_ASSERT_EQUAL( true, fixture.IsOne() );
97};
98
99
100/** UnitTest for operator versions of Vector::CopyVector(), Vector::AddVector, Vector::SubtractVector() and Vector::Scale().
101 */
102void VectorTest::OperatorAlgebraTest()
103{
104 // summation and scaling
105 CPPUNIT_ASSERT_EQUAL( true, (zero+unit).IsOne() );
106 CPPUNIT_ASSERT_EQUAL( true, (zero+unit).IsOne() );
107 CPPUNIT_ASSERT_EQUAL( true, (zero-unit).IsOne() );
108 CPPUNIT_ASSERT_EQUAL( false, (zero-unit).IsZero() );
109 CPPUNIT_ASSERT_EQUAL( true, (zero+zero).IsZero() );
110 CPPUNIT_ASSERT_EQUAL( true, (notunit-otherunit).IsOne() );
111 CPPUNIT_ASSERT_EQUAL( false, (unit+otherunit).IsOne() );
112 CPPUNIT_ASSERT_EQUAL( false, (notunit-unit-otherunit).IsZero() );
113 CPPUNIT_ASSERT_EQUAL( false, (unit*0.98).IsOne() );
114 CPPUNIT_ASSERT_EQUAL( true, (unit*1.).IsOne() );
115
116 CPPUNIT_ASSERT_EQUAL( unit, (zero+unit) );
117 CPPUNIT_ASSERT_EQUAL( Vector(0.,0.,1.), (notunit-otherunit) );
118 CPPUNIT_ASSERT_EQUAL( Vector(-1, 0., 1.), (notunit-unit-otherunit) );
119};
120
121/** UnitTest for scalar products.
122 */
123void VectorTest::EuclidianScalarProductTest()
124{
125 CPPUNIT_ASSERT_EQUAL( 0., zero.ScalarProduct(zero) );
126 CPPUNIT_ASSERT_EQUAL( 0., zero.ScalarProduct(unit) );
127 CPPUNIT_ASSERT_EQUAL( 0., zero.ScalarProduct(otherunit) );
128 CPPUNIT_ASSERT_EQUAL( 0., zero.ScalarProduct(notunit) );
129 CPPUNIT_ASSERT_EQUAL( 1., unit.ScalarProduct(unit) );
130 CPPUNIT_ASSERT_EQUAL( 0., otherunit.ScalarProduct(unit) );
131 CPPUNIT_ASSERT_EQUAL( 0., otherunit.ScalarProduct(unit) );
132 CPPUNIT_ASSERT_EQUAL( 1., otherunit.ScalarProduct(notunit) );
133 CPPUNIT_ASSERT_EQUAL( 2., two.ScalarProduct(unit) );
134 CPPUNIT_ASSERT_EQUAL( 1., two.ScalarProduct(otherunit) );
135 CPPUNIT_ASSERT_EQUAL( 1., two.ScalarProduct(notunit) );
136}
137
138/** UnitTest for norms.
139 */
140void VectorTest::EuclidianNormTest()
141{
142 CPPUNIT_ASSERT_EQUAL( 0., zero.Norm() );
143 CPPUNIT_ASSERT_EQUAL( 0., zero.NormSquared() );
144 CPPUNIT_ASSERT_EQUAL( 1., unit.Norm() );
145 CPPUNIT_ASSERT_EQUAL( 1., unit.NormSquared() );
146 CPPUNIT_ASSERT_EQUAL( 1., otherunit.Norm() );
147 CPPUNIT_ASSERT_EQUAL( 1., otherunit.NormSquared() );
148 CPPUNIT_ASSERT_EQUAL( 2., notunit.NormSquared() );
149 CPPUNIT_ASSERT_EQUAL( sqrt(2.), notunit.Norm() );
150}
151
152/** UnitTest for distances.
153 */
154void VectorTest::EuclidianDistancesTest()
155{
156 CPPUNIT_ASSERT_EQUAL( 1., zero.Distance(unit) );
157 CPPUNIT_ASSERT_EQUAL( sqrt(2.), otherunit.Distance(unit) );
158 CPPUNIT_ASSERT_EQUAL( sqrt(2.), zero.Distance(notunit) );
159 CPPUNIT_ASSERT_EQUAL( 1., otherunit.Distance(notunit) );
160 CPPUNIT_ASSERT_EQUAL( sqrt(5.), two.Distance(notunit) );
161}
162
163/** UnitTest for angles.
164 */
165void VectorTest::EuclidianAnglesTest()
166{
167 CPPUNIT_ASSERT_EQUAL( M_PI, zero.Angle(unit) );
168 CPPUNIT_ASSERT_EQUAL( 0., unit.Angle(unit) );
169 CPPUNIT_ASSERT_EQUAL( true, fabs(M_PI/2. - otherunit.Angle(unit)) < MYEPSILON );
170 CPPUNIT_ASSERT_EQUAL( true, fabs(M_PI/2. - unit.Angle(notunit)) < MYEPSILON );
171 CPPUNIT_ASSERT_EQUAL( true, fabs(M_PI/4. - otherunit.Angle(notunit)) < MYEPSILON );
172};
173
174/** UnitTest for projections.
175 */
176void VectorTest::ProjectionTest()
177{
178 CPPUNIT_ASSERT_EQUAL( zero, zero.Projection(unit) );
179 CPPUNIT_ASSERT_EQUAL( zero, otherunit.Projection(unit) );
180 CPPUNIT_ASSERT_EQUAL( Vector(0.4,0.2,0.), otherunit.Projection(two) );
181 CPPUNIT_ASSERT_EQUAL( Vector(0.,1.,0.), two.Projection(otherunit) );
182};
183
184/** UnitTest for line intersections.
185 */
186void VectorTest::LineIntersectionTest()
187{
188 // plane at (0,0,0) normal to (1,0,0) cuts line from (0,0,0) to (2,1,0) at ???
189 CPPUNIT_ASSERT_NO_THROW(fixture = Plane(unit, zero).GetIntersection(zero, two) );
190 CPPUNIT_ASSERT_EQUAL( zero, fixture );
191
192 // plane at (2,1,0) normal to (0,1,0) cuts line from (1,0,0) to (0,1,1) at ???
193 CPPUNIT_ASSERT_NO_THROW(fixture = Plane(otherunit, two).GetIntersection( unit, notunit) );
194 CPPUNIT_ASSERT_EQUAL( Vector(0., 1., 1.), fixture );
195
196 // four vectors equal to zero
197 CPPUNIT_ASSERT_THROW(fixture = GetIntersectionOfTwoLinesOnPlane(zero, zero, zero, zero), LinearDependenceException);
198 //CPPUNIT_ASSERT_EQUAL( zero, fixture );
199
200 // four vectors equal to unit
201 CPPUNIT_ASSERT_THROW(fixture = GetIntersectionOfTwoLinesOnPlane(unit, unit, unit, unit), LinearDependenceException);
202 //CPPUNIT_ASSERT_EQUAL( zero, fixture );
203
204 // two equal lines
205 CPPUNIT_ASSERT_NO_THROW(fixture = GetIntersectionOfTwoLinesOnPlane(unit, two, unit, two));
206 CPPUNIT_ASSERT_EQUAL( unit, fixture );
207
208 // line from (1,0,0) to (2,1,0) cuts line from (1,0,0) to (0,1,0) at ???
209 CPPUNIT_ASSERT_NO_THROW( fixture = GetIntersectionOfTwoLinesOnPlane(unit, two, unit, otherunit) );
210 CPPUNIT_ASSERT_EQUAL( unit, fixture );
211
212 // line from (1,0,0) to (0,0,0) cuts line from (0,0,0) to (2,1,0) at ???
213 CPPUNIT_ASSERT_NO_THROW( fixture = GetIntersectionOfTwoLinesOnPlane(unit, zero, zero, two) );
214 CPPUNIT_ASSERT_EQUAL( zero, fixture );
215
216 // line from (1,0,0) to (2,1,0) cuts line from (0,0,0) to (0,1,0) at ???
217 CPPUNIT_ASSERT_NO_THROW(fixture = GetIntersectionOfTwoLinesOnPlane(unit, two, zero, otherunit) );
218 CPPUNIT_ASSERT_EQUAL( Vector(0., -1., 0.), fixture );
219};
220
221/** UnitTest for vector rotations.
222 */
223void VectorTest::VectorRotationTest()
224{
225 fixture.Init(-1.,0.,0.);
226
227 // zero vector does not change
228 fixture = RotateVector(zero,unit, 1.);
229 CPPUNIT_ASSERT_EQUAL( zero, fixture );
230
231 fixture = RotateVector(zero, two, 1.);
232 CPPUNIT_ASSERT_EQUAL( zero, fixture);
233
234 // vector on axis does not change
235 fixture = RotateVector(unit,unit, 1.);
236 CPPUNIT_ASSERT_EQUAL( unit, fixture );
237
238 // rotations
239 fixture = RotateVector(otherunit, unit, M_PI);
240 CPPUNIT_ASSERT_EQUAL( Vector(0.,-1.,0.), fixture );
241
242 fixture = RotateVector(otherunit, unit, 2. * M_PI);
243 CPPUNIT_ASSERT_EQUAL( otherunit, fixture );
244
245 fixture = RotateVector(otherunit,unit, 0);
246 CPPUNIT_ASSERT_EQUAL( otherunit, fixture );
247
248 fixture = RotateVector(Vector(0.,0.,1.), notunit, M_PI);
249 CPPUNIT_ASSERT_EQUAL( otherunit, fixture );
250}
251
252/**
253 * UnitTest for Vector::IsInParallelepiped().
254 */
255void VectorTest::IsInParallelepipedTest()
256{
257 double parallelepiped[NDIM*NDIM];
258 parallelepiped[0] = 1;
259 parallelepiped[1] = 0;
260 parallelepiped[2] = 0;
261 parallelepiped[3] = 0;
262 parallelepiped[4] = 1;
263 parallelepiped[5] = 0;
264 parallelepiped[6] = 0;
265 parallelepiped[7] = 0;
266 parallelepiped[8] = 1;
267
268 fixture.CopyVector(zero);
269 CPPUNIT_ASSERT_EQUAL( false, fixture.IsInParallelepiped(Vector(2.,2.,2.), parallelepiped) );
270 fixture.Init(2.5,2.5,2.5);
271 CPPUNIT_ASSERT_EQUAL( true, fixture.IsInParallelepiped(Vector(2.,2.,2.), parallelepiped) );
272 fixture.Init(1.,1.,1.);
273 CPPUNIT_ASSERT_EQUAL( false, fixture.IsInParallelepiped(Vector(2.,2.,2.), parallelepiped) );
274 fixture.Init(3.5,3.5,3.5);
275 CPPUNIT_ASSERT_EQUAL( false, fixture.IsInParallelepiped(Vector(2.,2.,2.), parallelepiped) );
276 fixture.Init(2.,2.,2.);
277 CPPUNIT_ASSERT_EQUAL( true, fixture.IsInParallelepiped(Vector(2.,2.,2.), parallelepiped) );
278 fixture.Init(2.,3.,2.);
279 CPPUNIT_ASSERT_EQUAL( true, fixture.IsInParallelepiped(Vector(2.,2.,2.), parallelepiped) );
280 fixture.Init(-2.,2.,-1.);
281 CPPUNIT_ASSERT_EQUAL( false, fixture.IsInParallelepiped(Vector(2.,2.,2.), parallelepiped) );
282}
283
Note: See TracBrowser for help on using the repository browser.