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
RevLine 
[bcf653]1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
[0aa122]4 * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
[94d5ac6]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/>.
[bcf653]21 */
22
[57adc7]23/*
24 * MoleculeDescriptorTest.cpp
25 *
26 * Created on: Mar 4, 2010
27 * Author: crueger
28 */
29
[bf3817]30// include config.h
31#ifdef HAVE_CONFIG_H
32#include <config.h>
33#endif
34
[57adc7]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>
[12c4cb]42#include <Descriptors/MoleculeNameDescriptor.hpp>
43#include <Descriptors/MoleculeOrderDescriptor.hpp>
[57adc7]44
45#include "World.hpp"
46#include "molecule.hpp"
47
[6c9adc]48#include "MoleculeDescriptorUnitTest.hpp"
49
[57adc7]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(){
[23b547]60 World::getInstance();
[57adc7]61 for(int i=0;i<MOLECULE_COUNT;++i){
[23b547]62 molecules[i]= World::getInstance().createMolecule();
[57adc7]63 moleculeIds[i]= molecules[i]->getId();
64 }
65}
66
67void MoleculeDescriptorTest::tearDown(){
[23b547]68 World::purgeInstance();
[57adc7]69}
70
71// some helper functions
[1259df]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{
[57adc7]77 for(int i=0;i<MOLECULE_COUNT;++i){
78 moleculeId_t id = ids[i];
79 if(!excluded.count(id)){
[1259df]80 std::vector<const molecule*>::const_iterator iter;
[57adc7]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
[1259df]94static bool hasNoDuplicateMolecules(std::vector<const molecule*> &molecules){
[57adc7]95 std::set<moleculeId_t> found;
[1259df]96 std::vector<const molecule*>::const_iterator iter;
[57adc7]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(){
[1259df]108 std::vector<const molecule*> allMolecules =
109 const_cast<const World &>(World::getInstance()).getAllMolecules(AllMolecules());
[57adc7]110 CPPUNIT_ASSERT_EQUAL( true , hasAllMolecules(allMolecules,moleculeIds));
111 CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicateMolecules(allMolecules));
112
[1259df]113 std::vector<const molecule*> noMolecules =
114 const_cast<const World &>(World::getInstance()).getAllMolecules(NoMolecules());
[57adc7]115 CPPUNIT_ASSERT_EQUAL( true , noMolecules.empty());
116}
117void MoleculeDescriptorTest::MoleculeIdTest(){
118 // test Molecules from boundaries and middle of the set
[63fb7a]119 const molecule* testMolecule;
120 testMolecule = const_cast<const World &>(World::getInstance()).
121 getMolecule(MoleculeById(moleculeIds[0]));
[57adc7]122 CPPUNIT_ASSERT(testMolecule);
123 CPPUNIT_ASSERT_EQUAL( moleculeIds[0], testMolecule->getId());
[63fb7a]124 testMolecule = const_cast<const World &>(World::getInstance()).
125 getMolecule(MoleculeById(moleculeIds[MOLECULE_COUNT/2]));
[57adc7]126 CPPUNIT_ASSERT(testMolecule);
127 CPPUNIT_ASSERT_EQUAL( moleculeIds[MOLECULE_COUNT/2], testMolecule->getId());
[63fb7a]128 testMolecule = const_cast<const World &>(World::getInstance()).
129 getMolecule(MoleculeById(moleculeIds[MOLECULE_COUNT-1]));
[57adc7]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
[63fb7a]143 testMolecule = const_cast<const World &>(World::getInstance()).
144 getMolecule(MoleculeById(outsideId));
[57adc7]145 CPPUNIT_ASSERT(!testMolecule);
146}
147void MoleculeDescriptorTest::MoleculeCalcTest(){
148 // test some elementary set operations
149 {
[1259df]150 std::vector<const molecule*> testMolecules =
151 const_cast<const World &>(World::getInstance()).getAllMolecules(AllMolecules()||NoMolecules());
[57adc7]152 CPPUNIT_ASSERT_EQUAL( true , hasAllMolecules(testMolecules,moleculeIds));
153 CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicateMolecules(testMolecules));
154 }
155
156 {
[1259df]157 std::vector<const molecule*> testMolecules =
158 const_cast<const World &>(World::getInstance()).getAllMolecules(NoMolecules()||AllMolecules());
[57adc7]159 CPPUNIT_ASSERT_EQUAL( true , hasAllMolecules(testMolecules,moleculeIds));
160 CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicateMolecules(testMolecules));
161 }
162
163 {
[1259df]164 std::vector<const molecule*> testMolecules =
165 const_cast<const World &>(World::getInstance()).getAllMolecules(NoMolecules()&&AllMolecules());
[57adc7]166 CPPUNIT_ASSERT_EQUAL( true , testMolecules.empty());
167 }
168
169 {
[1259df]170 std::vector<const molecule*> testMolecules =
171 const_cast<const World &>(World::getInstance()).getAllMolecules(AllMolecules()&&NoMolecules());
[57adc7]172 CPPUNIT_ASSERT_EQUAL( true , testMolecules.empty());
173 }
174
175 {
[1259df]176 std::vector<const molecule*> testMolecules =
177 const_cast<const World &>(World::getInstance()).getAllMolecules(!AllMolecules());
[57adc7]178 CPPUNIT_ASSERT_EQUAL( true , testMolecules.empty());
179 }
180
181 {
[1259df]182 std::vector<const molecule*> testMolecules =
183 const_cast<const World &>(World::getInstance()).getAllMolecules(!NoMolecules());
[57adc7]184 CPPUNIT_ASSERT_EQUAL( true , hasAllMolecules(testMolecules,moleculeIds));
185 CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicateMolecules(testMolecules));
186 }
187
188 // exclude and include some molecules
189 {
[1259df]190 std::vector<const molecule*> testMolecules =
191 const_cast<const World &>(World::getInstance()).getAllMolecules(AllMolecules()&&(!MoleculeById(moleculeIds[MOLECULE_COUNT/2])));
[57adc7]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 {
[1259df]200 std::vector<const molecule*> testMolecules =
201 const_cast<const World &>(World::getInstance()).getAllMolecules(NoMolecules()||(MoleculeById(moleculeIds[MOLECULE_COUNT/2])));
[57adc7]202 CPPUNIT_ASSERT_EQUAL( (size_t)1, testMolecules.size());
203 CPPUNIT_ASSERT_EQUAL( moleculeIds[MOLECULE_COUNT/2], testMolecules[0]->getId());
204 }
205}
[12c4cb]206
207void MoleculeDescriptorTest::MoleculeNameTest()
208{
[63fb7a]209 const molecule* testMolecule;
[12c4cb]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) {
[63fb7a]217 testMolecule = const_cast<const World &>(World::getInstance()).
218 getMolecule(MoleculeByName(toString(i)));
[12c4cb]219 CPPUNIT_ASSERT_EQUAL( moleculeIds[i-1], testMolecule->getId());
220 }
221
222 // check for non-present name
[63fb7a]223 testMolecule = const_cast<const World &>(World::getInstance()).
224 getMolecule(MoleculeByName("not present"));
[12c4cb]225 CPPUNIT_ASSERT(!testMolecule);
226}
227
228
229void MoleculeDescriptorTest::MoleculeOrderTest()
230{
[63fb7a]231 const molecule* testMolecule;
[12c4cb]232
233 // test in normal order: 1, 2, ...
234 for(int i=1;i<=MOLECULE_COUNT;++i){
[63fb7a]235 testMolecule = const_cast<const World &>(World::getInstance()).
236 getMolecule(MoleculeByOrder(i));
[12c4cb]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){
[63fb7a]242 testMolecule = const_cast<const World &>(World::getInstance()).
243 getMolecule(MoleculeByOrder(-i));
[12c4cb]244 CPPUNIT_ASSERT_EQUAL( moleculeIds[(int)MOLECULE_COUNT-i], testMolecule->getId());
245 }
246
247 // test from outside of set
[63fb7a]248 testMolecule = const_cast<const World &>(World::getInstance()).
249 getMolecule(MoleculeByOrder(MOLECULE_COUNT+1));
[12c4cb]250 CPPUNIT_ASSERT(!testMolecule);
[63fb7a]251 testMolecule = const_cast<const World &>(World::getInstance()).
252 getMolecule(MoleculeByOrder(-MOLECULE_COUNT-1));
[12c4cb]253 CPPUNIT_ASSERT(!testMolecule);
254}
Note: See TracBrowser for help on using the repository browser.