source: src/Descriptors/unittests/MoleculeDescriptorUnitTest.cpp@ 63fb7a

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 63fb7a was 63fb7a, checked in by Frederik Heber <heber@…>, 10 years ago

Replaced getMolecule() by const access version where possible.

  • so far this is done done via const_casting the World.
  • Property mode set to 100644
File size: 8.3 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
72static bool hasAllMolecules(std::vector<molecule*> molecules,moleculeId_t ids[MOLECULE_COUNT], std::set<moleculeId_t> excluded = std::set<moleculeId_t>()){
73 for(int i=0;i<MOLECULE_COUNT;++i){
74 moleculeId_t id = ids[i];
75 if(!excluded.count(id)){
76 std::vector<molecule*>::iterator iter;
77 bool res=false;
78 for(iter=molecules.begin();iter!=molecules.end();++iter){
79 res |= (*iter)->getId() == id;
80 }
81 if(!res) {
82 cout << "Molecule " << id << " missing in returned list" << endl;
83 return false;
84 }
85 }
86 }
87 return true;
88}
89
90static bool hasNoDuplicateMolecules(std::vector<molecule*> molecules){
91 std::set<moleculeId_t> found;
92 std::vector<molecule*>::iterator iter;
93 for(iter=molecules.begin();iter!=molecules.end();++iter){
94 int id = (*iter)->getId();
95 if(found.count(id))
96 return false;
97 found.insert(id);
98 }
99 return true;
100}
101
102
103void MoleculeDescriptorTest::MoleculeBaseSetsTest(){
[23b547]104 std::vector<molecule*> allMolecules = World::getInstance().getAllMolecules(AllMolecules());
[57adc7]105 CPPUNIT_ASSERT_EQUAL( true , hasAllMolecules(allMolecules,moleculeIds));
106 CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicateMolecules(allMolecules));
107
[23b547]108 std::vector<molecule*> noMolecules = World::getInstance().getAllMolecules(NoMolecules());
[57adc7]109 CPPUNIT_ASSERT_EQUAL( true , noMolecules.empty());
110}
111void MoleculeDescriptorTest::MoleculeIdTest(){
112 // test Molecules from boundaries and middle of the set
[63fb7a]113 const molecule* testMolecule;
114 testMolecule = const_cast<const World &>(World::getInstance()).
115 getMolecule(MoleculeById(moleculeIds[0]));
[57adc7]116 CPPUNIT_ASSERT(testMolecule);
117 CPPUNIT_ASSERT_EQUAL( moleculeIds[0], testMolecule->getId());
[63fb7a]118 testMolecule = const_cast<const World &>(World::getInstance()).
119 getMolecule(MoleculeById(moleculeIds[MOLECULE_COUNT/2]));
[57adc7]120 CPPUNIT_ASSERT(testMolecule);
121 CPPUNIT_ASSERT_EQUAL( moleculeIds[MOLECULE_COUNT/2], testMolecule->getId());
[63fb7a]122 testMolecule = const_cast<const World &>(World::getInstance()).
123 getMolecule(MoleculeById(moleculeIds[MOLECULE_COUNT-1]));
[57adc7]124 CPPUNIT_ASSERT(testMolecule);
125 CPPUNIT_ASSERT_EQUAL( moleculeIds[MOLECULE_COUNT-1], testMolecule->getId());
126
127 // find some ID that has not been created
128 moleculeId_t outsideId=0;
129 bool res = false;
130 for(outsideId=0;!res;++outsideId) {
131 res = true;
132 for(int i = 0; i < MOLECULE_COUNT; ++i){
133 res &= moleculeIds[i]!=outsideId;
134 }
135 }
136 // test from outside of set
[63fb7a]137 testMolecule = const_cast<const World &>(World::getInstance()).
138 getMolecule(MoleculeById(outsideId));
[57adc7]139 CPPUNIT_ASSERT(!testMolecule);
140}
141void MoleculeDescriptorTest::MoleculeCalcTest(){
142 // test some elementary set operations
143 {
[23b547]144 std::vector<molecule*> testMolecules = World::getInstance().getAllMolecules(AllMolecules()||NoMolecules());
[57adc7]145 CPPUNIT_ASSERT_EQUAL( true , hasAllMolecules(testMolecules,moleculeIds));
146 CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicateMolecules(testMolecules));
147 }
148
149 {
[23b547]150 std::vector<molecule*> testMolecules = World::getInstance().getAllMolecules(NoMolecules()||AllMolecules());
[57adc7]151 CPPUNIT_ASSERT_EQUAL( true , hasAllMolecules(testMolecules,moleculeIds));
152 CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicateMolecules(testMolecules));
153 }
154
155 {
[23b547]156 std::vector<molecule*> testMolecules = World::getInstance().getAllMolecules(NoMolecules()&&AllMolecules());
[57adc7]157 CPPUNIT_ASSERT_EQUAL( true , testMolecules.empty());
158 }
159
160 {
[23b547]161 std::vector<molecule*> testMolecules = World::getInstance().getAllMolecules(AllMolecules()&&NoMolecules());
[57adc7]162 CPPUNIT_ASSERT_EQUAL( true , testMolecules.empty());
163 }
164
165 {
[23b547]166 std::vector<molecule*> testMolecules = World::getInstance().getAllMolecules(!AllMolecules());
[57adc7]167 CPPUNIT_ASSERT_EQUAL( true , testMolecules.empty());
168 }
169
170 {
[23b547]171 std::vector<molecule*> testMolecules = World::getInstance().getAllMolecules(!NoMolecules());
[57adc7]172 CPPUNIT_ASSERT_EQUAL( true , hasAllMolecules(testMolecules,moleculeIds));
173 CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicateMolecules(testMolecules));
174 }
175
176 // exclude and include some molecules
177 {
[23b547]178 std::vector<molecule*> testMolecules = World::getInstance().getAllMolecules(AllMolecules()&&(!MoleculeById(moleculeIds[MOLECULE_COUNT/2])));
[57adc7]179 std::set<moleculeId_t> excluded;
180 excluded.insert(moleculeIds[MOLECULE_COUNT/2]);
181 CPPUNIT_ASSERT_EQUAL( true , hasAllMolecules(testMolecules,moleculeIds,excluded));
182 CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicateMolecules(testMolecules));
183 CPPUNIT_ASSERT_EQUAL( (size_t)(MOLECULE_COUNT-1), testMolecules.size());
184 }
185
186 {
[23b547]187 std::vector<molecule*> testMolecules = World::getInstance().getAllMolecules(NoMolecules()||(MoleculeById(moleculeIds[MOLECULE_COUNT/2])));
[57adc7]188 CPPUNIT_ASSERT_EQUAL( (size_t)1, testMolecules.size());
189 CPPUNIT_ASSERT_EQUAL( moleculeIds[MOLECULE_COUNT/2], testMolecules[0]->getId());
190 }
191}
[12c4cb]192
193void MoleculeDescriptorTest::MoleculeNameTest()
194{
[63fb7a]195 const molecule* testMolecule;
[12c4cb]196
197 // name each molecule
198 for(int i=1;i<=MOLECULE_COUNT;++i)
199 molecules[i-1]->setName(toString(i));
200
201 // retrieve each
202 for(int i=1;i<=MOLECULE_COUNT;++i) {
[63fb7a]203 testMolecule = const_cast<const World &>(World::getInstance()).
204 getMolecule(MoleculeByName(toString(i)));
[12c4cb]205 CPPUNIT_ASSERT_EQUAL( moleculeIds[i-1], testMolecule->getId());
206 }
207
208 // check for non-present name
[63fb7a]209 testMolecule = const_cast<const World &>(World::getInstance()).
210 getMolecule(MoleculeByName("not present"));
[12c4cb]211 CPPUNIT_ASSERT(!testMolecule);
212}
213
214
215void MoleculeDescriptorTest::MoleculeOrderTest()
216{
[63fb7a]217 const molecule* testMolecule;
[12c4cb]218
219 // test in normal order: 1, 2, ...
220 for(int i=1;i<=MOLECULE_COUNT;++i){
[63fb7a]221 testMolecule = const_cast<const World &>(World::getInstance()).
222 getMolecule(MoleculeByOrder(i));
[12c4cb]223 CPPUNIT_ASSERT_EQUAL( moleculeIds[i-1], testMolecule->getId());
224 }
225
226 // test in reverse order: -1, -2, ...
227 for(int i=1; i<= MOLECULE_COUNT;++i){
[63fb7a]228 testMolecule = const_cast<const World &>(World::getInstance()).
229 getMolecule(MoleculeByOrder(-i));
[12c4cb]230 CPPUNIT_ASSERT_EQUAL( moleculeIds[(int)MOLECULE_COUNT-i], testMolecule->getId());
231 }
232
233 // test from outside of set
[63fb7a]234 testMolecule = const_cast<const World &>(World::getInstance()).
235 getMolecule(MoleculeByOrder(MOLECULE_COUNT+1));
[12c4cb]236 CPPUNIT_ASSERT(!testMolecule);
[63fb7a]237 testMolecule = const_cast<const World &>(World::getInstance()).
238 getMolecule(MoleculeByOrder(-MOLECULE_COUNT-1));
[12c4cb]239 CPPUNIT_ASSERT(!testMolecule);
240}
Note: See TracBrowser for help on using the repository browser.