1 | /*
|
---|
2 | * Project: MoleCuilder
|
---|
3 | * Description: creates and alters molecular systems
|
---|
4 | * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
|
---|
5 | *
|
---|
6 | *
|
---|
7 | * This file is part of MoleCuilder.
|
---|
8 | *
|
---|
9 | * MoleCuilder is free software: you can redistribute it and/or modify
|
---|
10 | * it under the terms of the GNU General Public License as published by
|
---|
11 | * the Free Software Foundation, either version 2 of the License, or
|
---|
12 | * (at your option) any later version.
|
---|
13 | *
|
---|
14 | * MoleCuilder is distributed in the hope that it will be useful,
|
---|
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
17 | * GNU General Public License for more details.
|
---|
18 | *
|
---|
19 | * You should have received a copy of the GNU General Public License
|
---|
20 | * along with MoleCuilder. If not, see <http://www.gnu.org/licenses/>.
|
---|
21 | */
|
---|
22 |
|
---|
23 | /*
|
---|
24 | * ParserTremoloUnitTest.cpp
|
---|
25 | *
|
---|
26 | * Created on: Mar 3, 2010
|
---|
27 | * Author: metzler
|
---|
28 | */
|
---|
29 |
|
---|
30 | // include config.h
|
---|
31 | #ifdef HAVE_CONFIG_H
|
---|
32 | #include <config.h>
|
---|
33 | #endif
|
---|
34 |
|
---|
35 | #include "ParserTremoloUnitTest.hpp"
|
---|
36 |
|
---|
37 | #include <cppunit/CompilerOutputter.h>
|
---|
38 | #include <cppunit/extensions/TestFactoryRegistry.h>
|
---|
39 | #include <cppunit/ui/text/TestRunner.h>
|
---|
40 |
|
---|
41 | #include "CodePatterns/Log.hpp"
|
---|
42 |
|
---|
43 | #include "Atom/atom.hpp"
|
---|
44 | #include "Atom/AtomObserver.hpp"
|
---|
45 | #include "Descriptors/AtomTypeDescriptor.hpp"
|
---|
46 | #include "Element/element.hpp"
|
---|
47 | #include "Element/periodentafel.hpp"
|
---|
48 | #include "Parser/TremoloParser.hpp"
|
---|
49 | #include "Parser/ChangeTracker.hpp"
|
---|
50 | #include "World.hpp"
|
---|
51 | #include "WorldTime.hpp"
|
---|
52 |
|
---|
53 | #ifdef HAVE_TESTRUNNER
|
---|
54 | #include "UnitTestMain.hpp"
|
---|
55 | #endif /*HAVE_TESTRUNNER*/
|
---|
56 |
|
---|
57 | using namespace std;
|
---|
58 |
|
---|
59 | // Registers the fixture into the 'registry'
|
---|
60 | CPPUNIT_TEST_SUITE_REGISTRATION( ParserTremoloUnitTest );
|
---|
61 |
|
---|
62 | static string Tremolo_Atomdata1 = "\
|
---|
63 | # ATOMDATA\tId\tname\ttype\tx=3\n\
|
---|
64 | # Box\t20\t0\t0\t0\t20\t0\t0\t0\t20\n";
|
---|
65 | static string Tremolo_Atomdata2 = "\
|
---|
66 | #\n\
|
---|
67 | #ATOMDATA Id name type x=3\n\
|
---|
68 | # Box\t20\t0\t0\t0\t20\t0\t0\t0\t20\n\
|
---|
69 | 1 hydrogen H 3.0 4.5 0.1\n\
|
---|
70 | \n";
|
---|
71 | static string Tremolo_invalidkey = "\
|
---|
72 | #\n\
|
---|
73 | #ATOMDATA Id name foo type x=3\n\
|
---|
74 | # Box\t20\t0\t0\t0\t20\t0\t0\t0\t20\n\
|
---|
75 | \n\n";
|
---|
76 | static string Tremolo_velocity = "\
|
---|
77 | #\n\
|
---|
78 | #ATOMDATA Id name type u=3\n\
|
---|
79 | # Box\t20\t0\t0\t0\t20\t0\t0\t0\t20\n\
|
---|
80 | 1 hydrogen H 3.0 4.5 0.1\n\
|
---|
81 | \n";
|
---|
82 | static string Tremolo_neighbours = "#\n\
|
---|
83 | #ATOMDATA Id type neighbors=2\n\
|
---|
84 | # Box\t20\t0\t0\t0\t20\t0\t0\t0\t20\n\
|
---|
85 | 1 H 3 0\n\
|
---|
86 | 2 H 3 0\n\
|
---|
87 | 3 O 1 2\n";
|
---|
88 | static string Tremolo_improper = "\
|
---|
89 | #\n\
|
---|
90 | #ATOMDATA Id type imprData\n\
|
---|
91 | # Box\t20\t0\t0\t0\t20\t0\t0\t0\t20\n\
|
---|
92 | 8 H 9-10\n\
|
---|
93 | 9 H 10-8,8-10\n\
|
---|
94 | 10 O -\n";
|
---|
95 | static string Tremolo_torsion = "\
|
---|
96 | #\n\
|
---|
97 | #ATOMDATA Id type torsion\n\
|
---|
98 | # Box\t20\t0\t0\t0\t20\t0\t0\t0\t20\n\
|
---|
99 | 8 H 9-10\n\
|
---|
100 | 9 H 10-8,8-10\n\
|
---|
101 | 10 O -\n";
|
---|
102 | static string Tremolo_full = "\
|
---|
103 | # 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\
|
---|
104 | # Box\t20\t0\t0\t0\t20\t0\t0\t0\t20\n\
|
---|
105 | 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";
|
---|
106 |
|
---|
107 | void ParserTremoloUnitTest::setUp() {
|
---|
108 | World::getInstance();
|
---|
109 |
|
---|
110 | setVerbosity(2);
|
---|
111 |
|
---|
112 | parser = new FormatParser<tremolo>();
|
---|
113 |
|
---|
114 | // we need hydrogens and oxygens in the following tests
|
---|
115 | CPPUNIT_ASSERT(World::getInstance().getPeriode()->FindElement(1) != NULL);
|
---|
116 | CPPUNIT_ASSERT(World::getInstance().getPeriode()->FindElement(8) != NULL);
|
---|
117 | }
|
---|
118 |
|
---|
119 | void ParserTremoloUnitTest::tearDown()
|
---|
120 | {
|
---|
121 | delete parser;
|
---|
122 | ChangeTracker::purgeInstance();
|
---|
123 | World::purgeInstance();
|
---|
124 | AtomObserver::purgeInstance();
|
---|
125 | }
|
---|
126 |
|
---|
127 | /************************************ tests ***********************************/
|
---|
128 |
|
---|
129 | void ParserTremoloUnitTest::readTremoloPreliminaryCommentsTest() {
|
---|
130 | // cout << "Testing the tremolo parser." << endl;
|
---|
131 | stringstream input, output;
|
---|
132 |
|
---|
133 | // Atomdata beginning with "# ATOMDATA"
|
---|
134 | {
|
---|
135 | input << Tremolo_Atomdata1;
|
---|
136 | parser->load(&input);
|
---|
137 | std::vector<atom *> atoms = World::getInstance().getAllAtoms();
|
---|
138 | parser->save(&output, atoms);
|
---|
139 | // std::cout << output.str() << std::endl;
|
---|
140 | // std::cout << Tremolo_Atomdata1 << std::endl;
|
---|
141 | CPPUNIT_ASSERT(Tremolo_Atomdata1 == output.str());
|
---|
142 | input.clear();
|
---|
143 | output.clear();
|
---|
144 | }
|
---|
145 |
|
---|
146 | // Atomdata beginning with "#ATOMDATA"
|
---|
147 | {
|
---|
148 | input << Tremolo_Atomdata2;
|
---|
149 | parser->load(&input);
|
---|
150 | std::vector<atom *> atoms = World::getInstance().getAllAtoms();
|
---|
151 | parser->save(&output, atoms);
|
---|
152 | // std::cout << output.str() << std::endl;
|
---|
153 | CPPUNIT_ASSERT(output.str().find("hydrogen") != string::npos);
|
---|
154 | input.clear();
|
---|
155 | output.clear();
|
---|
156 | }
|
---|
157 |
|
---|
158 | // Invalid key in Atomdata line
|
---|
159 | input << Tremolo_invalidkey;
|
---|
160 | CPPUNIT_ASSERT_THROW( parser->load(&input), IllegalParserKeyException);
|
---|
161 | //TODO: prove invalidity
|
---|
162 | input.clear();
|
---|
163 | }
|
---|
164 |
|
---|
165 | void ParserTremoloUnitTest::getsetAtomDataTest() {
|
---|
166 | stringstream input;
|
---|
167 | input << Tremolo_Atomdata1;
|
---|
168 | parser->load(&input);
|
---|
169 |
|
---|
170 | CPPUNIT_ASSERT_EQUAL( std::string("Id name type x=3"), parser->getAtomData() );
|
---|
171 |
|
---|
172 | // overwrite keys
|
---|
173 | const std::string fewkeys("Id type x=3 neighbors=2");
|
---|
174 | parser->resetAtomData(fewkeys);
|
---|
175 | CPPUNIT_ASSERT_EQUAL( fewkeys, parser->getAtomData() );
|
---|
176 |
|
---|
177 | // add some keys
|
---|
178 | const std::string morekeys("charge");
|
---|
179 | parser->setAtomData(morekeys);
|
---|
180 | CPPUNIT_ASSERT_EQUAL( fewkeys+std::string(" ")+morekeys, parser->getAtomData() );
|
---|
181 |
|
---|
182 | // add similar key
|
---|
183 | const std::string otherkey("neighbors=4");
|
---|
184 | parser->setAtomData(otherkey);
|
---|
185 | CPPUNIT_ASSERT( fewkeys+std::string(" ")+morekeys != parser->getAtomData() );
|
---|
186 | }
|
---|
187 |
|
---|
188 | void ParserTremoloUnitTest::readTremoloCoordinatesTest() {
|
---|
189 | stringstream input;
|
---|
190 |
|
---|
191 | // One simple data line
|
---|
192 | input << Tremolo_Atomdata2;
|
---|
193 | parser->load(&input);
|
---|
194 | CPPUNIT_ASSERT(World::getInstance().getAtom(AtomByType(1))->at(0) == 3.0);
|
---|
195 | input.clear();
|
---|
196 | }
|
---|
197 |
|
---|
198 | void ParserTremoloUnitTest::readTremoloVelocityTest() {
|
---|
199 | stringstream input;
|
---|
200 |
|
---|
201 | // One simple data line
|
---|
202 | input << Tremolo_velocity;
|
---|
203 | parser->load(&input);
|
---|
204 | CPPUNIT_ASSERT(World::getInstance().getAtom(AtomByType(1))->getAtomicVelocity()[0] == 3.0);
|
---|
205 | input.clear();
|
---|
206 | }
|
---|
207 |
|
---|
208 | void ParserTremoloUnitTest::readTremoloNeighborInformationTest() {
|
---|
209 | stringstream input;
|
---|
210 |
|
---|
211 | // Neighbor data
|
---|
212 | input << Tremolo_neighbours;
|
---|
213 | parser->load(&input);
|
---|
214 |
|
---|
215 | CPPUNIT_ASSERT_EQUAL(3, World::getInstance().numAtoms());
|
---|
216 | CPPUNIT_ASSERT(World::getInstance().getAtom(AtomByType(8))->
|
---|
217 | IsBondedTo(WorldTime::getTime(), World::getInstance().getAtom(AtomByType(1))));
|
---|
218 | input.clear();
|
---|
219 | }
|
---|
220 |
|
---|
221 | void ParserTremoloUnitTest::readAndWriteTremoloImprDataInformationTest() {
|
---|
222 | stringstream input, output;
|
---|
223 |
|
---|
224 | // Neighbor data
|
---|
225 | {
|
---|
226 | input << Tremolo_improper;
|
---|
227 | parser->load(&input);
|
---|
228 | std::vector<atom *> atoms = World::getInstance().getAllAtoms();
|
---|
229 | parser->save(&output, atoms);
|
---|
230 | CPPUNIT_ASSERT_EQUAL(3, World::getInstance().numAtoms());
|
---|
231 | // std::cout << output.str() << std::endl;
|
---|
232 | CPPUNIT_ASSERT(output.str().find("3-1,1-3") != string::npos);
|
---|
233 | input.clear();
|
---|
234 | output.clear();
|
---|
235 | }
|
---|
236 | }
|
---|
237 |
|
---|
238 | void ParserTremoloUnitTest::readAndWriteTremoloTorsionInformationTest() {
|
---|
239 | stringstream input, output;
|
---|
240 |
|
---|
241 | // Neighbor data
|
---|
242 | {
|
---|
243 | input << Tremolo_torsion;
|
---|
244 | parser->load(&input);
|
---|
245 | std::vector<atom *> atoms = World::getInstance().getAllAtoms();
|
---|
246 | parser->save(&output, atoms);
|
---|
247 | CPPUNIT_ASSERT_EQUAL(3, World::getInstance().numAtoms());
|
---|
248 | // std::cout << output.str() << std::endl;
|
---|
249 | CPPUNIT_ASSERT(output.str().find("3-1,1-3") != string::npos);
|
---|
250 | input.clear();
|
---|
251 | output.clear();
|
---|
252 | }
|
---|
253 | }
|
---|
254 |
|
---|
255 | void ParserTremoloUnitTest::writeTremoloTest() {
|
---|
256 | stringstream output;
|
---|
257 |
|
---|
258 | // with the maximum number of fields and minimal information, default values are printed
|
---|
259 | {
|
---|
260 | atom* newAtom = World::getInstance().createAtom();
|
---|
261 | newAtom->setType(1);
|
---|
262 | 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");
|
---|
263 | std::vector<atom *> atoms = World::getInstance().getAllAtoms();
|
---|
264 | parser->save(&output, atoms);
|
---|
265 | // std::cout << output.str() << std::endl;
|
---|
266 | // std::cout << Tremolo_full << std::endl;
|
---|
267 | CPPUNIT_ASSERT(output.str() == Tremolo_full);
|
---|
268 | }
|
---|
269 |
|
---|
270 | // cout << "testing the tremolo parser is done" << endl;
|
---|
271 | }
|
---|
272 |
|
---|