source: src/LinearAlgebra/unittests/VectorUnitTest.cpp@ 311da7b

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

CodePatterns places all includes now in subfolder CodePatterns/.

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