| [7b6b21f] | 1 | /* | 
|---|
|  | 2 | * Project: MoleCuilder | 
|---|
|  | 3 | * Description: creates and alters molecular systems | 
|---|
|  | 4 | * Copyright (C)  2012 University of Bonn. All rights reserved. | 
|---|
| [5aaa43] | 5 | * Copyright (C)  2013 Frederik Heber. All rights reserved. | 
|---|
| [7b6b21f] | 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/>. | 
|---|
|  | 22 | */ | 
|---|
|  | 23 |  | 
|---|
|  | 24 | /* | 
|---|
|  | 25 | * HomologyGraphUnitTest.cpp | 
|---|
|  | 26 | * | 
|---|
|  | 27 | *  Created on: Sep 25, 2012 | 
|---|
|  | 28 | *      Author: heber | 
|---|
|  | 29 | */ | 
|---|
|  | 30 |  | 
|---|
|  | 31 | // include config.h | 
|---|
|  | 32 | #ifdef HAVE_CONFIG_H | 
|---|
|  | 33 | #include <config.h> | 
|---|
|  | 34 | #endif | 
|---|
|  | 35 |  | 
|---|
|  | 36 | using namespace std; | 
|---|
|  | 37 |  | 
|---|
|  | 38 | #include <cppunit/CompilerOutputter.h> | 
|---|
|  | 39 | #include <cppunit/extensions/TestFactoryRegistry.h> | 
|---|
|  | 40 | #include <cppunit/ui/text/TestRunner.h> | 
|---|
|  | 41 |  | 
|---|
|  | 42 | // include headers that implement a archive in simple text format | 
|---|
|  | 43 | #include <boost/archive/text_oarchive.hpp> | 
|---|
|  | 44 | #include <boost/archive/text_iarchive.hpp> | 
|---|
|  | 45 |  | 
|---|
|  | 46 | #include "HomologyGraphUnitTest.hpp" | 
|---|
|  | 47 |  | 
|---|
|  | 48 | #include "CodePatterns/Assert.hpp" | 
|---|
|  | 49 |  | 
|---|
|  | 50 | #include <boost/assign.hpp> | 
|---|
| [67db80] | 51 | #include <sstream> | 
|---|
| [7b6b21f] | 52 |  | 
|---|
| [154e88] | 53 | #include "Fragmentation/Homology/HomologyGraph.hpp" | 
|---|
|  | 54 | #include "Fragmentation/Graph.hpp" | 
|---|
|  | 55 |  | 
|---|
| [7b6b21f] | 56 | using namespace boost::assign; | 
|---|
|  | 57 |  | 
|---|
|  | 58 | #ifdef HAVE_TESTRUNNER | 
|---|
|  | 59 | #include "UnitTestMain.hpp" | 
|---|
|  | 60 | #endif /*HAVE_TESTRUNNER*/ | 
|---|
|  | 61 |  | 
|---|
|  | 62 | /********************************************** Test classes **************************************/ | 
|---|
|  | 63 |  | 
|---|
|  | 64 | // Registers the fixture into the 'registry' | 
|---|
|  | 65 | CPPUNIT_TEST_SUITE_REGISTRATION( HomologyGraphTest ); | 
|---|
|  | 66 |  | 
|---|
|  | 67 |  | 
|---|
|  | 68 | void HomologyGraphTest::setUp() | 
|---|
|  | 69 | { | 
|---|
|  | 70 | // failing asserts should be thrown | 
|---|
|  | 71 | ASSERT_DO(Assert::Throw); | 
|---|
|  | 72 |  | 
|---|
|  | 73 | // add nodes | 
|---|
| [28e203] | 74 | nodes += | 
|---|
|  | 75 | std::make_pair(FragmentNode(1,1),1), | 
|---|
|  | 76 | std::make_pair(FragmentNode(1,4),1), | 
|---|
|  | 77 | std::make_pair(FragmentNode(2,2),1), | 
|---|
|  | 78 | std::make_pair(FragmentNode(2,4),1); | 
|---|
|  | 79 | othernodes += | 
|---|
|  | 80 | std::make_pair(FragmentNode(1,1),1), | 
|---|
|  | 81 | std::make_pair(FragmentNode(1,4),1); | 
|---|
| [7b6b21f] | 82 |  | 
|---|
|  | 83 | // add edges | 
|---|
| [28e203] | 84 | edges += | 
|---|
|  | 85 | std::make_pair(FragmentEdge(1,1),1), | 
|---|
|  | 86 | std::make_pair(FragmentEdge(1,4),1), | 
|---|
|  | 87 | std::make_pair(FragmentEdge(2,2),1), | 
|---|
|  | 88 | std::make_pair(FragmentEdge(2,4),1); | 
|---|
|  | 89 | otheredges += | 
|---|
|  | 90 | std::make_pair(FragmentEdge(1,4),1), | 
|---|
|  | 91 | std::make_pair(FragmentEdge(2,2),1); | 
|---|
| [7b6b21f] | 92 |  | 
|---|
|  | 93 | // construct graph | 
|---|
|  | 94 | graph = new HomologyGraph(nodes, edges); | 
|---|
|  | 95 | } | 
|---|
|  | 96 |  | 
|---|
|  | 97 |  | 
|---|
|  | 98 | void HomologyGraphTest::tearDown() | 
|---|
|  | 99 | { | 
|---|
|  | 100 | delete graph; | 
|---|
|  | 101 | } | 
|---|
|  | 102 |  | 
|---|
|  | 103 | /** UnitTest for whether homologies are correctly recognized | 
|---|
|  | 104 | */ | 
|---|
|  | 105 | void HomologyGraphTest::comparatorTest() | 
|---|
|  | 106 | { | 
|---|
|  | 107 | // construct other graphs | 
|---|
|  | 108 | HomologyGraph samegraph(nodes, edges); | 
|---|
|  | 109 | HomologyGraph othergraph(nodes, otheredges); | 
|---|
|  | 110 | HomologyGraph anothergraph(othernodes, edges); | 
|---|
|  | 111 | HomologyGraph yetanothergraph(othernodes, otheredges); | 
|---|
|  | 112 |  | 
|---|
|  | 113 | // sort them (order is: anothergraph, yetanothergraph, graph/samegraph, othergraph) | 
|---|
|  | 114 | CPPUNIT_ASSERT( !(*graph < samegraph) ); | 
|---|
|  | 115 | CPPUNIT_ASSERT( othergraph > *graph ); | 
|---|
|  | 116 | CPPUNIT_ASSERT( anothergraph < *graph ); | 
|---|
|  | 117 | CPPUNIT_ASSERT( yetanothergraph < *graph ); | 
|---|
|  | 118 | CPPUNIT_ASSERT( anothergraph < othergraph ); | 
|---|
|  | 119 | CPPUNIT_ASSERT( yetanothergraph < othergraph ); | 
|---|
|  | 120 | CPPUNIT_ASSERT( yetanothergraph > anothergraph ); | 
|---|
|  | 121 |  | 
|---|
|  | 122 | CPPUNIT_ASSERT( !(*graph > samegraph) ); | 
|---|
|  | 123 | CPPUNIT_ASSERT( *graph < othergraph ); | 
|---|
|  | 124 | CPPUNIT_ASSERT( *graph > anothergraph ); | 
|---|
|  | 125 | CPPUNIT_ASSERT( *graph > yetanothergraph ); | 
|---|
|  | 126 | CPPUNIT_ASSERT( othergraph > anothergraph ); | 
|---|
|  | 127 | CPPUNIT_ASSERT( othergraph > yetanothergraph ); | 
|---|
|  | 128 | CPPUNIT_ASSERT( anothergraph < yetanothergraph ); | 
|---|
|  | 129 |  | 
|---|
|  | 130 | // compare them | 
|---|
|  | 131 | CPPUNIT_ASSERT( *graph == samegraph ); | 
|---|
|  | 132 | CPPUNIT_ASSERT( *graph != othergraph ); | 
|---|
|  | 133 | CPPUNIT_ASSERT( *graph != anothergraph ); | 
|---|
|  | 134 | CPPUNIT_ASSERT( *graph != yetanothergraph ); | 
|---|
|  | 135 | CPPUNIT_ASSERT( othergraph != anothergraph ); | 
|---|
|  | 136 | CPPUNIT_ASSERT( othergraph != yetanothergraph ); | 
|---|
|  | 137 | CPPUNIT_ASSERT( anothergraph != yetanothergraph ); | 
|---|
|  | 138 | } | 
|---|
|  | 139 |  | 
|---|
|  | 140 | /** UnitTest for operator=() | 
|---|
|  | 141 | */ | 
|---|
|  | 142 | void HomologyGraphTest::assigmentTest() | 
|---|
|  | 143 | { | 
|---|
|  | 144 | HomologyGraph samegraph(nodes, edges); | 
|---|
|  | 145 | HomologyGraph othergraph(othernodes, otheredges); | 
|---|
|  | 146 |  | 
|---|
|  | 147 | // check initial status | 
|---|
|  | 148 | CPPUNIT_ASSERT( *graph == samegraph); | 
|---|
|  | 149 | CPPUNIT_ASSERT( *graph != othergraph); | 
|---|
|  | 150 |  | 
|---|
|  | 151 | // now assign | 
|---|
|  | 152 | samegraph = othergraph; | 
|---|
|  | 153 | othergraph = *graph; | 
|---|
|  | 154 |  | 
|---|
|  | 155 | // and check again | 
|---|
|  | 156 | CPPUNIT_ASSERT( *graph != samegraph); | 
|---|
|  | 157 | CPPUNIT_ASSERT( *graph == othergraph); | 
|---|
|  | 158 |  | 
|---|
|  | 159 | // also self-assign | 
|---|
|  | 160 | *graph = *graph; | 
|---|
|  | 161 | CPPUNIT_ASSERT( *graph == *graph); | 
|---|
|  | 162 | } | 
|---|
| [67db80] | 163 |  | 
|---|
|  | 164 |  | 
|---|
|  | 165 | /** UnitTest for serialization | 
|---|
|  | 166 | */ | 
|---|
|  | 167 | void HomologyGraphTest::serializeTest() | 
|---|
|  | 168 | { | 
|---|
|  | 169 | // serialize | 
|---|
|  | 170 | std::stringstream outputstream; | 
|---|
|  | 171 | boost::archive::text_oarchive oa(outputstream); | 
|---|
|  | 172 | oa << graph; | 
|---|
|  | 173 |  | 
|---|
|  | 174 | // deserialize | 
|---|
|  | 175 | HomologyGraph *samegraph; | 
|---|
|  | 176 | std::stringstream returnstream(outputstream.str()); | 
|---|
|  | 177 | boost::archive::text_iarchive ia(returnstream); | 
|---|
|  | 178 | ia >> samegraph; | 
|---|
|  | 179 |  | 
|---|
|  | 180 | CPPUNIT_ASSERT( *graph == *samegraph ); | 
|---|
|  | 181 |  | 
|---|
|  | 182 | delete samegraph; | 
|---|
|  | 183 | } | 
|---|