[ec87e4] | 1 | /*
|
---|
| 2 | * Project: MoleCuilder
|
---|
| 3 | * Description: creates and alters molecular systems
|
---|
[0aa122] | 4 | * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
|
---|
[94d5ac6] | 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/>.
|
---|
[ec87e4] | 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 |
|
---|
[06f41f3] | 41 | #include <boost/lambda/lambda.hpp>
|
---|
[ec87e4] | 42 | #include <iostream>
|
---|
[06f41f3] | 43 | #include <iterator>
|
---|
| 44 | #include <iostream>
|
---|
| 45 | #include <vector>
|
---|
[ec87e4] | 46 |
|
---|
| 47 | #include "CodePatterns/Assert.hpp"
|
---|
| 48 | #include "CodePatterns/Log.hpp"
|
---|
| 49 |
|
---|
[6f0841] | 50 | #include "Atom/atom.hpp"
|
---|
[ec87e4] | 51 | #include "Descriptors/AtomDescriptor.hpp"
|
---|
| 52 | #include "Element/element.hpp"
|
---|
| 53 | #include "Element/periodentafel.hpp"
|
---|
| 54 | #include "Graph/CheckAgainstAdjacencyFile.hpp"
|
---|
| 55 | #include "molecule.hpp"
|
---|
| 56 | #include "World.hpp"
|
---|
| 57 | #include "WorldTime.hpp"
|
---|
| 58 |
|
---|
| 59 | #ifdef HAVE_TESTRUNNER
|
---|
| 60 | #include "UnitTestMain.hpp"
|
---|
| 61 | #endif /*HAVE_TESTRUNNER*/
|
---|
| 62 |
|
---|
| 63 | /********************************************** Test classes **************************************/
|
---|
| 64 | // Registers the fixture into the 'registry'
|
---|
| 65 | CPPUNIT_TEST_SUITE_REGISTRATION( CheckAgainstAdjacencyFileTest );
|
---|
| 66 |
|
---|
| 67 | static std::string adjacencyfile ="\
|
---|
[52ed5b] | 68 | 1 2\n\
|
---|
[ec87e4] | 69 | 2 1 3\n\
|
---|
| 70 | 3 2 4\n\
|
---|
| 71 | 4 3 5\n\
|
---|
| 72 | 5 4 6\n\
|
---|
| 73 | 6 5 7\n\
|
---|
| 74 | 7 6 8\n\
|
---|
| 75 | 8 7 9\n\
|
---|
[52ed5b] | 76 | 9 8 10\n\
|
---|
| 77 | 10 9\n";
|
---|
[ec87e4] | 78 |
|
---|
| 79 | static std::string wrongadjacencyfile1 ="\
|
---|
[52ed5b] | 80 | 1 2\n\
|
---|
[06f41f3] | 81 | 2 1\n\
|
---|
| 82 | 4 5\n\
|
---|
[ec87e4] | 83 | 5 4 6\n\
|
---|
| 84 | 6 5 7\n\
|
---|
| 85 | 7 6 8\n\
|
---|
| 86 | 8 7 9\n\
|
---|
[52ed5b] | 87 | 9 8 10\n\
|
---|
| 88 | 10 9\n";
|
---|
[ec87e4] | 89 |
|
---|
| 90 | static std::string wrongadjacencyfile2 ="\
|
---|
[52ed5b] | 91 | 1 2\n\
|
---|
[ec87e4] | 92 | 2 1 3\n\
|
---|
| 93 | 3 2 4\n\
|
---|
| 94 | 4 3 5\n\
|
---|
| 95 | 5 4 6\n\
|
---|
| 96 | 6 5 7\n\
|
---|
| 97 | 7 6 8\n\
|
---|
| 98 | 8 7 9\n\
|
---|
| 99 | 9 8 10\n\
|
---|
[52ed5b] | 100 | 10 9 11\n\
|
---|
| 101 | 11 10";
|
---|
[ec87e4] | 102 |
|
---|
[06f41f3] | 103 | static std::string wrongadjacencyfile3 ="\
|
---|
| 104 | 1 2\n\
|
---|
| 105 | 2 1 3\n\
|
---|
| 106 | 3 2 4\n\
|
---|
| 107 | 4 3 5\n\
|
---|
| 108 | 5 4 7\n\
|
---|
| 109 | 6\n\
|
---|
| 110 | 7 5 8\n\
|
---|
| 111 | 8 7 9\n\
|
---|
| 112 | 9 8 10\n\
|
---|
| 113 | 10 9 11\n\
|
---|
| 114 | 11 10";
|
---|
| 115 |
|
---|
[ec87e4] | 116 | // set up and tear down
|
---|
| 117 | void CheckAgainstAdjacencyFileTest::setUp()
|
---|
| 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 |
|
---|
| 150 | void CheckAgainstAdjacencyFileTest::tearDown()
|
---|
| 151 | {
|
---|
| 152 | comparisonMap.clear();
|
---|
| 153 |
|
---|
| 154 | // destroy molecule and contained atoms
|
---|
| 155 | TestMolecule->removeAtomsinMolecule();
|
---|
| 156 | World::getInstance().destroyMolecule(TestMolecule);
|
---|
| 157 | // destroy World
|
---|
| 158 | World::purgeInstance();
|
---|
| 159 | // logger::purgeInstance();
|
---|
| 160 | // errorLogger::purgeInstance();
|
---|
| 161 | }
|
---|
| 162 |
|
---|
[06f41f3] | 163 | /** Unit tests for CheckAgainstAdjacencyFile::CreateExternalMap().
|
---|
[ec87e4] | 164 | *
|
---|
| 165 | */
|
---|
[06f41f3] | 166 | void CheckAgainstAdjacencyFileTest::CreateExternalMapTest()
|
---|
[ec87e4] | 167 | {
|
---|
[3501d2] | 168 | std::stringstream input;
|
---|
| 169 | CheckAgainstAdjacencyFile fileChecker(input);
|
---|
[06f41f3] | 170 | fileChecker.CreateExternalMap(atomIds);
|
---|
[ec87e4] | 171 |
|
---|
| 172 | // check size (it's 8*2 + 2*1 = 18 keys)
|
---|
[06f41f3] | 173 | CPPUNIT_ASSERT_EQUAL( (size_t)18, fileChecker.ExternalAtomBondMap.size() );
|
---|
| 174 | CPPUNIT_ASSERT_EQUAL( (size_t)0, fileChecker.InternalAtomBondMap.size() );
|
---|
[ec87e4] | 175 |
|
---|
| 176 | // check equality
|
---|
[06f41f3] | 177 | CPPUNIT_ASSERT( comparisonMap.size() == fileChecker.ExternalAtomBondMap.size() );
|
---|
[ec87e4] | 178 | // std::cout << "comparisonMap: " << comparisonMap << std::endl;
|
---|
| 179 | // std::cout << "fileChecker.InternalAtomBondMap: " << fileChecker.InternalAtomBondMap << std::endl;
|
---|
[06f41f3] | 180 | CPPUNIT_ASSERT( comparisonMap == fileChecker.ExternalAtomBondMap );
|
---|
[ec87e4] | 181 |
|
---|
| 182 | // check non-equality: more
|
---|
| 183 | comparisonMap.insert( std::make_pair( (atomId_t)10, (atomId_t)8) );
|
---|
[06f41f3] | 184 | CPPUNIT_ASSERT( comparisonMap != fileChecker.ExternalAtomBondMap );
|
---|
[ec87e4] | 185 | comparisonMap.erase((atomId_t)10);
|
---|
| 186 |
|
---|
| 187 | // check non-equality: less
|
---|
| 188 | comparisonMap.erase((atomId_t)9);
|
---|
[06f41f3] | 189 | CPPUNIT_ASSERT( comparisonMap != fileChecker.ExternalAtomBondMap );
|
---|
[ec87e4] | 190 | }
|
---|
| 191 |
|
---|
[06f41f3] | 192 | /** Unit tests for CheckAgainstAdjacencyFile::ParseInInternalMap().
|
---|
[ec87e4] | 193 | *
|
---|
| 194 | */
|
---|
[06f41f3] | 195 | void CheckAgainstAdjacencyFileTest::ParseInInternalMapTest()
|
---|
[ec87e4] | 196 | {
|
---|
| 197 | std::stringstream input(adjacencyfile);
|
---|
[3501d2] | 198 | CheckAgainstAdjacencyFile fileChecker(input);
|
---|
[06f41f3] | 199 | std::vector<atomId_t> noids;
|
---|
| 200 | fileChecker.CreateExternalMap(noids);
|
---|
[ec87e4] | 201 |
|
---|
| 202 | // check size (it's 8*2 + 2*1 = 18 keys)
|
---|
[06f41f3] | 203 | CPPUNIT_ASSERT_EQUAL( (size_t)0, fileChecker.ExternalAtomBondMap.size() );
|
---|
| 204 | CPPUNIT_ASSERT_EQUAL( (size_t)18, fileChecker.InternalAtomBondMap.size() );
|
---|
[ec87e4] | 205 |
|
---|
| 206 | // check equality
|
---|
[06f41f3] | 207 | CPPUNIT_ASSERT( comparisonMap.size() == fileChecker.InternalAtomBondMap.size() );
|
---|
| 208 | CPPUNIT_ASSERT( comparisonMap == fileChecker.InternalAtomBondMap );
|
---|
[ec87e4] | 209 |
|
---|
| 210 | // check non-equality: more
|
---|
| 211 | comparisonMap.insert( std::make_pair( (atomId_t)10, (atomId_t)8) );
|
---|
[06f41f3] | 212 | CPPUNIT_ASSERT( comparisonMap != fileChecker.InternalAtomBondMap );
|
---|
[ec87e4] | 213 | comparisonMap.erase((atomId_t)10);
|
---|
| 214 |
|
---|
| 215 | // check non-equality: less
|
---|
| 216 | comparisonMap.erase((atomId_t)9);
|
---|
[06f41f3] | 217 | CPPUNIT_ASSERT( comparisonMap != fileChecker.InternalAtomBondMap );
|
---|
[ec87e4] | 218 | }
|
---|
| 219 |
|
---|
| 220 | /** Unit tests for CheckAgainstAdjacencyFile::CompareInternalExternalMap().
|
---|
| 221 | *
|
---|
| 222 | */
|
---|
| 223 | void CheckAgainstAdjacencyFileTest::CompareInternalExternalMapTest()
|
---|
| 224 | {
|
---|
[3501d2] | 225 | std::stringstream input(adjacencyfile);
|
---|
| 226 | CheckAgainstAdjacencyFile fileChecker(input);
|
---|
[ec87e4] | 227 |
|
---|
[06f41f3] | 228 | // assert equality before parsing (empty sets should always return true)
|
---|
| 229 | CPPUNIT_ASSERT( fileChecker.ExternalAtomBondMap.size() != fileChecker.InternalAtomBondMap.size() );
|
---|
| 230 | CPPUNIT_ASSERT( fileChecker.ExternalAtomBondMap != fileChecker.InternalAtomBondMap );
|
---|
| 231 | CPPUNIT_ASSERT( fileChecker.CompareInternalExternalMap() );
|
---|
[ec87e4] | 232 |
|
---|
[06f41f3] | 233 | // parse in external map
|
---|
| 234 | fileChecker.CreateExternalMap(atomIds);
|
---|
[ec87e4] | 235 |
|
---|
| 236 | // assert equality after parsing
|
---|
[06f41f3] | 237 | CPPUNIT_ASSERT_EQUAL( fileChecker.ExternalAtomBondMap.size(), fileChecker.InternalAtomBondMap.size() );
|
---|
| 238 | CPPUNIT_ASSERT_EQUAL( fileChecker.ExternalAtomBondMap, fileChecker.InternalAtomBondMap );
|
---|
[ec87e4] | 239 | CPPUNIT_ASSERT( fileChecker.CompareInternalExternalMap() );
|
---|
| 240 | }
|
---|
| 241 |
|
---|
| 242 | /** Unit tests for CheckAgainstAdjacencyFile::operator()().
|
---|
| 243 | *
|
---|
| 244 | */
|
---|
| 245 | void CheckAgainstAdjacencyFileTest::operatorTest()
|
---|
| 246 | {
|
---|
| 247 | {
|
---|
| 248 | // parse right
|
---|
| 249 | std::stringstream input(adjacencyfile);
|
---|
[3501d2] | 250 | CheckAgainstAdjacencyFile fileChecker(input);
|
---|
[06f41f3] | 251 | CPPUNIT_ASSERT( fileChecker(atomIds) );
|
---|
[ec87e4] | 252 | }
|
---|
| 253 | {
|
---|
[06f41f3] | 254 | // parse wrong1 (more atoms in the world than in file, hence wrong)
|
---|
[ec87e4] | 255 | std::stringstream input(wrongadjacencyfile1);
|
---|
[3501d2] | 256 | CheckAgainstAdjacencyFile fileChecker(input);
|
---|
[06f41f3] | 257 | CPPUNIT_ASSERT( !fileChecker(atomIds) );
|
---|
[ec87e4] | 258 | }
|
---|
| 259 | {
|
---|
[06f41f3] | 260 | // remove third atom (index starts at 0) and test for equality then
|
---|
| 261 | std::vector<atomId_t> validids;
|
---|
| 262 | std::remove_copy_if(atomIds.begin(), atomIds.end(), std::back_inserter(validids), boost::lambda::_1 == (atomId_t)2);
|
---|
| 263 | CPPUNIT_ASSERT_EQUAL( (size_t)ATOM_COUNT-1, validids.size() );
|
---|
| 264 | // parse wrong1 (more atoms in the world than in file, hence wrong)
|
---|
| 265 | std::stringstream input(wrongadjacencyfile1);
|
---|
| 266 | CheckAgainstAdjacencyFile fileChecker(input);
|
---|
| 267 | CPPUNIT_ASSERT( fileChecker(validids) );
|
---|
| 268 | }
|
---|
| 269 | {
|
---|
| 270 | // parse wrong2 (there is no atom 10, but present in file. hence true)
|
---|
[ec87e4] | 271 | std::stringstream input(wrongadjacencyfile2);
|
---|
[3501d2] | 272 | CheckAgainstAdjacencyFile fileChecker(input);
|
---|
[06f41f3] | 273 | CPPUNIT_ASSERT( !fileChecker(atomIds) );
|
---|
| 274 | }
|
---|
| 275 | {
|
---|
| 276 | // parse wrong3 (6 is not connected)
|
---|
| 277 | std::stringstream input(wrongadjacencyfile3);
|
---|
| 278 | CheckAgainstAdjacencyFile fileChecker(input);
|
---|
| 279 | CPPUNIT_ASSERT( !fileChecker(atomIds) );
|
---|
[ec87e4] | 280 | }
|
---|
| 281 | }
|
---|