source: src/unittests/DescriptorUnittest.cpp@ 685100

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 685100 was 7a1ce5, checked in by Tillmann Crueger <crueger@…>, 15 years ago

Rebuilt AtomDescriptors using PIMPL-Idiom and added unittest for descriptors

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