| [bcf653] | 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/>. | 
|---|
| [bcf653] | 21 | */ | 
|---|
|  | 22 |  | 
|---|
| [4eb4fe] | 23 | /* | 
|---|
| [f844ef] | 24 | * PeriodentafelUnitTest.cpp | 
|---|
| [4eb4fe] | 25 | * | 
|---|
|  | 26 | *  Created on: May 18, 2010 | 
|---|
|  | 27 | *      Author: heber | 
|---|
|  | 28 | */ | 
|---|
|  | 29 |  | 
|---|
| [bf3817] | 30 | // include config.h | 
|---|
|  | 31 | #ifdef HAVE_CONFIG_H | 
|---|
|  | 32 | #include <config.h> | 
|---|
|  | 33 | #endif | 
|---|
|  | 34 |  | 
|---|
| [4eb4fe] | 35 | using namespace std; | 
|---|
|  | 36 |  | 
|---|
|  | 37 | #include <cppunit/CompilerOutputter.h> | 
|---|
|  | 38 | #include <cppunit/extensions/TestFactoryRegistry.h> | 
|---|
|  | 39 | #include <cppunit/ui/text/TestRunner.h> | 
|---|
|  | 40 |  | 
|---|
| [e5c0a1] | 41 | #include <sstream> | 
|---|
|  | 42 | #include <iostream> | 
|---|
|  | 43 |  | 
|---|
| [ad011c] | 44 | #include "CodePatterns/Assert.hpp" | 
|---|
| [4eb4fe] | 45 |  | 
|---|
| [3bdb6d] | 46 | #include "Element/element.hpp" | 
|---|
|  | 47 | #include "Element/elements_db.hpp" | 
|---|
|  | 48 | #include "Element/periodentafel.hpp" | 
|---|
| [4eb4fe] | 49 |  | 
|---|
| [f844ef] | 50 | #include "PeriodentafelUnitTest.hpp" | 
|---|
|  | 51 |  | 
|---|
| [b60804] | 52 | // include headers that implement a archive in simple text format | 
|---|
|  | 53 | #include <boost/archive/text_oarchive.hpp> | 
|---|
|  | 54 | #include <boost/archive/text_iarchive.hpp> | 
|---|
|  | 55 |  | 
|---|
| [4eb4fe] | 56 | #ifdef HAVE_TESTRUNNER | 
|---|
|  | 57 | #include "UnitTestMain.hpp" | 
|---|
|  | 58 | #endif /*HAVE_TESTRUNNER*/ | 
|---|
|  | 59 |  | 
|---|
|  | 60 | /********************************************** Test classes **************************************/ | 
|---|
|  | 61 |  | 
|---|
|  | 62 | // Registers the fixture into the 'registry' | 
|---|
|  | 63 | CPPUNIT_TEST_SUITE_REGISTRATION( periodentafelTest ); | 
|---|
|  | 64 |  | 
|---|
|  | 65 |  | 
|---|
|  | 66 | void periodentafelTest::setUp() | 
|---|
|  | 67 | { | 
|---|
|  | 68 | // make sure world is present | 
|---|
| [4ae823] | 69 | tafel = new periodentafel(true); | 
|---|
| [4eb4fe] | 70 | CPPUNIT_ASSERT(tafel != NULL && "could not obtain periodentafel"); | 
|---|
|  | 71 | }; | 
|---|
|  | 72 |  | 
|---|
|  | 73 |  | 
|---|
|  | 74 | void periodentafelTest::tearDown() | 
|---|
|  | 75 | { | 
|---|
| [3bdb6d] | 76 | delete tafel; | 
|---|
| [4eb4fe] | 77 | }; | 
|---|
|  | 78 |  | 
|---|
|  | 79 | /** UnitTest for AddRemoveTest(). | 
|---|
|  | 80 | */ | 
|---|
|  | 81 | void periodentafelTest::AddRemoveTest() | 
|---|
|  | 82 | { | 
|---|
|  | 83 | // copy the element | 
|---|
|  | 84 | const element * ElementPtr = tafel->FindElement(3); | 
|---|
|  | 85 | CPPUNIT_ASSERT( ElementPtr != NULL && "could not find element lithium"); | 
|---|
|  | 86 | element *Elemental = new element(*ElementPtr); | 
|---|
|  | 87 | // remove element | 
|---|
|  | 88 | tafel->RemoveElement((element * const)ElementPtr); | 
|---|
|  | 89 | ElementPtr = tafel->FindElement(3); | 
|---|
|  | 90 | CPPUNIT_ASSERT_EQUAL( (const element *)NULL, ElementPtr ); | 
|---|
|  | 91 | // add element again | 
|---|
|  | 92 | tafel->AddElement((element * const)Elemental); | 
|---|
|  | 93 | ElementPtr = tafel->FindElement(3); | 
|---|
|  | 94 | CPPUNIT_ASSERT( ElementPtr != NULL && "could not find re-added element lithium"); | 
|---|
|  | 95 | }; | 
|---|
|  | 96 |  | 
|---|
|  | 97 | /** UnitTest for LoadStoreTest(). | 
|---|
|  | 98 | * TODO: periodentafelTest::LoadStoreTest() not implemented yet | 
|---|
|  | 99 | */ | 
|---|
|  | 100 | void periodentafelTest::LoadStoreTest() | 
|---|
|  | 101 | { | 
|---|
|  | 102 | // reload all databases | 
|---|
| [e5c0a1] | 103 | stringstream elementsDBstream(elementsDB,ios_base::in); | 
|---|
| [67c92b] | 104 | stringstream ElectronegativityDBstream(ElectronegativitiesDB,ios_base::in); | 
|---|
|  | 105 | stringstream valenceDBstream(valenceDB,ios_base::in); | 
|---|
|  | 106 | stringstream orbitalsDBstream(orbitalsDB,ios_base::in); | 
|---|
|  | 107 | stringstream HbondangleDBstream(HbondangleDB,ios_base::in); | 
|---|
|  | 108 | stringstream HbonddistanceDBstream(HbonddistanceDB,ios_base::in); | 
|---|
| [064178] | 109 | stringstream ColorDBstream(ColorDB,ios_base::in); | 
|---|
| [e5c0a1] | 110 | CPPUNIT_ASSERT(tafel->LoadElementsDatabase(elementsDBstream) && "General element initialization failed"); | 
|---|
| [67c92b] | 111 | CPPUNIT_ASSERT(tafel->LoadElectronegativityDatabase(ElectronegativityDBstream) && "Electronegativity entry of element initialization failed"); | 
|---|
|  | 112 | CPPUNIT_ASSERT(tafel->LoadValenceDatabase(valenceDBstream) && "Valence entry of element initialization failed"); | 
|---|
|  | 113 | CPPUNIT_ASSERT(tafel->LoadOrbitalsDatabase(orbitalsDBstream) && "Orbitals entry of element initialization failed"); | 
|---|
|  | 114 | CPPUNIT_ASSERT(tafel->LoadHBondAngleDatabase(HbondangleDBstream) && "HBond angle entry of element initialization failed"); | 
|---|
|  | 115 | CPPUNIT_ASSERT(tafel->LoadHBondLengthsDatabase(HbonddistanceDBstream) && "HBond distance entry of element initialization failed"); | 
|---|
| [064178] | 116 | CPPUNIT_ASSERT(tafel->LoadColorDatabase(ColorDBstream) && "color entry of element initialization failed"); | 
|---|
| [4eb4fe] | 117 | // check presence of elements | 
|---|
|  | 118 | atomicNumber_t Z = 1; | 
|---|
|  | 119 | for(periodentafel::const_iterator ElementRunner = tafel->begin(); ElementRunner != tafel->end(); ++ElementRunner) | 
|---|
| [ed26ae] | 120 | CPPUNIT_ASSERT( (ElementRunner->second->getAtomicNumber() == Z++) && "element is missing in sequence"); | 
|---|
| [4eb4fe] | 121 | }; | 
|---|
| [b60804] | 122 |  | 
|---|
|  | 123 | /** UnitTest for serialization | 
|---|
|  | 124 | * | 
|---|
|  | 125 | */ | 
|---|
|  | 126 | void periodentafelTest::ComparisonTest() | 
|---|
|  | 127 | { | 
|---|
| [4ae823] | 128 | periodentafel *newtafel = new periodentafel(true); | 
|---|
| [b60804] | 129 |  | 
|---|
|  | 130 | CPPUNIT_ASSERT (*newtafel == *tafel ); | 
|---|
|  | 131 |  | 
|---|
|  | 132 | element *Elemental = new element(); | 
|---|
|  | 133 | newtafel->AddElement((element * const)Elemental); | 
|---|
|  | 134 |  | 
|---|
|  | 135 | CPPUNIT_ASSERT (*newtafel != *tafel ); | 
|---|
|  | 136 |  | 
|---|
|  | 137 | delete newtafel; | 
|---|
|  | 138 | } | 
|---|
|  | 139 |  | 
|---|
| [3bdb6d] | 140 | std::string gatherUndoInformation(const periodentafel *tafel) | 
|---|
| [b60804] | 141 | { | 
|---|
|  | 142 | // create undo state | 
|---|
|  | 143 | std::stringstream undostream; | 
|---|
|  | 144 | boost::archive::text_oarchive oa(undostream); | 
|---|
| [3bdb6d] | 145 | oa << tafel; | 
|---|
| [b60804] | 146 | return undostream.str(); | 
|---|
|  | 147 | } | 
|---|
|  | 148 |  | 
|---|
|  | 149 | /** UnitTest for serialization | 
|---|
|  | 150 | * | 
|---|
|  | 151 | */ | 
|---|
|  | 152 | void periodentafelTest::SerializeTest() | 
|---|
|  | 153 | { | 
|---|
|  | 154 | // write element to stream | 
|---|
| [3bdb6d] | 155 | std::stringstream stream(gatherUndoInformation(tafel)); | 
|---|
| [b60804] | 156 | //  boost::archive::text_oarchive oa(stream); | 
|---|
|  | 157 | //  oa << World::getInstance().getPeriode(); | 
|---|
|  | 158 | //  //oa << tafel; | 
|---|
|  | 159 | // | 
|---|
|  | 160 | //  //std::cout << "Contents of archive is " << stream.str() << std::endl; | 
|---|
|  | 161 | // | 
|---|
|  | 162 | //  // create and open an archive for input | 
|---|
|  | 163 | boost::archive::text_iarchive ia(stream); | 
|---|
|  | 164 | // read class state from archive | 
|---|
|  | 165 | periodentafel * newtafel; | 
|---|
|  | 166 |  | 
|---|
|  | 167 | ia >> newtafel; | 
|---|
|  | 168 |  | 
|---|
|  | 169 | CPPUNIT_ASSERT (*tafel == *newtafel); | 
|---|
|  | 170 | } | 
|---|