source: src/unittests/FormulaUnitTest.cpp@ 72d108

Action_Thermostats Add_AtomRandomPerturbation Add_FitFragmentPartialChargesAction Add_RotateAroundBondAction Add_SelectAtomByNameAction Added_ParseSaveFragmentResults AddingActions_SaveParseParticleParameters Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_ParticleName_to_Atom Adding_StructOpt_integration_tests AtomFragments Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.5.4 Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator CombiningParticlePotentialParsing Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_BoundInBox_CenterInBox_MoleculeActions Fix_ChargeSampling_PBC Fix_ChronosMutex Fix_FitPartialCharges Fix_FitPotential_needs_atomicnumbers Fix_ForceAnnealing Fix_IndependentFragmentGrids Fix_ParseParticles Fix_ParseParticles_split_forward_backward_Actions Fix_PopActions Fix_QtFragmentList_sorted_selection Fix_Restrictedkeyset_FragmentMolecule Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns Fix_fitting_potentials Fixes ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion FragmentAction_writes_AtomFragments FragmentMolecule_checks_bonddegrees GeometryObjects Gui_Fixes Gui_displays_atomic_force_velocity ImplicitCharges IndependentFragmentGrids IndependentFragmentGrids_IndividualZeroInstances IndependentFragmentGrids_IntegrationTest IndependentFragmentGrids_Sole_NN_Calculation JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix MoreRobust_FragmentAutomation ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PdbParser_setsAtomName PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks Rewrite_FitPartialCharges RotateToPrincipalAxisSystem_UndoRedo SaturateAtoms_findBestMatching SaturateAtoms_singleDegree StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_LinearAlgebra Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg Switchable_LogView ThirdParty_MPQC_rebuilt_buildsystem TrajectoryDependenant_MaxOrder TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps TremoloParser_setsAtomName Ubuntu_1604_changes stable
Last change on this file since 72d108 was 266875, checked in by Frederik Heber <heber@…>, 14 years ago

FIX: ParserError did not adhere to naming convention.

  • Property mode set to 100644
File size: 10.2 KB
Line 
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 * FormulaUnitTest.cpp
10 *
11 * Created on: Jul 21, 2010
12 * Author: crueger
13 */
14
15// include config.h
16#ifdef HAVE_CONFIG_H
17#include <config.h>
18#endif
19
20#include <cppunit/CompilerOutputter.h>
21#include <cppunit/extensions/TestFactoryRegistry.h>
22#include <cppunit/ui/text/TestRunner.h>
23
24#include <iostream>
25
26#include "Formula.hpp"
27#include "World.hpp"
28
29#include "FormulaUnitTest.hpp"
30
31#ifdef HAVE_TESTRUNNER
32#include "UnitTestMain.hpp"
33#endif /*HAVE_TESTRUNNER*/
34
35CPPUNIT_TEST_SUITE_REGISTRATION( FormulaUnittest );
36
37void FormulaUnittest::setUp(){}
38void FormulaUnittest::tearDown(){
39 World::purgeInstance();
40}
41
42void FormulaUnittest::baseTest(){
43 Formula formula;
44 CPPUNIT_ASSERT_EQUAL(formula.getElementCount(),(unsigned int)0);
45 CPPUNIT_ASSERT(formula.begin()==formula.end());
46
47 // test some one letter element names
48 formula += "H";
49 CPPUNIT_ASSERT_EQUAL(formula["H"],(unsigned int)1);
50 CPPUNIT_ASSERT_EQUAL(formula["O"],(unsigned int)0);
51 CPPUNIT_ASSERT_EQUAL(formula["C"],(unsigned int)0);
52 CPPUNIT_ASSERT_EQUAL(formula["He"],(unsigned int)0);
53 CPPUNIT_ASSERT_EQUAL(formula.getElementCount(),(unsigned int)1);
54 CPPUNIT_ASSERT(formula.begin()!=formula.end());
55 formula += "H";
56 CPPUNIT_ASSERT_EQUAL(formula["H"],(unsigned int)2);
57 CPPUNIT_ASSERT_EQUAL(formula["O"],(unsigned int)0);
58 CPPUNIT_ASSERT_EQUAL(formula["C"],(unsigned int)0);
59 CPPUNIT_ASSERT_EQUAL(formula["He"],(unsigned int)0);
60 CPPUNIT_ASSERT_EQUAL(formula.getElementCount(),(unsigned int)1);
61 CPPUNIT_ASSERT(formula.begin()!=formula.end());
62 formula += "O";
63 CPPUNIT_ASSERT_EQUAL(formula["H"],(unsigned int)2);
64 CPPUNIT_ASSERT_EQUAL(formula["O"],(unsigned int)1);
65 CPPUNIT_ASSERT_EQUAL(formula["C"],(unsigned int)0);
66 CPPUNIT_ASSERT_EQUAL(formula["He"],(unsigned int)0);
67 CPPUNIT_ASSERT_EQUAL(formula.getElementCount(),(unsigned int)2);
68 CPPUNIT_ASSERT(formula.begin()!=formula.end());
69
70 // test copy constructor
71 Formula formula2=formula;
72 CPPUNIT_ASSERT_EQUAL(formula2["H"],(unsigned int)2);
73 CPPUNIT_ASSERT_EQUAL(formula2["O"],(unsigned int)1);
74 CPPUNIT_ASSERT_EQUAL(formula2["C"],(unsigned int)0);
75 CPPUNIT_ASSERT_EQUAL(formula["He"],(unsigned int)0);
76 CPPUNIT_ASSERT_EQUAL(formula2.getElementCount(),(unsigned int)2);
77 CPPUNIT_ASSERT(formula2.begin()!=formula2.end());
78
79 // test Assignment operator
80 formula2=Formula();
81 CPPUNIT_ASSERT_EQUAL(formula2["H"],(unsigned int)0);
82 CPPUNIT_ASSERT_EQUAL(formula2["O"],(unsigned int)0);
83 CPPUNIT_ASSERT_EQUAL(formula2["C"],(unsigned int)0);
84 CPPUNIT_ASSERT_EQUAL(formula["He"],(unsigned int)0);
85 CPPUNIT_ASSERT_EQUAL(formula2.getElementCount(),(unsigned int)0);
86 CPPUNIT_ASSERT(formula2.begin()==formula2.end());
87
88 // test comparison
89 CPPUNIT_ASSERT(formula!=formula2);
90 Formula formula3;
91 formula3 += "H";
92 formula3 += "H";
93 formula3 += "O";
94 CPPUNIT_ASSERT(formula==formula3);
95
96 // inequality
97 formula3 += "O";
98 CPPUNIT_ASSERT(formula!=formula3);
99
100 // after removing it should be fine again
101 formula3 -= "O";
102 CPPUNIT_ASSERT(formula==formula3);
103
104 // add something from the far end, to test resizing
105 formula3 += "Rh";
106 CPPUNIT_ASSERT(formula!=formula3);
107 formula3 -= "Rh";
108 CPPUNIT_ASSERT(formula==formula3);
109
110 // removal of elements
111 formula -= "H";
112 CPPUNIT_ASSERT_EQUAL(formula["H"],(unsigned int)1);
113 CPPUNIT_ASSERT_EQUAL(formula["O"],(unsigned int)1);
114 CPPUNIT_ASSERT_EQUAL(formula["C"],(unsigned int)0);
115 CPPUNIT_ASSERT_EQUAL(formula["He"],(unsigned int)0);
116 CPPUNIT_ASSERT_EQUAL(formula.getElementCount(),(unsigned int)2);
117 CPPUNIT_ASSERT(formula.begin()!=formula.end());
118
119 formula -= "O";
120 CPPUNIT_ASSERT_EQUAL(formula["H"],(unsigned int)1);
121 CPPUNIT_ASSERT_EQUAL(formula["O"],(unsigned int)0);
122 CPPUNIT_ASSERT_EQUAL(formula["C"],(unsigned int)0);
123 CPPUNIT_ASSERT_EQUAL(formula["He"],(unsigned int)0);
124 CPPUNIT_ASSERT_EQUAL(formula.getElementCount(),(unsigned int)1);
125 CPPUNIT_ASSERT(formula.begin()!=formula.end());
126
127 formula -= "H";
128 CPPUNIT_ASSERT_EQUAL(formula["H"],(unsigned int)0);
129 CPPUNIT_ASSERT_EQUAL(formula["O"],(unsigned int)0);
130 CPPUNIT_ASSERT_EQUAL(formula["C"],(unsigned int)0);
131 CPPUNIT_ASSERT_EQUAL(formula["He"],(unsigned int)0);
132 CPPUNIT_ASSERT_EQUAL(formula.getElementCount(),(unsigned int)0);
133 CPPUNIT_ASSERT(formula.begin()==formula.end());
134}
135
136void FormulaUnittest::parseTest(){
137 {
138 Formula formula("");
139 CPPUNIT_ASSERT_EQUAL(formula["H"],(unsigned int)0);
140 CPPUNIT_ASSERT_EQUAL(formula["O"],(unsigned int)0);
141 CPPUNIT_ASSERT_EQUAL(formula["C"],(unsigned int)0);
142 CPPUNIT_ASSERT_EQUAL(formula["He"],(unsigned int)0);
143 }
144 {
145 Formula formula("H");
146 CPPUNIT_ASSERT_EQUAL(formula["H"],(unsigned int)1);
147 CPPUNIT_ASSERT_EQUAL(formula["O"],(unsigned int)0);
148 CPPUNIT_ASSERT_EQUAL(formula["C"],(unsigned int)0);
149 CPPUNIT_ASSERT_EQUAL(formula["He"],(unsigned int)0);
150 }
151 {
152 Formula formula("H2");
153 CPPUNIT_ASSERT_EQUAL(formula["H"],(unsigned int)2);
154 CPPUNIT_ASSERT_EQUAL(formula["O"],(unsigned int)0);
155 CPPUNIT_ASSERT_EQUAL(formula["C"],(unsigned int)0);
156 CPPUNIT_ASSERT_EQUAL(formula["He"],(unsigned int)0);
157 }
158 {
159 Formula formula("H2O");
160 CPPUNIT_ASSERT_EQUAL(formula["H"],(unsigned int)2);
161 CPPUNIT_ASSERT_EQUAL(formula["O"],(unsigned int)1);
162 CPPUNIT_ASSERT_EQUAL(formula["C"],(unsigned int)0);
163 CPPUNIT_ASSERT_EQUAL(formula["He"],(unsigned int)0);
164 }
165 {
166 Formula formula("CH3COOH");
167 CPPUNIT_ASSERT_EQUAL(formula["H"],(unsigned int)4);
168 CPPUNIT_ASSERT_EQUAL(formula["O"],(unsigned int)2);
169 CPPUNIT_ASSERT_EQUAL(formula["C"],(unsigned int)2);
170 CPPUNIT_ASSERT_EQUAL(formula["He"],(unsigned int)0);
171 }
172 {
173 Formula formula("CH3COONa");
174 CPPUNIT_ASSERT_EQUAL(formula["H"],(unsigned int)3);
175 CPPUNIT_ASSERT_EQUAL(formula["O"],(unsigned int)2);
176 CPPUNIT_ASSERT_EQUAL(formula["C"],(unsigned int)2);
177 CPPUNIT_ASSERT_EQUAL(formula["Na"],(unsigned int)1);
178 CPPUNIT_ASSERT_EQUAL(formula["He"],(unsigned int)0);
179 }
180 {
181 Formula formula("NaCH3COO");
182 CPPUNIT_ASSERT_EQUAL(formula["H"],(unsigned int)3);
183 CPPUNIT_ASSERT_EQUAL(formula["O"],(unsigned int)2);
184 CPPUNIT_ASSERT_EQUAL(formula["C"],(unsigned int)2);
185 CPPUNIT_ASSERT_EQUAL(formula["Na"],(unsigned int)1);
186 CPPUNIT_ASSERT_EQUAL(formula["He"],(unsigned int)0);
187 }
188 {
189 Formula formula("Na2CO3");
190 CPPUNIT_ASSERT_EQUAL(formula["H"],(unsigned int)0);
191 CPPUNIT_ASSERT_EQUAL(formula["O"],(unsigned int)3);
192 CPPUNIT_ASSERT_EQUAL(formula["C"],(unsigned int)1);
193 CPPUNIT_ASSERT_EQUAL(formula["Na"],(unsigned int)2);
194 CPPUNIT_ASSERT_EQUAL(formula["He"],(unsigned int)0);
195 }
196 {
197 Formula formula("CH2(COOH)2");
198 CPPUNIT_ASSERT_EQUAL(formula["H"],(unsigned int)4);
199 CPPUNIT_ASSERT_EQUAL(formula["O"],(unsigned int)4);
200 CPPUNIT_ASSERT_EQUAL(formula["C"],(unsigned int)3);
201 CPPUNIT_ASSERT_EQUAL(formula["Na"],(unsigned int)0);
202 CPPUNIT_ASSERT_EQUAL(formula["He"],(unsigned int)0);
203 }
204 {
205 Formula formula("K4[Fe(CN)6]");
206 CPPUNIT_ASSERT_EQUAL(formula["H"],(unsigned int)0);
207 CPPUNIT_ASSERT_EQUAL(formula["O"],(unsigned int)0);
208 CPPUNIT_ASSERT_EQUAL(formula["C"],(unsigned int)6);
209 CPPUNIT_ASSERT_EQUAL(formula["Na"],(unsigned int)0);
210 CPPUNIT_ASSERT_EQUAL(formula["He"],(unsigned int)0);
211 CPPUNIT_ASSERT_EQUAL(formula["K"],(unsigned int)4);
212 CPPUNIT_ASSERT_EQUAL(formula["Fe"],(unsigned int)1);
213 CPPUNIT_ASSERT_EQUAL(formula["N"],(unsigned int)6);
214 }
215 {
216 Formula formula("[CrCl3(H2O)3]");
217 CPPUNIT_ASSERT_EQUAL(formula["H"],(unsigned int)6);
218 CPPUNIT_ASSERT_EQUAL(formula["O"],(unsigned int)3);
219 CPPUNIT_ASSERT_EQUAL(formula["C"],(unsigned int)0);
220 CPPUNIT_ASSERT_EQUAL(formula["Na"],(unsigned int)0);
221 CPPUNIT_ASSERT_EQUAL(formula["He"],(unsigned int)0);
222 CPPUNIT_ASSERT_EQUAL(formula["Cr"],(unsigned int)1);
223 CPPUNIT_ASSERT_EQUAL(formula["Cl"],(unsigned int)3);
224 }
225 {
226 Formula formula("Mg3[Fe(CN)6]2");
227 CPPUNIT_ASSERT_EQUAL(formula["H"],(unsigned int)0);
228 CPPUNIT_ASSERT_EQUAL(formula["O"],(unsigned int)0);
229 CPPUNIT_ASSERT_EQUAL(formula["C"],(unsigned int)12);
230 CPPUNIT_ASSERT_EQUAL(formula["Na"],(unsigned int)0);
231 CPPUNIT_ASSERT_EQUAL(formula["He"],(unsigned int)0);
232 CPPUNIT_ASSERT_EQUAL(formula["Mg"],(unsigned int)3);
233 CPPUNIT_ASSERT_EQUAL(formula["Fe"],(unsigned int)2);
234 CPPUNIT_ASSERT_EQUAL(formula["N"],(unsigned int)12);
235 }
236 {
237 Formula formula("Na[Fe((HO2CCH2)2NCH2CH2N(CH2CO2H)2)]");
238 CPPUNIT_ASSERT_EQUAL(formula["H"],(unsigned int)16);
239 CPPUNIT_ASSERT_EQUAL(formula["O"],(unsigned int)8);
240 CPPUNIT_ASSERT_EQUAL(formula["C"],(unsigned int)10);
241 CPPUNIT_ASSERT_EQUAL(formula["Na"],(unsigned int)1);
242 CPPUNIT_ASSERT_EQUAL(formula["He"],(unsigned int)0);
243 CPPUNIT_ASSERT_EQUAL(formula["Mg"],(unsigned int)0);
244 CPPUNIT_ASSERT_EQUAL(formula["Fe"],(unsigned int)1);
245 CPPUNIT_ASSERT_EQUAL(formula["N"],(unsigned int)2);
246 }
247 {
248 CPPUNIT_ASSERT_THROW(Formula formula("74107"),FormulaStringParseException);
249 CPPUNIT_ASSERT_THROW(Formula formula(" "),FormulaStringParseException);
250 CPPUNIT_ASSERT_THROW(Formula formula("nAcL"),FormulaStringParseException);
251 CPPUNIT_ASSERT_THROW(Formula formula("NAcL"),FormulaStringParseException);
252 CPPUNIT_ASSERT_THROW(Formula formula("Na Cl"),FormulaStringParseException);
253 CPPUNIT_ASSERT_THROW(Formula formula("1NaCl"),FormulaStringParseException);
254 CPPUNIT_ASSERT_THROW(Formula formula("Mag"),FormulaStringParseException);
255 CPPUNIT_ASSERT_THROW(Formula formula("AgCl)"),FormulaStringParseException);
256 CPPUNIT_ASSERT_THROW(Formula formula("(Na"),FormulaStringParseException);
257 CPPUNIT_ASSERT_THROW(Formula formula("(Mag)"),FormulaStringParseException);
258 CPPUNIT_ASSERT_THROW(Formula formula("MgCl2)"),FormulaStringParseException);
259 CPPUNIT_ASSERT_THROW(Formula formula("((MgCl2)"),FormulaStringParseException);
260 CPPUNIT_ASSERT_THROW(Formula formula("(MgCl2))"),FormulaStringParseException);
261 CPPUNIT_ASSERT_THROW(Formula formula("(MgCl2]"),FormulaStringParseException);
262 CPPUNIT_ASSERT_THROW(Formula formula("[MgCl2)"),FormulaStringParseException);
263 CPPUNIT_ASSERT_THROW(Formula formula("N(aCl"),FormulaStringParseException);
264 CPPUNIT_ASSERT_THROW(Formula formula("Na()Cl"),FormulaStringParseException);
265 }
266
267
268}
Note: See TracBrowser for help on using the repository browser.