| 1 | /*
 | 
|---|
| 2 |  * Project: MoleCuilder
 | 
|---|
| 3 |  * Description: creates and alters molecular systems
 | 
|---|
| 4 |  * Copyright (C)  2010 University of Bonn. All rights reserved.
 | 
|---|
| 5 |  * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
 | 
|---|
| 6 |  */
 | 
|---|
| 7 | 
 | 
|---|
| 8 | /*
 | 
|---|
| 9 |  * ParserXyzUnitTest.cpp
 | 
|---|
| 10 |  *
 | 
|---|
| 11 |  *  Created on: Mar 3, 2010
 | 
|---|
| 12 |  *      Author: metzler
 | 
|---|
| 13 |  */
 | 
|---|
| 14 | 
 | 
|---|
| 15 | // include config.h
 | 
|---|
| 16 | #ifdef HAVE_CONFIG_H
 | 
|---|
| 17 | #include <config.h>
 | 
|---|
| 18 | #endif
 | 
|---|
| 19 | 
 | 
|---|
| 20 | #include "ParserXyzUnitTest.hpp"
 | 
|---|
| 21 | 
 | 
|---|
| 22 | #include <cppunit/CompilerOutputter.h>
 | 
|---|
| 23 | #include <cppunit/extensions/TestFactoryRegistry.h>
 | 
|---|
| 24 | #include <cppunit/ui/text/TestRunner.h>
 | 
|---|
| 25 | 
 | 
|---|
| 26 | #include "Parser/XyzParser.hpp"
 | 
|---|
| 27 | #include "World.hpp"
 | 
|---|
| 28 | #include "atom.hpp"
 | 
|---|
| 29 | #include "Element/element.hpp"
 | 
|---|
| 30 | #include "Element/periodentafel.hpp"
 | 
|---|
| 31 | #include "CodePatterns/Log.hpp"
 | 
|---|
| 32 | #include "Descriptors/AtomTypeDescriptor.hpp"
 | 
|---|
| 33 | 
 | 
|---|
| 34 | #ifdef HAVE_TESTRUNNER
 | 
|---|
| 35 | #include "UnitTestMain.hpp"
 | 
|---|
| 36 | #endif /*HAVE_TESTRUNNER*/
 | 
|---|
| 37 | 
 | 
|---|
| 38 | using namespace std;
 | 
|---|
| 39 | 
 | 
|---|
| 40 | // Registers the fixture into the 'registry'
 | 
|---|
| 41 | CPPUNIT_TEST_SUITE_REGISTRATION( ParserXyzUnitTest );
 | 
|---|
| 42 | 
 | 
|---|
| 43 | static string waterXyz = "\
 | 
|---|
| 44 | 3\n\
 | 
|---|
| 45 | \tH2O: water molecule\n\
 | 
|---|
| 46 | O\t0\t0\t0\n\
 | 
|---|
| 47 | H\t0.758602\t0\t0.504284\n\
 | 
|---|
| 48 | H\t0.758602\t0\t-0.504284\n";
 | 
|---|
| 49 | static string waterMultiXyz = "\
 | 
|---|
| 50 | 3\n\
 | 
|---|
| 51 | \tH2O: water molecule, time step 0\n\
 | 
|---|
| 52 | O\t0\t0\t0\n\
 | 
|---|
| 53 | H\t0.758602\t0\t0.504284\n\
 | 
|---|
| 54 | H\t0.758602\t0\t-0.504284\n\
 | 
|---|
| 55 | 3\n\
 | 
|---|
| 56 | \tH2O: water molecule, time step 1\n\
 | 
|---|
| 57 | O\t0\t0\t0\n\
 | 
|---|
| 58 | H\t0.76\t0\t0.504284\n\
 | 
|---|
| 59 | H\t0.756\t0\t-0.504284\n";
 | 
|---|
| 60 | 
 | 
|---|
| 61 | void ParserXyzUnitTest::setUp() {
 | 
|---|
| 62 |   World::getInstance();
 | 
|---|
| 63 | 
 | 
|---|
| 64 |   setVerbosity(2);
 | 
|---|
| 65 | 
 | 
|---|
| 66 |   // we need hydrogens and oxygens in the following tests
 | 
|---|
| 67 |   CPPUNIT_ASSERT(World::getInstance().getPeriode()->FindElement(1) != NULL);
 | 
|---|
| 68 |   CPPUNIT_ASSERT(World::getInstance().getPeriode()->FindElement(8) != NULL);
 | 
|---|
| 69 | }
 | 
|---|
| 70 | 
 | 
|---|
| 71 | void ParserXyzUnitTest::tearDown() {
 | 
|---|
| 72 |   ChangeTracker::purgeInstance();
 | 
|---|
| 73 |   World::purgeInstance();
 | 
|---|
| 74 | }
 | 
|---|
| 75 | 
 | 
|---|
| 76 | /************************************ tests ***********************************/
 | 
|---|
| 77 | 
 | 
|---|
| 78 | void ParserXyzUnitTest::rewriteAnXyzTest() {
 | 
|---|
| 79 |   cout << "Testing the XYZ parser." << endl;
 | 
|---|
| 80 |   XyzParser* testParser = new XyzParser();
 | 
|---|
| 81 |   stringstream input;
 | 
|---|
| 82 |   input << waterXyz;
 | 
|---|
| 83 |   testParser->load(&input);
 | 
|---|
| 84 |   input.clear();
 | 
|---|
| 85 | 
 | 
|---|
| 86 |   CPPUNIT_ASSERT_EQUAL(3, World::getInstance().numAtoms());
 | 
|---|
| 87 | 
 | 
|---|
| 88 |   // store and parse in again
 | 
|---|
| 89 |   {
 | 
|---|
| 90 |     stringstream output;
 | 
|---|
| 91 |     std::vector<atom *> atoms = World::getInstance().getAllAtoms();
 | 
|---|
| 92 |     testParser->save(&output, atoms);
 | 
|---|
| 93 |     input << output.str();
 | 
|---|
| 94 |     testParser->load(&input);
 | 
|---|
| 95 |   }
 | 
|---|
| 96 | 
 | 
|---|
| 97 |   // now twice as many
 | 
|---|
| 98 |   CPPUNIT_ASSERT_EQUAL(6, World::getInstance().numAtoms());
 | 
|---|
| 99 | 
 | 
|---|
| 100 |   // check every atom
 | 
|---|
| 101 |   std::vector<atom *> atoms = World::getInstance().getAllAtoms();
 | 
|---|
| 102 |   std::vector<atom *>::const_iterator firstiter = atoms.begin();
 | 
|---|
| 103 |   std::vector<atom *>::const_iterator seconditer = atoms.begin();
 | 
|---|
| 104 |   for (size_t i=0;i<3;i++)
 | 
|---|
| 105 |     ++seconditer;
 | 
|---|
| 106 |   for (;
 | 
|---|
| 107 |       seconditer != atoms.end();
 | 
|---|
| 108 |       ++firstiter,++seconditer) {
 | 
|---|
| 109 |     // check position and type (only stuff xyz stores)
 | 
|---|
| 110 |     CPPUNIT_ASSERT_EQUAL((*firstiter)->getPosition(),(*seconditer)->getPosition());
 | 
|---|
| 111 |     CPPUNIT_ASSERT_EQUAL((*firstiter)->getType(),(*seconditer)->getType());
 | 
|---|
| 112 |   }
 | 
|---|
| 113 | }
 | 
|---|
| 114 | 
 | 
|---|
| 115 | void ParserXyzUnitTest::readMultiXyzTest() {
 | 
|---|
| 116 |   cout << "Testing the multi time step XYZ parser." << endl;
 | 
|---|
| 117 |   XyzParser* testParser = new XyzParser();
 | 
|---|
| 118 |   stringstream input;
 | 
|---|
| 119 |   input << waterMultiXyz;
 | 
|---|
| 120 |   testParser->load(&input);
 | 
|---|
| 121 |   input.clear();
 | 
|---|
| 122 | 
 | 
|---|
| 123 |   // 3 not 6 atoms!
 | 
|---|
| 124 |   CPPUNIT_ASSERT_EQUAL(3, World::getInstance().numAtoms());
 | 
|---|
| 125 | 
 | 
|---|
| 126 |   // check for trajectory size
 | 
|---|
| 127 |   BOOST_FOREACH (atom *_atom, World::getInstance().getAllAtoms())
 | 
|---|
| 128 |     CPPUNIT_ASSERT_EQUAL((size_t) 2, _atom->getTrajectorySize());
 | 
|---|
| 129 | }
 | 
|---|
| 130 | 
 | 
|---|
| 131 | void ParserXyzUnitTest::writeMultiXyzTest() {
 | 
|---|
| 132 |   XyzParser* testParser = new XyzParser();
 | 
|---|
| 133 |   stringstream input;
 | 
|---|
| 134 |   input << waterMultiXyz;
 | 
|---|
| 135 |   testParser->load(&input);
 | 
|---|
| 136 |   input.clear();
 | 
|---|
| 137 | 
 | 
|---|
| 138 |   // 3 not 6 atoms!
 | 
|---|
| 139 |   CPPUNIT_ASSERT_EQUAL(3, World::getInstance().numAtoms());
 | 
|---|
| 140 | 
 | 
|---|
| 141 |   // store and parse in again
 | 
|---|
| 142 |   {
 | 
|---|
| 143 |     stringstream output;
 | 
|---|
| 144 |     std::vector<atom *> atoms = World::getInstance().getAllAtoms();
 | 
|---|
| 145 |     testParser->save(&output, atoms);
 | 
|---|
| 146 |     input << output.str();
 | 
|---|
| 147 |     testParser->load(&input);
 | 
|---|
| 148 |   }
 | 
|---|
| 149 | 
 | 
|---|
| 150 |   // now twice as many
 | 
|---|
| 151 |   CPPUNIT_ASSERT_EQUAL(6, World::getInstance().numAtoms());
 | 
|---|
| 152 | 
 | 
|---|
| 153 |   // check for trajectory size of all 6! atoms
 | 
|---|
| 154 |   BOOST_FOREACH (atom *_atom, World::getInstance().getAllAtoms())
 | 
|---|
| 155 |     CPPUNIT_ASSERT_EQUAL((size_t) 2, _atom->getTrajectorySize());
 | 
|---|
| 156 | 
 | 
|---|
| 157 |   // check every atom
 | 
|---|
| 158 |   std::vector<atom *> atoms = World::getInstance().getAllAtoms();
 | 
|---|
| 159 |   std::vector<atom *>::const_iterator firstiter = atoms.begin();
 | 
|---|
| 160 |   std::vector<atom *>::const_iterator seconditer = atoms.begin();
 | 
|---|
| 161 |   for (size_t i=0;i<3;i++)
 | 
|---|
| 162 |     ++seconditer;
 | 
|---|
| 163 |   for (;
 | 
|---|
| 164 |       seconditer != atoms.end();
 | 
|---|
| 165 |       ++firstiter,++seconditer) {
 | 
|---|
| 166 |     CPPUNIT_ASSERT_EQUAL((*firstiter)->getType(),(*seconditer)->getType());
 | 
|---|
| 167 |     for (unsigned int step = 0; step < 2; ++step) {
 | 
|---|
| 168 |       // check position and type (only stuff xyz stores)
 | 
|---|
| 169 |       CPPUNIT_ASSERT_EQUAL(
 | 
|---|
| 170 |           (*firstiter)->getPositionAtStep(step),
 | 
|---|
| 171 |           (*seconditer)->getPositionAtStep(step));
 | 
|---|
| 172 |     }
 | 
|---|
| 173 |   }
 | 
|---|
| 174 | }
 | 
|---|