/* * ParserUnitTest.cpp * * Created on: Mar 3, 2010 * Author: metzler */ #include "ParserUnitTest.hpp" #include #include #include #include "Parser/XyzParser.hpp" #include "Parser/TremoloParser.hpp" #include "World.hpp" #include "atom.hpp" #include "element.hpp" #include "periodentafel.hpp" #include "Descriptors/AtomTypeDescriptor.hpp" #ifdef HAVE_TESTRUNNER #include "UnitTestMain.hpp" #endif /*HAVE_TESTRUNNER*/ using namespace std; // Registers the fixture into the 'registry' CPPUNIT_TEST_SUITE_REGISTRATION( ParserUnitTest ); void ParserUnitTest::setUp() { World::getInstance(); } void ParserUnitTest::tearDown() { World::purgeInstance(); } /************************************ tests ***********************************/ void ParserUnitTest::rewriteAnXyzTest() { cout << "Testing the XYZ parser." << endl; XyzParser* testParser = new XyzParser(); 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"; stringstream input; input << waterXyz; testParser->load(&input); CPPUNIT_ASSERT_EQUAL(3, World::getInstance().numAtoms()); string newWaterXyz = ""; stringstream output; testParser->save(&output); newWaterXyz = output.str(); CPPUNIT_ASSERT(waterXyz == newWaterXyz); } void ParserUnitTest::readTremoloPreliminaryCommentsTest() { cout << "Testing the tremolo parser." << endl; TremoloParser* testParser = new TremoloParser(); stringstream input, output; string waterTremolo; // Atomdata beginning with "# ATOMDATA" waterTremolo = "# ATOMDATA\tId\tname\tType\tx=3\n"; input << waterTremolo; testParser->load(&input); testParser->save(&output); CPPUNIT_ASSERT(waterTremolo == output.str()); input.clear(); output.clear(); // Atomdata beginning with "#ATOMDATA" waterTremolo = "#\n#ATOMDATA Id name Type x=3\n1 hydrogen H 3.0 4.5 0.1\n\n"; input << waterTremolo; testParser->load(&input); testParser->save(&output); CPPUNIT_ASSERT(output.str().find("hydrogen") != string::npos); input.clear(); output.clear(); // Invalid key in Atomdata line waterTremolo = "#\n#ATOMDATA Id name foo Type x=3\n\n\n"; input << waterTremolo; testParser->load(&input); //TODO: proove invalidity input.clear(); } void ParserUnitTest::readTremoloCoordinatesTest() { TremoloParser* testParser = new TremoloParser(); stringstream input; string waterTremolo; // One simple data line waterTremolo = "#\n#ATOMDATA Id name Type x=3\n1 hydrogen H 3.0 4.5 0.1\n\n"; input << waterTremolo; testParser->load(&input); CPPUNIT_ASSERT(World::getInstance().getAtom(AtomByType(1))->x[0] == 3.0); input.clear(); } void ParserUnitTest::readTremoloVelocityTest() { TremoloParser* testParser = new TremoloParser(); stringstream input; string waterTremolo; // One simple data line waterTremolo = "#\n#ATOMDATA Id name Type u=3\n1 hydrogen H 3.0 4.5 0.1\n\n"; input << waterTremolo; testParser->load(&input); CPPUNIT_ASSERT(World::getInstance().getAtom(AtomByType(1))->v[0] == 3.0); input.clear(); } void ParserUnitTest::readTremoloNeighborInformationTest() { TremoloParser* testParser = new TremoloParser(); stringstream input; string waterTremolo; // Neighbor data waterTremolo = "#\n#ATOMDATA Id Type neighbors=2\n1 H 3 0\n2 H 3 0\n3 O 1 2\n"; input << waterTremolo; testParser->load(&input); CPPUNIT_ASSERT_EQUAL(3, World::getInstance().numAtoms()); CPPUNIT_ASSERT(World::getInstance().getAtom(AtomByType(8))-> IsBondedTo(World::getInstance().getAtom(AtomByType(1)))); input.clear(); } void ParserUnitTest::readAndWriteTremoloImprDataInformationTest() { TremoloParser* testParser = new TremoloParser(); stringstream input, output; string waterTremolo; // Neighbor data waterTremolo = "#\n#ATOMDATA Id Type imprData\n8 H 9-10\n9 H 10-8,8-10\n10 O -\n"; input << waterTremolo; testParser->load(&input); testParser->save(&output); CPPUNIT_ASSERT_EQUAL(3, World::getInstance().numAtoms()); CPPUNIT_ASSERT(output.str().find("2-0,0-2") != string::npos); input.clear(); output.clear(); } void ParserUnitTest::readAndWriteTremoloTorsionInformationTest() { TremoloParser* testParser = new TremoloParser(); stringstream input, output; string waterTremolo; // Neighbor data waterTremolo = "#\n#ATOMDATA Id Type torsion\n8 H 9-10\n9 H 10-8,8-10\n10 O -\n"; input << waterTremolo; testParser->load(&input); testParser->save(&output); CPPUNIT_ASSERT_EQUAL(3, World::getInstance().numAtoms()); CPPUNIT_ASSERT(output.str().find("2-0,0-2") != string::npos); input.clear(); output.clear(); } void ParserUnitTest::writeTremoloTest() { TremoloParser* testParser = new TremoloParser(); stringstream output; // with the maximum number of fields and minimal information, default values are printed atom* newAtom = World::getInstance().createAtom(); newAtom->type = World::getInstance().getPeriode()->FindElement(1); 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"); testParser->save(&output); 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"); cout << "testing the tremolo parser is done" << endl; }