source: src/vectorunittest.cpp@ d52e70c

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

Small fixes.

  • FIX: atom::~atom(): Name is free'd
  • config::Load(): MDSteps is set to 0 (switched off) if there is only one step
  • vectorunittest.cpp: includes sorted in alphabetical order
  • Property mode set to 100644
File size: 8.3 KB
RevLine 
[54a746]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"
[d52e70c]16#include "vector.hpp"
17#include "vectorunittest.hpp"
[54a746]18
19/********************************************** Test classes **************************************/
20
21// Registers the fixture into the 'registry'
22CPPUNIT_TEST_SUITE_REGISTRATION( VectorTest );
23
24
25void VectorTest::setUp()
26{
[ef9df36]27 zero.Init(0.,0.,0.);
28 unit.Init(1.,0.,0.);
29 otherunit.Init(0.,1.,0.);
30 notunit.Init(0.,1.,1.);
31 two.Init(2.,1.,0.);
[54a746]32};
33
34
35void VectorTest::tearDown()
36{
37};
38
39/** UnitTest for Constructors and Vector::IsZero() and Vector::IsOne().
40 */
41void VectorTest::UnityTest()
42{
43 // unity and zero tests
44 CPPUNIT_ASSERT_EQUAL( true, zero.IsZero() );
45 CPPUNIT_ASSERT_EQUAL( false, zero.IsOne() );
46 CPPUNIT_ASSERT_EQUAL( false, unit.IsZero() );
47 CPPUNIT_ASSERT_EQUAL( true, unit.IsOne() );
48 CPPUNIT_ASSERT_EQUAL( false, notunit.IsOne() );
49 CPPUNIT_ASSERT_EQUAL( true, otherunit.IsOne() );
50 CPPUNIT_ASSERT_EQUAL( false, otherunit.IsZero() );
51};
52
53/** UnitTest for Vector::CopyVector(), Vector::AddVector, Vector::SubtractVector() and Vector::Scale()/
54 */
55void VectorTest::SimpleAlgebraTest()
56{
57 Vector helper;
58 double factor;
59 // summation and scaling
60 helper.CopyVector(&zero);
61 helper.AddVector(&unit);
62 CPPUNIT_ASSERT_EQUAL( true, helper.IsOne() );
63 helper.CopyVector(&zero);
64 helper.SubtractVector(&unit);
65 CPPUNIT_ASSERT_EQUAL( true, helper.IsOne() );
66 CPPUNIT_ASSERT_EQUAL( false, helper.IsZero() );
67 helper.CopyVector(&zero);
68 helper.AddVector(&zero);
69 CPPUNIT_ASSERT_EQUAL( true, helper.IsZero() );
70 helper.CopyVector(&notunit);
71 helper.SubtractVector(&otherunit);
72 CPPUNIT_ASSERT_EQUAL( true, helper.IsOne() );
73 helper.CopyVector(&unit);
74 helper.AddVector(&otherunit);
75 CPPUNIT_ASSERT_EQUAL( false, helper.IsOne() );
76 helper.CopyVector(&notunit);
77 helper.SubtractVector(&unit);
78 helper.SubtractVector(&otherunit);
79 CPPUNIT_ASSERT_EQUAL( false, helper.IsZero() );
80 helper.CopyVector(&unit);
81 helper.Scale(0.98);
82 CPPUNIT_ASSERT_EQUAL( false, helper.IsOne() );
83 helper.CopyVector(&unit);
84 helper.Scale(1.);
85 CPPUNIT_ASSERT_EQUAL( true, helper.IsOne() );
86 helper.CopyVector(&unit);
87 factor = 0.98;
88 helper.Scale(factor);
89 CPPUNIT_ASSERT_EQUAL( false, helper.IsOne() );
90 helper.CopyVector(&unit);
91 factor = 1.;
92 helper.Scale(factor);
93 CPPUNIT_ASSERT_EQUAL( true, helper.IsOne() );
94};
95
96
97/** UnitTest for operator versions of Vector::CopyVector(), Vector::AddVector, Vector::SubtractVector() and Vector::Scale().
98 */
99void VectorTest::OperatorAlgebraTest()
100{
101 // summation and scaling
102 CPPUNIT_ASSERT_EQUAL( true, (zero+unit).IsOne() );
[ef9df36]103 CPPUNIT_ASSERT_EQUAL( true, (zero+unit).IsOne() );
[54a746]104 CPPUNIT_ASSERT_EQUAL( true, (zero-unit).IsOne() );
105 CPPUNIT_ASSERT_EQUAL( false, (zero-unit).IsZero() );
106 CPPUNIT_ASSERT_EQUAL( true, (zero+zero).IsZero() );
107 CPPUNIT_ASSERT_EQUAL( true, (notunit-otherunit).IsOne() );
108 CPPUNIT_ASSERT_EQUAL( false, (unit+otherunit).IsOne() );
109 CPPUNIT_ASSERT_EQUAL( false, (notunit-unit-otherunit).IsZero() );
110 CPPUNIT_ASSERT_EQUAL( false, (unit*0.98).IsOne() );
111 CPPUNIT_ASSERT_EQUAL( true, (unit*1.).IsOne() );
[ef9df36]112
113 CPPUNIT_ASSERT_EQUAL( unit, (zero+unit) );
114 CPPUNIT_ASSERT_EQUAL( Vector(0.,0.,1.), (notunit-otherunit) );
115 CPPUNIT_ASSERT_EQUAL( Vector(-1, 0., 1.), (notunit-unit-otherunit) );
[54a746]116};
117
[ef9df36]118/** UnitTest for scalar products.
[54a746]119 */
[ef9df36]120void VectorTest::EuclidianScalarProductTest()
[54a746]121{
122 CPPUNIT_ASSERT_EQUAL( 0., zero.ScalarProduct(&zero) );
123 CPPUNIT_ASSERT_EQUAL( 0., zero.ScalarProduct(&unit) );
124 CPPUNIT_ASSERT_EQUAL( 0., zero.ScalarProduct(&otherunit) );
125 CPPUNIT_ASSERT_EQUAL( 0., zero.ScalarProduct(&notunit) );
126 CPPUNIT_ASSERT_EQUAL( 1., unit.ScalarProduct(&unit) );
127 CPPUNIT_ASSERT_EQUAL( 0., otherunit.ScalarProduct(&unit) );
128 CPPUNIT_ASSERT_EQUAL( 0., otherunit.ScalarProduct(&unit) );
129 CPPUNIT_ASSERT_EQUAL( 1., otherunit.ScalarProduct(&notunit) );
130 CPPUNIT_ASSERT_EQUAL( 2., two.ScalarProduct(&unit) );
131 CPPUNIT_ASSERT_EQUAL( 1., two.ScalarProduct(&otherunit) );
132 CPPUNIT_ASSERT_EQUAL( 1., two.ScalarProduct(&notunit) );
[ef9df36]133}
[54a746]134
[ef9df36]135/** UnitTest for norms.
136 */
137void VectorTest::EuclidianNormTest()
138{
[54a746]139 CPPUNIT_ASSERT_EQUAL( 0., zero.Norm() );
140 CPPUNIT_ASSERT_EQUAL( 0., zero.NormSquared() );
141 CPPUNIT_ASSERT_EQUAL( 1., unit.Norm() );
142 CPPUNIT_ASSERT_EQUAL( 1., unit.NormSquared() );
143 CPPUNIT_ASSERT_EQUAL( 1., otherunit.Norm() );
144 CPPUNIT_ASSERT_EQUAL( 1., otherunit.NormSquared() );
145 CPPUNIT_ASSERT_EQUAL( 2., notunit.NormSquared() );
146 CPPUNIT_ASSERT_EQUAL( sqrt(2.), notunit.Norm() );
[ef9df36]147}
[54a746]148
[ef9df36]149/** UnitTest for distances.
150 */
151void VectorTest::EuclidianDistancesTest()
152{
[54a746]153 CPPUNIT_ASSERT_EQUAL( 1., zero.Distance(&unit) );
154 CPPUNIT_ASSERT_EQUAL( sqrt(2.), otherunit.Distance(&unit) );
155 CPPUNIT_ASSERT_EQUAL( sqrt(2.), zero.Distance(&notunit) );
156 CPPUNIT_ASSERT_EQUAL( 1., otherunit.Distance(&notunit) );
157 CPPUNIT_ASSERT_EQUAL( sqrt(5.), two.Distance(&notunit) );
[ef9df36]158}
[54a746]159
[ef9df36]160/** UnitTest for angles.
161 */
162void VectorTest::EuclidianAnglesTest()
163{
[54a746]164 CPPUNIT_ASSERT_EQUAL( M_PI, zero.Angle(&unit) );
[ef9df36]165 CPPUNIT_ASSERT_EQUAL( 0., unit.Angle(&unit) );
166 CPPUNIT_ASSERT_EQUAL( true, fabs(M_PI/2. - otherunit.Angle(&unit)) < MYEPSILON );
167 CPPUNIT_ASSERT_EQUAL( true, fabs(M_PI/2. - unit.Angle(&notunit)) < MYEPSILON );
168 CPPUNIT_ASSERT_EQUAL( true, fabs(M_PI/4. - otherunit.Angle(&notunit)) < MYEPSILON );
[54a746]169};
170
[ef9df36]171/** UnitTest for projections.
172 */
173void VectorTest::ProjectionTest()
174{
175 CPPUNIT_ASSERT_EQUAL( zero, zero.Projection(&unit) );
176 CPPUNIT_ASSERT_EQUAL( zero, otherunit.Projection(&unit) );
177 CPPUNIT_ASSERT_EQUAL( Vector(0.4,0.2,0.), otherunit.Projection(&two) );
178 CPPUNIT_ASSERT_EQUAL( Vector(0.,1.,0.), two.Projection(&otherunit) );
179};
180
181/** UnitTest for line intersections.
182 */
183void VectorTest::LineIntersectionTest()
184{
185 Vector helper;
186 // plane at (0,0,0) normal to (1,0,0) cuts line from (0,0,0) to (2,1,0) at ???
187 CPPUNIT_ASSERT_EQUAL( true, helper.GetIntersectionWithPlane((ofstream *)&cout, &unit, &zero, &zero, &two) );
188 CPPUNIT_ASSERT_EQUAL( zero, helper );
189
190 // plane at (2,1,0) normal to (0,1,0) cuts line from (1,0,0) to (0,1,1) at ???
191 CPPUNIT_ASSERT_EQUAL( true, helper.GetIntersectionWithPlane((ofstream *)&cout, &otherunit, &two, &unit, &notunit) );
192 CPPUNIT_ASSERT_EQUAL( Vector(0., 1., 1.), helper );
193
194 // four vectors equal to zero
195 CPPUNIT_ASSERT_EQUAL( false, helper.GetIntersectionOfTwoLinesOnPlane((ofstream *)&cout, &zero, &zero, &zero, &zero, NULL) );
196 CPPUNIT_ASSERT_EQUAL( zero, helper );
197
198 // four vectors equal to unit
199 CPPUNIT_ASSERT_EQUAL( false, helper.GetIntersectionOfTwoLinesOnPlane((ofstream *)&cout, &unit, &unit, &unit, &unit, NULL) );
200 CPPUNIT_ASSERT_EQUAL( zero, helper );
201
202 // two equal lines
203 CPPUNIT_ASSERT_EQUAL( true, helper.GetIntersectionOfTwoLinesOnPlane((ofstream *)&cout, &unit, &two, &unit, &two, NULL) );
204 CPPUNIT_ASSERT_EQUAL( unit, helper );
205
206 // line from (1,0,0) to (2,1,0) cuts line from (1,0,0) to (0,1,0) at ???
207 CPPUNIT_ASSERT_EQUAL( true, helper.GetIntersectionOfTwoLinesOnPlane((ofstream *)&cout, &unit, &two, &unit, &otherunit, NULL) );
208 CPPUNIT_ASSERT_EQUAL( unit, helper );
209
210 // line from (1,0,0) to (0,0,0) cuts line from (0,0,0) to (2,1,0) at ???
211 CPPUNIT_ASSERT_EQUAL( true, helper.GetIntersectionOfTwoLinesOnPlane((ofstream *)&cout, &unit, &zero, &zero, &two, NULL) );
212 CPPUNIT_ASSERT_EQUAL( zero, helper );
213
214 // line from (1,0,0) to (2,1,0) cuts line from (0,0,0) to (0,1,0) at ???
215 CPPUNIT_ASSERT_EQUAL( true, helper.GetIntersectionOfTwoLinesOnPlane((ofstream *)&cout, &unit, &two, &zero, &otherunit, NULL) );
216 CPPUNIT_ASSERT_EQUAL( Vector(0., -1., 0.), helper );
217};
[54a746]218
219/********************************************** Main routine **************************************/
220
221int main(int argc, char **argv)
222{
223 // Get the top level suite from the registry
224 CppUnit::Test *suite = CppUnit::TestFactoryRegistry::getRegistry().makeTest();
225
226 // Adds the test to the list of test to run
227 CppUnit::TextUi::TestRunner runner;
228 runner.addTest( suite );
229
230 // Change the default outputter to a compiler error format outputter
231 runner.setOutputter( new CppUnit::CompilerOutputter( &runner.result(),
232 std::cerr ) );
233 // Run the tests.
234 bool wasSucessful = runner.run();
235
236 // Return error code 1 if the one of test failed.
237 return wasSucessful ? 0 : 1;
238};
Note: See TracBrowser for help on using the repository browser.