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