1 | /*
|
---|
2 | * Project: MoleCuilder
|
---|
3 | * Description: creates and alters molecular systems
|
---|
4 | * Copyright (C) 2010-2011 University of Bonn. All rights reserved.
|
---|
5 | * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
|
---|
6 | */
|
---|
7 |
|
---|
8 | /*
|
---|
9 | * AtomDescriptorUnitTest.cpp
|
---|
10 | *
|
---|
11 | * Created on: Feb 9, 2010
|
---|
12 | * Author: crueger
|
---|
13 | */
|
---|
14 |
|
---|
15 | // include config.h
|
---|
16 | #ifdef HAVE_CONFIG_H
|
---|
17 | #include <config.h>
|
---|
18 | #endif
|
---|
19 |
|
---|
20 | #include "AtomDescriptorUnitTest.hpp"
|
---|
21 |
|
---|
22 | #include <cppunit/CompilerOutputter.h>
|
---|
23 | #include <cppunit/extensions/TestFactoryRegistry.h>
|
---|
24 | #include <cppunit/ui/text/TestRunner.h>
|
---|
25 | #include <iostream>
|
---|
26 |
|
---|
27 | #include <Descriptors/AtomDescriptor.hpp>
|
---|
28 | #include <Descriptors/AtomIdDescriptor.hpp>
|
---|
29 | #include <Descriptors/AtomOfMoleculeDescriptor.hpp>
|
---|
30 |
|
---|
31 | #include "World.hpp"
|
---|
32 | #include "Atom/atom.hpp"
|
---|
33 | #include "molecule.hpp"
|
---|
34 |
|
---|
35 | #ifdef HAVE_TESTRUNNER
|
---|
36 | #include "UnitTestMain.hpp"
|
---|
37 | #endif /*HAVE_TESTRUNNER*/
|
---|
38 |
|
---|
39 | /********************************************** Test classes **************************************/
|
---|
40 | // Registers the fixture into the 'registry'
|
---|
41 | CPPUNIT_TEST_SUITE_REGISTRATION( AtomDescriptorTest );
|
---|
42 |
|
---|
43 | // set up and tear down
|
---|
44 | void AtomDescriptorTest::setUp(){
|
---|
45 | World::getInstance();
|
---|
46 | for(int i=0;i<ATOM_COUNT;++i){
|
---|
47 | atoms[i]= World::getInstance().createAtom();
|
---|
48 | atomIds[i]= atoms[i]->getId();
|
---|
49 | }
|
---|
50 | }
|
---|
51 |
|
---|
52 | void AtomDescriptorTest::tearDown(){
|
---|
53 | World::purgeInstance();
|
---|
54 | }
|
---|
55 |
|
---|
56 | // some helper functions
|
---|
57 | static bool hasAllAtoms(std::vector<atom*> atoms,atomId_t ids[ATOM_COUNT], std::set<atomId_t> excluded = std::set<atomId_t>()){
|
---|
58 | for(int i=0;i<ATOM_COUNT;++i){
|
---|
59 | atomId_t id = ids[i];
|
---|
60 | if(!excluded.count(id)){
|
---|
61 | std::vector<atom*>::iterator iter;
|
---|
62 | bool res=false;
|
---|
63 | for(iter=atoms.begin();iter!=atoms.end();++iter){
|
---|
64 | res |= (*iter)->getId() == id;
|
---|
65 | }
|
---|
66 | if(!res) {
|
---|
67 | cout << "Atom " << id << " missing in returned list" << endl;
|
---|
68 | return false;
|
---|
69 | }
|
---|
70 | }
|
---|
71 | }
|
---|
72 | return true;
|
---|
73 | }
|
---|
74 |
|
---|
75 | static bool hasNoDuplicateAtoms(std::vector<atom*> atoms){
|
---|
76 | std::set<atomId_t> found;
|
---|
77 | std::vector<atom*>::iterator iter;
|
---|
78 | for(iter=atoms.begin();iter!=atoms.end();++iter){
|
---|
79 | int id = (*iter)->getId();
|
---|
80 | if(found.count(id))
|
---|
81 | return false;
|
---|
82 | found.insert(id);
|
---|
83 | }
|
---|
84 | return true;
|
---|
85 | }
|
---|
86 |
|
---|
87 |
|
---|
88 | void AtomDescriptorTest::AtomBaseSetsTest(){
|
---|
89 | std::vector<atom*> allAtoms = World::getInstance().getAllAtoms(AllAtoms());
|
---|
90 | CPPUNIT_ASSERT_EQUAL( true , hasAllAtoms(allAtoms,atomIds));
|
---|
91 | CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicateAtoms(allAtoms));
|
---|
92 |
|
---|
93 | std::vector<atom*> noAtoms = World::getInstance().getAllAtoms(NoAtoms());
|
---|
94 | CPPUNIT_ASSERT_EQUAL( true , noAtoms.empty());
|
---|
95 | }
|
---|
96 | void AtomDescriptorTest::AtomIdTest(){
|
---|
97 | // test Atoms from boundaries and middle of the set
|
---|
98 | atom* testAtom;
|
---|
99 | testAtom = World::getInstance().getAtom(AtomById(atomIds[0]));
|
---|
100 | CPPUNIT_ASSERT(testAtom);
|
---|
101 | CPPUNIT_ASSERT_EQUAL( atomIds[0], testAtom->getId());
|
---|
102 | testAtom = World::getInstance().getAtom(AtomById(atomIds[ATOM_COUNT/2]));
|
---|
103 | CPPUNIT_ASSERT(testAtom);
|
---|
104 | CPPUNIT_ASSERT_EQUAL( atomIds[ATOM_COUNT/2], testAtom->getId());
|
---|
105 | testAtom = World::getInstance().getAtom(AtomById(atomIds[ATOM_COUNT-1]));
|
---|
106 | CPPUNIT_ASSERT(testAtom);
|
---|
107 | CPPUNIT_ASSERT_EQUAL( atomIds[ATOM_COUNT-1], testAtom->getId());
|
---|
108 |
|
---|
109 | // find some ID that has not been created
|
---|
110 | atomId_t outsideId=0;
|
---|
111 | bool res = false;
|
---|
112 | for(outsideId=0;!res;++outsideId) {
|
---|
113 | res = true;
|
---|
114 | for(int i = 0; i < ATOM_COUNT; ++i){
|
---|
115 | res &= atomIds[i]!=outsideId;
|
---|
116 | }
|
---|
117 | }
|
---|
118 | // test from outside of set
|
---|
119 | testAtom = World::getInstance().getAtom(AtomById(outsideId));
|
---|
120 | CPPUNIT_ASSERT(!testAtom);
|
---|
121 | }
|
---|
122 | void AtomDescriptorTest::AtomOfMoleculeTest(){
|
---|
123 | // test Atoms from boundaries and middle of the set
|
---|
124 | atom* testAtom;
|
---|
125 | testAtom = World::getInstance().getAtom(AtomById(atomIds[0]));
|
---|
126 | CPPUNIT_ASSERT(testAtom);
|
---|
127 | CPPUNIT_ASSERT_EQUAL( atomIds[0], testAtom->getId());
|
---|
128 |
|
---|
129 | // create some molecule and associate atom to it
|
---|
130 | testAtom->setType(1);
|
---|
131 | molecule * newmol = World::getInstance().createMolecule();
|
---|
132 | newmol->AddAtom(testAtom);
|
---|
133 | CPPUNIT_ASSERT_EQUAL(newmol->getId(), testAtom->getMolecule()->getId());
|
---|
134 |
|
---|
135 | // get atom by descriptor
|
---|
136 | World::AtomComposite atoms = World::getInstance().getAllAtoms(AtomOfMolecule(newmol->getId()));
|
---|
137 | CPPUNIT_ASSERT_EQUAL( (size_t)1, atoms.size() );
|
---|
138 | CPPUNIT_ASSERT_EQUAL( (*atoms.begin())->getId(), testAtom->getId() );
|
---|
139 |
|
---|
140 | // remove molecule again
|
---|
141 | World::getInstance().destroyMolecule(newmol);
|
---|
142 | }
|
---|
143 | void AtomDescriptorTest::AtomCalcTest(){
|
---|
144 | // test some elementary set operations
|
---|
145 | {
|
---|
146 | std::vector<atom*> testAtoms = World::getInstance().getAllAtoms(AllAtoms()||NoAtoms());
|
---|
147 | CPPUNIT_ASSERT_EQUAL( true , hasAllAtoms(testAtoms,atomIds));
|
---|
148 | CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicateAtoms(testAtoms));
|
---|
149 | }
|
---|
150 |
|
---|
151 | {
|
---|
152 | std::vector<atom*> testAtoms = World::getInstance().getAllAtoms(NoAtoms()||AllAtoms());
|
---|
153 | CPPUNIT_ASSERT_EQUAL( true , hasAllAtoms(testAtoms,atomIds));
|
---|
154 | CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicateAtoms(testAtoms));
|
---|
155 | }
|
---|
156 |
|
---|
157 | {
|
---|
158 | std::vector<atom*> testAtoms = World::getInstance().getAllAtoms(NoAtoms()&&AllAtoms());
|
---|
159 | CPPUNIT_ASSERT_EQUAL( true , testAtoms.empty());
|
---|
160 | }
|
---|
161 |
|
---|
162 | {
|
---|
163 | std::vector<atom*> testAtoms = World::getInstance().getAllAtoms(AllAtoms()&&NoAtoms());
|
---|
164 | CPPUNIT_ASSERT_EQUAL( true , testAtoms.empty());
|
---|
165 | }
|
---|
166 |
|
---|
167 | {
|
---|
168 | std::vector<atom*> testAtoms = World::getInstance().getAllAtoms(!AllAtoms());
|
---|
169 | CPPUNIT_ASSERT_EQUAL( true , testAtoms.empty());
|
---|
170 | }
|
---|
171 |
|
---|
172 | {
|
---|
173 | std::vector<atom*> testAtoms = World::getInstance().getAllAtoms(!NoAtoms());
|
---|
174 | CPPUNIT_ASSERT_EQUAL( true , hasAllAtoms(testAtoms,atomIds));
|
---|
175 | CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicateAtoms(testAtoms));
|
---|
176 | }
|
---|
177 | // exclude and include some atoms
|
---|
178 | {
|
---|
179 | std::vector<atom*> testAtoms = World::getInstance().getAllAtoms(AllAtoms()&&(!AtomById(atomIds[ATOM_COUNT/2])));
|
---|
180 | std::set<atomId_t> excluded;
|
---|
181 | excluded.insert(atomIds[ATOM_COUNT/2]);
|
---|
182 | CPPUNIT_ASSERT_EQUAL( true , hasAllAtoms(testAtoms,atomIds,excluded));
|
---|
183 | CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicateAtoms(testAtoms));
|
---|
184 | CPPUNIT_ASSERT_EQUAL( (size_t)(ATOM_COUNT-1), testAtoms.size());
|
---|
185 | }
|
---|
186 |
|
---|
187 | {
|
---|
188 | std::vector<atom*> testAtoms = World::getInstance().getAllAtoms(NoAtoms()||(AtomById(atomIds[ATOM_COUNT/2])));
|
---|
189 | CPPUNIT_ASSERT_EQUAL( (size_t)1, testAtoms.size());
|
---|
190 | CPPUNIT_ASSERT_EQUAL( atomIds[ATOM_COUNT/2], testAtoms[0]->getId());
|
---|
191 | }
|
---|
192 | }
|
---|