1 | /*
|
---|
2 | * Project: MoleCuilder
|
---|
3 | * Description: creates and alters molecular systems
|
---|
4 | * Copyright (C) 2012 University of Bonn. All rights reserved.
|
---|
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/>.
|
---|
21 | */
|
---|
22 |
|
---|
23 | /*
|
---|
24 | * SubsetMapUnitTest.cpp
|
---|
25 | *
|
---|
26 | * Created on: Jul 3, 2012
|
---|
27 | * Author: heber
|
---|
28 | */
|
---|
29 |
|
---|
30 | // include config.h
|
---|
31 | #ifdef HAVE_CONFIG_H
|
---|
32 | #include <config.h>
|
---|
33 | #endif
|
---|
34 |
|
---|
35 | using namespace std;
|
---|
36 |
|
---|
37 | #include <cppunit/CompilerOutputter.h>
|
---|
38 | #include <cppunit/extensions/TestFactoryRegistry.h>
|
---|
39 | #include <cppunit/ui/text/TestRunner.h>
|
---|
40 |
|
---|
41 | #include "Fragmentation/Summation/SubsetMap.hpp"
|
---|
42 |
|
---|
43 | #include "SubsetMapUnitTest.hpp"
|
---|
44 |
|
---|
45 | #include <boost/assign.hpp>
|
---|
46 | #include <boost/foreach.hpp>
|
---|
47 |
|
---|
48 | #include "CodePatterns/Assert.hpp"
|
---|
49 | #include "CodePatterns/Log.hpp"
|
---|
50 | #include "CodePatterns/Verbose.hpp"
|
---|
51 |
|
---|
52 | #ifdef HAVE_TESTRUNNER
|
---|
53 | #include "UnitTestMain.hpp"
|
---|
54 | #endif /*HAVE_TESTRUNNER*/
|
---|
55 |
|
---|
56 | using namespace boost::assign;
|
---|
57 |
|
---|
58 | /********************************************** Test classes **************************************/
|
---|
59 |
|
---|
60 | // Registers the fixture into the 'registry'
|
---|
61 | CPPUNIT_TEST_SUITE_REGISTRATION( SubsetMapTest );
|
---|
62 |
|
---|
63 |
|
---|
64 | void SubsetMapTest::setUp()
|
---|
65 | {
|
---|
66 | // failing asserts should be thrown
|
---|
67 | ASSERT_DO(Assert::Throw);
|
---|
68 |
|
---|
69 | setVerbosity(4);
|
---|
70 |
|
---|
71 | allsets.reserve(5);
|
---|
72 | IndexSet tempset;
|
---|
73 | tempset += 1;
|
---|
74 | allsets += tempset;
|
---|
75 | tempset += 2;
|
---|
76 | allsets += tempset;
|
---|
77 | tempset.clear();
|
---|
78 | tempset += 3;
|
---|
79 | allsets += tempset;
|
---|
80 | tempset += 2;
|
---|
81 | allsets += tempset;
|
---|
82 | tempset += 4;
|
---|
83 | allsets += tempset;
|
---|
84 | tempset += 1;
|
---|
85 | allsets += tempset;
|
---|
86 | CPPUNIT_ASSERT_EQUAL( (size_t)6, allsets.size() );
|
---|
87 |
|
---|
88 | SM = NULL;
|
---|
89 | }
|
---|
90 |
|
---|
91 |
|
---|
92 | void SubsetMapTest::tearDown()
|
---|
93 | {
|
---|
94 | allsets.clear();
|
---|
95 | delete SM;
|
---|
96 | }
|
---|
97 |
|
---|
98 | /** UnitTest for getNoPowerSets()
|
---|
99 | */
|
---|
100 | void SubsetMapTest::getNoPowerSetsTest()
|
---|
101 | {
|
---|
102 | CPPUNIT_ASSERT_EQUAL( (size_t)2, SubsetMap::getNoPowerSets(1) );
|
---|
103 | CPPUNIT_ASSERT_EQUAL( (size_t)4, SubsetMap::getNoPowerSets(2) );
|
---|
104 | CPPUNIT_ASSERT_EQUAL( (size_t)8, SubsetMap::getNoPowerSets(3) );
|
---|
105 | CPPUNIT_ASSERT_EQUAL( (size_t)16, SubsetMap::getNoPowerSets(4) );
|
---|
106 | CPPUNIT_ASSERT_EQUAL( (size_t)32, SubsetMap::getNoPowerSets(5) );
|
---|
107 | CPPUNIT_ASSERT_EQUAL( (size_t)64, SubsetMap::getNoPowerSets(6) );
|
---|
108 | }
|
---|
109 |
|
---|
110 |
|
---|
111 | /** UnitTest for getPowerSetIndex()
|
---|
112 | */
|
---|
113 | void SubsetMapTest::getPowerSetIndexTest()
|
---|
114 | {
|
---|
115 | // we just check here all subsets of order 4 "manually"
|
---|
116 | IndexSet_ptr ptr(new IndexSet(allsets.back()));
|
---|
117 | IndexSet tempset;
|
---|
118 | // subset order 4
|
---|
119 | CPPUNIT_ASSERT_EQUAL( (size_t)(0), SubsetMap::getPowerSetIndex(ptr, tempset));
|
---|
120 | tempset += 1;
|
---|
121 | CPPUNIT_ASSERT_EQUAL( (size_t)(1), SubsetMap::getPowerSetIndex(ptr, tempset));
|
---|
122 | tempset += 2;
|
---|
123 | CPPUNIT_ASSERT_EQUAL( (size_t)(1 | 2), SubsetMap::getPowerSetIndex(ptr, tempset));
|
---|
124 | tempset += 3;
|
---|
125 | CPPUNIT_ASSERT_EQUAL( (size_t)(1 | 2 | 4), SubsetMap::getPowerSetIndex(ptr, tempset));
|
---|
126 | tempset += 4;
|
---|
127 | CPPUNIT_ASSERT_EQUAL( (size_t)(1 | 2 | 4 | 8), SubsetMap::getPowerSetIndex(ptr, tempset));
|
---|
128 |
|
---|
129 | // subset order 3
|
---|
130 | tempset.clear();
|
---|
131 | tempset += 2;
|
---|
132 | CPPUNIT_ASSERT_EQUAL( (size_t)(2), SubsetMap::getPowerSetIndex(ptr, tempset));
|
---|
133 | tempset += 3;
|
---|
134 | CPPUNIT_ASSERT_EQUAL( (size_t)(2 | 4), SubsetMap::getPowerSetIndex(ptr, tempset));
|
---|
135 | tempset += 4;
|
---|
136 | CPPUNIT_ASSERT_EQUAL( (size_t)(2 | 4 | 8), SubsetMap::getPowerSetIndex(ptr, tempset));
|
---|
137 |
|
---|
138 | tempset.clear();
|
---|
139 | tempset += 1;
|
---|
140 | // CPPUNIT_ASSERT_EQUAL( (size_t)(1), SubsetMap::getPowerSetIndex(ptr, tempset));
|
---|
141 | tempset += 3;
|
---|
142 | CPPUNIT_ASSERT_EQUAL( (size_t)(1 | 4), SubsetMap::getPowerSetIndex(ptr, tempset));
|
---|
143 | tempset += 4;
|
---|
144 | CPPUNIT_ASSERT_EQUAL( (size_t)(1 | 4 | 8), SubsetMap::getPowerSetIndex(ptr, tempset));
|
---|
145 |
|
---|
146 | tempset.clear();
|
---|
147 | tempset += 1;
|
---|
148 | // CPPUNIT_ASSERT_EQUAL( (size_t)(1), SubsetMap::getPowerSetIndex(ptr, tempset));
|
---|
149 | tempset += 2;
|
---|
150 | CPPUNIT_ASSERT_EQUAL( (size_t)(1 | 2), SubsetMap::getPowerSetIndex(ptr, tempset));
|
---|
151 | tempset += 4;
|
---|
152 | CPPUNIT_ASSERT_EQUAL( (size_t)(1 | 2 | 8), SubsetMap::getPowerSetIndex(ptr, tempset));
|
---|
153 |
|
---|
154 | // subset order 2
|
---|
155 | tempset.clear();
|
---|
156 | tempset += 1;
|
---|
157 | // CPPUNIT_ASSERT_EQUAL( (size_t)(1), SubsetMap::getPowerSetIndex(ptr, tempset));
|
---|
158 | tempset += 3;
|
---|
159 | CPPUNIT_ASSERT_EQUAL( (size_t)(1 | 4), SubsetMap::getPowerSetIndex(ptr, tempset));
|
---|
160 |
|
---|
161 | tempset.clear();
|
---|
162 | tempset += 1;
|
---|
163 | // CPPUNIT_ASSERT_EQUAL( (size_t)(1), SubsetMap::getPowerSetIndex(ptr, tempset));
|
---|
164 | tempset += 4;
|
---|
165 | CPPUNIT_ASSERT_EQUAL( (size_t)(1 | 8), SubsetMap::getPowerSetIndex(ptr, tempset));
|
---|
166 |
|
---|
167 | tempset.clear();
|
---|
168 | tempset += 2;
|
---|
169 | // CPPUNIT_ASSERT_EQUAL( (size_t)(2), SubsetMap::getPowerSetIndex(ptr, tempset));
|
---|
170 | tempset += 3;
|
---|
171 | CPPUNIT_ASSERT_EQUAL( (size_t)(2 | 4), SubsetMap::getPowerSetIndex(ptr, tempset));
|
---|
172 |
|
---|
173 | tempset.clear();
|
---|
174 | tempset += 2;
|
---|
175 | // CPPUNIT_ASSERT_EQUAL( (size_t)(2), SubsetMap::getPowerSetIndex(ptr, tempset));
|
---|
176 | tempset += 4;
|
---|
177 | CPPUNIT_ASSERT_EQUAL( (size_t)(2 | 8), SubsetMap::getPowerSetIndex(ptr, tempset));
|
---|
178 |
|
---|
179 | tempset.clear();
|
---|
180 | tempset += 3;
|
---|
181 | CPPUNIT_ASSERT_EQUAL( (size_t)(4), SubsetMap::getPowerSetIndex(ptr, tempset));
|
---|
182 | tempset += 4;
|
---|
183 | CPPUNIT_ASSERT_EQUAL( (size_t)(4 | 8), SubsetMap::getPowerSetIndex(ptr, tempset));
|
---|
184 |
|
---|
185 | // subset order 1
|
---|
186 | tempset.clear();
|
---|
187 | tempset += 4;
|
---|
188 | CPPUNIT_ASSERT_EQUAL( (size_t)(8), SubsetMap::getPowerSetIndex(ptr, tempset));
|
---|
189 | }
|
---|
190 |
|
---|
191 | /** UnitTest for getSubset()
|
---|
192 | */
|
---|
193 | void SubsetMapTest::getSubsetTest()
|
---|
194 | {
|
---|
195 | // we just check here all subsets of order 4 "manually"
|
---|
196 | IndexSet_ptr ptr(new IndexSet(allsets.back()));
|
---|
197 | IndexSet tempset;
|
---|
198 | // subset order 4
|
---|
199 | CPPUNIT_ASSERT_EQUAL( tempset, SubsetMap::getSubset(*ptr, (size_t)(0) ));
|
---|
200 | tempset += 1;
|
---|
201 | CPPUNIT_ASSERT_EQUAL( tempset, SubsetMap::getSubset(*ptr, (size_t)(1) ));
|
---|
202 | tempset += 2;
|
---|
203 | CPPUNIT_ASSERT_EQUAL( tempset, SubsetMap::getSubset(*ptr, (size_t)(1 | 2) ));
|
---|
204 | tempset += 3;
|
---|
205 | CPPUNIT_ASSERT_EQUAL( tempset, SubsetMap::getSubset(*ptr, (size_t)(1 | 2 | 4) ));
|
---|
206 | tempset += 4;
|
---|
207 | CPPUNIT_ASSERT_EQUAL( tempset, SubsetMap::getSubset(*ptr, (size_t)(1 | 2 | 4 | 8) ));
|
---|
208 |
|
---|
209 | // subset order 3
|
---|
210 | tempset.clear();
|
---|
211 | tempset += 2;
|
---|
212 | CPPUNIT_ASSERT_EQUAL( tempset, SubsetMap::getSubset(*ptr, (size_t)(2) ));
|
---|
213 | tempset += 3;
|
---|
214 | CPPUNIT_ASSERT_EQUAL( tempset, SubsetMap::getSubset(*ptr, (size_t)(2 | 4) ));
|
---|
215 | tempset += 4;
|
---|
216 | CPPUNIT_ASSERT_EQUAL( tempset, SubsetMap::getSubset(*ptr, (size_t)(2 | 4 | 8) ));
|
---|
217 |
|
---|
218 | tempset.clear();
|
---|
219 | tempset += 1;
|
---|
220 | // CPPUNIT_ASSERT_EQUAL( tempset, SubsetMap::getSubset(*ptr, (size_t)(1) ));
|
---|
221 | tempset += 3;
|
---|
222 | CPPUNIT_ASSERT_EQUAL( tempset, SubsetMap::getSubset(*ptr, (size_t)(1 | 4) ));
|
---|
223 | tempset += 4;
|
---|
224 | CPPUNIT_ASSERT_EQUAL( tempset, SubsetMap::getSubset(*ptr, (size_t)(1 | 4 | 8) ));
|
---|
225 |
|
---|
226 | tempset.clear();
|
---|
227 | tempset += 1;
|
---|
228 | // CPPUNIT_ASSERT_EQUAL( tempset, SubsetMap::getSubset(*ptr, (size_t)(1) ));
|
---|
229 | tempset += 2;
|
---|
230 | // CPPUNIT_ASSERT_EQUAL( tempset, SubsetMap::getSubset(*ptr, (size_t)(1 | 2) ));
|
---|
231 | tempset += 4;
|
---|
232 | CPPUNIT_ASSERT_EQUAL( tempset, SubsetMap::getSubset(*ptr, (size_t)(1 | 2 | 8) ));
|
---|
233 |
|
---|
234 | // subset order 2
|
---|
235 | tempset.clear();
|
---|
236 | tempset += 1;
|
---|
237 | // CPPUNIT_ASSERT_EQUAL( tempset, SubsetMap::getSubset(*ptr, (size_t)(1) ));
|
---|
238 | tempset += 3;
|
---|
239 | // CPPUNIT_ASSERT_EQUAL( tempset, SubsetMap::getSubset(*ptr, (size_t)(1 | 4) ));
|
---|
240 |
|
---|
241 | tempset.clear();
|
---|
242 | tempset += 1;
|
---|
243 | // CPPUNIT_ASSERT_EQUAL( tempset, SubsetMap::getSubset(*ptr, (size_t)(1) ));
|
---|
244 | tempset += 4;
|
---|
245 | CPPUNIT_ASSERT_EQUAL( tempset, SubsetMap::getSubset(*ptr, (size_t)(1 | 8) ));
|
---|
246 |
|
---|
247 | tempset.clear();
|
---|
248 | tempset += 2;
|
---|
249 | // CPPUNIT_ASSERT_EQUAL( tempset, SubsetMap::getSubset(*ptr, (size_t)(2) ));
|
---|
250 | tempset += 3;
|
---|
251 | // CPPUNIT_ASSERT_EQUAL( tempset, SubsetMap::getSubset(*ptr, (size_t)(2 | 4) ));
|
---|
252 |
|
---|
253 | tempset.clear();
|
---|
254 | tempset += 2;
|
---|
255 | // CPPUNIT_ASSERT_EQUAL( tempset, SubsetMap::getSubset(*ptr, (size_t)(2) ));
|
---|
256 | tempset += 4;
|
---|
257 | CPPUNIT_ASSERT_EQUAL( tempset, SubsetMap::getSubset(*ptr, (size_t)(2 | 8) ));
|
---|
258 |
|
---|
259 | tempset.clear();
|
---|
260 | tempset += 3;
|
---|
261 | CPPUNIT_ASSERT_EQUAL( tempset, SubsetMap::getSubset(*ptr, (size_t)(4) ));
|
---|
262 | tempset += 4;
|
---|
263 | CPPUNIT_ASSERT_EQUAL( tempset, SubsetMap::getSubset(*ptr, (size_t)(4 | 8) ));
|
---|
264 |
|
---|
265 | // subset order 1
|
---|
266 | tempset.clear();
|
---|
267 | tempset += 4;
|
---|
268 | CPPUNIT_ASSERT_EQUAL( tempset, SubsetMap::getSubset(*ptr, (size_t)(8) ));
|
---|
269 | }
|
---|
270 |
|
---|
271 | /** UnitTest for gatherSubset()
|
---|
272 | */
|
---|
273 | void SubsetMapTest::gatherSubsetTest()
|
---|
274 | {
|
---|
275 | /// we create a SubsetMap from an empty container
|
---|
276 | IndexSet tempset;
|
---|
277 | std::vector<IndexSet> sets;
|
---|
278 | sets.push_back(tempset);
|
---|
279 | tempset += 1;
|
---|
280 | sets.push_back(tempset);
|
---|
281 | tempset += 2;
|
---|
282 | sets.push_back(tempset);
|
---|
283 | tempset.clear();
|
---|
284 | tempset += 2;
|
---|
285 | sets.push_back(tempset);
|
---|
286 | IndexSetContainer container( sets );
|
---|
287 | // now we have {}, {1}, {2}, {1,2} in container
|
---|
288 |
|
---|
289 | SM = new SubsetMap(container);
|
---|
290 | CPPUNIT_ASSERT( SM != NULL );
|
---|
291 | CPPUNIT_ASSERT_EQUAL( (size_t)4, SM->Lookup.size() );
|
---|
292 | const IndexSetContainer::Container_t &_container = container.getContainer();
|
---|
293 | for (IndexSetContainer::Container_t::const_iterator iter = _container.begin();
|
---|
294 | iter != _container.end(); ++iter) {
|
---|
295 | LOG(1, "INFO: Current set is " << **iter << ".");
|
---|
296 | SubsetMap::Lookup_t::const_iterator lookupiter = SM->Lookup.find(*iter);
|
---|
297 | CPPUNIT_ASSERT( lookupiter != SM->Lookup.end() );
|
---|
298 | CPPUNIT_ASSERT_EQUAL( lookupiter->second->getContainer().size(), SM->getSubsets(*iter)->getContainer().size() );
|
---|
299 | CPPUNIT_ASSERT_EQUAL( (size_t)(( 1 << (*iter)->size()) - 1), lookupiter->second->getContainer().size() );
|
---|
300 | }
|
---|
301 | }
|
---|
302 |
|
---|
303 | /** UnitTest for getMaximumSetLevel()
|
---|
304 | */
|
---|
305 | void SubsetMapTest::getMaximumSetLevelTest()
|
---|
306 | {
|
---|
307 | // create small container
|
---|
308 | {
|
---|
309 | IndexSet tempset;
|
---|
310 | std::vector<IndexSet> sets;
|
---|
311 | sets.push_back(tempset);
|
---|
312 | tempset += 1;
|
---|
313 | sets.push_back(tempset);
|
---|
314 | tempset += 2;
|
---|
315 | sets.push_back(tempset);
|
---|
316 | tempset.clear();
|
---|
317 | tempset += 2;
|
---|
318 | sets.push_back(tempset);
|
---|
319 | IndexSetContainer container( sets );
|
---|
320 | // now we have {}, {1}, {2}, {1,2} in container
|
---|
321 |
|
---|
322 | SM = new SubsetMap(container);
|
---|
323 | CPPUNIT_ASSERT( SM != NULL );
|
---|
324 |
|
---|
325 | CPPUNIT_ASSERT_EQUAL( (size_t)2, SM->getMaximumSetLevel() );
|
---|
326 | // we are strictly the last set
|
---|
327 | CPPUNIT_ASSERT( SM->getMaximumSetLevel() == (--SM->Lookup.end())->first->size() );
|
---|
328 | }
|
---|
329 |
|
---|
330 | // create larger container
|
---|
331 | {
|
---|
332 | IndexSetContainer container( allsets );
|
---|
333 | delete SM;
|
---|
334 | SM = new SubsetMap(container);
|
---|
335 | CPPUNIT_ASSERT( SM != NULL );
|
---|
336 |
|
---|
337 | CPPUNIT_ASSERT_EQUAL( (size_t)4, SM->getMaximumSetLevel() );
|
---|
338 | CPPUNIT_ASSERT( SM->getMaximumSetLevel() == (--SM->Lookup.end())->first->size() );
|
---|
339 | }
|
---|
340 | }
|
---|