| 1 | /* | 
|---|
| 2 | * Project: MoleCuilder | 
|---|
| 3 | * Description: creates and alters molecular systems | 
|---|
| 4 | * Copyright (C)  2010-2012 University of Bonn. All rights reserved. | 
|---|
| 5 | * Copyright (C)  2013 Frederik Heber. All rights reserved. | 
|---|
| 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 | * ParserTremoloUnitTest.cpp | 
|---|
| 26 | * | 
|---|
| 27 | *  Created on: Mar 3, 2010 | 
|---|
| 28 | *      Author: metzler | 
|---|
| 29 | */ | 
|---|
| 30 |  | 
|---|
| 31 | // include config.h | 
|---|
| 32 | #ifdef HAVE_CONFIG_H | 
|---|
| 33 | #include <config.h> | 
|---|
| 34 | #endif | 
|---|
| 35 |  | 
|---|
| 36 | #include "ParserTremoloUnitTest.hpp" | 
|---|
| 37 |  | 
|---|
| 38 | #include <cppunit/CompilerOutputter.h> | 
|---|
| 39 | #include <cppunit/extensions/TestFactoryRegistry.h> | 
|---|
| 40 | #include <cppunit/ui/text/TestRunner.h> | 
|---|
| 41 |  | 
|---|
| 42 | #include "CodePatterns/Log.hpp" | 
|---|
| 43 |  | 
|---|
| 44 | #include "Atom/atom.hpp" | 
|---|
| 45 | #include "Atom/AtomObserver.hpp" | 
|---|
| 46 | #include "Descriptors/AtomTypeDescriptor.hpp" | 
|---|
| 47 | #include "Element/element.hpp" | 
|---|
| 48 | #include "Element/periodentafel.hpp" | 
|---|
| 49 | #include "Parser/TremoloParser.hpp" | 
|---|
| 50 | #include "Parser/ChangeTracker.hpp" | 
|---|
| 51 | #include "World.hpp" | 
|---|
| 52 | #include "WorldTime.hpp" | 
|---|
| 53 |  | 
|---|
| 54 | #ifdef HAVE_TESTRUNNER | 
|---|
| 55 | #include "UnitTestMain.hpp" | 
|---|
| 56 | #endif /*HAVE_TESTRUNNER*/ | 
|---|
| 57 |  | 
|---|
| 58 | using namespace std; | 
|---|
| 59 |  | 
|---|
| 60 | // Registers the fixture into the 'registry' | 
|---|
| 61 | CPPUNIT_TEST_SUITE_REGISTRATION( ParserTremoloUnitTest ); | 
|---|
| 62 |  | 
|---|
| 63 | static string Tremolo_Atomdata1 = "\ | 
|---|
| 64 | # ATOMDATA\tId\tname\ttype\tx=3\n\ | 
|---|
| 65 | # Box\t20\t0\t0\t0\t20\t0\t0\t0\t20\n"; | 
|---|
| 66 | static string Tremolo_Atomdata2 = "\ | 
|---|
| 67 | #\n\ | 
|---|
| 68 | #ATOMDATA Id name type x=3\n\ | 
|---|
| 69 | # Box\t20\t0\t0\t0\t20\t0\t0\t0\t20\n\ | 
|---|
| 70 | 1 hydrogen H 3.0 4.5 0.1\n\ | 
|---|
| 71 | \n"; | 
|---|
| 72 | static string Tremolo_invalidkey = "\ | 
|---|
| 73 | #\n\ | 
|---|
| 74 | #ATOMDATA Id name foo type x=3\n\ | 
|---|
| 75 | # Box\t20\t0\t0\t0\t20\t0\t0\t0\t20\n\ | 
|---|
| 76 | \n\n"; | 
|---|
| 77 | static string Tremolo_velocity = "\ | 
|---|
| 78 | #\n\ | 
|---|
| 79 | #ATOMDATA Id name type u=3\n\ | 
|---|
| 80 | # Box\t20\t0\t0\t0\t20\t0\t0\t0\t20\n\ | 
|---|
| 81 | 1 hydrogen H 3.0 4.5 0.1\n\ | 
|---|
| 82 | \n"; | 
|---|
| 83 | static string Tremolo_neighbours = "#\n\ | 
|---|
| 84 | #ATOMDATA Id type neighbors=2\n\ | 
|---|
| 85 | # Box\t20\t0\t0\t0\t20\t0\t0\t0\t20\n\ | 
|---|
| 86 | 1 H 3 0\n\ | 
|---|
| 87 | 2 H 3 0\n\ | 
|---|
| 88 | 3 O 1 2\n"; | 
|---|
| 89 | static string Tremolo_improper = "\ | 
|---|
| 90 | #\n\ | 
|---|
| 91 | #ATOMDATA Id type imprData\n\ | 
|---|
| 92 | # Box\t20\t0\t0\t0\t20\t0\t0\t0\t20\n\ | 
|---|
| 93 | 8 H 9-10\n\ | 
|---|
| 94 | 9 H 10-8,8-10\n\ | 
|---|
| 95 | 10 O -\n"; | 
|---|
| 96 | static string Tremolo_torsion = "\ | 
|---|
| 97 | #\n\ | 
|---|
| 98 | #ATOMDATA Id type torsion\n\ | 
|---|
| 99 | # Box\t20\t0\t0\t0\t20\t0\t0\t0\t20\n\ | 
|---|
| 100 | 8 H 9-10\n\ | 
|---|
| 101 | 9 H 10-8,8-10\n\ | 
|---|
| 102 | 10 O -\n"; | 
|---|
| 103 | static string Tremolo_full = "\ | 
|---|
| 104 | # ATOMDATA\tx=3\tu=3\tF=3\tstress\tId\tneighbors=5\timprData\tGroupMeasureTypeNo\ttype\textType\tname\tresName\tchainID\tresSeq\toccupancy\ttempFactor\tsegID\tCharge\tcharge\tGrpTypeNo\ttorsion\n\ | 
|---|
| 105 | # Box\t20\t0\t0\t0\t20\t0\t0\t0\t20\n\ | 
|---|
| 106 | 0\t0\t0\t0\t0\t0\t0\t0\t0\t0\t1\t0\t0\t0\t0\t0\t-\t0\tH\t-\t-\t-\t0\t0\t0\t0\t0\t0\t0\t0\t-\t\n"; | 
|---|
| 107 |  | 
|---|
| 108 | void ParserTremoloUnitTest::setUp() { | 
|---|
| 109 | World::getInstance(); | 
|---|
| 110 |  | 
|---|
| 111 | setVerbosity(2); | 
|---|
| 112 |  | 
|---|
| 113 | parser = new FormatParser<tremolo>(); | 
|---|
| 114 |  | 
|---|
| 115 | // we need hydrogens and oxygens in the following tests | 
|---|
| 116 | CPPUNIT_ASSERT(World::getInstance().getPeriode()->FindElement(1) != NULL); | 
|---|
| 117 | CPPUNIT_ASSERT(World::getInstance().getPeriode()->FindElement(8) != NULL); | 
|---|
| 118 | } | 
|---|
| 119 |  | 
|---|
| 120 | void ParserTremoloUnitTest::tearDown() | 
|---|
| 121 | { | 
|---|
| 122 | delete parser; | 
|---|
| 123 | ChangeTracker::purgeInstance(); | 
|---|
| 124 | World::purgeInstance(); | 
|---|
| 125 | AtomObserver::purgeInstance(); | 
|---|
| 126 | } | 
|---|
| 127 |  | 
|---|
| 128 | /************************************ tests ***********************************/ | 
|---|
| 129 |  | 
|---|
| 130 | void ParserTremoloUnitTest::readTremoloPreliminaryCommentsTest() { | 
|---|
| 131 | //  cout << "Testing the tremolo parser." << endl; | 
|---|
| 132 | stringstream input, output; | 
|---|
| 133 |  | 
|---|
| 134 | // Atomdata beginning with "# ATOMDATA" | 
|---|
| 135 | { | 
|---|
| 136 | input << Tremolo_Atomdata1; | 
|---|
| 137 | parser->load(&input); | 
|---|
| 138 | std::vector<const atom *> atoms = const_cast<const World &>(World::getInstance()). | 
|---|
| 139 | getAllAtoms(); | 
|---|
| 140 | parser->save(&output, atoms); | 
|---|
| 141 | //    std::cout << output.str() << std::endl; | 
|---|
| 142 | //    std::cout << Tremolo_Atomdata1 << std::endl; | 
|---|
| 143 | CPPUNIT_ASSERT(Tremolo_Atomdata1 == output.str()); | 
|---|
| 144 | input.clear(); | 
|---|
| 145 | output.clear(); | 
|---|
| 146 | } | 
|---|
| 147 |  | 
|---|
| 148 | // Atomdata beginning with "#ATOMDATA" | 
|---|
| 149 | { | 
|---|
| 150 | input << Tremolo_Atomdata2; | 
|---|
| 151 | parser->load(&input); | 
|---|
| 152 | std::vector<const atom *> atoms = const_cast<const World &>(World::getInstance()). | 
|---|
| 153 | getAllAtoms(); | 
|---|
| 154 | parser->save(&output, atoms); | 
|---|
| 155 | //    std::cout << output.str() << std::endl; | 
|---|
| 156 | CPPUNIT_ASSERT(output.str().find("hydrogen") != string::npos); | 
|---|
| 157 | input.clear(); | 
|---|
| 158 | output.clear(); | 
|---|
| 159 | } | 
|---|
| 160 |  | 
|---|
| 161 | // Invalid key in Atomdata line | 
|---|
| 162 | input << Tremolo_invalidkey; | 
|---|
| 163 | CPPUNIT_ASSERT_THROW( parser->load(&input), IllegalParserKeyException); | 
|---|
| 164 | //TODO: prove invalidity | 
|---|
| 165 | input.clear(); | 
|---|
| 166 | } | 
|---|
| 167 |  | 
|---|
| 168 | void ParserTremoloUnitTest::getsetAtomDataTest() { | 
|---|
| 169 | stringstream input; | 
|---|
| 170 | input << Tremolo_Atomdata1; | 
|---|
| 171 | parser->load(&input); | 
|---|
| 172 |  | 
|---|
| 173 | CPPUNIT_ASSERT_EQUAL( std::string("Id name type x=3"), parser->getAtomData() ); | 
|---|
| 174 |  | 
|---|
| 175 | // overwrite keys | 
|---|
| 176 | const std::string fewkeys("Id type x=3 neighbors=2"); | 
|---|
| 177 | parser->resetAtomData(fewkeys); | 
|---|
| 178 | CPPUNIT_ASSERT_EQUAL( fewkeys, parser->getAtomData() ); | 
|---|
| 179 |  | 
|---|
| 180 | // add some keys | 
|---|
| 181 | const std::string morekeys("charge"); | 
|---|
| 182 | parser->setAtomData(morekeys); | 
|---|
| 183 | CPPUNIT_ASSERT_EQUAL( fewkeys+std::string(" ")+morekeys, parser->getAtomData() ); | 
|---|
| 184 |  | 
|---|
| 185 | // add similar key | 
|---|
| 186 | const std::string otherkey("neighbors=4"); | 
|---|
| 187 | parser->setAtomData(otherkey); | 
|---|
| 188 | CPPUNIT_ASSERT( fewkeys+std::string(" ")+morekeys != parser->getAtomData() ); | 
|---|
| 189 | } | 
|---|
| 190 |  | 
|---|
| 191 | void ParserTremoloUnitTest::readTremoloCoordinatesTest() { | 
|---|
| 192 | stringstream input; | 
|---|
| 193 |  | 
|---|
| 194 | // One simple data line | 
|---|
| 195 | input << Tremolo_Atomdata2; | 
|---|
| 196 | parser->load(&input); | 
|---|
| 197 | CPPUNIT_ASSERT(const_cast<const World &>(World::getInstance()).getAtom(AtomByType(1))->at(0) == 3.0); | 
|---|
| 198 | input.clear(); | 
|---|
| 199 | } | 
|---|
| 200 |  | 
|---|
| 201 | void ParserTremoloUnitTest::readTremoloVelocityTest() { | 
|---|
| 202 | stringstream input; | 
|---|
| 203 |  | 
|---|
| 204 | // One simple data line | 
|---|
| 205 | input << Tremolo_velocity; | 
|---|
| 206 | parser->load(&input); | 
|---|
| 207 | CPPUNIT_ASSERT(const_cast<const World &>(World::getInstance()).getAtom(AtomByType(1))->getAtomicVelocity()[0] == 3.0); | 
|---|
| 208 | input.clear(); | 
|---|
| 209 | } | 
|---|
| 210 |  | 
|---|
| 211 | void ParserTremoloUnitTest::readTremoloNeighborInformationTest() { | 
|---|
| 212 | stringstream input; | 
|---|
| 213 |  | 
|---|
| 214 | // Neighbor data | 
|---|
| 215 | input << Tremolo_neighbours; | 
|---|
| 216 | parser->load(&input); | 
|---|
| 217 |  | 
|---|
| 218 | CPPUNIT_ASSERT_EQUAL(3, World::getInstance().numAtoms()); | 
|---|
| 219 | CPPUNIT_ASSERT(const_cast<const World &>(World::getInstance()).getAtom(AtomByType(8))-> | 
|---|
| 220 | IsBondedTo(WorldTime::getTime(), const_cast<const World &>(World::getInstance()).getAtom(AtomByType(1)))); | 
|---|
| 221 | input.clear(); | 
|---|
| 222 | } | 
|---|
| 223 |  | 
|---|
| 224 | void ParserTremoloUnitTest::readAndWriteTremoloImprDataInformationTest() { | 
|---|
| 225 | stringstream input, output; | 
|---|
| 226 |  | 
|---|
| 227 | // Neighbor data | 
|---|
| 228 | { | 
|---|
| 229 | input << Tremolo_improper; | 
|---|
| 230 | parser->load(&input); | 
|---|
| 231 | std::vector<const atom *> atoms = const_cast<const World &>(World::getInstance()). | 
|---|
| 232 | getAllAtoms(); | 
|---|
| 233 | parser->save(&output, atoms); | 
|---|
| 234 | CPPUNIT_ASSERT_EQUAL(3, const_cast<const World &>(World::getInstance()).numAtoms()); | 
|---|
| 235 | //    std::cout << output.str() << std::endl; | 
|---|
| 236 | CPPUNIT_ASSERT(output.str().find("3-1,1-3") != string::npos); | 
|---|
| 237 | input.clear(); | 
|---|
| 238 | output.clear(); | 
|---|
| 239 | } | 
|---|
| 240 | } | 
|---|
| 241 |  | 
|---|
| 242 | void ParserTremoloUnitTest::readAndWriteTremoloTorsionInformationTest() { | 
|---|
| 243 | stringstream input, output; | 
|---|
| 244 |  | 
|---|
| 245 | // Neighbor data | 
|---|
| 246 | { | 
|---|
| 247 | input << Tremolo_torsion; | 
|---|
| 248 | parser->load(&input); | 
|---|
| 249 | std::vector<const atom *> atoms = const_cast<const World &>(World::getInstance()). | 
|---|
| 250 | getAllAtoms(); | 
|---|
| 251 | parser->save(&output, atoms); | 
|---|
| 252 | CPPUNIT_ASSERT_EQUAL(3, const_cast<const World &>(World::getInstance()).numAtoms()); | 
|---|
| 253 | //    std::cout << output.str() << std::endl; | 
|---|
| 254 | CPPUNIT_ASSERT(output.str().find("3-1,1-3") != string::npos); | 
|---|
| 255 | input.clear(); | 
|---|
| 256 | output.clear(); | 
|---|
| 257 | } | 
|---|
| 258 | } | 
|---|
| 259 |  | 
|---|
| 260 | void ParserTremoloUnitTest::writeTremoloTest() { | 
|---|
| 261 | stringstream output; | 
|---|
| 262 |  | 
|---|
| 263 | // with the maximum number of fields and minimal information, default values are printed | 
|---|
| 264 | { | 
|---|
| 265 | atom* newAtom = World::getInstance().createAtom(); | 
|---|
| 266 | newAtom->setType(1); | 
|---|
| 267 | parser->setAtomData("x=3 u=3 F=3 stress Id neighbors=5 imprData GroupMeasureTypeNo type extType name resName chainID resSeq occupancy tempFactor segID Charge charge GrpTypeNo torsion"); | 
|---|
| 268 | std::vector<const atom *> atoms = const_cast<const World &>(World::getInstance()). | 
|---|
| 269 | getAllAtoms(); | 
|---|
| 270 | parser->save(&output, atoms); | 
|---|
| 271 | //    std::cout << output.str() << std::endl; | 
|---|
| 272 | //    std::cout << Tremolo_full << std::endl; | 
|---|
| 273 | CPPUNIT_ASSERT(output.str() == Tremolo_full); | 
|---|
| 274 | } | 
|---|
| 275 |  | 
|---|
| 276 | //  cout << "testing the tremolo parser is done" << endl; | 
|---|
| 277 | } | 
|---|
| 278 |  | 
|---|