source: src/Graph/unittests/AdjacencyListUnitTest.cpp@ dabe63

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.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator Docu_Python_wait EmpiricalPotential_contain_HomologyGraph_documentation Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph 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 PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks RotateToPrincipalAxisSystem_UndoRedo StoppableMakroAction TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps Ubuntu_1604_changes
Last change on this file since dabe63 was a7aebd, checked in by Frederik Heber <heber@…>, 10 years ago

World removes empty molecules.

  • on destroyAtom() of molecule's last atom, destroyMolecule() is called.
  • FIX: destroyMolecule(molecule *) did use OBSERVE unncessarily. This caused seg'fault.
  • extracted removeAtomsinMolecule from molecule class. This is necessary since World will automatically remove empty molecules.
  • Property mode set to 100644
File size: 9.8 KB
RevLine 
[ec87e4]1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
[0aa122]4 * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
[5aaa43]5 * Copyright (C) 2013 Frederik Heber. All rights reserved.
[94d5ac6]6 *
7 *
8 * This file is part of MoleCuilder.
9 *
10 * MoleCuilder is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * MoleCuilder is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with MoleCuilder. If not, see <http://www.gnu.org/licenses/>.
[ec87e4]22 */
23
24/*
[0fad93]25 * AdjacencyListUnitTest.cpp
[ec87e4]26 *
27 * Created on: Oct 17, 2011
28 * Author: heber
29 */
30
31// include config.h
32#ifdef HAVE_CONFIG_H
33#include <config.h>
34#endif
35
[0fad93]36#include "AdjacencyListUnitTest.hpp"
[ec87e4]37
38#include <cppunit/CompilerOutputter.h>
39#include <cppunit/extensions/TestFactoryRegistry.h>
40#include <cppunit/ui/text/TestRunner.h>
41
[06f41f3]42#include <boost/lambda/lambda.hpp>
[ec87e4]43#include <iostream>
[06f41f3]44#include <iterator>
45#include <iostream>
46#include <vector>
[ec87e4]47
48#include "CodePatterns/Assert.hpp"
49#include "CodePatterns/Log.hpp"
50
[6f0841]51#include "Atom/atom.hpp"
[ec87e4]52#include "Descriptors/AtomDescriptor.hpp"
53#include "Element/element.hpp"
54#include "Element/periodentafel.hpp"
[0fad93]55#include "Graph/AdjacencyList.hpp"
[ec87e4]56#include "molecule.hpp"
57#include "World.hpp"
58#include "WorldTime.hpp"
59
60#ifdef HAVE_TESTRUNNER
61#include "UnitTestMain.hpp"
62#endif /*HAVE_TESTRUNNER*/
63
64/********************************************** Test classes **************************************/
65// Registers the fixture into the 'registry'
[0fad93]66CPPUNIT_TEST_SUITE_REGISTRATION( AdjacencyListTest );
[ec87e4]67
[3aa8a5]68const std::string adjacencyfile ="\
[52ed5b]691 2\n\
[ec87e4]702 1 3\n\
713 2 4\n\
724 3 5\n\
735 4 6\n\
746 5 7\n\
757 6 8\n\
768 7 9\n\
[52ed5b]779 8 10\n\
7810 9\n";
[ec87e4]79
[3aa8a5]80const std::string wrongadjacencyfile1 ="\
[52ed5b]811 2\n\
[06f41f3]822 1\n\
834 5\n\
[ec87e4]845 4 6\n\
856 5 7\n\
867 6 8\n\
878 7 9\n\
[52ed5b]889 8 10\n\
8910 9\n";
[ec87e4]90
[3aa8a5]91const std::string wrongadjacencyfile2 ="\
[52ed5b]921 2\n\
[ec87e4]932 1 3\n\
943 2 4\n\
954 3 5\n\
965 4 6\n\
976 5 7\n\
987 6 8\n\
998 7 9\n\
1009 8 10\n\
[52ed5b]10110 9 11\n\
[3aa8a5]10211 10\n";
[ec87e4]103
[3aa8a5]104const std::string wrongadjacencyfile3 ="\
[06f41f3]1051 2\n\
1062 1 3\n\
1073 2 4\n\
1084 3 5\n\
1095 4 7\n\
1106\n\
1117 5 8\n\
1128 7 9\n\
1139 8 10\n\
[3aa8a5]11410 9\n";
[06f41f3]115
[ec87e4]116// set up and tear down
[0fad93]117void AdjacencyListTest::setUp()
[ec87e4]118{
119 // failing asserts should be thrown
120 ASSERT_DO(Assert::Throw);
121
[3501d2]122 const element *hydrogen = World::getInstance().getPeriode()->FindElement(1);
123 CPPUNIT_ASSERT(hydrogen != NULL);
124
[ec87e4]125 TestMolecule = World::getInstance().createMolecule();
126 CPPUNIT_ASSERT(TestMolecule != NULL);
127 for(int i=0;i<ATOM_COUNT;++i){
[06f41f3]128 atom *_atom = World::getInstance().createAtom();
129 CPPUNIT_ASSERT(_atom != NULL);
130 _atom->setType(hydrogen);
131 TestMolecule->AddAtom(_atom);
132 atoms.push_back(_atom);
133 atomIds.push_back(_atom->getId());
[ec87e4]134 }
[06f41f3]135 CPPUNIT_ASSERT_EQUAL( (size_t)ATOM_COUNT, atoms.size());
136 CPPUNIT_ASSERT_EQUAL( (size_t)ATOM_COUNT, atomIds.size());
[ec87e4]137 // create linear chain
[06f41f3]138 for(int i=0;i<ATOM_COUNT-1;++i)
[ec87e4]139 atoms[i]->addBond(WorldTime::getTime(), atoms[i+1]);
140
141 // create map as it should be
142 for(int i=0;i<ATOM_COUNT;++i) {
143 if (i != 0) // first has only one bond
144 comparisonMap.insert( std::make_pair(atomIds[i], atomIds[i-1]) );
145 if (i != ATOM_COUNT-1) // last has only one bond
146 comparisonMap.insert( std::make_pair(atomIds[i], atomIds[i+1]) );
147 }
148}
149
[0fad93]150void AdjacencyListTest::tearDown()
[ec87e4]151{
152 comparisonMap.clear();
153
154 // destroy molecule and contained atoms
[a7aebd]155 removeAtomsinMolecule(TestMolecule);
[ec87e4]156 // destroy World
157 World::purgeInstance();
158// logger::purgeInstance();
159// errorLogger::purgeInstance();
160}
161
[5197e5]162/** Unit tests for AdjacencyList::CreateMap().
[ec87e4]163 *
164 */
[5197e5]165void AdjacencyListTest::CreateMapTest()
[ec87e4]166{
[3501d2]167 std::stringstream input;
[3aa8a5]168 AdjacencyList FileAdjacency(input);
169 AdjacencyList WorldAdjacency(atomIds);
[ec87e4]170
171 // check size (it's 8*2 + 2*1 = 18 keys)
[3aa8a5]172 CPPUNIT_ASSERT_EQUAL( (size_t)18, WorldAdjacency.atombondmap.size() );
173 CPPUNIT_ASSERT_EQUAL( (size_t)0, FileAdjacency.atombondmap.size() );
[ec87e4]174
175 // check equality
[3aa8a5]176 CPPUNIT_ASSERT( comparisonMap.size() == WorldAdjacency.atombondmap.size() );
[ec87e4]177// std::cout << "comparisonMap: " << comparisonMap << std::endl;
[3aa8a5]178// std::cout << "WorldAdjacency.atombondmap: " << WorldAdjacency.atombondmap << std::endl;
179 CPPUNIT_ASSERT( comparisonMap == WorldAdjacency.atombondmap );
[ec87e4]180
181 // check non-equality: more
182 comparisonMap.insert( std::make_pair( (atomId_t)10, (atomId_t)8) );
[3aa8a5]183 CPPUNIT_ASSERT( comparisonMap != WorldAdjacency.atombondmap );
[ec87e4]184 comparisonMap.erase((atomId_t)10);
185
186 // check non-equality: less
187 comparisonMap.erase((atomId_t)9);
[3aa8a5]188 CPPUNIT_ASSERT( comparisonMap != WorldAdjacency.atombondmap );
[ec87e4]189}
190
[5197e5]191/** Unit tests for AdjacencyList::ParseIntoMap().
[ec87e4]192 *
193 */
[5197e5]194void AdjacencyListTest::ParseIntoMapTest()
[ec87e4]195{
196 std::stringstream input(adjacencyfile);
[3aa8a5]197 AdjacencyList FileAdjacency(input);
198 AdjacencyList WorldAdjacency;
[ec87e4]199
200 // check size (it's 8*2 + 2*1 = 18 keys)
[3aa8a5]201 CPPUNIT_ASSERT_EQUAL( (size_t)0, WorldAdjacency.atombondmap.size() );
202 CPPUNIT_ASSERT_EQUAL( (size_t)18, FileAdjacency.atombondmap.size() );
[ec87e4]203
204 // check equality
[3aa8a5]205 CPPUNIT_ASSERT( comparisonMap.size() == FileAdjacency.atombondmap.size() );
206 CPPUNIT_ASSERT( comparisonMap == FileAdjacency.atombondmap );
[ec87e4]207
208 // check non-equality: more
209 comparisonMap.insert( std::make_pair( (atomId_t)10, (atomId_t)8) );
[3aa8a5]210 CPPUNIT_ASSERT( comparisonMap != FileAdjacency.atombondmap );
[ec87e4]211 comparisonMap.erase((atomId_t)10);
212
213 // check non-equality: less
214 comparisonMap.erase((atomId_t)9);
[3aa8a5]215 CPPUNIT_ASSERT( comparisonMap != FileAdjacency.atombondmap );
216}
217
218/** Unit tests for AdjacencyList::StoreToFile().
219 *
220 */
221void AdjacencyListTest::StoreToFileTest()
222{
223 std::stringstream input(adjacencyfile);
224 AdjacencyList FileAdjacency(input);
225
226 // check size (it's 8*2 + 2*1 = 18 keys)
227 CPPUNIT_ASSERT_EQUAL( (size_t)18, FileAdjacency.atombondmap.size() );
228
229 // store to file
230 std::stringstream output;
231 FileAdjacency.StoreToFile(output);
232 CPPUNIT_ASSERT_EQUAL( input.str(), output.str() );
233}
234
235/** Unit tests for AdjacencyList::operator<().
236 *
237 */
238void AdjacencyListTest::operatorGreaterLessTest()
239{
240 std::stringstream input(adjacencyfile);
241 AdjacencyList FileAdjacency(input);
242 AdjacencyList WorldAdjacency;
243
244 // assert empty set is subset of some filled set (empty sets should always return true)
245 CPPUNIT_ASSERT( FileAdjacency.atombondmap.size() != WorldAdjacency.atombondmap.size() );
246 CPPUNIT_ASSERT( FileAdjacency.atombondmap != WorldAdjacency.atombondmap );
247 CPPUNIT_ASSERT( WorldAdjacency < FileAdjacency );
248 CPPUNIT_ASSERT( !(WorldAdjacency > FileAdjacency) );
249
250 // parse in external map
251 WorldAdjacency.CreateMap(atomIds);
252
253 // assert equality after parsing
254 CPPUNIT_ASSERT_EQUAL( FileAdjacency.atombondmap.size(), WorldAdjacency.atombondmap.size() );
[87d6bd]255 CPPUNIT_ASSERT( FileAdjacency.atombondmap == WorldAdjacency.atombondmap );
[3aa8a5]256 CPPUNIT_ASSERT( WorldAdjacency < FileAdjacency );
257 CPPUNIT_ASSERT( WorldAdjacency > FileAdjacency );
258
259 // remove one entry from the world
260 WorldAdjacency.atombondmap.erase((atomId_t)9);
261 CPPUNIT_ASSERT( WorldAdjacency < FileAdjacency );
262 CPPUNIT_ASSERT( !(WorldAdjacency > FileAdjacency) );
[ec87e4]263}
264
[3aa8a5]265/** Unit tests for AdjacencyList::operator==().
[ec87e4]266 *
267 */
[3aa8a5]268void AdjacencyListTest::operatorEqualTest()
[ec87e4]269{
[3501d2]270 std::stringstream input(adjacencyfile);
[3aa8a5]271 AdjacencyList FileAdjacency(input);
272 AdjacencyList WorldAdjacency;
[ec87e4]273
[06f41f3]274 // assert equality before parsing (empty sets should always return true)
[3aa8a5]275 CPPUNIT_ASSERT( FileAdjacency.atombondmap.size() != WorldAdjacency.atombondmap.size() );
276 CPPUNIT_ASSERT( FileAdjacency.atombondmap != WorldAdjacency.atombondmap );
277 CPPUNIT_ASSERT( WorldAdjacency != FileAdjacency );
[ec87e4]278
[06f41f3]279 // parse in external map
[3aa8a5]280 WorldAdjacency.CreateMap(atomIds);
[ec87e4]281
282 // assert equality after parsing
[3aa8a5]283 CPPUNIT_ASSERT_EQUAL( FileAdjacency.atombondmap.size(), WorldAdjacency.atombondmap.size() );
[87d6bd]284 CPPUNIT_ASSERT( FileAdjacency.atombondmap == WorldAdjacency.atombondmap );
[3aa8a5]285 CPPUNIT_ASSERT( WorldAdjacency == FileAdjacency );
[ec87e4]286}
287
[0fad93]288/** Unit tests for AdjacencyList::operator()().
[ec87e4]289 *
290 */
[0fad93]291void AdjacencyListTest::CheckAgainstSubsetTest()
[ec87e4]292{
[3aa8a5]293 AdjacencyList WorldAdjacency(atomIds);
[ec87e4]294 {
295 // parse right
296 std::stringstream input(adjacencyfile);
[3aa8a5]297 AdjacencyList FileAdjacency(input);
298 CPPUNIT_ASSERT( (WorldAdjacency > FileAdjacency) && (WorldAdjacency < FileAdjacency) );
[ec87e4]299 }
300 {
[06f41f3]301 // parse wrong1 (more atoms in the world than in file, hence wrong)
[ec87e4]302 std::stringstream input(wrongadjacencyfile1);
[3aa8a5]303 AdjacencyList FileAdjacency(input);
304 CPPUNIT_ASSERT( !(WorldAdjacency < FileAdjacency) && (WorldAdjacency > FileAdjacency) );
[ec87e4]305 }
306 {
[06f41f3]307 // remove third atom (index starts at 0) and test for equality then
308 std::vector<atomId_t> validids;
309 std::remove_copy_if(atomIds.begin(), atomIds.end(), std::back_inserter(validids), boost::lambda::_1 == (atomId_t)2);
310 CPPUNIT_ASSERT_EQUAL( (size_t)ATOM_COUNT-1, validids.size() );
[3aa8a5]311 AdjacencyList RestrictedWorldAdjacency(validids);
[06f41f3]312 // parse wrong1 (more atoms in the world than in file, hence wrong)
313 std::stringstream input(wrongadjacencyfile1);
[3aa8a5]314 AdjacencyList FileAdjacency(input);
315 CPPUNIT_ASSERT( RestrictedWorldAdjacency == FileAdjacency );
[06f41f3]316 }
317 {
318 // parse wrong2 (there is no atom 10, but present in file. hence true)
[ec87e4]319 std::stringstream input(wrongadjacencyfile2);
[3aa8a5]320 AdjacencyList FileAdjacency(input);
321 CPPUNIT_ASSERT( !(WorldAdjacency > FileAdjacency) && (WorldAdjacency < FileAdjacency) );
[06f41f3]322 }
323 {
[3aa8a5]324 // parse wrong3 (6 is skipped in connection, hence neither is subset)
[06f41f3]325 std::stringstream input(wrongadjacencyfile3);
[3aa8a5]326 AdjacencyList FileAdjacency(input);
327// WorldAdjacency.StoreToFile((std::ostream &)std::cout);
328// FileAdjacency.StoreToFile((std::ostream &)std::cout);
329 CPPUNIT_ASSERT( WorldAdjacency != FileAdjacency );
[ec87e4]330 }
331}
Note: See TracBrowser for help on using the repository browser.