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