source: src/unittests/gslvectorunittest.cpp@ 388049

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

Added operators and state checkers to GSLVector.

  • added operators +,-,*, +=, -=, *=, == and =
  • added IsZero() and IsOne().
  • gslvectorunittest extended with state and operator test.

Signed-off-by: Frederik Heber <heber@…>

  • Property mode set to 100644
File size: 4.2 KB
Line 
1/*
2 * gslvectorunittest.cpp
3 *
4 * Created on: Jan 8, 2010
5 * Author: heber
6 */
7
8using namespace std;
9
10#include <cppunit/CompilerOutputter.h>
11#include <cppunit/extensions/TestFactoryRegistry.h>
12#include <cppunit/ui/text/TestRunner.h>
13
14#include "gslvectorunittest.hpp"
15
16/********************************************** Test classes **************************************/
17
18// Registers the fixture into the 'registry'
19CPPUNIT_TEST_SUITE_REGISTRATION( GSLVectorTest );
20
21
22void GSLVectorTest::setUp()
23{
24 v = new GSLVector(3);
25};
26
27void GSLVectorTest::tearDown()
28{
29 delete(v);
30};
31
32/** Unit test for accessing elements in the vector.
33 *
34 */
35void GSLVectorTest::AccessTest()
36{
37 // set
38 for (int i=0;i<3;i++)
39 v->Set(i,i);
40
41 // and check
42 double *ptr = NULL;
43 for (int i=0;i<3;i++) {
44 CPPUNIT_ASSERT_EQUAL( (double)i, v->Get(i) );
45 ptr = v->Pointer(i);
46 CPPUNIT_ASSERT_EQUAL( (double)i, *ptr );
47 }
48
49 // out of bounds
50 //CPPUNIT_ASSERT_EQUAL(0., v->Get(3) );
51 //CPPUNIT_ASSERT_EQUAL(0., v->Get(-1) );
52};
53
54
55/** Unit test for initializing the vector.
56 *
57 */
58void GSLVectorTest::InitializaionTest()
59{
60 // set zero
61 v->SetZero();
62 for (int i=0;i<3;i++)
63 CPPUNIT_ASSERT_EQUAL( 0., v->Get(i) );
64
65 // set basis
66 for (int i=0;i<3;i++) {
67 v->SetBasis(i);
68 for (int j=0;j<3;j++)
69 CPPUNIT_ASSERT_EQUAL( i == j ? 1. : 0. , v->Get(j) );
70 }
71
72 // set all
73 v->SetAll(1.5);
74 for (int i=0;i<3;i++)
75 CPPUNIT_ASSERT_EQUAL( 1.5, v->Get(i) );
76};
77
78/** Unit test for copying vectors.
79 *
80 */
81void GSLVectorTest::CopyTest()
82{
83 // set basis
84 GSLVector *dest = NULL;
85 for (int i=0;i<3;i++) {
86 v->SetBasis(i);
87 dest = new GSLVector(v);
88 for (int j=0;j<3;j++)
89 CPPUNIT_ASSERT_EQUAL( v->Get(j) , dest->Get(j) );
90
91 delete(dest);
92 }
93};
94
95/** Unit test for exchanging elements of a vector.
96 *
97 */
98void GSLVectorTest::ExchangeTest()
99{
100 // set basis
101 for (int i=0;i<3;i++) {
102 v->SetBasis(i);
103 v->SwapElements((i)%3,(i+1)%3);
104 for (int j=0;j<3;j++)
105 CPPUNIT_ASSERT_EQUAL( (i+1)%3 == j ? 1. : 0. , v->Get(j) );
106 }
107
108 // set to 1,2,3 and reverse
109 for (int i=0;i<3;i++)
110 v->Set(i,i+1);
111 v->Reverse();
112 for (int j=0;j<3;j++)
113 CPPUNIT_ASSERT_EQUAL( (double)(3-j), v->Get(j) );
114};
115
116
117/** UnitTest for operators.
118 */
119void GSLVectorTest::OperatorIsTest()
120{
121 GSLVector zero(3);
122 GSLVector unit(3);
123 zero.SetZero();
124 unit.SetZero();
125 unit.Set(1,1.);
126 // summation and scaling
127 CPPUNIT_ASSERT_EQUAL( true, unit.IsOne() );
128 CPPUNIT_ASSERT_EQUAL( false, zero.IsOne() );
129 CPPUNIT_ASSERT_EQUAL( false, unit.IsZero() );
130 CPPUNIT_ASSERT_EQUAL( true, zero.IsZero() );
131};
132
133/** UnitTest for operators.
134 */
135void GSLVectorTest::OperatorAlgebraTest()
136{
137 GSLVector zero(3);
138 GSLVector unit(3);
139 zero.SetZero();
140 unit.SetZero();
141 unit.Set(1,1.);
142 // summation and scaling
143 CPPUNIT_ASSERT_EQUAL( true, (zero+unit).IsOne() );
144 CPPUNIT_ASSERT_EQUAL( true, (zero+unit).IsOne() );
145 CPPUNIT_ASSERT_EQUAL( true, (zero-unit).IsOne() );
146 CPPUNIT_ASSERT_EQUAL( false, (zero-unit).IsZero() );
147 CPPUNIT_ASSERT_EQUAL( true, (zero+zero).IsZero() );
148 CPPUNIT_ASSERT_EQUAL( false, (unit*0.98).IsOne() );
149 CPPUNIT_ASSERT_EQUAL( false, (0.98*unit).IsOne() );
150 CPPUNIT_ASSERT_EQUAL( true, (unit*1.).IsOne() );
151 CPPUNIT_ASSERT_EQUAL( true, (1.*unit).IsOne() );
152
153 CPPUNIT_ASSERT_EQUAL( unit, (zero+unit) );
154 CPPUNIT_ASSERT_EQUAL( zero, (zero+zero) );
155 CPPUNIT_ASSERT_EQUAL( unit, (unit+zero) );
156
157 unit += zero;
158 CPPUNIT_ASSERT_EQUAL( true, unit.IsOne() );
159 unit *= 1.;
160 CPPUNIT_ASSERT_EQUAL( true, unit.IsOne() );
161};
162
163/********************************************** Main routine **************************************/
164
165int main(int argc, char **argv)
166{
167 // Get the top level suite from the registry
168 CppUnit::Test *suite = CppUnit::TestFactoryRegistry::getRegistry().makeTest();
169
170 // Adds the test to the list of test to run
171 CppUnit::TextUi::TestRunner runner;
172 runner.addTest( suite );
173
174 // Change the default outputter to a compiler error format outputter
175 runner.setOutputter( new CppUnit::CompilerOutputter( &runner.result(),
176 std::cerr ) );
177 // Run the tests.
178 bool wasSucessful = runner.run();
179
180 // Return error code 1 if the one of test failed.
181 return wasSucessful ? 0 : 1;
182};
Note: See TracBrowser for help on using the repository browser.