source: src/unittests/gslmatrixunittest.cpp@ 9879f6

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 9879f6 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.2 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 "gslmatrixunittest.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( GSLMatrixTest );
24
25
26void GSLMatrixTest::setUp()
27{
28 m = new GSLMatrix(4,3);
29};
30
31void GSLMatrixTest::tearDown()
32{
33 delete(m);
34};
35
36/** Unit Test for accessing matrix elements.
37 *
38 */
39void GSLMatrixTest::AccessTest()
40{
41 // check whether all elements are initially zero
42 for (int i=0;i<4;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<4;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<4;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<4;i++)
62 for (int j=0;j<3;j++)
63 m->Set(i,j, i*3+j );
64 GSLMatrix *dest = new GSLMatrix(4,3);
65 *dest = *m;
66 for (int i=0;i<4;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(5,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 GSLMatrixTest::InitializationTest()
84{
85 // set zero
86 m->SetZero();
87 for (int i=0;i<4;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<4;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<4;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 0., 0., 0. };
108 m->SetFromDoubleArray(array);
109 for (int i=0;i<4;i++)
110 for (int j=0;j<3;j++)
111 CPPUNIT_ASSERT_EQUAL( i == j ? 1. : 0. , m->Get(i,j) );
112
113};
114
115/** Unit Test for copying matrices.
116 *
117 */
118void GSLMatrixTest::CopyTest()
119{
120 // set basis
121 GSLMatrix *dest = NULL;
122 for (int i=0;i<4;i++) {
123 m->SetAll(i);
124 dest = new GSLMatrix(m);
125 for (int j=0;j<3;j++)
126 CPPUNIT_ASSERT_EQUAL( m->Get(i,j) , dest->Get(i,j) );
127
128 delete(dest);
129 }
130};
131
132/** Unit Test for exchanging rows and columns.
133 *
134 */
135void GSLMatrixTest::ExchangeTest()
136{
137 // set to 1,1,1,2, ...
138 for (int i=0;i<4;i++)
139 for (int j=0;j<3;j++)
140 m->Set(i,j, i+1 );
141
142 // swap such that nothing happens
143 CPPUNIT_ASSERT_EQUAL( true, m->SwapColumns(1,2) );
144 for (int i=0;i<4;i++)
145 for (int j=0;j<3;j++)
146 CPPUNIT_ASSERT_EQUAL( (double)i+1., m->Get(i,j) );
147
148 // swap two rows
149 CPPUNIT_ASSERT_EQUAL( true, m->SwapRows(1,2) );
150 for (int i=0;i<4;i++)
151 for (int j=0;j<3;j++)
152 switch (i) {
153 case 0:
154 CPPUNIT_ASSERT_EQUAL( 1., m->Get(i,j) );
155 break;
156 case 1:
157 CPPUNIT_ASSERT_EQUAL( 3., m->Get(i,j) );
158 break;
159 case 2:
160 CPPUNIT_ASSERT_EQUAL( 2., m->Get(i,j) );
161 break;
162 case 3:
163 CPPUNIT_ASSERT_EQUAL( 4., m->Get(i,j) );
164 break;
165 default:
166 CPPUNIT_ASSERT_EQUAL( -1., m->Get(i,j) );
167 }
168 // check that op is reversable
169 CPPUNIT_ASSERT_EQUAL( true, m->SwapRows(1,2) );
170 for (int i=0;i<4;i++)
171 for (int j=0;j<3;j++)
172 CPPUNIT_ASSERT_EQUAL( (double)i+1., m->Get(i,j) );
173
174 // set to 1,2,3,1, ...
175 for (int i=0;i<4;i++)
176 for (int j=0;j<3;j++)
177 m->Set(i,j, j+1. );
178
179 // swap such that nothing happens
180 CPPUNIT_ASSERT_EQUAL( true, m->SwapRows(0,2) );
181 for (int i=0;i<4;i++)
182 for (int j=0;j<3;j++)
183 CPPUNIT_ASSERT_EQUAL( (double)j+1., m->Get(i,j) );
184
185 // swap two columns
186 CPPUNIT_ASSERT_EQUAL( true, m->SwapColumns(0,2) );
187 for (int i=0;i<4;i++)
188 for (int j=0;j<3;j++)
189 switch (j) {
190 case 0:
191 CPPUNIT_ASSERT_EQUAL( 3., m->Get(i,j) );
192 break;
193 case 1:
194 CPPUNIT_ASSERT_EQUAL( 2., m->Get(i,j) );
195 break;
196 case 2:
197 CPPUNIT_ASSERT_EQUAL( 1., m->Get(i,j) );
198 break;
199 default:
200 CPPUNIT_ASSERT_EQUAL( -1., m->Get(i,j) );
201 }
202 // check that op is reversable
203 CPPUNIT_ASSERT_EQUAL( true, m->SwapColumns(0,2) );
204 for (int i=0;i<4;i++)
205 for (int j=0;j<3;j++)
206 CPPUNIT_ASSERT_EQUAL( (double)j+1., m->Get(i,j) );
207
208
209 // set to 1,2,3,4, ...
210 for (int i=0;i<4;i++)
211 for (int j=0;j<3;j++)
212 m->Set(i,j, 3*i+j+1 );
213 // transpose
214 CPPUNIT_ASSERT_EQUAL( true, m->Transpose() );
215 for (int i=0;i<4;i++)
216 for (int j=0;j<3;j++)
217 CPPUNIT_ASSERT_EQUAL( (double)(3*i+j+1), m->Get(j,i) );
218 // second transpose
219 CPPUNIT_ASSERT_EQUAL( true, m->Transpose() );
220 for (int i=0;i<4;i++)
221 for (int j=0;j<3;j++)
222 CPPUNIT_ASSERT_EQUAL( (double)(3*i+j+1), m->Get(i,j) );
223};
224
225/** Unit Test for matrix properties.
226 *
227 */
228void GSLMatrixTest::PropertiesTest()
229{
230 // is zero
231 m->SetZero();
232 CPPUNIT_ASSERT_EQUAL( true, m->IsNull() );
233 CPPUNIT_ASSERT_EQUAL( false, m->IsPositive() );
234 CPPUNIT_ASSERT_EQUAL( false, m->IsNegative() );
235 CPPUNIT_ASSERT_EQUAL( true, m->IsNonNegative() );
236
237 // is positive
238 m->SetAll(0.5);
239 CPPUNIT_ASSERT_EQUAL( false, m->IsNull() );
240 CPPUNIT_ASSERT_EQUAL( true, m->IsPositive() );
241 CPPUNIT_ASSERT_EQUAL( false, m->IsNegative() );
242 CPPUNIT_ASSERT_EQUAL( true, m->IsNonNegative() );
243
244 // is negative
245 m->SetAll(-0.1);
246 CPPUNIT_ASSERT_EQUAL( false, m->IsNull() );
247 CPPUNIT_ASSERT_EQUAL( false, m->IsPositive() );
248 CPPUNIT_ASSERT_EQUAL( true, m->IsNegative() );
249 CPPUNIT_ASSERT_EQUAL( false, m->IsNonNegative() );
250
251 // is positive definite
252 CPPUNIT_ASSERT_EQUAL( false, m->IsPositiveDefinite() );
253};
Note: See TracBrowser for help on using the repository browser.