| [4fbca9c] | 1 | /* | 
|---|
|  | 2 | * Project: MoleCuilder | 
|---|
|  | 3 | * Description: creates and alters molecular systems | 
|---|
| [0aa122] | 4 | * Copyright (C)  2010-2012 University of Bonn. All rights reserved. | 
|---|
| [5aaa43] | 5 | * Copyright (C)  2013 Frederik Heber. All rights reserved. | 
|---|
| [94d5ac6] | 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/>. | 
|---|
| [4fbca9c] | 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 |  | 
|---|
| [5a667d] | 42 | #include "CodePatterns/Log.hpp" | 
|---|
|  | 43 |  | 
|---|
| [6f0841] | 44 | #include "Atom/atom.hpp" | 
|---|
| [006e1e] | 45 | #include "Atom/AtomObserver.hpp" | 
|---|
| [073a9e4] | 46 | #include "Descriptors/AtomTypeDescriptor.hpp" | 
|---|
| [3bdb6d] | 47 | #include "Element/element.hpp" | 
|---|
|  | 48 | #include "Element/periodentafel.hpp" | 
|---|
| [4fbca9c] | 49 | #include "Parser/TremoloParser.hpp" | 
|---|
| [765f16] | 50 | #include "Parser/ChangeTracker.hpp" | 
|---|
| [073a9e4] | 51 | #include "World.hpp" | 
|---|
|  | 52 | #include "WorldTime.hpp" | 
|---|
| [4fbca9c] | 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 |  | 
|---|
| [4d4d33] | 63 | static string Tremolo_Atomdata1 = "\ | 
|---|
| [ccb487] | 64 | # ATOMDATA\tId\tname\ttype\tx=3\n\ | 
|---|
|  | 65 | # Box\t20\t0\t0\t0\t20\t0\t0\t0\t20\n"; | 
|---|
| [4d4d33] | 66 | static string Tremolo_Atomdata2 = "\ | 
|---|
|  | 67 | #\n\ | 
|---|
| [305e7e] | 68 | #ATOMDATA Id name type x=3\n\ | 
|---|
| [ccb487] | 69 | # Box\t20\t0\t0\t0\t20\t0\t0\t0\t20\n\ | 
|---|
| [4d4d33] | 70 | 1 hydrogen H 3.0 4.5 0.1\n\ | 
|---|
|  | 71 | \n"; | 
|---|
|  | 72 | static string Tremolo_invalidkey = "\ | 
|---|
|  | 73 | #\n\ | 
|---|
| [305e7e] | 74 | #ATOMDATA Id name foo type x=3\n\ | 
|---|
| [ccb487] | 75 | # Box\t20\t0\t0\t0\t20\t0\t0\t0\t20\n\ | 
|---|
| [4d4d33] | 76 | \n\n"; | 
|---|
|  | 77 | static string Tremolo_velocity = "\ | 
|---|
|  | 78 | #\n\ | 
|---|
| [305e7e] | 79 | #ATOMDATA Id name type u=3\n\ | 
|---|
| [ccb487] | 80 | # Box\t20\t0\t0\t0\t20\t0\t0\t0\t20\n\ | 
|---|
| [4d4d33] | 81 | 1 hydrogen H 3.0 4.5 0.1\n\ | 
|---|
|  | 82 | \n"; | 
|---|
|  | 83 | static string Tremolo_neighbours = "#\n\ | 
|---|
| [305e7e] | 84 | #ATOMDATA Id type neighbors=2\n\ | 
|---|
| [ccb487] | 85 | # Box\t20\t0\t0\t0\t20\t0\t0\t0\t20\n\ | 
|---|
| [4d4d33] | 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\ | 
|---|
| [305e7e] | 91 | #ATOMDATA Id type imprData\n\ | 
|---|
| [ccb487] | 92 | # Box\t20\t0\t0\t0\t20\t0\t0\t0\t20\n\ | 
|---|
| [4d4d33] | 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\ | 
|---|
| [305e7e] | 98 | #ATOMDATA Id type torsion\n\ | 
|---|
| [ccb487] | 99 | # Box\t20\t0\t0\t0\t20\t0\t0\t0\t20\n\ | 
|---|
| [4d4d33] | 100 | 8 H 9-10\n\ | 
|---|
|  | 101 | 9 H 10-8,8-10\n\ | 
|---|
|  | 102 | 10 O -\n"; | 
|---|
|  | 103 | static string Tremolo_full = "\ | 
|---|
| [e6e4a0] | 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\ | 
|---|
| [ccb487] | 105 | # Box\t20\t0\t0\t0\t20\t0\t0\t0\t20\n\ | 
|---|
| [e6e4a0] | 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"; | 
|---|
| [4fbca9c] | 107 |  | 
|---|
|  | 108 | void ParserTremoloUnitTest::setUp() { | 
|---|
|  | 109 | World::getInstance(); | 
|---|
|  | 110 |  | 
|---|
| [05e2ed] | 111 | setVerbosity(2); | 
|---|
| [5a667d] | 112 |  | 
|---|
| [765f16] | 113 | parser = new FormatParser<tremolo>(); | 
|---|
|  | 114 |  | 
|---|
| [4fbca9c] | 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 |  | 
|---|
| [765f16] | 120 | void ParserTremoloUnitTest::tearDown() | 
|---|
|  | 121 | { | 
|---|
|  | 122 | delete parser; | 
|---|
| [4fbca9c] | 123 | ChangeTracker::purgeInstance(); | 
|---|
|  | 124 | World::purgeInstance(); | 
|---|
| [708277] | 125 | AtomObserver::purgeInstance(); | 
|---|
| [4fbca9c] | 126 | } | 
|---|
|  | 127 |  | 
|---|
|  | 128 | /************************************ tests ***********************************/ | 
|---|
|  | 129 |  | 
|---|
|  | 130 | void ParserTremoloUnitTest::readTremoloPreliminaryCommentsTest() { | 
|---|
| [05e2ed] | 131 | //  cout << "Testing the tremolo parser." << endl; | 
|---|
| [4fbca9c] | 132 | stringstream input, output; | 
|---|
|  | 133 |  | 
|---|
|  | 134 | // Atomdata beginning with "# ATOMDATA" | 
|---|
| [73916f] | 135 | { | 
|---|
|  | 136 | input << Tremolo_Atomdata1; | 
|---|
| [765f16] | 137 | parser->load(&input); | 
|---|
| [73916f] | 138 | std::vector<atom *> atoms = World::getInstance().getAllAtoms(); | 
|---|
| [765f16] | 139 | parser->save(&output, atoms); | 
|---|
| [4d4d33] | 140 | //    std::cout << output.str() << std::endl; | 
|---|
|  | 141 | //    std::cout << Tremolo_Atomdata1 << std::endl; | 
|---|
| [73916f] | 142 | CPPUNIT_ASSERT(Tremolo_Atomdata1 == output.str()); | 
|---|
|  | 143 | input.clear(); | 
|---|
|  | 144 | output.clear(); | 
|---|
|  | 145 | } | 
|---|
| [4fbca9c] | 146 |  | 
|---|
|  | 147 | // Atomdata beginning with "#ATOMDATA" | 
|---|
| [73916f] | 148 | { | 
|---|
|  | 149 | input << Tremolo_Atomdata2; | 
|---|
| [765f16] | 150 | parser->load(&input); | 
|---|
| [73916f] | 151 | std::vector<atom *> atoms = World::getInstance().getAllAtoms(); | 
|---|
| [765f16] | 152 | parser->save(&output, atoms); | 
|---|
| [05e2ed] | 153 | //    std::cout << output.str() << std::endl; | 
|---|
| [73916f] | 154 | CPPUNIT_ASSERT(output.str().find("hydrogen") != string::npos); | 
|---|
|  | 155 | input.clear(); | 
|---|
|  | 156 | output.clear(); | 
|---|
|  | 157 | } | 
|---|
| [4fbca9c] | 158 |  | 
|---|
|  | 159 | // Invalid key in Atomdata line | 
|---|
|  | 160 | input << Tremolo_invalidkey; | 
|---|
| [5a667d] | 161 | CPPUNIT_ASSERT_THROW( parser->load(&input), IllegalParserKeyException); | 
|---|
| [73916f] | 162 | //TODO: prove invalidity | 
|---|
| [4fbca9c] | 163 | input.clear(); | 
|---|
|  | 164 | } | 
|---|
|  | 165 |  | 
|---|
| [ca331c] | 166 | void ParserTremoloUnitTest::getsetAtomDataTest() { | 
|---|
|  | 167 | stringstream input; | 
|---|
|  | 168 | input << Tremolo_Atomdata1; | 
|---|
|  | 169 | parser->load(&input); | 
|---|
|  | 170 |  | 
|---|
|  | 171 | CPPUNIT_ASSERT_EQUAL( std::string("Id name type x=3"), parser->getAtomData() ); | 
|---|
|  | 172 |  | 
|---|
|  | 173 | // overwrite keys | 
|---|
|  | 174 | const std::string fewkeys("Id type x=3 neighbors=2"); | 
|---|
|  | 175 | parser->resetAtomData(fewkeys); | 
|---|
|  | 176 | CPPUNIT_ASSERT_EQUAL( fewkeys, parser->getAtomData() ); | 
|---|
|  | 177 |  | 
|---|
|  | 178 | // add some keys | 
|---|
|  | 179 | const std::string morekeys("charge"); | 
|---|
|  | 180 | parser->setAtomData(morekeys); | 
|---|
|  | 181 | CPPUNIT_ASSERT_EQUAL( fewkeys+std::string(" ")+morekeys, parser->getAtomData() ); | 
|---|
|  | 182 |  | 
|---|
|  | 183 | // add similar key | 
|---|
|  | 184 | const std::string otherkey("neighbors=4"); | 
|---|
|  | 185 | parser->setAtomData(otherkey); | 
|---|
|  | 186 | CPPUNIT_ASSERT( fewkeys+std::string(" ")+morekeys != parser->getAtomData() ); | 
|---|
|  | 187 | } | 
|---|
|  | 188 |  | 
|---|
| [4fbca9c] | 189 | void ParserTremoloUnitTest::readTremoloCoordinatesTest() { | 
|---|
|  | 190 | stringstream input; | 
|---|
|  | 191 |  | 
|---|
|  | 192 | // One simple data line | 
|---|
|  | 193 | input << Tremolo_Atomdata2; | 
|---|
| [765f16] | 194 | parser->load(&input); | 
|---|
| [4fbca9c] | 195 | CPPUNIT_ASSERT(World::getInstance().getAtom(AtomByType(1))->at(0) == 3.0); | 
|---|
|  | 196 | input.clear(); | 
|---|
|  | 197 | } | 
|---|
|  | 198 |  | 
|---|
|  | 199 | void ParserTremoloUnitTest::readTremoloVelocityTest() { | 
|---|
|  | 200 | stringstream input; | 
|---|
|  | 201 |  | 
|---|
|  | 202 | // One simple data line | 
|---|
|  | 203 | input << Tremolo_velocity; | 
|---|
| [765f16] | 204 | parser->load(&input); | 
|---|
| [bce72c] | 205 | CPPUNIT_ASSERT(World::getInstance().getAtom(AtomByType(1))->getAtomicVelocity()[0] == 3.0); | 
|---|
| [4fbca9c] | 206 | input.clear(); | 
|---|
|  | 207 | } | 
|---|
|  | 208 |  | 
|---|
|  | 209 | void ParserTremoloUnitTest::readTremoloNeighborInformationTest() { | 
|---|
|  | 210 | stringstream input; | 
|---|
|  | 211 |  | 
|---|
|  | 212 | // Neighbor data | 
|---|
|  | 213 | input << Tremolo_neighbours; | 
|---|
| [765f16] | 214 | parser->load(&input); | 
|---|
| [4fbca9c] | 215 |  | 
|---|
|  | 216 | CPPUNIT_ASSERT_EQUAL(3, World::getInstance().numAtoms()); | 
|---|
|  | 217 | CPPUNIT_ASSERT(World::getInstance().getAtom(AtomByType(8))-> | 
|---|
| [073a9e4] | 218 | IsBondedTo(WorldTime::getTime(), World::getInstance().getAtom(AtomByType(1)))); | 
|---|
| [4fbca9c] | 219 | input.clear(); | 
|---|
|  | 220 | } | 
|---|
|  | 221 |  | 
|---|
|  | 222 | void ParserTremoloUnitTest::readAndWriteTremoloImprDataInformationTest() { | 
|---|
|  | 223 | stringstream input, output; | 
|---|
|  | 224 |  | 
|---|
|  | 225 | // Neighbor data | 
|---|
| [73916f] | 226 | { | 
|---|
|  | 227 | input << Tremolo_improper; | 
|---|
| [765f16] | 228 | parser->load(&input); | 
|---|
| [73916f] | 229 | std::vector<atom *> atoms = World::getInstance().getAllAtoms(); | 
|---|
| [765f16] | 230 | parser->save(&output, atoms); | 
|---|
| [73916f] | 231 | CPPUNIT_ASSERT_EQUAL(3, World::getInstance().numAtoms()); | 
|---|
| [05e2ed] | 232 | //    std::cout << output.str() << std::endl; | 
|---|
|  | 233 | CPPUNIT_ASSERT(output.str().find("3-1,1-3") != string::npos); | 
|---|
| [73916f] | 234 | input.clear(); | 
|---|
|  | 235 | output.clear(); | 
|---|
|  | 236 | } | 
|---|
| [4fbca9c] | 237 | } | 
|---|
|  | 238 |  | 
|---|
|  | 239 | void ParserTremoloUnitTest::readAndWriteTremoloTorsionInformationTest() { | 
|---|
|  | 240 | stringstream input, output; | 
|---|
|  | 241 |  | 
|---|
|  | 242 | // Neighbor data | 
|---|
| [73916f] | 243 | { | 
|---|
|  | 244 | input << Tremolo_torsion; | 
|---|
| [765f16] | 245 | parser->load(&input); | 
|---|
| [73916f] | 246 | std::vector<atom *> atoms = World::getInstance().getAllAtoms(); | 
|---|
| [765f16] | 247 | parser->save(&output, atoms); | 
|---|
| [73916f] | 248 | CPPUNIT_ASSERT_EQUAL(3, World::getInstance().numAtoms()); | 
|---|
| [05e2ed] | 249 | //    std::cout << output.str() << std::endl; | 
|---|
|  | 250 | CPPUNIT_ASSERT(output.str().find("3-1,1-3") != string::npos); | 
|---|
| [73916f] | 251 | input.clear(); | 
|---|
|  | 252 | output.clear(); | 
|---|
|  | 253 | } | 
|---|
| [4fbca9c] | 254 | } | 
|---|
|  | 255 |  | 
|---|
|  | 256 | void ParserTremoloUnitTest::writeTremoloTest() { | 
|---|
|  | 257 | stringstream output; | 
|---|
|  | 258 |  | 
|---|
|  | 259 | // with the maximum number of fields and minimal information, default values are printed | 
|---|
| [73916f] | 260 | { | 
|---|
|  | 261 | atom* newAtom = World::getInstance().createAtom(); | 
|---|
|  | 262 | newAtom->setType(1); | 
|---|
| [e6e4a0] | 263 | 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] | 264 | std::vector<atom *> atoms = World::getInstance().getAllAtoms(); | 
|---|
| [765f16] | 265 | parser->save(&output, atoms); | 
|---|
| [ccb487] | 266 | //    std::cout << output.str() << std::endl; | 
|---|
|  | 267 | //    std::cout << Tremolo_full << std::endl; | 
|---|
| [73916f] | 268 | CPPUNIT_ASSERT(output.str() == Tremolo_full); | 
|---|
|  | 269 | } | 
|---|
| [4fbca9c] | 270 |  | 
|---|
| [05e2ed] | 271 | //  cout << "testing the tremolo parser is done" << endl; | 
|---|
| [4fbca9c] | 272 | } | 
|---|
| [765f16] | 273 |  | 
|---|