source: src/LinearAlgebra/unittests/MatrixContentUnitTest.cpp@ 311da7b

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 311da7b was fff54f, checked in by Frederik Heber <heber@…>, 14 years ago

Moved all Matrix...Unittest to LinearAlgebra/unittests/Matrix...UnitTest.

  • Property mode set to 100644
File size: 6.5 KB
RevLine 
[bcf653]1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
4 * Copyright (C) 2010 University of Bonn. All rights reserved.
5 * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
6 */
7
[fc3b67]8/*
[fff54f]9 * MatrixContentUnitTest.cpp
[fc3b67]10 *
11 * Created on: Jan 8, 2010
12 * Author: heber
13 */
14
[bf3817]15// include config.h
16#ifdef HAVE_CONFIG_H
17#include <config.h>
18#endif
19
[fc3b67]20using namespace std;
21
22#include <cppunit/CompilerOutputter.h>
23#include <cppunit/extensions/TestFactoryRegistry.h>
24#include <cppunit/ui/text/TestRunner.h>
25
[fff54f]26#include "MatrixContentUnitTest.hpp"
[0d4424]27
28#include "LinearAlgebra/MatrixContent.hpp"
[fc3b67]29
[9b6b2f]30#ifdef HAVE_TESTRUNNER
31#include "UnitTestMain.hpp"
32#endif /*HAVE_TESTRUNNER*/
33
[fc3b67]34/********************************************** Test classes **************************************/
35
36// Registers the fixture into the 'registry'
[0d4424]37CPPUNIT_TEST_SUITE_REGISTRATION( MatrixContentTest );
[fc3b67]38
39
[0d4424]40void MatrixContentTest::setUp()
[fc3b67]41{
[0d4424]42 m = new MatrixContent(4,3);
[fc3b67]43};
44
[0d4424]45void MatrixContentTest::tearDown()
[fc3b67]46{
47 delete(m);
48};
49
50/** Unit Test for accessing matrix elements.
51 *
52 */
[0d4424]53void MatrixContentTest::AccessTest()
[fc3b67]54{
55 // check whether all elements are initially zero
56 for (int i=0;i<4;i++)
57 for (int j=0;j<3;j++)
[0d4424]58 CPPUNIT_ASSERT_EQUAL( 0., m->at(i,j) );
[fc3b67]59
60 // set
61 for (int i=0;i<4;i++)
62 for (int j=0;j<3;j++)
[0d4424]63 m->set(i,j, i*3+j );
[fc3b67]64
65 // and check
66 double *ptr = NULL;
67 for (int i=0;i<4;i++)
68 for (int j=0;j<3;j++) {
[0d4424]69 CPPUNIT_ASSERT_EQUAL( (double)(i*3+j), m->at(i,j) );
[fc3b67]70 ptr = m->Pointer(i,j);
71 CPPUNIT_ASSERT_EQUAL( (double)(i*3+j), *ptr );
72 }
73
74 // assignment
75 for (int i=0;i<4;i++)
76 for (int j=0;j<3;j++)
[0d4424]77 m->set(i,j, i*3+j );
78 MatrixContent *dest = new MatrixContent(4,3);
[fc3b67]79 *dest = *m;
80 for (int i=0;i<4;i++)
81 for (int j=0;j<3;j++)
[0d4424]82 CPPUNIT_ASSERT_EQUAL( dest->at(i,j), m->at(i,j) );
[fc3b67]83 delete(dest);
84
85 // out of bounds
[0d4424]86 //CPPUNIT_ASSERT_EQUAL(0., v->at(5,2) );
87 //CPPUNIT_ASSERT_EQUAL(0., v->at(2,17) );
88 //CPPUNIT_ASSERT_EQUAL(0., v->at(1024,140040) );
89 //CPPUNIT_ASSERT_EQUAL(0., v->at(-1,0) );
90 //CPPUNIT_ASSERT_EQUAL(0., v->at(0,-1) );
91 //CPPUNIT_ASSERT_EQUAL(0., v->at(-1,-1) );
[fc3b67]92};
93
94/** Unit Test for initializating matrices.
95 *
96 */
[0d4424]97void MatrixContentTest::InitializationTest()
[fc3b67]98{
99 // set zero
[0d4424]100 m->setZero();
[fc3b67]101 for (int i=0;i<4;i++)
102 for (int j=0;j<3;j++)
[0d4424]103 CPPUNIT_ASSERT_EQUAL( 0., m->at(i,j) );
[fc3b67]104
105 // set all
[0d4424]106 m->setValue(1.5);
[fc3b67]107 for (int i=0;i<4;i++)
108 for (int j=0;j<3;j++)
[0d4424]109 CPPUNIT_ASSERT_EQUAL( 1.5, m->at(i,j) );
[fc3b67]110
111 // set basis
[0d4424]112 m->setIdentity();
[fc3b67]113 for (int i=0;i<4;i++)
114 for (int j=0;j<3;j++)
[0d4424]115 CPPUNIT_ASSERT_EQUAL( i == j ? 1. : 0. , m->at(i,j) );
[fc3b67]116
117 // set from array
118 double array[] = { 1., 0., 0.,
119 0., 1., 0.,
120 0., 0., 1.,
121 0., 0., 0. };
[0d4424]122 m->setFromDoubleArray(array);
[fc3b67]123 for (int i=0;i<4;i++)
124 for (int j=0;j<3;j++)
[0d4424]125 CPPUNIT_ASSERT_EQUAL( i == j ? 1. : 0. , m->at(i,j) );
[fc3b67]126
127};
128
129/** Unit Test for copying matrices.
130 *
131 */
[0d4424]132void MatrixContentTest::CopyTest()
[fc3b67]133{
134 // set basis
[0d4424]135 MatrixContent *dest = NULL;
[fc3b67]136 for (int i=0;i<4;i++) {
[0d4424]137 m->setValue(i);
138 dest = new MatrixContent(m);
[fc3b67]139 for (int j=0;j<3;j++)
[0d4424]140 CPPUNIT_ASSERT_EQUAL( m->at(i,j) , dest->at(i,j) );
[fc3b67]141
142 delete(dest);
143 }
144};
145
146/** Unit Test for exchanging rows and columns.
147 *
148 */
[0d4424]149void MatrixContentTest::ExchangeTest()
[fc3b67]150{
151 // set to 1,1,1,2, ...
152 for (int i=0;i<4;i++)
153 for (int j=0;j<3;j++)
[0d4424]154 m->set(i,j, i+1 );
[fc3b67]155
156 // swap such that nothing happens
157 CPPUNIT_ASSERT_EQUAL( true, m->SwapColumns(1,2) );
158 for (int i=0;i<4;i++)
159 for (int j=0;j<3;j++)
[0d4424]160 CPPUNIT_ASSERT_EQUAL( (double)i+1., m->at(i,j) );
[fc3b67]161
162 // swap two rows
163 CPPUNIT_ASSERT_EQUAL( true, m->SwapRows(1,2) );
164 for (int i=0;i<4;i++)
165 for (int j=0;j<3;j++)
166 switch (i) {
167 case 0:
[0d4424]168 CPPUNIT_ASSERT_EQUAL( 1., m->at(i,j) );
[fc3b67]169 break;
170 case 1:
[0d4424]171 CPPUNIT_ASSERT_EQUAL( 3., m->at(i,j) );
[fc3b67]172 break;
173 case 2:
[0d4424]174 CPPUNIT_ASSERT_EQUAL( 2., m->at(i,j) );
[fc3b67]175 break;
176 case 3:
[0d4424]177 CPPUNIT_ASSERT_EQUAL( 4., m->at(i,j) );
[fc3b67]178 break;
179 default:
[0d4424]180 CPPUNIT_ASSERT_EQUAL( -1., m->at(i,j) );
[fc3b67]181 }
182 // check that op is reversable
183 CPPUNIT_ASSERT_EQUAL( true, m->SwapRows(1,2) );
184 for (int i=0;i<4;i++)
185 for (int j=0;j<3;j++)
[0d4424]186 CPPUNIT_ASSERT_EQUAL( (double)i+1., m->at(i,j) );
[fc3b67]187
188 // set to 1,2,3,1, ...
189 for (int i=0;i<4;i++)
190 for (int j=0;j<3;j++)
[0d4424]191 m->set(i,j, j+1. );
[fc3b67]192
193 // swap such that nothing happens
194 CPPUNIT_ASSERT_EQUAL( true, m->SwapRows(0,2) );
195 for (int i=0;i<4;i++)
196 for (int j=0;j<3;j++)
[0d4424]197 CPPUNIT_ASSERT_EQUAL( (double)j+1., m->at(i,j) );
[fc3b67]198
199 // swap two columns
200 CPPUNIT_ASSERT_EQUAL( true, m->SwapColumns(0,2) );
201 for (int i=0;i<4;i++)
202 for (int j=0;j<3;j++)
203 switch (j) {
204 case 0:
[0d4424]205 CPPUNIT_ASSERT_EQUAL( 3., m->at(i,j) );
[fc3b67]206 break;
207 case 1:
[0d4424]208 CPPUNIT_ASSERT_EQUAL( 2., m->at(i,j) );
[fc3b67]209 break;
210 case 2:
[0d4424]211 CPPUNIT_ASSERT_EQUAL( 1., m->at(i,j) );
[fc3b67]212 break;
213 default:
[0d4424]214 CPPUNIT_ASSERT_EQUAL( -1., m->at(i,j) );
[fc3b67]215 }
216 // check that op is reversable
217 CPPUNIT_ASSERT_EQUAL( true, m->SwapColumns(0,2) );
218 for (int i=0;i<4;i++)
219 for (int j=0;j<3;j++)
[0d4424]220 CPPUNIT_ASSERT_EQUAL( (double)j+1., m->at(i,j) );
[fc3b67]221
222
223 // set to 1,2,3,4, ...
[0d4424]224 MatrixContent *n = new MatrixContent(3,4);
[fc3b67]225 for (int i=0;i<4;i++)
[0d4424]226 for (int j=0;j<3;j++) {
227 m->set(i,j, 3*i+j+1 );
228 n->set(j,i, 3*i+j+1 );
229 }
[fc3b67]230 // transpose
[0d4424]231 MatrixContent res = ((const MatrixContent)(*m)).transpose();
232 CPPUNIT_ASSERT( *n == res );
[fc3b67]233 // second transpose
[0d4424]234 MatrixContent res2 = ((const MatrixContent)res).transpose();
235 CPPUNIT_ASSERT( *m == res2 );
[bbf1bd]236 delete n;
[fc3b67]237};
238
239/** Unit Test for matrix properties.
240 *
241 */
[0d4424]242void MatrixContentTest::PropertiesTest()
[fc3b67]243{
244 // is zero
[0d4424]245 m->setZero();
[fc3b67]246 CPPUNIT_ASSERT_EQUAL( true, m->IsNull() );
247 CPPUNIT_ASSERT_EQUAL( false, m->IsPositive() );
248 CPPUNIT_ASSERT_EQUAL( false, m->IsNegative() );
249 CPPUNIT_ASSERT_EQUAL( true, m->IsNonNegative() );
250
251 // is positive
[0d4424]252 m->setValue(0.5);
[fc3b67]253 CPPUNIT_ASSERT_EQUAL( false, m->IsNull() );
254 CPPUNIT_ASSERT_EQUAL( true, m->IsPositive() );
255 CPPUNIT_ASSERT_EQUAL( false, m->IsNegative() );
256 CPPUNIT_ASSERT_EQUAL( true, m->IsNonNegative() );
257
258 // is negative
[0d4424]259 m->setValue(-0.1);
[fc3b67]260 CPPUNIT_ASSERT_EQUAL( false, m->IsNull() );
261 CPPUNIT_ASSERT_EQUAL( false, m->IsPositive() );
262 CPPUNIT_ASSERT_EQUAL( true, m->IsNegative() );
263 CPPUNIT_ASSERT_EQUAL( false, m->IsNonNegative() );
264
265 // is positive definite
266 CPPUNIT_ASSERT_EQUAL( false, m->IsPositiveDefinite() );
267};
Note: See TracBrowser for help on using the repository browser.