source: src/Graph/unittests/CheckAgainstAdjacencyFileUnitTest.cpp@ b78dfd

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 b78dfd was 3501d2, checked in by Frederik Heber <heber@…>, 13 years ago

REFACTOR: CheckAgainstAdjacencyFile now gets File in cstor.

  • we cannot have multiple files, but we may have multiple fragmentations. Hence, CheckAgainstAdjacencyFile should get and parse the File in its cstor and get a set of atoms in its operator() to check against.
  • Property mode set to 100644
File size: 8.0 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 * CheckAgainstAdjacencyFileUnitTest.cpp
25 *
26 * Created on: Oct 17, 2011
27 * Author: heber
28 */
29
30// include config.h
31#ifdef HAVE_CONFIG_H
32#include <config.h>
33#endif
34
35#include "CheckAgainstAdjacencyFileUnitTest.hpp"
36
37#include <cppunit/CompilerOutputter.h>
38#include <cppunit/extensions/TestFactoryRegistry.h>
39#include <cppunit/ui/text/TestRunner.h>
40
41#include <iostream>
42#include <string>
43
44#include "CodePatterns/Assert.hpp"
45#include "CodePatterns/Log.hpp"
46
47#include "Atom/atom.hpp"
48#include "Descriptors/AtomDescriptor.hpp"
49#include "Element/element.hpp"
50#include "Element/periodentafel.hpp"
51#include "Graph/CheckAgainstAdjacencyFile.hpp"
52#include "molecule.hpp"
53#include "World.hpp"
54#include "WorldTime.hpp"
55
56#ifdef HAVE_TESTRUNNER
57#include "UnitTestMain.hpp"
58#endif /*HAVE_TESTRUNNER*/
59
60/********************************************** Test classes **************************************/
61// Registers the fixture into the 'registry'
62CPPUNIT_TEST_SUITE_REGISTRATION( CheckAgainstAdjacencyFileTest );
63
64static std::string adjacencyfile ="\
651 2\n\
662 1 3\n\
673 2 4\n\
684 3 5\n\
695 4 6\n\
706 5 7\n\
717 6 8\n\
728 7 9\n\
739 8 10\n\
7410 9\n";
75
76static std::string wrongadjacencyfile1 ="\
771 2\n\
782 1 3\n\
794 3 5\n\
805 4 6\n\
816 5 7\n\
827 6 8\n\
838 7 9\n\
849 8 10\n\
8510 9\n";
86
87static std::string wrongadjacencyfile2 ="\
881 2\n\
892 1 3\n\
903 2 4\n\
914 3 5\n\
925 4 6\n\
936 5 7\n\
947 6 8\n\
958 7 9\n\
969 8 10\n\
9710 9 11\n\
9811 10";
99
100// set up and tear down
101void CheckAgainstAdjacencyFileTest::setUp()
102{
103 // failing asserts should be thrown
104 ASSERT_DO(Assert::Throw);
105
106 const element *hydrogen = World::getInstance().getPeriode()->FindElement(1);
107 CPPUNIT_ASSERT(hydrogen != NULL);
108
109 TestMolecule = World::getInstance().createMolecule();
110 CPPUNIT_ASSERT(TestMolecule != NULL);
111 for(int i=0;i<ATOM_COUNT;++i){
112 atoms[i]= World::getInstance().createAtom();
113 CPPUNIT_ASSERT(atoms[i] != NULL);
114 atoms[i]->setType(hydrogen);
115 TestMolecule->AddAtom(atoms[i]);
116 atomIds[i]= atoms[i]->getId();
117 }
118 // create linear chain
119 for(int i=0;i<ATOM_COUNT-1;++i){
120 atoms[i]->addBond(WorldTime::getTime(), atoms[i+1]);
121 }
122
123 // create map as it should be
124 for(int i=0;i<ATOM_COUNT;++i) {
125 if (i != 0) // first has only one bond
126 comparisonMap.insert( std::make_pair(atomIds[i], atomIds[i-1]) );
127 if (i != ATOM_COUNT-1) // last has only one bond
128 comparisonMap.insert( std::make_pair(atomIds[i], atomIds[i+1]) );
129 }
130}
131
132void CheckAgainstAdjacencyFileTest::tearDown()
133{
134 comparisonMap.clear();
135
136 // destroy molecule and contained atoms
137 TestMolecule->removeAtomsinMolecule();
138 World::getInstance().destroyMolecule(TestMolecule);
139 // destroy World
140 World::purgeInstance();
141// logger::purgeInstance();
142// errorLogger::purgeInstance();
143}
144
145/** Unit tests for CheckAgainstAdjacencyFile::CreateInternalMap().
146 *
147 */
148void CheckAgainstAdjacencyFileTest::CreateInternalMapTest()
149{
150 std::stringstream input;
151 CheckAgainstAdjacencyFile fileChecker(input);
152 World::getInstance().selectAllAtoms(AllAtoms());
153 const World &world = World::getInstance();
154 fileChecker.CreateInternalMap(world.beginAtomSelection(), world.endAtomSelection());
155
156 // check size (it's 8*2 + 2*1 = 18 keys)
157 CPPUNIT_ASSERT_EQUAL( (size_t)18, fileChecker.InternalAtomBondMap.size() );
158 CPPUNIT_ASSERT_EQUAL( (size_t)0, fileChecker.ExternalAtomBondMap.size() );
159
160 // check equality
161 CPPUNIT_ASSERT( comparisonMap.size() == fileChecker.InternalAtomBondMap.size() );
162// std::cout << "comparisonMap: " << comparisonMap << std::endl;
163// std::cout << "fileChecker.InternalAtomBondMap: " << fileChecker.InternalAtomBondMap << std::endl;
164 CPPUNIT_ASSERT( comparisonMap == fileChecker.InternalAtomBondMap );
165
166 // check non-equality: more
167 comparisonMap.insert( std::make_pair( (atomId_t)10, (atomId_t)8) );
168 CPPUNIT_ASSERT( comparisonMap != fileChecker.InternalAtomBondMap );
169 comparisonMap.erase((atomId_t)10);
170
171 // check non-equality: less
172 comparisonMap.erase((atomId_t)9);
173 CPPUNIT_ASSERT( comparisonMap != fileChecker.InternalAtomBondMap );
174}
175
176/** Unit tests for CheckAgainstAdjacencyFile::ParseInExternalMap().
177 *
178 */
179void CheckAgainstAdjacencyFileTest::ParseInExternalMapTest()
180{
181 std::stringstream input(adjacencyfile);
182 World::getInstance().selectAllAtoms(NoAtoms());
183 CheckAgainstAdjacencyFile fileChecker(input);
184 fileChecker.CreateInternalMap(World::getInstance().beginAtomSelection(), World::getInstance().endAtomSelection());
185
186 // check size (it's 8*2 + 2*1 = 18 keys)
187 CPPUNIT_ASSERT_EQUAL( (size_t)0, fileChecker.InternalAtomBondMap.size() );
188 CPPUNIT_ASSERT_EQUAL( (size_t)18, fileChecker.ExternalAtomBondMap.size() );
189
190 // check equality
191 CPPUNIT_ASSERT( comparisonMap.size() == fileChecker.ExternalAtomBondMap.size() );
192 CPPUNIT_ASSERT( comparisonMap == fileChecker.ExternalAtomBondMap );
193
194 // check non-equality: more
195 comparisonMap.insert( std::make_pair( (atomId_t)10, (atomId_t)8) );
196 CPPUNIT_ASSERT( comparisonMap != fileChecker.ExternalAtomBondMap );
197 comparisonMap.erase((atomId_t)10);
198
199 // check non-equality: less
200 comparisonMap.erase((atomId_t)9);
201 CPPUNIT_ASSERT( comparisonMap != fileChecker.ExternalAtomBondMap );
202}
203
204/** Unit tests for CheckAgainstAdjacencyFile::CompareInternalExternalMap().
205 *
206 */
207void CheckAgainstAdjacencyFileTest::CompareInternalExternalMapTest()
208{
209 std::stringstream input(adjacencyfile);
210 CheckAgainstAdjacencyFile fileChecker(input);
211
212 // assert non-equality before parsing
213 CPPUNIT_ASSERT( fileChecker.InternalAtomBondMap.size() != fileChecker.ExternalAtomBondMap.size() );
214 CPPUNIT_ASSERT( fileChecker.InternalAtomBondMap != fileChecker.ExternalAtomBondMap );
215 CPPUNIT_ASSERT( !fileChecker.CompareInternalExternalMap() );
216
217 // parse
218 World::getInstance().selectAllAtoms(AllAtoms());
219 fileChecker.CreateInternalMap(World::getInstance().beginAtomSelection(), World::getInstance().endAtomSelection());
220
221 // assert equality after parsing
222 CPPUNIT_ASSERT_EQUAL( fileChecker.InternalAtomBondMap.size(), fileChecker.ExternalAtomBondMap.size() );
223 CPPUNIT_ASSERT_EQUAL( fileChecker.InternalAtomBondMap, fileChecker.ExternalAtomBondMap );
224 CPPUNIT_ASSERT( fileChecker.CompareInternalExternalMap() );
225}
226
227/** Unit tests for CheckAgainstAdjacencyFile::operator()().
228 *
229 */
230void CheckAgainstAdjacencyFileTest::operatorTest()
231{
232 World::getInstance().selectAllAtoms(AllAtoms());
233 {
234 // parse right
235 std::stringstream input(adjacencyfile);
236 CheckAgainstAdjacencyFile fileChecker(input);
237 CPPUNIT_ASSERT( fileChecker(World::getInstance().beginAtomSelection(), World::getInstance().endAtomSelection()) );
238 }
239 {
240 // parse wrong1
241 std::stringstream input(wrongadjacencyfile1);
242 CheckAgainstAdjacencyFile fileChecker(input);
243 CPPUNIT_ASSERT( !fileChecker(World::getInstance().beginAtomSelection(), World::getInstance().endAtomSelection()) );
244 }
245 {
246 // parse wrong2 (there is no atom 10)
247 std::stringstream input(wrongadjacencyfile2);
248#ifndef NDEBUG
249 std::cout << "The following Assert failures are intended and do not indicate a failure of the test." << std::endl;
250 CPPUNIT_ASSERT_THROW( CheckAgainstAdjacencyFile fileChecker(input), Assert::AssertionFailure );
251#else
252 CheckAgainstAdjacencyFile fileChecker(input);
253 CPPUNIT_ASSERT( !fileChecker(World::getInstance().beginAtomSelection(), World::getInstance().endAtomSelection()) );
254#endif
255 }
256}
Note: See TracBrowser for help on using the repository browser.