source: src/unittests/FormulaUnittest.cpp@ 13e3c3

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 13e3c3 was bf3817, checked in by Frederik Heber <heber@…>, 14 years ago

Added ifdef HAVE_CONFIG and config.h include to each and every cpp file.

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