source: src/unittests/gslmatrixsymmetricunittest.cpp@ c6efc1

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

Tests now work with Eclipse ECUT's TestRunner.

  • new switch in configure.ac: --enable-ecut
  • all tests are compiled as single test as before
  • and a new TestRunner test suite that combines all test into a single executable which can be run as CppUnit program in Eclipse (and then gives JUnit like output).
  • Property mode set to 100644
File size: 6.5 KB
Line 
1/*
2 * gslmatrixunittest.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 "gslmatrixsymmetricunittest.hpp"
15
16#ifdef HAVE_TESTRUNNER
17#include "UnitTestMain.hpp"
18#endif /*HAVE_TESTRUNNER*/
19
20/********************************************** Test classes **************************************/
21
22// Registers the fixture into the 'registry'
23CPPUNIT_TEST_SUITE_REGISTRATION( GSLMatrixSymmetricTest );
24
25
26void GSLMatrixSymmetricTest::setUp()
27{
28 m = new GSLMatrix(3,3);
29};
30
31void GSLMatrixSymmetricTest::tearDown()
32{
33 delete(m);
34};
35
36/** Unit Test for accessing matrix elements.
37 *
38 */
39void GSLMatrixSymmetricTest::AccessTest()
40{
41 // check whether all elements are initially zero
42 for (int i=0;i<3;i++)
43 for (int j=0;j<3;j++)
44 CPPUNIT_ASSERT_EQUAL( 0., m->Get(i,j) );
45
46 // set
47 for (int i=0;i<3;i++)
48 for (int j=0;j<3;j++)
49 m->Set(i,j, i*3+j );
50
51 // and check
52 double *ptr = NULL;
53 for (int i=0;i<3;i++)
54 for (int j=0;j<3;j++) {
55 CPPUNIT_ASSERT_EQUAL( (double)(i*3+j), m->Get(i,j) );
56 ptr = m->Pointer(i,j);
57 CPPUNIT_ASSERT_EQUAL( (double)(i*3+j), *ptr );
58 }
59
60 // assignment
61 for (int i=0;i<3;i++)
62 for (int j=0;j<3;j++)
63 m->Set(i,j, i*3+j );
64 GSLMatrix *dest = new GSLMatrix(3,3);
65 *dest = *m;
66 for (int i=0;i<3;i++)
67 for (int j=0;j<3;j++)
68 CPPUNIT_ASSERT_EQUAL( dest->Get(i,j), m->Get(i,j) );
69 delete(dest);
70
71 // out of bounds
72 //CPPUNIT_ASSERT_EQUAL(0., v->Get(4,2) );
73 //CPPUNIT_ASSERT_EQUAL(0., v->Get(2,17) );
74 //CPPUNIT_ASSERT_EQUAL(0., v->Get(1024,140040) );
75 //CPPUNIT_ASSERT_EQUAL(0., v->Get(-1,0) );
76 //CPPUNIT_ASSERT_EQUAL(0., v->Get(0,-1) );
77 //CPPUNIT_ASSERT_EQUAL(0., v->Get(-1,-1) );
78};
79
80/** Unit Test for initializating matrices.
81 *
82 */
83void GSLMatrixSymmetricTest::InitializationTest()
84{
85 // set zero
86 m->SetZero();
87 for (int i=0;i<3;i++)
88 for (int j=0;j<3;j++)
89 CPPUNIT_ASSERT_EQUAL( 0., m->Get(i,j) );
90
91 // set all
92 m->SetAll(1.5);
93 for (int i=0;i<3;i++)
94 for (int j=0;j<3;j++)
95 CPPUNIT_ASSERT_EQUAL( 1.5, m->Get(i,j) );
96
97 // set basis
98 m->SetIdentity();
99 for (int i=0;i<3;i++)
100 for (int j=0;j<3;j++)
101 CPPUNIT_ASSERT_EQUAL( i == j ? 1. : 0. , m->Get(i,j) );
102
103 // set from array
104 double array[] = { 1., 0., 0.,
105 0., 1., 0.,
106 0., 0., 1. };
107 m->SetFromDoubleArray(array);
108 for (int i=0;i<3;i++)
109 for (int j=0;j<3;j++)
110 CPPUNIT_ASSERT_EQUAL( i == j ? 1. : 0. , m->Get(i,j) );
111
112};
113
114/** Unit Test for copying matrices.
115 *
116 */
117void GSLMatrixSymmetricTest::CopyTest()
118{
119 // set basis
120 GSLMatrix *dest = NULL;
121 for (int i=0;i<3;i++) {
122 m->SetAll(i);
123 dest = new GSLMatrix(m);
124 for (int j=0;j<3;j++)
125 CPPUNIT_ASSERT_EQUAL( m->Get(i,j) , dest->Get(i,j) );
126
127 delete(dest);
128 }
129};
130
131/** Unit Test for exchanging rows and columns.
132 *
133 */
134void GSLMatrixSymmetricTest::ExchangeTest()
135{
136 // set to 1,1,1,2, ...
137 for (int i=0;i<3;i++)
138 for (int j=0;j<3;j++)
139 m->Set(i,j, i+1 );
140
141 // swap such that nothing happens
142 m->SwapColumns(1,2);
143 for (int i=0;i<3;i++)
144 for (int j=0;j<3;j++)
145 CPPUNIT_ASSERT_EQUAL( (double)i+1., m->Get(i,j) );
146
147 // swap two rows
148 m->SwapRows(1,2);
149 for (int i=0;i<3;i++)
150 for (int j=0;j<3;j++)
151 switch (j) {
152 case 0:
153 CPPUNIT_ASSERT_EQUAL( 1., m->Get(j,i) );
154 break;
155 case 1:
156 CPPUNIT_ASSERT_EQUAL( 3., m->Get(j,i) );
157 break;
158 case 2:
159 CPPUNIT_ASSERT_EQUAL( 2., m->Get(j,i) );
160 break;
161 default:
162 CPPUNIT_ASSERT_EQUAL( -1., m->Get(i,j) );
163 }
164 // check that op is reversable
165 m->SwapRows(1,2);
166 for (int i=0;i<3;i++)
167 for (int j=0;j<3;j++)
168 CPPUNIT_ASSERT_EQUAL( (double)i+1., m->Get(i,j) );
169
170 // set to 1,2,3,1, ...
171 for (int i=0;i<3;i++)
172 for (int j=0;j<3;j++)
173 m->Set(i,j, j+1. );
174
175 // swap such that nothing happens
176 m->SwapRows(0,2);
177 for (int i=0;i<3;i++)
178 for (int j=0;j<3;j++)
179 CPPUNIT_ASSERT_EQUAL( (double)j+1., m->Get(i,j) );
180
181 // swap two columns
182 m->SwapColumns(0,2);
183 for (int i=0;i<3;i++)
184 for (int j=0;j<3;j++)
185 switch (j) {
186 case 0:
187 CPPUNIT_ASSERT_EQUAL( 3., m->Get(i,j) );
188 break;
189 case 1:
190 CPPUNIT_ASSERT_EQUAL( 2., m->Get(i,j) );
191 break;
192 case 2:
193 CPPUNIT_ASSERT_EQUAL( 1., m->Get(i,j) );
194 break;
195 default:
196 CPPUNIT_ASSERT_EQUAL( -1., m->Get(i,j) );
197 }
198 // check that op is reversable
199 m->SwapColumns(0,2);
200 for (int i=0;i<3;i++)
201 for (int j=0;j<3;j++)
202 CPPUNIT_ASSERT_EQUAL( (double)j+1., m->Get(i,j) );
203
204
205 // set to 1,2,3,4, ...
206 for (int i=0;i<3;i++)
207 for (int j=0;j<3;j++)
208 m->Set(i,j, 3*i+j+1 );
209 // transpose
210 m->Transpose();
211 for (int i=0;i<3;i++)
212 for (int j=0;j<3;j++)
213 CPPUNIT_ASSERT_EQUAL( (double)(3*i+j+1), m->Get(j,i) );
214 // second transpose
215 m->Transpose();
216 for (int i=0;i<3;i++)
217 for (int j=0;j<3;j++)
218 CPPUNIT_ASSERT_EQUAL( (double)(3*i+j+1), m->Get(i,j) );
219};
220
221/** Unit Test for matrix properties.
222 *
223 */
224void GSLMatrixSymmetricTest::PropertiesTest()
225{
226 // is zero
227 m->SetZero();
228 CPPUNIT_ASSERT_EQUAL( true, m->IsNull() );
229 CPPUNIT_ASSERT_EQUAL( false, m->IsPositive() );
230 CPPUNIT_ASSERT_EQUAL( false, m->IsNegative() );
231 CPPUNIT_ASSERT_EQUAL( true, m->IsNonNegative() );
232
233 // is positive
234 m->SetAll(0.5);
235 CPPUNIT_ASSERT_EQUAL( false, m->IsNull() );
236 CPPUNIT_ASSERT_EQUAL( true, m->IsPositive() );
237 CPPUNIT_ASSERT_EQUAL( false, m->IsNegative() );
238 CPPUNIT_ASSERT_EQUAL( true, m->IsNonNegative() );
239
240 // is negative
241 m->SetAll(-0.1);
242 CPPUNIT_ASSERT_EQUAL( false, m->IsNull() );
243 CPPUNIT_ASSERT_EQUAL( false, m->IsPositive() );
244 CPPUNIT_ASSERT_EQUAL( true, m->IsNegative() );
245 CPPUNIT_ASSERT_EQUAL( false, m->IsNonNegative() );
246
247 // is positive definite
248 double array[] = { 1., 0., 0.,
249 0., 1., 1.,
250 0., 0., 1. };
251 m->SetFromDoubleArray(array);
252 CPPUNIT_ASSERT_EQUAL( true, m->IsPositiveDefinite() );
253
254 //determinant
255 m->SetIdentity();
256 CPPUNIT_ASSERT_EQUAL( 1., m->Determinant() );
257
258 m->SetZero();
259 CPPUNIT_ASSERT_EQUAL( 0., m->Determinant() );
260
261 m->Set( 0, 0, 1.);
262 m->Set( 1, 1, 1.);
263 m->Set( 2, 1, 1.);
264 CPPUNIT_ASSERT_EQUAL( 0., m->Determinant() );
265
266 double array2[] = { 2., 0., 1.,
267 -3., 1., 1.,
268 1., 5.5, 1. };
269 m->SetFromDoubleArray(array2);
270 CPPUNIT_ASSERT_EQUAL( -26.5, m->Determinant() );
271};
Note: See TracBrowser for help on using the repository browser.