| 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 |  * BreadthFirstSearchGathererUnitTest.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/Log.hpp"
 | 
|---|
| 44 | 
 | 
|---|
| 45 | #include "Graph/BoostGraphCreator.hpp"
 | 
|---|
| 46 | #include "Graph/BreadthFirstSearchGatherer.hpp"
 | 
|---|
| 47 | 
 | 
|---|
| 48 | #include "BreadthFirstSearchGathererUnitTest.hpp"
 | 
|---|
| 49 | 
 | 
|---|
| 50 | #ifdef HAVE_TESTRUNNER
 | 
|---|
| 51 | #include "UnitTestMain.hpp"
 | 
|---|
| 52 | #endif /*HAVE_TESTRUNNER*/
 | 
|---|
| 53 | 
 | 
|---|
| 54 | using namespace boost::assign;
 | 
|---|
| 55 | 
 | 
|---|
| 56 | /********************************************** Test classes **************************************/
 | 
|---|
| 57 | 
 | 
|---|
| 58 | // Registers the fixture into the 'registry'
 | 
|---|
| 59 | CPPUNIT_TEST_SUITE_REGISTRATION( BreadthFirstSearchGathererTest );
 | 
|---|
| 60 | 
 | 
|---|
| 61 | typedef std::pair<int,int> E;
 | 
|---|
| 62 | 
 | 
|---|
| 63 | void BreadthFirstSearchGathererTest::setUp()
 | 
|---|
| 64 | {
 | 
|---|
| 65 |   BGCreator = new BoostGraphCreator();
 | 
|---|
| 66 | };
 | 
|---|
| 67 | 
 | 
|---|
| 68 | 
 | 
|---|
| 69 | void BreadthFirstSearchGathererTest::tearDown()
 | 
|---|
| 70 | {
 | 
|---|
| 71 |   delete BGCreator;
 | 
|---|
| 72 | };
 | 
|---|
| 73 | 
 | 
|---|
| 74 | /** Little helper function to prepare a linear graph with vertices
 | 
|---|
| 75 |  * correctly named (not just indexed) and also the internal node
 | 
|---|
| 76 |  * map prepared.
 | 
|---|
| 77 |  */
 | 
|---|
| 78 | void BreadthFirstSearchGathererTest::prepareLinearGraph()
 | 
|---|
| 79 | {
 | 
|---|
| 80 |   E edges[] = { E(0,1), E(1,2), E(2,3), E(3,4) };
 | 
|---|
| 81 |   const size_t no_nodes = 5;
 | 
|---|
| 82 |   BGCreator->graph =
 | 
|---|
| 83 |       BoostGraphCreator::UndirectedGraph(edges, edges + sizeof(edges) / sizeof(E), no_nodes);
 | 
|---|
| 84 |   BGCreator->atomids_nodeids +=
 | 
|---|
| 85 |       make_pair(0,0), make_pair(1,1), make_pair(2,2), make_pair(3,3), make_pair(4,4);
 | 
|---|
| 86 |   for (size_t i=0;i<no_nodes;++i)
 | 
|---|
| 87 |     boost::put(boost::get(boost::vertex_name, BGCreator->graph), boost::vertex(i, BGCreator->graph), i+1);
 | 
|---|
| 88 | }
 | 
|---|
| 89 | 
 | 
|---|
| 90 | /** Tests whether operator() works.
 | 
|---|
| 91 |  */
 | 
|---|
| 92 | void BreadthFirstSearchGathererTest::operatorTest()
 | 
|---|
| 93 | {
 | 
|---|
| 94 |   // create linear test graph
 | 
|---|
| 95 |   prepareLinearGraph();
 | 
|---|
| 96 | 
 | 
|---|
| 97 |   // call operator with default argument
 | 
|---|
| 98 |   BreadthFirstSearchGatherer gatherer(*BGCreator);
 | 
|---|
| 99 |   std::vector<atomId_t> atomids = gatherer(0);
 | 
|---|
| 100 | 
 | 
|---|
| 101 |   // create comparator set
 | 
|---|
| 102 |   std::vector<atomId_t> compareids;
 | 
|---|
| 103 |   compareids += 1,2,3,4,5;
 | 
|---|
| 104 |   CPPUNIT_ASSERT_EQUAL ((size_t)5, atomids.size());
 | 
|---|
| 105 |   CPPUNIT_ASSERT_EQUAL (compareids, atomids);
 | 
|---|
| 106 | };
 | 
|---|
| 107 | 
 | 
|---|
| 108 | /** Tests whether operator() with limited distance works.
 | 
|---|
| 109 |  */
 | 
|---|
| 110 | void BreadthFirstSearchGathererTest::limitedDistanceTest()
 | 
|---|
| 111 | {
 | 
|---|
| 112 |   // create linear test graph
 | 
|---|
| 113 |   prepareLinearGraph();
 | 
|---|
| 114 | 
 | 
|---|
| 115 |   // call operator
 | 
|---|
| 116 |   BreadthFirstSearchGatherer gatherer(*BGCreator);
 | 
|---|
| 117 |   {
 | 
|---|
| 118 |     std::vector<atomId_t> atomids = gatherer(0, 3);
 | 
|---|
| 119 | 
 | 
|---|
| 120 |     // create comparator set
 | 
|---|
| 121 |     std::vector<atomId_t> compareids;
 | 
|---|
| 122 |     compareids += 1,2,3,4;
 | 
|---|
| 123 |     CPPUNIT_ASSERT_EQUAL ((size_t)4, atomids.size());
 | 
|---|
| 124 |     CPPUNIT_ASSERT_EQUAL (compareids, atomids);
 | 
|---|
| 125 |   }
 | 
|---|
| 126 |   // zero distance means just find the initial node
 | 
|---|
| 127 |   {
 | 
|---|
| 128 |     std::vector<atomId_t> atomids = gatherer(0, 0);
 | 
|---|
| 129 | 
 | 
|---|
| 130 |     // create comparator set
 | 
|---|
| 131 |     std::vector<atomId_t> compareids;
 | 
|---|
| 132 |     compareids += 1;
 | 
|---|
| 133 |     CPPUNIT_ASSERT_EQUAL ((size_t)1, atomids.size());
 | 
|---|
| 134 |     CPPUNIT_ASSERT_EQUAL (compareids, atomids);
 | 
|---|
| 135 |   }
 | 
|---|
| 136 |   // negative distance means find all
 | 
|---|
| 137 |   {
 | 
|---|
| 138 |     std::vector<atomId_t> atomids = gatherer(0, -1);
 | 
|---|
| 139 | 
 | 
|---|
| 140 |     // create comparator set
 | 
|---|
| 141 |     std::vector<atomId_t> compareids;
 | 
|---|
| 142 |     compareids += 1,2,3,4,5;
 | 
|---|
| 143 |     CPPUNIT_ASSERT_EQUAL ((size_t)5, atomids.size());
 | 
|---|
| 144 |     CPPUNIT_ASSERT_EQUAL (compareids, atomids);
 | 
|---|
| 145 |   }
 | 
|---|
| 146 | };
 | 
|---|