source: src/unittests/FormulaUnitTest.cpp@ 30c753

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 30c753 was d103d3, checked in by Frederik Heber <heber@…>, 13 years ago

Added additional check in CodeCheck's project disclaimer for current year in copyright.

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