| 1 | /*
 | 
|---|
| 2 |  * ParserUnitTest.cpp
 | 
|---|
| 3 |  *
 | 
|---|
| 4 |  *  Created on: Mar 3, 2010
 | 
|---|
| 5 |  *      Author: metzler
 | 
|---|
| 6 |  */
 | 
|---|
| 7 | 
 | 
|---|
| 8 | #include "ParserUnitTest.hpp"
 | 
|---|
| 9 | 
 | 
|---|
| 10 | #include <cppunit/CompilerOutputter.h>
 | 
|---|
| 11 | #include <cppunit/extensions/TestFactoryRegistry.h>
 | 
|---|
| 12 | #include <cppunit/ui/text/TestRunner.h>
 | 
|---|
| 13 | 
 | 
|---|
| 14 | #include "Parser/XyzParser.hpp"
 | 
|---|
| 15 | #include "Parser/TremoloParser.hpp"
 | 
|---|
| 16 | #include "World.hpp"
 | 
|---|
| 17 | #include "atom.hpp"
 | 
|---|
| 18 | #include "element.hpp"
 | 
|---|
| 19 | #include "periodentafel.hpp"
 | 
|---|
| 20 | #include "Descriptors/AtomTypeDescriptor.hpp"
 | 
|---|
| 21 | 
 | 
|---|
| 22 | #ifdef HAVE_TESTRUNNER
 | 
|---|
| 23 | #include "UnitTestMain.hpp"
 | 
|---|
| 24 | #endif /*HAVE_TESTRUNNER*/
 | 
|---|
| 25 | 
 | 
|---|
| 26 | using namespace std;
 | 
|---|
| 27 | 
 | 
|---|
| 28 | // Registers the fixture into the 'registry'
 | 
|---|
| 29 | CPPUNIT_TEST_SUITE_REGISTRATION( ParserUnitTest );
 | 
|---|
| 30 | 
 | 
|---|
| 31 | 
 | 
|---|
| 32 | void ParserUnitTest::setUp() {
 | 
|---|
| 33 |   World::getInstance();
 | 
|---|
| 34 | }
 | 
|---|
| 35 | 
 | 
|---|
| 36 | void ParserUnitTest::tearDown() {
 | 
|---|
| 37 |   World::purgeInstance();
 | 
|---|
| 38 | }
 | 
|---|
| 39 | 
 | 
|---|
| 40 | /************************************ tests ***********************************/
 | 
|---|
| 41 | 
 | 
|---|
| 42 | void ParserUnitTest::rewriteAnXyzTest() {
 | 
|---|
| 43 |   cout << "Testing the XYZ parser." << endl;
 | 
|---|
| 44 |   XyzParser* testParser = new XyzParser();
 | 
|---|
| 45 |   string waterXyz = "3\nH2O: water molecule\nO\t0.000000\t0.000000\t0.000000\nH\t0.758602\t0.000000\t0.504284\nH\t0.758602\t0.000000\t-0.504284\n";
 | 
|---|
| 46 |   stringstream input;
 | 
|---|
| 47 |   input << waterXyz;
 | 
|---|
| 48 |   testParser->load(&input);
 | 
|---|
| 49 | 
 | 
|---|
| 50 |   CPPUNIT_ASSERT_EQUAL(3, World::getInstance().numAtoms());
 | 
|---|
| 51 | 
 | 
|---|
| 52 |   string newWaterXyz = "";
 | 
|---|
| 53 |   stringstream output;
 | 
|---|
| 54 |   testParser->save(&output);
 | 
|---|
| 55 |   newWaterXyz = output.str();
 | 
|---|
| 56 | 
 | 
|---|
| 57 |   CPPUNIT_ASSERT(waterXyz == newWaterXyz);
 | 
|---|
| 58 | }
 | 
|---|
| 59 | 
 | 
|---|
| 60 | void ParserUnitTest::readTremoloPreliminaryCommentsTest() {
 | 
|---|
| 61 |   cout << "Testing the tremolo parser." << endl;
 | 
|---|
| 62 |   TremoloParser* testParser = new TremoloParser();
 | 
|---|
| 63 |   stringstream input, output;
 | 
|---|
| 64 |   string waterTremolo;
 | 
|---|
| 65 | 
 | 
|---|
| 66 |   // Atomdata beginning with "# ATOMDATA"
 | 
|---|
| 67 |   waterTremolo = "# ATOMDATA\tId\tname\tType\tx=3\n";
 | 
|---|
| 68 |   input << waterTremolo;
 | 
|---|
| 69 |   testParser->load(&input);
 | 
|---|
| 70 |   testParser->save(&output);
 | 
|---|
| 71 |   CPPUNIT_ASSERT(waterTremolo == output.str());
 | 
|---|
| 72 |   input.clear();
 | 
|---|
| 73 |   output.clear();
 | 
|---|
| 74 | 
 | 
|---|
| 75 |   // Atomdata beginning with "#ATOMDATA"
 | 
|---|
| 76 |   waterTremolo = "#\n#ATOMDATA Id name Type x=3\n1 hydrogen H 3.0 4.5 0.1\n\n";
 | 
|---|
| 77 |   input << waterTremolo;
 | 
|---|
| 78 |   testParser->load(&input);
 | 
|---|
| 79 |   testParser->save(&output);
 | 
|---|
| 80 |   CPPUNIT_ASSERT(output.str().find("hydrogen") != string::npos);
 | 
|---|
| 81 |   input.clear();
 | 
|---|
| 82 |   output.clear();
 | 
|---|
| 83 | 
 | 
|---|
| 84 |   // Invalid key in Atomdata line
 | 
|---|
| 85 |   waterTremolo = "#\n#ATOMDATA Id name foo Type x=3\n\n\n";
 | 
|---|
| 86 |   input << waterTremolo;
 | 
|---|
| 87 |   testParser->load(&input);
 | 
|---|
| 88 |   //TODO: proove invalidity
 | 
|---|
| 89 |   input.clear();
 | 
|---|
| 90 | }
 | 
|---|
| 91 | 
 | 
|---|
| 92 | void ParserUnitTest::readTremoloCoordinatesTest() {
 | 
|---|
| 93 |   TremoloParser* testParser = new TremoloParser();
 | 
|---|
| 94 |   stringstream input;
 | 
|---|
| 95 |   string waterTremolo;
 | 
|---|
| 96 | 
 | 
|---|
| 97 |   // One simple data line
 | 
|---|
| 98 |   waterTremolo = "#\n#ATOMDATA Id name Type x=3\n1 hydrogen H 3.0 4.5 0.1\n\n";
 | 
|---|
| 99 |   input << waterTremolo;
 | 
|---|
| 100 |   testParser->load(&input);
 | 
|---|
| 101 |   CPPUNIT_ASSERT(World::getInstance().getAtom(AtomByType(1))->x[0] == 3.0);
 | 
|---|
| 102 |   input.clear();
 | 
|---|
| 103 | }
 | 
|---|
| 104 | 
 | 
|---|
| 105 | void ParserUnitTest::readTremoloVelocityTest() {
 | 
|---|
| 106 |   TremoloParser* testParser = new TremoloParser();
 | 
|---|
| 107 |   stringstream input;
 | 
|---|
| 108 |   string waterTremolo;
 | 
|---|
| 109 | 
 | 
|---|
| 110 |   // One simple data line
 | 
|---|
| 111 |   waterTremolo = "#\n#ATOMDATA Id name Type u=3\n1 hydrogen H 3.0 4.5 0.1\n\n";
 | 
|---|
| 112 |   input << waterTremolo;
 | 
|---|
| 113 |   testParser->load(&input);
 | 
|---|
| 114 |   CPPUNIT_ASSERT(World::getInstance().getAtom(AtomByType(1))->v[0] == 3.0);
 | 
|---|
| 115 |   input.clear();
 | 
|---|
| 116 | }
 | 
|---|
| 117 | 
 | 
|---|
| 118 | void ParserUnitTest::readTremoloNeighborInformationTest() {
 | 
|---|
| 119 |   TremoloParser* testParser = new TremoloParser();
 | 
|---|
| 120 |   stringstream input;
 | 
|---|
| 121 |   string waterTremolo;
 | 
|---|
| 122 | 
 | 
|---|
| 123 |   // Neighbor data
 | 
|---|
| 124 |   waterTremolo = "#\n#ATOMDATA Id Type neighbors=2\n1 H 3 0\n2 H 3 0\n3 O 1 2\n";
 | 
|---|
| 125 |   input << waterTremolo;
 | 
|---|
| 126 |   testParser->load(&input);
 | 
|---|
| 127 | 
 | 
|---|
| 128 |   CPPUNIT_ASSERT_EQUAL(3, World::getInstance().numAtoms());
 | 
|---|
| 129 |   CPPUNIT_ASSERT(World::getInstance().getAtom(AtomByType(8))->
 | 
|---|
| 130 |       IsBondedTo(World::getInstance().getAtom(AtomByType(1))));
 | 
|---|
| 131 |   input.clear();
 | 
|---|
| 132 | }
 | 
|---|
| 133 | 
 | 
|---|
| 134 | void ParserUnitTest::readAndWriteTremoloImprDataInformationTest() {
 | 
|---|
| 135 |   TremoloParser* testParser = new TremoloParser();
 | 
|---|
| 136 |   stringstream input, output;
 | 
|---|
| 137 |   string waterTremolo;
 | 
|---|
| 138 | 
 | 
|---|
| 139 |   // Neighbor data
 | 
|---|
| 140 |   waterTremolo = "#\n#ATOMDATA Id Type imprData\n8 H 9-10\n9 H 10-8,8-10\n10 O -\n";
 | 
|---|
| 141 |   input << waterTremolo;
 | 
|---|
| 142 |   testParser->load(&input);
 | 
|---|
| 143 |   testParser->save(&output);
 | 
|---|
| 144 |   CPPUNIT_ASSERT_EQUAL(3, World::getInstance().numAtoms());
 | 
|---|
| 145 |   CPPUNIT_ASSERT(output.str().find("2-0,0-2") != string::npos);
 | 
|---|
| 146 |   input.clear();
 | 
|---|
| 147 |   output.clear();
 | 
|---|
| 148 | }
 | 
|---|
| 149 | 
 | 
|---|
| 150 | void ParserUnitTest::readAndWriteTremoloTorsionInformationTest() {
 | 
|---|
| 151 |   TremoloParser* testParser = new TremoloParser();
 | 
|---|
| 152 |   stringstream input, output;
 | 
|---|
| 153 |   string waterTremolo;
 | 
|---|
| 154 | 
 | 
|---|
| 155 |   // Neighbor data
 | 
|---|
| 156 |   waterTremolo = "#\n#ATOMDATA Id Type torsion\n8 H 9-10\n9 H 10-8,8-10\n10 O -\n";
 | 
|---|
| 157 |   input << waterTremolo;
 | 
|---|
| 158 |   testParser->load(&input);
 | 
|---|
| 159 |   testParser->save(&output);
 | 
|---|
| 160 |   CPPUNIT_ASSERT_EQUAL(3, World::getInstance().numAtoms());
 | 
|---|
| 161 |   CPPUNIT_ASSERT(output.str().find("2-0,0-2") != string::npos);
 | 
|---|
| 162 |   input.clear();
 | 
|---|
| 163 |   output.clear();
 | 
|---|
| 164 | }
 | 
|---|
| 165 | 
 | 
|---|
| 166 | void ParserUnitTest::writeTremoloTest() {
 | 
|---|
| 167 |   TremoloParser* testParser = new TremoloParser();
 | 
|---|
| 168 |   stringstream output;
 | 
|---|
| 169 | 
 | 
|---|
| 170 |   // with the maximum number of fields and minimal information, default values are printed
 | 
|---|
| 171 |   atom* newAtom = World::getInstance().createAtom();
 | 
|---|
| 172 |   newAtom->type = World::getInstance().getPeriode()->FindElement(1);
 | 
|---|
| 173 |   testParser->setFieldsForSave("x=3 u=3 F stress Id neighbors=5 imprData GroupMeasureTypeNo Type extType name resName chainID resSeq occupancy tempFactor segID Charge charge GrpTypeNo torsion");
 | 
|---|
| 174 |   testParser->save(&output);
 | 
|---|
| 175 |   CPPUNIT_ASSERT(output.str() == "# ATOMDATA\tx=3\tu=3\tF\tstress\tId\tneighbors=5\timprData\tGroupMeasureTypeNo\tType\textType\tname\tresName\tchainID\tresSeq\toccupancy\ttempFactor\tsegID\tCharge\tcharge\tGrpTypeNo\ttorsion\n0\t0\t0\t0\t0\t0\t0\t0\t0\t0\t0\t0\t0\t0\t-\t0\tH\t-\t-\t-\t0\t0\t0\t0\t0\t0\t0\t0\t-\t\n");
 | 
|---|
| 176 | 
 | 
|---|
| 177 |   cout << "testing the tremolo parser is done" << endl;
 | 
|---|
| 178 | }
 | 
|---|