source: src/LinearAlgebra/unittests/VectorUnitTest.cpp@ 783e88

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

Removed LinearDependenceException, MultipleSolutionsException and MathException from Exceptions.

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