source: ThirdParty/LinearAlgebra/src/unittests/VectorContentUnitTest.cpp@ d8a1ff

Action_Thermostats Add_AtomRandomPerturbation Add_RotateAroundBondAction Add_SelectAtomByNameAction Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_StructOpt_integration_tests AutomationFragmentation_failures Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_ChronosMutex Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion GeometryObjects Gui_displays_atomic_force_velocity IndependentFragmentGrids_IntegrationTest JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix PartialCharges_OrthogonalSummation PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks RotateToPrincipalAxisSystem_UndoRedo StoppableMakroAction TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps Ubuntu_1604_changes stable
Last change on this file since d8a1ff was 4ecb2d, checked in by Frederik Heber <heber@…>, 8 years ago

Moved LinearAlgebra sub-package into ThirdParty folder.

  • needed to adapt location of libLinearAlgebra.la in all Makefile.am's.
  • relinked m4 subfolder, relinked am_doxygen_include.am. Both point to those present in molecuilder parent folder.
  • adapted configure.ac's:
  • Property mode set to 100644
File size: 5.8 KB
Line 
1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
4 * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
5 *
6 *
7 * This file is part of MoleCuilder.
8 *
9 * MoleCuilder is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * MoleCuilder is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with MoleCuilder. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23/*
24 * VectorContentUnittest.cpp
25 *
26 * Created on: Jan 8, 2010
27 * Author: heber
28 */
29
30// include config.h
31#ifdef HAVE_CONFIG_H
32#include <config.h>
33#endif
34
35using namespace std;
36
37#include <cppunit/CompilerOutputter.h>
38#include <cppunit/extensions/TestFactoryRegistry.h>
39#include <cppunit/ui/text/TestRunner.h>
40
41// include headers that implement a archive in simple text format
42#include <boost/archive/text_oarchive.hpp>
43#include <boost/archive/text_iarchive.hpp>
44
45#include "VectorContentUnitTest.hpp"
46
47#ifdef HAVE_TESTRUNNER
48#include "UnitTestMain.hpp"
49#endif /*HAVE_TESTRUNNER*/
50
51/********************************************** Test classes **************************************/
52
53// Registers the fixture into the 'registry'
54CPPUNIT_TEST_SUITE_REGISTRATION( VectorContentTest );
55
56
57void VectorContentTest::setUp()
58{
59 v = new VectorContent(3);
60};
61
62void VectorContentTest::tearDown()
63{
64 delete(v);
65};
66
67/** Unit test for accessing elements in the vector.
68 *
69 */
70void VectorContentTest::AccessTest()
71{
72 // set
73 for (int i=0;i<3;i++)
74 v->at(i) = (double)i;
75
76 // and check
77 double *ptr = NULL;
78 for (int i=0;i<3;i++) {
79 CPPUNIT_ASSERT_EQUAL( (double)i, v->at(i) );
80 ptr = v->Pointer(i);
81 CPPUNIT_ASSERT_EQUAL( (double)i, *ptr );
82 }
83
84 // out of bounds
85 //CPPUNIT_ASSERT_EQUAL(0., v->at(3) );
86 //CPPUNIT_ASSERT_EQUAL(0., v->at(-1) );
87};
88
89
90/** Unit test for initializing the vector.
91 *
92 */
93void VectorContentTest::InitializaionTest()
94{
95 // set zero
96 v->setZero();
97 for (int i=0;i<3;i++)
98 CPPUNIT_ASSERT_EQUAL( 0., v->at(i) );
99
100 // set basis
101 for (int i=0;i<3;i++) {
102 v->setBasis(i);
103 for (int j=0;j<3;j++)
104 CPPUNIT_ASSERT_EQUAL( i == j ? 1. : 0. , v->at(j) );
105 }
106
107 // set all
108 v->setValue(1.5);
109 for (int i=0;i<3;i++)
110 CPPUNIT_ASSERT_EQUAL( 1.5, v->at(i) );
111};
112
113/** Unit test for copying vectors.
114 *
115 */
116void VectorContentTest::CopyTest()
117{
118 // set basis
119 VectorContent *dest = NULL;
120 for (int i=0;i<3;i++) {
121 v->setBasis(i);
122 dest = new VectorContent(v);
123 for (int j=0;j<3;j++)
124 CPPUNIT_ASSERT_EQUAL( v->at(j) , dest->at(j) );
125
126 delete(dest);
127 }
128};
129
130/** Unit test for exchanging elements of a vector.
131 *
132 */
133void VectorContentTest::ExchangeTest()
134{
135 // set basis
136 for (int i=0;i<3;i++) {
137 v->setBasis(i);
138 v->SwapElements((i)%3,(i+1)%3);
139 for (int j=0;j<3;j++)
140 CPPUNIT_ASSERT_EQUAL( (i+1)%3 == j ? 1. : 0. , v->at(j) );
141 }
142
143 // set to 1,2,3 and reverse
144 for (int i=0;i<3;i++)
145 v->at(i) = (double)(i+1);
146 v->Reverse();
147 for (int j=0;j<3;j++)
148 CPPUNIT_ASSERT_EQUAL( (double)(3-j), v->at(j) );
149};
150
151/** UnitTest for operators.
152 */
153void VectorContentTest::OperatorIsTest()
154{
155 VectorContent zero(3);
156 VectorContent unit(3);
157 zero.setZero();
158 unit.setZero();
159 unit[1] = 1.;
160 // summation and scaling
161 CPPUNIT_ASSERT_EQUAL( true, unit.IsOne() );
162 CPPUNIT_ASSERT_EQUAL( false, zero.IsOne() );
163 CPPUNIT_ASSERT_EQUAL( false, unit.IsZero() );
164 CPPUNIT_ASSERT_EQUAL( true, zero.IsZero() );
165};
166
167/** UnitTest for operators.
168 */
169void VectorContentTest::OperatorAlgebraTest()
170{
171 VectorContent zero(3);
172 VectorContent unit(3);
173 zero.setZero();
174 unit.setZero();
175 unit[1] = 1.;
176 // summation and scaling
177 CPPUNIT_ASSERT_EQUAL( true, (zero+unit).IsOne() );
178 CPPUNIT_ASSERT_EQUAL( true, (zero+unit).IsOne() );
179 CPPUNIT_ASSERT_EQUAL( true, (zero-unit).IsOne() );
180 CPPUNIT_ASSERT_EQUAL( false, (zero-unit).IsZero() );
181 CPPUNIT_ASSERT_EQUAL( true, (zero+zero).IsZero() );
182 CPPUNIT_ASSERT_EQUAL( false, (unit*0.98).IsOne() );
183 CPPUNIT_ASSERT_EQUAL( false, (0.98*unit).IsOne() );
184 CPPUNIT_ASSERT_EQUAL( true, (unit*1.).IsOne() );
185 CPPUNIT_ASSERT_EQUAL( true, (1.*unit).IsOne() );
186
187 CPPUNIT_ASSERT_EQUAL( unit, (zero+unit) );
188 CPPUNIT_ASSERT_EQUAL( zero, (zero+zero) );
189 CPPUNIT_ASSERT_EQUAL( unit, (unit+zero) );
190
191 unit += zero;
192 CPPUNIT_ASSERT_EQUAL( true, unit.IsOne() );
193 unit *= 1.;
194 CPPUNIT_ASSERT_EQUAL( true, unit.IsOne() );
195};
196
197/** Unit test for reading from and writing vector to stream
198 *
199 */
200void VectorContentTest::ReadWriteTest()
201{
202 // set up matrix
203 VectorContent v((size_t)3);
204 for (size_t i=0; i<3;++i)
205 v.at(i) = i+1.;
206
207 // write to stream
208 std::stringstream vectorstream;
209 v.write(vectorstream);
210
211 // parse in dimensions and check
212 size_t vectordimension = VectorContent::preparseVectorDimensions(vectorstream);
213 CPPUNIT_ASSERT_EQUAL( (size_t)3, vectordimension );
214 // parse in vector and check
215 VectorContent w(3, vectorstream);
216 CPPUNIT_ASSERT_EQUAL( v, w );
217}
218
219/** UnitTest for serialization.
220 */
221void VectorContentTest::SerializationTest()
222{
223 v->setZero();
224 (*v)[0] = 0.;
225 (*v)[1] = 1.;
226 (*v)[2] = 2.;
227 // write element to stream
228 std::stringstream stream;
229 boost::archive::text_oarchive oa(stream);
230 oa << v;
231
232 //std::cout << "Contents of archive is " << stream.str() << std::endl;
233
234 // create and open an archive for input
235 boost::archive::text_iarchive ia(stream);
236 // read class state from archive
237 VectorContent *newv;
238
239 ia >> newv;
240
241 CPPUNIT_ASSERT (*v == *newv);
242}
Note: See TracBrowser for help on using the repository browser.