| 1 | /*
 | 
|---|
| 2 |  * Project: MoleCuilder
 | 
|---|
| 3 |  * Description: creates and alters molecular systems
 | 
|---|
| 4 |  * Copyright (C)  2017 Frederik Heber. 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 |  * BoostGraphCreatorUnitTest.cpp
 | 
|---|
| 25 |  *
 | 
|---|
| 26 |  *  Created on: May 19, 2017
 | 
|---|
| 27 |  *      Author: heber
 | 
|---|
| 28 |  */
 | 
|---|
| 29 | 
 | 
|---|
| 30 | // include config.h
 | 
|---|
| 31 | #ifdef HAVE_CONFIG_H
 | 
|---|
| 32 | #include <config.h>
 | 
|---|
| 33 | #endif
 | 
|---|
| 34 | 
 | 
|---|
| 35 | using namespace std;
 | 
|---|
| 36 | 
 | 
|---|
| 37 | #include <cppunit/CompilerOutputter.h>
 | 
|---|
| 38 | #include <cppunit/extensions/TestFactoryRegistry.h>
 | 
|---|
| 39 | #include <cppunit/ui/text/TestRunner.h>
 | 
|---|
| 40 | 
 | 
|---|
| 41 | #include <boost/assign.hpp>
 | 
|---|
| 42 | 
 | 
|---|
| 43 | #include "CodePatterns/Assert.hpp"
 | 
|---|
| 44 | 
 | 
|---|
| 45 | #include "Atom/atom.hpp"
 | 
|---|
| 46 | #include "Graph/BoostGraphCreator.hpp"
 | 
|---|
| 47 | #include "Graph/BreadthFirstSearchGatherer.hpp"
 | 
|---|
| 48 | #include "molecule.hpp"
 | 
|---|
| 49 | #include "Element/periodentafel.hpp"
 | 
|---|
| 50 | #include "World.hpp"
 | 
|---|
| 51 | 
 | 
|---|
| 52 | #include "BoostGraphCreatorUnitTest.hpp"
 | 
|---|
| 53 | 
 | 
|---|
| 54 | #ifdef HAVE_TESTRUNNER
 | 
|---|
| 55 | #include "UnitTestMain.hpp"
 | 
|---|
| 56 | #endif /*HAVE_TESTRUNNER*/
 | 
|---|
| 57 | 
 | 
|---|
| 58 | using namespace boost::assign;
 | 
|---|
| 59 | 
 | 
|---|
| 60 | /********************************************** Test classes **************************************/
 | 
|---|
| 61 | 
 | 
|---|
| 62 | // Registers the fixture into the 'registry'
 | 
|---|
| 63 | CPPUNIT_TEST_SUITE_REGISTRATION( BoostGraphCreatorTest );
 | 
|---|
| 64 | 
 | 
|---|
| 65 | 
 | 
|---|
| 66 | void BoostGraphCreatorTest::setUp()
 | 
|---|
| 67 | {
 | 
|---|
| 68 |   BGCreator = new BoostGraphCreator;
 | 
|---|
| 69 | 
 | 
|---|
| 70 |   // construct element
 | 
|---|
| 71 |   hydrogen = World::getInstance().getPeriode()->FindElement(1);
 | 
|---|
| 72 |   carbon = World::getInstance().getPeriode()->FindElement(6);
 | 
|---|
| 73 |   CPPUNIT_ASSERT(hydrogen != NULL && "could not find element hydrogen");
 | 
|---|
| 74 |   CPPUNIT_ASSERT(carbon != NULL && "could not find element carbon");
 | 
|---|
| 75 | 
 | 
|---|
| 76 |   // construct molecule (tetraeder of hydrogens)
 | 
|---|
| 77 |   TestMolecule = World::getInstance().createMolecule();
 | 
|---|
| 78 |   CPPUNIT_ASSERT(TestMolecule != NULL && "could not create molecule");
 | 
|---|
| 79 |   atom *Walker = World::getInstance().createAtom();
 | 
|---|
| 80 |   CPPUNIT_ASSERT(Walker != NULL && "could not create atom");
 | 
|---|
| 81 |   Walker->setType(carbon);
 | 
|---|
| 82 |   Walker->setPosition(Vector(5., 5., 5. ));
 | 
|---|
| 83 |   TestMolecule->AddAtom(Walker);
 | 
|---|
| 84 | 
 | 
|---|
| 85 |   atom *OtherWalker = World::getInstance().createAtom();
 | 
|---|
| 86 |   CPPUNIT_ASSERT(OtherWalker != NULL && "could not create atom");
 | 
|---|
| 87 |   OtherWalker->setType(carbon);
 | 
|---|
| 88 |   Walker->setPosition(Vector(6.5, 5., 5. ));
 | 
|---|
| 89 |   TestMolecule->AddAtom(OtherWalker);
 | 
|---|
| 90 |   Walker->addBond(OtherWalker);
 | 
|---|
| 91 | 
 | 
|---|
| 92 |   atom *HWalker = World::getInstance().createAtom();
 | 
|---|
| 93 |   CPPUNIT_ASSERT(HWalker != NULL && "could not create atom");
 | 
|---|
| 94 |   HWalker->setType(hydrogen);
 | 
|---|
| 95 |   HWalker->setPosition(Vector(4.3, 4.5, 5. ));
 | 
|---|
| 96 |   TestMolecule->AddAtom(HWalker);
 | 
|---|
| 97 |   Walker->addBond(HWalker);
 | 
|---|
| 98 | 
 | 
|---|
| 99 |   HWalker = World::getInstance().createAtom();
 | 
|---|
| 100 |   CPPUNIT_ASSERT(HWalker != NULL && "could not create atom");
 | 
|---|
| 101 |   HWalker->setType(hydrogen);
 | 
|---|
| 102 |   HWalker->setPosition(Vector(4.3, 5.5, 5. ));
 | 
|---|
| 103 |   TestMolecule->AddAtom(HWalker);
 | 
|---|
| 104 |   Walker->addBond(HWalker);
 | 
|---|
| 105 | 
 | 
|---|
| 106 |   HWalker = World::getInstance().createAtom();
 | 
|---|
| 107 |   CPPUNIT_ASSERT(HWalker != NULL && "could not create atom");
 | 
|---|
| 108 |   HWalker->setType(hydrogen);
 | 
|---|
| 109 |   HWalker->setPosition(Vector(7.2, 4.5, 5. ));
 | 
|---|
| 110 |   TestMolecule->AddAtom(HWalker);
 | 
|---|
| 111 |   OtherWalker->addBond(HWalker);
 | 
|---|
| 112 | 
 | 
|---|
| 113 |   HWalker = World::getInstance().createAtom();
 | 
|---|
| 114 |   CPPUNIT_ASSERT(HWalker != NULL && "could not create atom");
 | 
|---|
| 115 |   HWalker->setType(hydrogen);
 | 
|---|
| 116 |   HWalker->setPosition(Vector(7.2, 5.5, 5. ));
 | 
|---|
| 117 |   TestMolecule->AddAtom(HWalker);
 | 
|---|
| 118 |   OtherWalker->addBond(HWalker);
 | 
|---|
| 119 | 
 | 
|---|
| 120 |   // check that TestMolecule was correctly constructed
 | 
|---|
| 121 |   CPPUNIT_ASSERT_EQUAL( TestMolecule->getAtomCount(), 6 );
 | 
|---|
| 122 | 
 | 
|---|
| 123 | };
 | 
|---|
| 124 | 
 | 
|---|
| 125 | 
 | 
|---|
| 126 | void BoostGraphCreatorTest::tearDown()
 | 
|---|
| 127 | {
 | 
|---|
| 128 |   delete BGCreator;
 | 
|---|
| 129 | 
 | 
|---|
| 130 |   // remove molecule
 | 
|---|
| 131 |   World::getInstance().destroyMolecule(TestMolecule);
 | 
|---|
| 132 |   // note that all the atoms, molecules, the tafel and the elements
 | 
|---|
| 133 |   // are all cleaned when the world is destroyed
 | 
|---|
| 134 |   World::purgeInstance();
 | 
|---|
| 135 |   logger::purgeInstance();
 | 
|---|
| 136 | };
 | 
|---|
| 137 | 
 | 
|---|
| 138 | /** Tests whether setup works.
 | 
|---|
| 139 |  */
 | 
|---|
| 140 | void BoostGraphCreatorTest::SetupTest()
 | 
|---|
| 141 | {
 | 
|---|
| 142 | //  CPPUNIT_ASSERT_EQUAL (false, TestMolecule->empty());
 | 
|---|
| 143 | }
 | 
|---|
| 144 | 
 | 
|---|
| 145 | /** Tests whether createFromRange() works.
 | 
|---|
| 146 |  */
 | 
|---|
| 147 | void BoostGraphCreatorTest::createFromRangeTest()
 | 
|---|
| 148 | {
 | 
|---|
| 149 | //  CPPUNIT_ASSERT_EQUAL (false, TestMolecule->empty());
 | 
|---|
| 150 | };
 | 
|---|
| 151 | 
 | 
|---|
| 152 | /** Tests whether createFromMolecule() works.
 | 
|---|
| 153 |  */
 | 
|---|
| 154 | void BoostGraphCreatorTest::createFromMoleculeTest()
 | 
|---|
| 155 | {
 | 
|---|
| 156 |   BGCreator->createFromMolecule(*TestMolecule, BreadthFirstSearchGatherer::AlwaysTruePredicate);
 | 
|---|
| 157 | 
 | 
|---|
| 158 |   CPPUNIT_ASSERT_EQUAL ((size_t)6, BGCreator->getNumVertices());
 | 
|---|
| 159 |   CPPUNIT_ASSERT_EQUAL ((size_t)5, BGCreator->getNumEdges());
 | 
|---|
| 160 | };
 | 
|---|
| 161 | 
 | 
|---|
| 162 | /** Tests whether createFromAtoms() works.
 | 
|---|
| 163 |  */
 | 
|---|
| 164 | void BoostGraphCreatorTest::createFromAtomsTest()
 | 
|---|
| 165 | {
 | 
|---|
| 166 |   std::vector<atom *> atoms;
 | 
|---|
| 167 |   std::copy(TestMolecule->begin(), TestMolecule->end(), std::back_inserter(atoms));
 | 
|---|
| 168 |   BGCreator->createFromAtoms(atoms, BreadthFirstSearchGatherer::AlwaysTruePredicate);
 | 
|---|
| 169 | 
 | 
|---|
| 170 |   CPPUNIT_ASSERT_EQUAL ((size_t)6, BGCreator->getNumVertices());
 | 
|---|
| 171 |   CPPUNIT_ASSERT_EQUAL ((size_t)5, BGCreator->getNumEdges());
 | 
|---|
| 172 | };
 | 
|---|
| 173 | 
 | 
|---|
| 174 | /** Tests whether adding and removing edges works.
 | 
|---|
| 175 |  */
 | 
|---|
| 176 | void BoostGraphCreatorTest::addremoveEdgeTest()
 | 
|---|
| 177 | {
 | 
|---|
| 178 |   typedef std::pair<int,int> E;
 | 
|---|
| 179 | 
 | 
|---|
| 180 |   E edges[] = { E(0,1), E(1,2), E(2,3), E(3,4) };
 | 
|---|
| 181 |   const size_t no_nodes = 5;
 | 
|---|
| 182 |   BGCreator->graph =
 | 
|---|
| 183 |       BoostGraphCreator::UndirectedGraph(edges, edges + sizeof(edges) / sizeof(E), no_nodes);
 | 
|---|
| 184 |   BGCreator->atomids_nodeids +=
 | 
|---|
| 185 |       make_pair(0,0), make_pair(1,1), make_pair(2,2), make_pair(3,3), make_pair(4,4);
 | 
|---|
| 186 |   for (size_t i=0;i<no_nodes;++i)
 | 
|---|
| 187 |     boost::put(boost::get(boost::vertex_name, BGCreator->graph), boost::vertex(i, BGCreator->graph), i);
 | 
|---|
| 188 | 
 | 
|---|
| 189 |   CPPUNIT_ASSERT_EQUAL ((size_t)5, BGCreator->getNumVertices());
 | 
|---|
| 190 |   CPPUNIT_ASSERT_EQUAL ((size_t)4, BGCreator->getNumEdges());
 | 
|---|
| 191 | 
 | 
|---|
| 192 |   bool status;
 | 
|---|
| 193 |   // remove a valid edge
 | 
|---|
| 194 |   {
 | 
|---|
| 195 |     status = BGCreator->removeEdge((atomId_t)0,(atomId_t)1);
 | 
|---|
| 196 |     CPPUNIT_ASSERT_EQUAL (true, status);
 | 
|---|
| 197 |     status = BGCreator->addEdge((atomId_t)0,(atomId_t)1);
 | 
|---|
| 198 |     CPPUNIT_ASSERT_EQUAL (true, status);
 | 
|---|
| 199 |     // check again whether edge has really been added again
 | 
|---|
| 200 |     status = BGCreator->removeEdge((atomId_t)0,(atomId_t)1);
 | 
|---|
| 201 |     CPPUNIT_ASSERT_EQUAL (true, status);
 | 
|---|
| 202 |     status = BGCreator->addEdge((atomId_t)0,(atomId_t)1);
 | 
|---|
| 203 |     CPPUNIT_ASSERT_EQUAL (true, status);
 | 
|---|
| 204 |   }
 | 
|---|
| 205 | 
 | 
|---|
| 206 |   // remove an invalid edge
 | 
|---|
| 207 |   {
 | 
|---|
| 208 |     status = BGCreator->removeEdge((atomId_t)0,(atomId_t)2);
 | 
|---|
| 209 |     CPPUNIT_ASSERT_EQUAL (false, status);
 | 
|---|
| 210 |   }
 | 
|---|
| 211 | 
 | 
|---|
| 212 |   // add a present edge
 | 
|---|
| 213 |   {
 | 
|---|
| 214 |     status = BGCreator->addEdge((atomId_t)0,(atomId_t)1);
 | 
|---|
| 215 |     CPPUNIT_ASSERT_EQUAL (false, status);
 | 
|---|
| 216 |   }
 | 
|---|
| 217 | };
 | 
|---|