source: src/Descriptors/unittests/MoleculeDescriptorUnitTest.cpp@ 143263

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 143263 was 1259df, checked in by Frederik Heber <heber@…>, 9 years ago

Replaced World::getAllMolecules() wherever possible by const version.

  • Property mode set to 100644
File size: 8.7 KB
Line 
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 * MoleculeDescriptorTest.cpp
25 *
26 * Created on: Mar 4, 2010
27 * Author: crueger
28 */
29
30// include config.h
31#ifdef HAVE_CONFIG_H
32#include <config.h>
33#endif
34
35#include <cppunit/CompilerOutputter.h>
36#include <cppunit/extensions/TestFactoryRegistry.h>
37#include <cppunit/ui/text/TestRunner.h>
38#include <iostream>
39
40#include <Descriptors/MoleculeDescriptor.hpp>
41#include <Descriptors/MoleculeIdDescriptor.hpp>
42#include <Descriptors/MoleculeNameDescriptor.hpp>
43#include <Descriptors/MoleculeOrderDescriptor.hpp>
44
45#include "World.hpp"
46#include "molecule.hpp"
47
48#include "MoleculeDescriptorUnitTest.hpp"
49
50#ifdef HAVE_TESTRUNNER
51#include "UnitTestMain.hpp"
52#endif /*HAVE_TESTRUNNER*/
53
54/********************************************** Test classes **************************************/
55// Registers the fixture into the 'registry'
56CPPUNIT_TEST_SUITE_REGISTRATION( MoleculeDescriptorTest );
57
58// set up and tear down
59void MoleculeDescriptorTest::setUp(){
60 World::getInstance();
61 for(int i=0;i<MOLECULE_COUNT;++i){
62 molecules[i]= World::getInstance().createMolecule();
63 moleculeIds[i]= molecules[i]->getId();
64 }
65}
66
67void MoleculeDescriptorTest::tearDown(){
68 World::purgeInstance();
69}
70
71// some helper functions
72static bool hasAllMolecules(
73 std::vector<const molecule*> &molecules,
74 moleculeId_t ids[MOLECULE_COUNT],
75 std::set<moleculeId_t> excluded = std::set<moleculeId_t>())
76{
77 for(int i=0;i<MOLECULE_COUNT;++i){
78 moleculeId_t id = ids[i];
79 if(!excluded.count(id)){
80 std::vector<const molecule*>::const_iterator iter;
81 bool res=false;
82 for(iter=molecules.begin();iter!=molecules.end();++iter){
83 res |= (*iter)->getId() == id;
84 }
85 if(!res) {
86 cout << "Molecule " << id << " missing in returned list" << endl;
87 return false;
88 }
89 }
90 }
91 return true;
92}
93
94static bool hasNoDuplicateMolecules(std::vector<const molecule*> &molecules){
95 std::set<moleculeId_t> found;
96 std::vector<const molecule*>::const_iterator iter;
97 for(iter=molecules.begin();iter!=molecules.end();++iter){
98 int id = (*iter)->getId();
99 if(found.count(id))
100 return false;
101 found.insert(id);
102 }
103 return true;
104}
105
106
107void MoleculeDescriptorTest::MoleculeBaseSetsTest(){
108 std::vector<const molecule*> allMolecules =
109 const_cast<const World &>(World::getInstance()).getAllMolecules(AllMolecules());
110 CPPUNIT_ASSERT_EQUAL( true , hasAllMolecules(allMolecules,moleculeIds));
111 CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicateMolecules(allMolecules));
112
113 std::vector<const molecule*> noMolecules =
114 const_cast<const World &>(World::getInstance()).getAllMolecules(NoMolecules());
115 CPPUNIT_ASSERT_EQUAL( true , noMolecules.empty());
116}
117void MoleculeDescriptorTest::MoleculeIdTest(){
118 // test Molecules from boundaries and middle of the set
119 const molecule* testMolecule;
120 testMolecule = const_cast<const World &>(World::getInstance()).
121 getMolecule(MoleculeById(moleculeIds[0]));
122 CPPUNIT_ASSERT(testMolecule);
123 CPPUNIT_ASSERT_EQUAL( moleculeIds[0], testMolecule->getId());
124 testMolecule = const_cast<const World &>(World::getInstance()).
125 getMolecule(MoleculeById(moleculeIds[MOLECULE_COUNT/2]));
126 CPPUNIT_ASSERT(testMolecule);
127 CPPUNIT_ASSERT_EQUAL( moleculeIds[MOLECULE_COUNT/2], testMolecule->getId());
128 testMolecule = const_cast<const World &>(World::getInstance()).
129 getMolecule(MoleculeById(moleculeIds[MOLECULE_COUNT-1]));
130 CPPUNIT_ASSERT(testMolecule);
131 CPPUNIT_ASSERT_EQUAL( moleculeIds[MOLECULE_COUNT-1], testMolecule->getId());
132
133 // find some ID that has not been created
134 moleculeId_t outsideId=0;
135 bool res = false;
136 for(outsideId=0;!res;++outsideId) {
137 res = true;
138 for(int i = 0; i < MOLECULE_COUNT; ++i){
139 res &= moleculeIds[i]!=outsideId;
140 }
141 }
142 // test from outside of set
143 testMolecule = const_cast<const World &>(World::getInstance()).
144 getMolecule(MoleculeById(outsideId));
145 CPPUNIT_ASSERT(!testMolecule);
146}
147void MoleculeDescriptorTest::MoleculeCalcTest(){
148 // test some elementary set operations
149 {
150 std::vector<const molecule*> testMolecules =
151 const_cast<const World &>(World::getInstance()).getAllMolecules(AllMolecules()||NoMolecules());
152 CPPUNIT_ASSERT_EQUAL( true , hasAllMolecules(testMolecules,moleculeIds));
153 CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicateMolecules(testMolecules));
154 }
155
156 {
157 std::vector<const molecule*> testMolecules =
158 const_cast<const World &>(World::getInstance()).getAllMolecules(NoMolecules()||AllMolecules());
159 CPPUNIT_ASSERT_EQUAL( true , hasAllMolecules(testMolecules,moleculeIds));
160 CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicateMolecules(testMolecules));
161 }
162
163 {
164 std::vector<const molecule*> testMolecules =
165 const_cast<const World &>(World::getInstance()).getAllMolecules(NoMolecules()&&AllMolecules());
166 CPPUNIT_ASSERT_EQUAL( true , testMolecules.empty());
167 }
168
169 {
170 std::vector<const molecule*> testMolecules =
171 const_cast<const World &>(World::getInstance()).getAllMolecules(AllMolecules()&&NoMolecules());
172 CPPUNIT_ASSERT_EQUAL( true , testMolecules.empty());
173 }
174
175 {
176 std::vector<const molecule*> testMolecules =
177 const_cast<const World &>(World::getInstance()).getAllMolecules(!AllMolecules());
178 CPPUNIT_ASSERT_EQUAL( true , testMolecules.empty());
179 }
180
181 {
182 std::vector<const molecule*> testMolecules =
183 const_cast<const World &>(World::getInstance()).getAllMolecules(!NoMolecules());
184 CPPUNIT_ASSERT_EQUAL( true , hasAllMolecules(testMolecules,moleculeIds));
185 CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicateMolecules(testMolecules));
186 }
187
188 // exclude and include some molecules
189 {
190 std::vector<const molecule*> testMolecules =
191 const_cast<const World &>(World::getInstance()).getAllMolecules(AllMolecules()&&(!MoleculeById(moleculeIds[MOLECULE_COUNT/2])));
192 std::set<moleculeId_t> excluded;
193 excluded.insert(moleculeIds[MOLECULE_COUNT/2]);
194 CPPUNIT_ASSERT_EQUAL( true , hasAllMolecules(testMolecules,moleculeIds,excluded));
195 CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicateMolecules(testMolecules));
196 CPPUNIT_ASSERT_EQUAL( (size_t)(MOLECULE_COUNT-1), testMolecules.size());
197 }
198
199 {
200 std::vector<const molecule*> testMolecules =
201 const_cast<const World &>(World::getInstance()).getAllMolecules(NoMolecules()||(MoleculeById(moleculeIds[MOLECULE_COUNT/2])));
202 CPPUNIT_ASSERT_EQUAL( (size_t)1, testMolecules.size());
203 CPPUNIT_ASSERT_EQUAL( moleculeIds[MOLECULE_COUNT/2], testMolecules[0]->getId());
204 }
205}
206
207void MoleculeDescriptorTest::MoleculeNameTest()
208{
209 const molecule* testMolecule;
210
211 // name each molecule
212 for(int i=1;i<=MOLECULE_COUNT;++i)
213 molecules[i-1]->setName(toString(i));
214
215 // retrieve each
216 for(int i=1;i<=MOLECULE_COUNT;++i) {
217 testMolecule = const_cast<const World &>(World::getInstance()).
218 getMolecule(MoleculeByName(toString(i)));
219 CPPUNIT_ASSERT_EQUAL( moleculeIds[i-1], testMolecule->getId());
220 }
221
222 // check for non-present name
223 testMolecule = const_cast<const World &>(World::getInstance()).
224 getMolecule(MoleculeByName("not present"));
225 CPPUNIT_ASSERT(!testMolecule);
226}
227
228
229void MoleculeDescriptorTest::MoleculeOrderTest()
230{
231 const molecule* testMolecule;
232
233 // test in normal order: 1, 2, ...
234 for(int i=1;i<=MOLECULE_COUNT;++i){
235 testMolecule = const_cast<const World &>(World::getInstance()).
236 getMolecule(MoleculeByOrder(i));
237 CPPUNIT_ASSERT_EQUAL( moleculeIds[i-1], testMolecule->getId());
238 }
239
240 // test in reverse order: -1, -2, ...
241 for(int i=1; i<= MOLECULE_COUNT;++i){
242 testMolecule = const_cast<const World &>(World::getInstance()).
243 getMolecule(MoleculeByOrder(-i));
244 CPPUNIT_ASSERT_EQUAL( moleculeIds[(int)MOLECULE_COUNT-i], testMolecule->getId());
245 }
246
247 // test from outside of set
248 testMolecule = const_cast<const World &>(World::getInstance()).
249 getMolecule(MoleculeByOrder(MOLECULE_COUNT+1));
250 CPPUNIT_ASSERT(!testMolecule);
251 testMolecule = const_cast<const World &>(World::getInstance()).
252 getMolecule(MoleculeByOrder(-MOLECULE_COUNT-1));
253 CPPUNIT_ASSERT(!testMolecule);
254}
Note: See TracBrowser for help on using the repository browser.