source: src/vectorunittest.cpp@ 54a746

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 Candidate_v1.7.0 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 54a746 was 54a746, checked in by Frederik Heber <heber@…>, 16 years ago

Incorporation of Unit test on class Vector.

  • new file leastsquaremin.[ch]pp has least square minimisation which is otherwise unclean between classes molecules and Vector

Unit test (later tests rely on good results of earlier ones)

changes to class Vector:

  • Vector::IsNull() -> IsZero()
  • new function Vector::IsOne() similar to IsZero()
  • BUGFIX: Vector::IsNULL() (thx to unit test :)
  • Tesselation::getAngle() changed due to above rename
  • Property mode set to 100644
File size: 5.8 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 "vectorunittest.hpp"
16#include "vector.hpp"
17#include "defs.hpp"
18
19/********************************************** Test classes **************************************/
20
21// Registers the fixture into the 'registry'
22CPPUNIT_TEST_SUITE_REGISTRATION( VectorTest );
23
24
25void VectorTest::setUp()
26{
27};
28
29
30void VectorTest::tearDown()
31{
32};
33
34/** UnitTest for Constructors and Vector::IsZero() and Vector::IsOne().
35 */
36void VectorTest::UnityTest()
37{
38 Vector zero(0.,0.,0.);
39 Vector unit(1.,0.,0.);
40 Vector otherunit(1./sqrt(2.),0.,1./sqrt(2.));
41 Vector notunit(0.,1.,1.);
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 zero(0.,0.,0.);
58 Vector unit(1.,0.,0.);
59 Vector otherunit(0.,1.,0.);
60 Vector notunit(0.,1.,1.);
61 Vector helper;
62 double factor;
63
64 // summation and scaling
65 helper.CopyVector(&zero);
66 helper.AddVector(&unit);
67 CPPUNIT_ASSERT_EQUAL( true, helper.IsOne() );
68 helper.CopyVector(&zero);
69 helper.SubtractVector(&unit);
70 CPPUNIT_ASSERT_EQUAL( true, helper.IsOne() );
71 CPPUNIT_ASSERT_EQUAL( false, helper.IsZero() );
72 helper.CopyVector(&zero);
73 helper.AddVector(&zero);
74 CPPUNIT_ASSERT_EQUAL( true, helper.IsZero() );
75 helper.CopyVector(&notunit);
76 helper.SubtractVector(&otherunit);
77 CPPUNIT_ASSERT_EQUAL( true, helper.IsOne() );
78 helper.CopyVector(&unit);
79 helper.AddVector(&otherunit);
80 CPPUNIT_ASSERT_EQUAL( false, helper.IsOne() );
81 helper.CopyVector(&notunit);
82 helper.SubtractVector(&unit);
83 helper.SubtractVector(&otherunit);
84 CPPUNIT_ASSERT_EQUAL( false, helper.IsZero() );
85 helper.CopyVector(&unit);
86 helper.Scale(0.98);
87 CPPUNIT_ASSERT_EQUAL( false, helper.IsOne() );
88 helper.CopyVector(&unit);
89 helper.Scale(1.);
90 CPPUNIT_ASSERT_EQUAL( true, helper.IsOne() );
91 helper.CopyVector(&unit);
92 factor = 0.98;
93 helper.Scale(factor);
94 CPPUNIT_ASSERT_EQUAL( false, helper.IsOne() );
95 helper.CopyVector(&unit);
96 factor = 1.;
97 helper.Scale(factor);
98 CPPUNIT_ASSERT_EQUAL( true, helper.IsOne() );
99};
100
101
102/** UnitTest for operator versions of Vector::CopyVector(), Vector::AddVector, Vector::SubtractVector() and Vector::Scale().
103 */
104void VectorTest::OperatorAlgebraTest()
105{
106 Vector zero(0.,0.,0.);
107 Vector unit(1.,0.,0.);
108 Vector otherunit(0.,1.,0.);
109 Vector notunit(0.,1.,1.);
110
111 // summation and scaling
112 CPPUNIT_ASSERT_EQUAL( true, (zero+unit).IsOne() );
113 CPPUNIT_ASSERT_EQUAL( true, (zero-unit).IsOne() );
114 CPPUNIT_ASSERT_EQUAL( false, (zero-unit).IsZero() );
115 CPPUNIT_ASSERT_EQUAL( true, (zero+zero).IsZero() );
116 CPPUNIT_ASSERT_EQUAL( true, (notunit-otherunit).IsOne() );
117 CPPUNIT_ASSERT_EQUAL( false, (unit+otherunit).IsOne() );
118 CPPUNIT_ASSERT_EQUAL( false, (notunit-unit-otherunit).IsZero() );
119 CPPUNIT_ASSERT_EQUAL( false, (unit*0.98).IsOne() );
120 CPPUNIT_ASSERT_EQUAL( true, (unit*1.).IsOne() );
121};
122
123/** UnitTest for scalar products, norms, distances and angles.
124 */
125void VectorTest::EuclidianTest()
126{
127 Vector zero(0.,0.,0.);
128 Vector unit(1.,0.,0.);
129 Vector otherunit(0.,1.,0.);
130 Vector notunit(0.,1.,1.);
131 Vector two(2.,1.,0.);
132
133 // SCP
134 CPPUNIT_ASSERT_EQUAL( 0., zero.ScalarProduct(&zero) );
135 CPPUNIT_ASSERT_EQUAL( 0., zero.ScalarProduct(&unit) );
136 CPPUNIT_ASSERT_EQUAL( 0., zero.ScalarProduct(&otherunit) );
137 CPPUNIT_ASSERT_EQUAL( 0., zero.ScalarProduct(&notunit) );
138 CPPUNIT_ASSERT_EQUAL( 1., unit.ScalarProduct(&unit) );
139 CPPUNIT_ASSERT_EQUAL( 0., otherunit.ScalarProduct(&unit) );
140 CPPUNIT_ASSERT_EQUAL( 0., otherunit.ScalarProduct(&unit) );
141 CPPUNIT_ASSERT_EQUAL( 1., otherunit.ScalarProduct(&notunit) );
142 CPPUNIT_ASSERT_EQUAL( 2., two.ScalarProduct(&unit) );
143 CPPUNIT_ASSERT_EQUAL( 1., two.ScalarProduct(&otherunit) );
144 CPPUNIT_ASSERT_EQUAL( 1., two.ScalarProduct(&notunit) );
145
146 // Norms
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 // distances
157 CPPUNIT_ASSERT_EQUAL( 1., zero.Distance(&unit) );
158 CPPUNIT_ASSERT_EQUAL( sqrt(2.), otherunit.Distance(&unit) );
159 CPPUNIT_ASSERT_EQUAL( sqrt(2.), zero.Distance(&notunit) );
160 CPPUNIT_ASSERT_EQUAL( 1., otherunit.Distance(&notunit) );
161 CPPUNIT_ASSERT_EQUAL( sqrt(5.), two.Distance(&notunit) );
162
163 // Angles
164 CPPUNIT_ASSERT_EQUAL( M_PI, zero.Angle(&unit) );
165};
166
167
168/********************************************** Main routine **************************************/
169
170int main(int argc, char **argv)
171{
172 // Get the top level suite from the registry
173 CppUnit::Test *suite = CppUnit::TestFactoryRegistry::getRegistry().makeTest();
174
175 // Adds the test to the list of test to run
176 CppUnit::TextUi::TestRunner runner;
177 runner.addTest( suite );
178
179 // Change the default outputter to a compiler error format outputter
180 runner.setOutputter( new CppUnit::CompilerOutputter( &runner.result(),
181 std::cerr ) );
182 // Run the tests.
183 bool wasSucessful = runner.run();
184
185 // Return error code 1 if the one of test failed.
186 return wasSucessful ? 0 : 1;
187};
Note: See TracBrowser for help on using the repository browser.