source: src/unittests/vectorunittest.cpp@ d74077

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

Member variable Vector and element of class atom are now private.

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