source: LinearAlgebra/src/unittests/VectorContentUnitTest.cpp@ 68c923

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 68c923 was 29cbe9, checked in by Frederik Heber <heber@…>, 13 years ago

Added serialization capabilities to VectorContent and MatrixContent.

  • Property mode set to 100644
File size: 5.1 KB
Line 
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
8/*
9 * VectorContentUnittest.cpp
10 *
11 * Created on: Jan 8, 2010
12 * Author: heber
13 */
14
15// include config.h
16#ifdef HAVE_CONFIG_H
17#include <config.h>
18#endif
19
20using namespace std;
21
22#include <cppunit/CompilerOutputter.h>
23#include <cppunit/extensions/TestFactoryRegistry.h>
24#include <cppunit/ui/text/TestRunner.h>
25
26// include headers that implement a archive in simple text format
27#include <boost/archive/text_oarchive.hpp>
28#include <boost/archive/text_iarchive.hpp>
29
30#include "VectorContentUnitTest.hpp"
31
32#ifdef HAVE_TESTRUNNER
33#include "UnitTestMain.hpp"
34#endif /*HAVE_TESTRUNNER*/
35
36/********************************************** Test classes **************************************/
37
38// Registers the fixture into the 'registry'
39CPPUNIT_TEST_SUITE_REGISTRATION( VectorContentTest );
40
41
42void VectorContentTest::setUp()
43{
44 v = new VectorContent(3);
45};
46
47void VectorContentTest::tearDown()
48{
49 delete(v);
50};
51
52/** Unit test for accessing elements in the vector.
53 *
54 */
55void VectorContentTest::AccessTest()
56{
57 // set
58 for (int i=0;i<3;i++)
59 v->at(i) = (double)i;
60
61 // and check
62 double *ptr = NULL;
63 for (int i=0;i<3;i++) {
64 CPPUNIT_ASSERT_EQUAL( (double)i, v->at(i) );
65 ptr = v->Pointer(i);
66 CPPUNIT_ASSERT_EQUAL( (double)i, *ptr );
67 }
68
69 // out of bounds
70 //CPPUNIT_ASSERT_EQUAL(0., v->at(3) );
71 //CPPUNIT_ASSERT_EQUAL(0., v->at(-1) );
72};
73
74
75/** Unit test for initializing the vector.
76 *
77 */
78void VectorContentTest::InitializaionTest()
79{
80 // set zero
81 v->setZero();
82 for (int i=0;i<3;i++)
83 CPPUNIT_ASSERT_EQUAL( 0., v->at(i) );
84
85 // set basis
86 for (int i=0;i<3;i++) {
87 v->setBasis(i);
88 for (int j=0;j<3;j++)
89 CPPUNIT_ASSERT_EQUAL( i == j ? 1. : 0. , v->at(j) );
90 }
91
92 // set all
93 v->setValue(1.5);
94 for (int i=0;i<3;i++)
95 CPPUNIT_ASSERT_EQUAL( 1.5, v->at(i) );
96};
97
98/** Unit test for copying vectors.
99 *
100 */
101void VectorContentTest::CopyTest()
102{
103 // set basis
104 VectorContent *dest = NULL;
105 for (int i=0;i<3;i++) {
106 v->setBasis(i);
107 dest = new VectorContent(v);
108 for (int j=0;j<3;j++)
109 CPPUNIT_ASSERT_EQUAL( v->at(j) , dest->at(j) );
110
111 delete(dest);
112 }
113};
114
115/** Unit test for exchanging elements of a vector.
116 *
117 */
118void VectorContentTest::ExchangeTest()
119{
120 // set basis
121 for (int i=0;i<3;i++) {
122 v->setBasis(i);
123 v->SwapElements((i)%3,(i+1)%3);
124 for (int j=0;j<3;j++)
125 CPPUNIT_ASSERT_EQUAL( (i+1)%3 == j ? 1. : 0. , v->at(j) );
126 }
127
128 // set to 1,2,3 and reverse
129 for (int i=0;i<3;i++)
130 v->at(i) = (double)(i+1);
131 v->Reverse();
132 for (int j=0;j<3;j++)
133 CPPUNIT_ASSERT_EQUAL( (double)(3-j), v->at(j) );
134};
135
136/** UnitTest for operators.
137 */
138void VectorContentTest::OperatorIsTest()
139{
140 VectorContent zero(3);
141 VectorContent unit(3);
142 zero.setZero();
143 unit.setZero();
144 unit[1] = 1.;
145 // summation and scaling
146 CPPUNIT_ASSERT_EQUAL( true, unit.IsOne() );
147 CPPUNIT_ASSERT_EQUAL( false, zero.IsOne() );
148 CPPUNIT_ASSERT_EQUAL( false, unit.IsZero() );
149 CPPUNIT_ASSERT_EQUAL( true, zero.IsZero() );
150};
151
152/** UnitTest for operators.
153 */
154void VectorContentTest::OperatorAlgebraTest()
155{
156 VectorContent zero(3);
157 VectorContent unit(3);
158 zero.setZero();
159 unit.setZero();
160 unit[1] = 1.;
161 // summation and scaling
162 CPPUNIT_ASSERT_EQUAL( true, (zero+unit).IsOne() );
163 CPPUNIT_ASSERT_EQUAL( true, (zero+unit).IsOne() );
164 CPPUNIT_ASSERT_EQUAL( true, (zero-unit).IsOne() );
165 CPPUNIT_ASSERT_EQUAL( false, (zero-unit).IsZero() );
166 CPPUNIT_ASSERT_EQUAL( true, (zero+zero).IsZero() );
167 CPPUNIT_ASSERT_EQUAL( false, (unit*0.98).IsOne() );
168 CPPUNIT_ASSERT_EQUAL( false, (0.98*unit).IsOne() );
169 CPPUNIT_ASSERT_EQUAL( true, (unit*1.).IsOne() );
170 CPPUNIT_ASSERT_EQUAL( true, (1.*unit).IsOne() );
171
172 CPPUNIT_ASSERT_EQUAL( unit, (zero+unit) );
173 CPPUNIT_ASSERT_EQUAL( zero, (zero+zero) );
174 CPPUNIT_ASSERT_EQUAL( unit, (unit+zero) );
175
176 unit += zero;
177 CPPUNIT_ASSERT_EQUAL( true, unit.IsOne() );
178 unit *= 1.;
179 CPPUNIT_ASSERT_EQUAL( true, unit.IsOne() );
180};
181
182/** Unit test for reading from and writing vector to stream
183 *
184 */
185void VectorContentTest::ReadWriteTest()
186{
187 // set up matrix
188 VectorContent v((size_t)3);
189 for (size_t i=0; i<3;++i)
190 v.at(i) = i+1.;
191
192 // write to stream
193 std::stringstream vectorstream;
194 v.write(vectorstream);
195
196 // parse in dimensions and check
197 size_t vectordimension = VectorContent::preparseVectorDimensions(vectorstream);
198 CPPUNIT_ASSERT_EQUAL( (size_t)3, vectordimension );
199 // parse in vector and check
200 VectorContent w(3, vectorstream);
201 CPPUNIT_ASSERT_EQUAL( v, w );
202}
203
204/** UnitTest for serialization.
205 */
206void VectorContentTest::SerializationTest()
207{
208 v->setZero();
209 (*v)[0] = 0.;
210 (*v)[1] = 1.;
211 (*v)[2] = 2.;
212 // write element to stream
213 std::stringstream stream;
214 boost::archive::text_oarchive oa(stream);
215 oa << v;
216
217 //std::cout << "Contents of archive is " << stream.str() << std::endl;
218
219 // create and open an archive for input
220 boost::archive::text_iarchive ia(stream);
221 // read class state from archive
222 VectorContent *newv;
223
224 ia >> newv;
225
226 CPPUNIT_ASSERT (*v == *newv);
227}
Note: See TracBrowser for help on using the repository browser.