source: LinearAlgebra/src/unittests/VectorUnitTest.cpp@ dc8827

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

Moved LinearAlgebra from folder src/ into distinct sub-package.

  • src/LinearAlgebra folder is now LinearAlgebra/src/LinearAlgebra due to include consistency.
  • src/LinearAlgebra/unittests have been moved to LinearAlgebra/unittests.
  • All Makefile.am's had to be changed due to changed location of library.
  • renamed libMolecuilderLinearAlgebra to libLinearAlgebra.
  • CONFIG_AUX_DIR is build-aux 9for molecuilder and LinearAlgebra).
  • m4 is symlinked from below.
  • build-aux now contains small README such that it is created automatically.
  • Property mode set to 100644
File size: 8.6 KB
RevLine 
[bcf653]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
[54a746]8/*
9 * unittest.cpp
10 *
11 * Created on: Aug 17, 2009
12 * Author: heber
13 */
14
[bf3817]15// include config.h
16#ifdef HAVE_CONFIG_H
17#include <config.h>
18#endif
[54a746]19
20using namespace std;
21
22#include <cppunit/CompilerOutputter.h>
23#include <cppunit/extensions/TestFactoryRegistry.h>
24#include <cppunit/ui/text/TestRunner.h>
25
[9b410d]26#include <limits>
27
[ad011c]28#include "CodePatterns/Log.hpp"
[bf4b9f]29#include "defs.hpp"
30#include "Plane.hpp"
31#include "RealSpaceMatrix.hpp"
32#include "Vector.hpp"
33#include "vector_ops.hpp"
[54a746]34
[dfafe7]35#include "VectorUnitTest.hpp"
36
[9b6b2f]37#ifdef HAVE_TESTRUNNER
38#include "UnitTestMain.hpp"
39#endif /*HAVE_TESTRUNNER*/
40
[1bd79e]41#include <iostream>
42
[54a746]43/********************************************** Test classes **************************************/
44
45// Registers the fixture into the 'registry'
46CPPUNIT_TEST_SUITE_REGISTRATION( VectorTest );
47
48
49void VectorTest::setUp()
50{
[1bd79e]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.);
[1829c4]56 three = Vector(1,2,3);
[54a746]57};
58
59
60void VectorTest::tearDown()
61{
[e6fdbe]62 logger::purgeInstance();
63 errorLogger::purgeInstance();
[54a746]64};
65
[d74077]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
[54a746]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;
[78b73c]134 // copy vector
[1bd79e]135 fixture = Vector(2.,3.,4.);
[78b73c]136 CPPUNIT_ASSERT_EQUAL( Vector(2.,3.,4.), fixture );
[54a746]137 // summation and scaling
[273382]138 fixture = zero + unit;
[78b73c]139 CPPUNIT_ASSERT_EQUAL( true, fixture.IsOne() );
[273382]140 fixture = zero - unit;
[78b73c]141 CPPUNIT_ASSERT_EQUAL( true, fixture.IsOne() );
142 CPPUNIT_ASSERT_EQUAL( false, fixture.IsZero() );
[273382]143 fixture = zero + zero;
[78b73c]144 CPPUNIT_ASSERT_EQUAL( true, fixture.IsZero() );
[273382]145 fixture = notunit - otherunit;
[78b73c]146 CPPUNIT_ASSERT_EQUAL( true, fixture.IsOne() );
[273382]147 fixture = unit - otherunit;
[78b73c]148 CPPUNIT_ASSERT_EQUAL( false, fixture.IsOne() );
[273382]149 fixture = notunit - unit - otherunit;
[78b73c]150 CPPUNIT_ASSERT_EQUAL( false, fixture.IsZero() );
[273382]151 fixture = 0.98 * unit;
[78b73c]152 CPPUNIT_ASSERT_EQUAL( false, fixture.IsOne() );
[273382]153 fixture = 1. * unit;
[78b73c]154 CPPUNIT_ASSERT_EQUAL( true, fixture.IsOne() );
[54a746]155 factor = 0.98;
[273382]156 fixture = factor * unit;
[78b73c]157 CPPUNIT_ASSERT_EQUAL( false, fixture.IsOne() );
[54a746]158 factor = 1.;
[273382]159 fixture = factor * unit;
[78b73c]160 CPPUNIT_ASSERT_EQUAL( true, fixture.IsOne() );
[54a746]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() );
[ef9df36]170 CPPUNIT_ASSERT_EQUAL( true, (zero+unit).IsOne() );
[54a746]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() );
[ef9df36]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) );
[54a746]183};
184
[ef9df36]185/** UnitTest for scalar products.
[54a746]186 */
[ef9df36]187void VectorTest::EuclidianScalarProductTest()
[54a746]188{
[273382]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) );
[ef9df36]200}
[54a746]201
[ef9df36]202/** UnitTest for norms.
203 */
204void VectorTest::EuclidianNormTest()
205{
[54a746]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() );
[ef9df36]214}
[54a746]215
[ef9df36]216/** UnitTest for distances.
217 */
218void VectorTest::EuclidianDistancesTest()
219{
[1513a74]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) );
[ef9df36]225}
[54a746]226
[ef9df36]227/** UnitTest for angles.
228 */
229void VectorTest::EuclidianAnglesTest()
230{
[273382]231 CPPUNIT_ASSERT_EQUAL( M_PI, zero.Angle(unit) );
232 CPPUNIT_ASSERT_EQUAL( 0., unit.Angle(unit) );
[71129f]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() );
[54a746]236};
237
[ef9df36]238/** UnitTest for projections.
239 */
240void VectorTest::ProjectionTest()
241{
[273382]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) );
[ef9df36]246};
247
[1829c4]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));
[71129f]258 CPPUNIT_ASSERT(testVector.ScalarProduct(unit) <= LINALG_MYEPSILON());
[1829c4]259
260 // second one-component system
261 CPPUNIT_ASSERT(testVector.GetOneNormalVector(otherunit));
[71129f]262 CPPUNIT_ASSERT(testVector.ScalarProduct(otherunit) <= LINALG_MYEPSILON());
[1829c4]263
264 // first two-component system
265 CPPUNIT_ASSERT(testVector.GetOneNormalVector(notunit));
[71129f]266 CPPUNIT_ASSERT(testVector.ScalarProduct(notunit) <= LINALG_MYEPSILON());
[1829c4]267
268 // second two-component system
269 CPPUNIT_ASSERT(testVector.GetOneNormalVector(two));
[71129f]270 CPPUNIT_ASSERT(testVector.ScalarProduct(two) <= LINALG_MYEPSILON());
[1829c4]271
272 // three component system
273 CPPUNIT_ASSERT(testVector.GetOneNormalVector(three));
[71129f]274 CPPUNIT_ASSERT(testVector.ScalarProduct(three) <= LINALG_MYEPSILON());
[1829c4]275}
Note: See TracBrowser for help on using the repository browser.