source: src/unittests/vectorunittest.cpp@ 84c494

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 84c494 was 9df5c6, checked in by Tillmann Crueger <crueger@…>, 15 years ago

Made the Vector::IsInParallelepiped() method take a matrix instead of a double*

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