source: src/unittests/vectorunittest.cpp@ 13e3c3

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 13e3c3 was bf3817, checked in by Frederik Heber <heber@…>, 14 years ago

Added ifdef HAVE_CONFIG and config.h include to each and every cpp file.

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