source: src/unittests/DescriptorUnittest.cpp@ 02ee15

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 02ee15 was 46d958, checked in by Tillmann Crueger <crueger@…>, 15 years ago

Made the world solely responsible for creating and destroying atoms.

  • Property mode set to 100644
File size: 5.3 KB
Line 
1/*
2 * DescriptorUnittest.cpp
3 *
4 * Created on: Feb 9, 2010
5 * Author: crueger
6 */
7
8#include "DescriptorUnittest.hpp"
9
10#include <cppunit/CompilerOutputter.h>
11#include <cppunit/extensions/TestFactoryRegistry.h>
12#include <cppunit/ui/text/TestRunner.h>
13#include <iostream>
14
15#include <Descriptors/AtomDescriptor.hpp>
16#include <Descriptors/AtomIdDescriptor.hpp>
17
18#include "World.hpp"
19#include "atom.hpp"
20
21// Registers the fixture into the 'registry'
22CPPUNIT_TEST_SUITE_REGISTRATION( DescriptorUnittest );
23
24// set up and tear down
25void DescriptorUnittest::setUp(){
26 World::get();
27 for(int i=0;i<ATOM_COUNT;++i){
28 atoms[i]= World::get()->createAtom();
29 atomIds[i] = atoms[i]->getId();
30 }
31}
32void DescriptorUnittest::tearDown(){
33 World::destroy();
34}
35
36// some helper functions
37bool hasAll(std::vector<atom*> atoms,int ids[ATOM_COUNT], std::set<int> excluded = std::set<int>()){
38 for(int i=0;i<ATOM_COUNT;++i){
39 int id = ids[i];
40 if(!excluded.count(id)){
41 std::vector<atom*>::iterator iter;
42 bool res=false;
43 for(iter=atoms.begin();iter!=atoms.end();++iter){
44 res |= (*iter)->getId() == id;
45 }
46 if(!res) {
47 cout << "Atom " << id << " missing in returned list" << endl;
48 return false;
49 }
50 }
51 }
52 return true;
53}
54
55bool hasNoDuplicates(std::vector<atom*> atoms){
56 std::set<int> found;
57 std::vector<atom*>::iterator iter;
58 for(iter=atoms.begin();iter!=atoms.end();++iter){
59 int id = (*iter)->getId();
60 if(found.count(id))
61 return false;
62 found.insert(id);
63 }
64 return true;
65}
66
67
68void DescriptorUnittest::AtomBaseSetsTest(){
69 std::vector<atom*> allAtoms = World::get()->getAllAtoms(AllAtoms());
70 CPPUNIT_ASSERT_EQUAL( true , hasAll(allAtoms,atomIds));
71 CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicates(allAtoms));
72
73 std::vector<atom*> noAtoms = World::get()->getAllAtoms(NoAtoms());
74 CPPUNIT_ASSERT_EQUAL( true , noAtoms.empty());
75}
76void DescriptorUnittest::AtomIdTest(){
77 // test Atoms from boundaries and middle of the set
78 atom* testAtom;
79 testAtom = World::get()->getAtom(AtomById(atomIds[0]));
80 CPPUNIT_ASSERT(testAtom);
81 CPPUNIT_ASSERT_EQUAL( atomIds[0], testAtom->getId());
82 testAtom = World::get()->getAtom(AtomById(atomIds[ATOM_COUNT/2]));
83 CPPUNIT_ASSERT(testAtom);
84 CPPUNIT_ASSERT_EQUAL( atomIds[ATOM_COUNT/2], testAtom->getId());
85 testAtom = World::get()->getAtom(AtomById(atomIds[ATOM_COUNT-1]));
86 CPPUNIT_ASSERT(testAtom);
87 CPPUNIT_ASSERT_EQUAL( atomIds[ATOM_COUNT-1], testAtom->getId());
88
89 // find some ID that has not been created
90 int outsideId =-1;
91 bool res = false;
92 while(!res) {
93 ++outsideId;
94 res = true;
95 for(int i = 0; i < ATOM_COUNT; ++i){
96 res &= atomIds[i]!=outsideId;
97 }
98 }
99 // test from outside of set
100 testAtom = World::get()->getAtom(AtomById(outsideId));
101 CPPUNIT_ASSERT(!testAtom);
102}
103void DescriptorUnittest::AtomCalcTest(){
104 // test some elementary set operations
105 {
106 std::vector<atom*> testAtoms = World::get()->getAllAtoms(AllAtoms()||NoAtoms());
107 CPPUNIT_ASSERT_EQUAL( true , hasAll(testAtoms,atomIds));
108 CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicates(testAtoms));
109 }
110
111 {
112 std::vector<atom*> testAtoms = World::get()->getAllAtoms(NoAtoms()||AllAtoms());
113 CPPUNIT_ASSERT_EQUAL( true , hasAll(testAtoms,atomIds));
114 CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicates(testAtoms));
115 }
116
117 {
118 std::vector<atom*> testAtoms = World::get()->getAllAtoms(NoAtoms()&&AllAtoms());
119 CPPUNIT_ASSERT_EQUAL( true , testAtoms.empty());
120 }
121
122 {
123 std::vector<atom*> testAtoms = World::get()->getAllAtoms(AllAtoms()&&NoAtoms());
124 CPPUNIT_ASSERT_EQUAL( true , testAtoms.empty());
125 }
126
127 {
128 std::vector<atom*> testAtoms = World::get()->getAllAtoms(!AllAtoms());
129 CPPUNIT_ASSERT_EQUAL( true , testAtoms.empty());
130 }
131
132 {
133 std::vector<atom*> testAtoms = World::get()->getAllAtoms(!NoAtoms());
134 CPPUNIT_ASSERT_EQUAL( true , hasAll(testAtoms,atomIds));
135 CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicates(testAtoms));
136 }
137
138 // exclude and include some atoms
139 {
140 std::vector<atom*> testAtoms = World::get()->getAllAtoms(AllAtoms()&&(!AtomById(atomIds[ATOM_COUNT/2])));
141 std::set<int> excluded;
142 excluded.insert(atomIds[ATOM_COUNT/2]);
143 CPPUNIT_ASSERT_EQUAL( true , hasAll(testAtoms,atomIds,excluded));
144 CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicates(testAtoms));
145 CPPUNIT_ASSERT_EQUAL( (size_t)(ATOM_COUNT-1), testAtoms.size());
146 }
147
148 {
149 std::vector<atom*> testAtoms = World::get()->getAllAtoms(NoAtoms()||(AtomById(atomIds[ATOM_COUNT/2])));
150 CPPUNIT_ASSERT_EQUAL( (size_t)1, testAtoms.size());
151 CPPUNIT_ASSERT_EQUAL( atomIds[ATOM_COUNT/2], testAtoms[0]->getId());
152 }
153}
154
155/********************************************** Main routine **************************************/
156
157int main(int argc, char **argv)
158{
159 // Get the top level suite from the registry
160 CppUnit::Test *suite = CppUnit::TestFactoryRegistry::getRegistry().makeTest();
161
162 // Adds the test to the list of test to run
163 CppUnit::TextUi::TestRunner runner;
164 runner.addTest( suite );
165
166 // Change the default outputter to a compiler error format outputter
167 runner.setOutputter( new CppUnit::CompilerOutputter( &runner.result(),
168 std::cerr ) );
169 // Run the tests.
170 bool wasSucessful = runner.run();
171
172 // Return error code 1 if the one of test failed.
173 return wasSucessful ? 0 : 1;
174};
Note: See TracBrowser for help on using the repository browser.