source: src/unittests/FormulaUnittest.cpp@ 4c1230

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 4c1230 was bcf653, checked in by Frederik Heber <heber@…>, 14 years ago

Added copyright note to each .cpp file and an extensive one to builder.cpp.

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