source: src/Graph/unittests/BondGraphUnitTest.cpp@ 829761

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 829761 was 1ac24b, checked in by Frederik Heber <heber@…>, 13 years ago

Added unit test function SerializationTest to BondGraphUnitTest.

  • 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 University of Bonn. All rights reserved.
5 * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
6 */
7
8/*
9 * BondGraphUnitTest.cpp
10 *
11 * Created on: Oct 29, 2009
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 <iostream>
31#include <stdio.h>
32#include <cstring>
33
34#include "CodePatterns/Assert.hpp"
35
36#include "atom.hpp"
37#include "Bond/bond.hpp"
38#include "CodePatterns/Log.hpp"
39#include "Element/element.hpp"
40#include "Graph/BondGraph.hpp"
41#include "molecule.hpp"
42#include "Element/periodentafel.hpp"
43#include "World.hpp"
44#include "WorldTime.hpp"
45
46#include "BondGraphUnitTest.hpp"
47
48#ifdef HAVE_TESTRUNNER
49#include "UnitTestMain.hpp"
50#endif /*HAVE_TESTRUNNER*/
51
52/********************************************** Test classes **************************************/
53
54// Registers the fixture into the 'registry'
55CPPUNIT_TEST_SUITE_REGISTRATION( BondGraphTest );
56
57
58void BondGraphTest::setUp()
59{
60 atom *Walker = NULL;
61
62 // construct element
63 hydrogen = World::getInstance().getPeriode()->FindElement(1);
64 carbon = World::getInstance().getPeriode()->FindElement(6);
65 CPPUNIT_ASSERT(hydrogen != NULL && "could not find element hydrogen");
66 CPPUNIT_ASSERT(carbon != NULL && "could not find element carbon");
67
68 // construct molecule (tetraeder of hydrogens)
69 TestMolecule = World::getInstance().createMolecule();
70 CPPUNIT_ASSERT(TestMolecule != NULL && "could not create molecule");
71 Walker = World::getInstance().createAtom();
72 CPPUNIT_ASSERT(Walker != NULL && "could not create atom");
73 Walker->setType(carbon);
74 Walker->setPosition(Vector(1., 0., 1. ));
75 TestMolecule->AddAtom(Walker);
76
77 Walker = World::getInstance().createAtom();
78 CPPUNIT_ASSERT(Walker != NULL && "could not create atom");
79 Walker->setType(carbon);
80 Walker->setPosition(Vector(0., 1., 1. ));
81 TestMolecule->AddAtom(Walker);
82
83 Walker = World::getInstance().createAtom();
84 CPPUNIT_ASSERT(Walker != NULL && "could not create atom");
85 Walker->setType(carbon);
86 Walker->setPosition(Vector(1., 1., 0. ));
87 TestMolecule->AddAtom(Walker);
88
89 Walker = World::getInstance().createAtom();
90 CPPUNIT_ASSERT(Walker != NULL && "could not create atom");
91 Walker->setType(carbon);
92 Walker->setPosition(Vector(0., 0., 0. ));
93 TestMolecule->AddAtom(Walker);
94
95 // check that TestMolecule was correctly constructed
96 CPPUNIT_ASSERT_EQUAL( TestMolecule->getAtomCount(), 4 );
97
98 // create stream with table
99 test << ".\tH\tHe\tLi\tBe\tB\tC\n";
100 test << "H\t1.\t1.\t1.\t1.\t1.\t1.2\n";
101 test << "He\t1.\t1.\t1.\t1.\t1.\t1.\n";
102 test << "Li\t1.\t1.\t1.\t1.\t1.\t1.\n";
103 test << "Be\t1.\t1.\t1.\t1.\t1.\t1.\n";
104 test << "B\t1.\t1.\t1.\t1.\t1.\t1.\n";
105 test << "C\t1.2\t1.\t1.\t1.\t1.\t1.5\n";
106 // created bad stream (i.e. non-present file)
107 dummy.setstate(ios::eofbit);
108 CPPUNIT_ASSERT(dummy.eof());
109 BG = new BondGraph(true);
110 CPPUNIT_ASSERT(BG != NULL && "could not create BondGraph");
111};
112
113
114void BondGraphTest::tearDown()
115{
116 // remove the file
117 delete(BG);
118
119 // remove molecule
120 World::getInstance().destroyMolecule(TestMolecule);
121 // note that all the atoms, molecules, the tafel and the elements
122 // are all cleaned when the world is destroyed
123 World::purgeInstance();
124 logger::purgeInstance();
125};
126
127/** Tests whether setup worked.
128 */
129void BondGraphTest::SetupTest()
130{
131 CPPUNIT_ASSERT_EQUAL (false, TestMolecule->empty());
132 CPPUNIT_ASSERT_EQUAL ((size_t)4, TestMolecule->size());
133};
134
135/** UnitTest for BondGraphTest::LoadBondLengthTable().
136 */
137void BondGraphTest::LoadTableTest()
138{
139 CPPUNIT_ASSERT_EQUAL( true , BG->LoadBondLengthTable(test) );
140 CPPUNIT_ASSERT_EQUAL( 1., BG->GetBondLength(0,0) );
141 CPPUNIT_ASSERT_EQUAL( 1.2, BG->GetBondLength(0,5) );
142 CPPUNIT_ASSERT_EQUAL( 1.5, BG->GetBondLength(5,5) );
143};
144
145/** UnitTest for BondGraphTest::CreateAdjacency().
146 */
147void BondGraphTest::ConstructGraphFromTableTest()
148{
149 molecule::iterator Walker = TestMolecule->begin();
150 molecule::iterator Runner = TestMolecule->begin();
151 Runner++;
152 CPPUNIT_ASSERT_EQUAL( true , BG->LoadBondLengthTable(test) );
153 molecule::atomVector Set = TestMolecule->getAtomSet();
154 BG->CreateAdjacency(Set);
155 CPPUNIT_ASSERT( TestMolecule->getBondCount() != 0);
156 CPPUNIT_ASSERT_EQUAL( true , (*Walker)->IsBondedTo(WorldTime::getTime(), (*Runner)) );
157};
158
159/** UnitTest for BondGraphTest::CreateAdjacency().
160 */
161void BondGraphTest::ConstructGraphFromCovalentRadiiTest()
162{
163
164 CPPUNIT_ASSERT_EQUAL( false , BG->LoadBondLengthTable(dummy) );
165 molecule::atomVector Set = TestMolecule->getAtomSet();
166 BG->CreateAdjacency(Set);
167 CPPUNIT_ASSERT( TestMolecule->getBondCount() != 0);
168
169 // this cannot be assured using dynamic IDs
170 //CPPUNIT_ASSERT_EQUAL( true , Walker->IsBondedTo(Runner) );
171};
172
173/** UnitTest for operator==()
174 */
175void BondGraphTest::EqualityTest()
176{
177 // compare to self
178 CPPUNIT_ASSERT( *BG == *BG );
179
180 // create other instance
181 BondGraph *BG2 = new BondGraph(true);
182 CPPUNIT_ASSERT( *BG == *BG2 );
183 BG2->IsAngstroem = false;
184 CPPUNIT_ASSERT( *BG != *BG2 );
185 delete BG2;
186};
187
188/** UnitTest for serialization
189 */
190void BondGraphTest::SerializationTest()
191{
192 // write element to stream
193 std::stringstream stream;
194 boost::archive::text_oarchive oa(stream);
195 oa << BG;
196
197 std::cout << "Contents of archive is " << stream.str() << std::endl;
198
199 // create and open an archive for input
200 boost::archive::text_iarchive ia(stream);
201 // read class state from archive
202 BondGraph *BG2;
203
204 ia >> BG2;
205
206 CPPUNIT_ASSERT (*BG == *BG2);
207
208 delete BG2;
209};
210
Note: See TracBrowser for help on using the repository browser.