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