1 | /*
|
---|
2 | * Project: MoleCuilder
|
---|
3 | * Description: creates and alters molecular systems
|
---|
4 | * Copyright (C) 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 | * FragmentQueueUnitTest.cpp
|
---|
10 | *
|
---|
11 | * Created on: Oct 23, 2011
|
---|
12 | * Author: heber
|
---|
13 | */
|
---|
14 |
|
---|
15 | // include config.h
|
---|
16 | #ifdef HAVE_CONFIG_H
|
---|
17 | #include <config.h>
|
---|
18 | #endif
|
---|
19 |
|
---|
20 | #include <cppunit/CompilerOutputter.h>
|
---|
21 | #include <cppunit/extensions/TestFactoryRegistry.h>
|
---|
22 | #include <cppunit/ui/text/TestRunner.h>
|
---|
23 |
|
---|
24 | #include "FragmentQueueUnitTest.hpp"
|
---|
25 |
|
---|
26 | #include <vector>
|
---|
27 |
|
---|
28 | #include "CodePatterns/Assert.hpp"
|
---|
29 | #include "FragmentQueue.hpp"
|
---|
30 |
|
---|
31 |
|
---|
32 | #ifdef HAVE_TESTRUNNER
|
---|
33 | #include "UnitTestMain.hpp"
|
---|
34 | #endif /*HAVE_TESTRUNNER*/
|
---|
35 |
|
---|
36 | /********************************************** Test classes **************************************/
|
---|
37 |
|
---|
38 | // Registers the fixture into the 'registry'
|
---|
39 | CPPUNIT_TEST_SUITE_REGISTRATION( FragmentQueueTest );
|
---|
40 |
|
---|
41 |
|
---|
42 | void FragmentQueueTest::setUp()
|
---|
43 | {
|
---|
44 | // Throw assertions
|
---|
45 | ASSERT_DO(Assert::Throw);
|
---|
46 |
|
---|
47 | queue = new FragmentQueue();
|
---|
48 | }
|
---|
49 |
|
---|
50 |
|
---|
51 | void FragmentQueueTest::tearDown()
|
---|
52 | {
|
---|
53 | delete queue;
|
---|
54 | }
|
---|
55 |
|
---|
56 | /** UnitTest for working JobQueue
|
---|
57 | */
|
---|
58 | void FragmentQueueTest::JobTest()
|
---|
59 | {
|
---|
60 | FragmentJob::ptr testJob(new FragmentJob);
|
---|
61 | /// check for illegal id
|
---|
62 | #ifndef NDEBUG
|
---|
63 | std::cout << "The following assertion is intended and does not indicate a failure." << std::endl;
|
---|
64 | CPPUNIT_ASSERT_THROW( queue->pushJob(testJob), Assert::AssertionFailure );
|
---|
65 | #endif
|
---|
66 | // set to valid id
|
---|
67 | testJob->setId(1);
|
---|
68 | testJob->outputfile = std::string("do something");
|
---|
69 |
|
---|
70 | CPPUNIT_ASSERT_EQUAL(false, queue->isJobPresent() );
|
---|
71 |
|
---|
72 | #ifndef NDEBUG
|
---|
73 | CPPUNIT_ASSERT_NO_THROW( queue->pushJob(testJob) );
|
---|
74 | #else
|
---|
75 | queue->pushJob(testJob);
|
---|
76 | #endif
|
---|
77 |
|
---|
78 | CPPUNIT_ASSERT_EQUAL((size_t)1, queue->jobs.size());
|
---|
79 | CPPUNIT_ASSERT_EQUAL(true, queue->isJobPresent() );
|
---|
80 | CPPUNIT_ASSERT_EQUAL((size_t)1, queue->results.size());
|
---|
81 | {
|
---|
82 | FragmentQueue::ResultMap::const_iterator iter = queue->results.find((JobId_t)1);
|
---|
83 | CPPUNIT_ASSERT( iter != queue->results.end() );
|
---|
84 | CPPUNIT_ASSERT( FragmentQueue::NoResult == iter->second );
|
---|
85 | }
|
---|
86 |
|
---|
87 | // push same id again
|
---|
88 | #ifndef NDEBUG
|
---|
89 | std::cout << "The following assertion is intended and does not indicate a failure." << std::endl;
|
---|
90 | CPPUNIT_ASSERT_THROW( queue->pushJob(testJob), Assert::AssertionFailure );
|
---|
91 | #endif
|
---|
92 |
|
---|
93 | CPPUNIT_ASSERT_EQUAL((size_t)1, queue->jobs.size());
|
---|
94 | CPPUNIT_ASSERT_EQUAL((size_t)1, queue->results.size());
|
---|
95 |
|
---|
96 | FragmentJob::ptr poppedJob;
|
---|
97 | #ifndef NDEBUG
|
---|
98 | CPPUNIT_ASSERT_NO_THROW( poppedJob = queue->popJob() );
|
---|
99 | #else
|
---|
100 | poppedJob = queue->popJob();
|
---|
101 | #endif
|
---|
102 | CPPUNIT_ASSERT( poppedJob == testJob );
|
---|
103 |
|
---|
104 | CPPUNIT_ASSERT_EQUAL((size_t)0, queue->jobs.size());
|
---|
105 | CPPUNIT_ASSERT_EQUAL(false, queue->isJobPresent() );
|
---|
106 | CPPUNIT_ASSERT_EQUAL((size_t)1, queue->results.size());
|
---|
107 | {
|
---|
108 | FragmentQueue::ResultMap::const_iterator iter = queue->results.find((JobId_t)1);
|
---|
109 | CPPUNIT_ASSERT( iter != queue->results.end() );
|
---|
110 | CPPUNIT_ASSERT( FragmentQueue::NoResultQueued == iter->second );
|
---|
111 | }
|
---|
112 |
|
---|
113 | #ifndef NDEBUG
|
---|
114 | std::cout << "The following assertion is intended and does not indicate a failure." << std::endl;
|
---|
115 | CPPUNIT_ASSERT_THROW( queue->popJob(), Assert::AssertionFailure );
|
---|
116 | #endif
|
---|
117 | }
|
---|
118 |
|
---|
119 | /** UnitTest for adding multiple jobs at a time.
|
---|
120 | */
|
---|
121 | void FragmentQueueTest::JobsTest()
|
---|
122 | {
|
---|
123 | // prepare some jobs
|
---|
124 | FragmentJob::ptr testJob(new FragmentJob);
|
---|
125 | testJob->setId((JobId_t)1);
|
---|
126 | testJob->outputfile = std::string("do something");
|
---|
127 | FragmentJob::ptr anothertestJob(new FragmentJob);
|
---|
128 | anothertestJob->setId((JobId_t)2);
|
---|
129 | anothertestJob->outputfile = std::string("do something else");
|
---|
130 |
|
---|
131 | // prepare a vector of them
|
---|
132 | std::vector<FragmentJob::ptr> testjobs;
|
---|
133 | testjobs.push_back(testJob);
|
---|
134 | testjobs.push_back(anothertestJob);
|
---|
135 |
|
---|
136 | // prepare another vector of them
|
---|
137 | std::vector<FragmentJob::ptr> sametestjobs;
|
---|
138 | sametestjobs.push_back(testJob);
|
---|
139 | sametestjobs.push_back(anothertestJob);
|
---|
140 |
|
---|
141 | // push the vector
|
---|
142 | CPPUNIT_ASSERT_EQUAL( (size_t)0, queue->jobs.size() );
|
---|
143 | #ifndef NDEBUG
|
---|
144 | CPPUNIT_ASSERT_NO_THROW( queue->pushJobs(testjobs) );
|
---|
145 | #else
|
---|
146 | queue->pushJobs(testjobs);
|
---|
147 | #endif
|
---|
148 | CPPUNIT_ASSERT_EQUAL( (size_t)2, queue->jobs.size() );
|
---|
149 | CPPUNIT_ASSERT_EQUAL((size_t)2, queue->results.size());
|
---|
150 | {
|
---|
151 | FragmentQueue::ResultMap::const_iterator iter = queue->results.find((JobId_t)1);
|
---|
152 | CPPUNIT_ASSERT( iter != queue->results.end() );
|
---|
153 | CPPUNIT_ASSERT( FragmentQueue::NoResult == iter->second );
|
---|
154 | }
|
---|
155 | {
|
---|
156 | FragmentQueue::ResultMap::const_iterator iter = queue->results.find((JobId_t)2);
|
---|
157 | CPPUNIT_ASSERT( iter != queue->results.end() );
|
---|
158 | CPPUNIT_ASSERT( FragmentQueue::NoResult == iter->second );
|
---|
159 | }
|
---|
160 | // push again
|
---|
161 | #ifndef NDEBUG
|
---|
162 | std::cout << "The following assertion is intended and does not indicate a failure." << std::endl;
|
---|
163 | CPPUNIT_ASSERT_THROW( queue->pushJobs(testjobs), Assert::AssertionFailure );
|
---|
164 | #endif
|
---|
165 |
|
---|
166 | CPPUNIT_ASSERT_EQUAL( (size_t)2, queue->jobs.size() );
|
---|
167 | CPPUNIT_ASSERT_EQUAL((size_t)2, queue->results.size());
|
---|
168 | }
|
---|
169 |
|
---|
170 | /** UnitTest for working ResultMap
|
---|
171 | */
|
---|
172 | void FragmentQueueTest::ResultsTest()
|
---|
173 | {
|
---|
174 | // prepare a job
|
---|
175 | FragmentJob::ptr testJob(new FragmentJob);
|
---|
176 | testJob->setId(1);
|
---|
177 | testJob->outputfile = std::string("do something");
|
---|
178 | #ifndef NDEBUG
|
---|
179 | CPPUNIT_ASSERT_NO_THROW( queue->pushJob(testJob) );
|
---|
180 | #else
|
---|
181 | queue->pushJob(testJob);
|
---|
182 | #endif
|
---|
183 | #ifndef NDEBUG
|
---|
184 | CPPUNIT_ASSERT_NO_THROW( queue->popJob() );
|
---|
185 | #else
|
---|
186 | queue->popJob();
|
---|
187 | #endif
|
---|
188 |
|
---|
189 |
|
---|
190 | // prepare a result
|
---|
191 | FragmentResult::ptr testResult( new FragmentResult(1) );
|
---|
192 | FragmentResult::ptr wrongIdResult( new FragmentResult(2) );
|
---|
193 |
|
---|
194 | // check that none are present and we can't get result yet
|
---|
195 | CPPUNIT_ASSERT_EQUAL( (size_t)0, queue->getDoneJobs() );
|
---|
196 | CPPUNIT_ASSERT( !queue->isResultPresent(1) );
|
---|
197 | CPPUNIT_ASSERT( !queue->isResultPresent(2) );
|
---|
198 | #ifndef NDEBUG
|
---|
199 | std::cout << "The following assertion is intended and does not indicate a failure." << std::endl;
|
---|
200 | CPPUNIT_ASSERT_THROW( queue->getResult(1), Assert::AssertionFailure );
|
---|
201 | #endif
|
---|
202 |
|
---|
203 | /// check for admonishing wrong id
|
---|
204 | #ifndef NDEBUG
|
---|
205 | std::cout << "The following assertion is intended and does not indicate a failure." << std::endl;
|
---|
206 | CPPUNIT_ASSERT_THROW( queue->pushResult(wrongIdResult), Assert::AssertionFailure );
|
---|
207 | #endif
|
---|
208 |
|
---|
209 | // push correct result
|
---|
210 | #ifndef NDEBUG
|
---|
211 | CPPUNIT_ASSERT_NO_THROW( queue->pushResult(testResult) );
|
---|
212 | #else
|
---|
213 | queue->pushResult(testResult);
|
---|
214 | #endif
|
---|
215 |
|
---|
216 | // check presence again
|
---|
217 | CPPUNIT_ASSERT( queue->isResultPresent(1) );
|
---|
218 | CPPUNIT_ASSERT_EQUAL( (size_t)1, queue->getDoneJobs() );
|
---|
219 |
|
---|
220 | // obtain result again
|
---|
221 | #ifndef NDEBUG
|
---|
222 | CPPUNIT_ASSERT_NO_THROW( queue->getResult(1) );
|
---|
223 | #else
|
---|
224 | queue->getResult(1);
|
---|
225 | #endif
|
---|
226 |
|
---|
227 | // check presence one more time
|
---|
228 | CPPUNIT_ASSERT_EQUAL( (size_t)0, queue->getDoneJobs() );
|
---|
229 | CPPUNIT_ASSERT( !queue->isResultPresent(1) );
|
---|
230 | CPPUNIT_ASSERT( !queue->isResultPresent(2) );
|
---|
231 | #ifndef NDEBUG
|
---|
232 | std::cout << "The following assertion is intended and does not indicate a failure." << std::endl;
|
---|
233 | CPPUNIT_ASSERT_THROW( queue->getResult(1), Assert::AssertionFailure );
|
---|
234 | #endif
|
---|
235 | }
|
---|
236 |
|
---|
237 | /** UnitTest for working ResultMap
|
---|
238 | */
|
---|
239 | void FragmentQueueTest::AllResultsTest()
|
---|
240 | {
|
---|
241 | // prepare a job
|
---|
242 | FragmentJob::ptr testJob( new FragmentJob );
|
---|
243 | testJob->setId(1);
|
---|
244 | testJob->outputfile = std::string("do something");
|
---|
245 | FragmentJob::ptr anothertestJob( new FragmentJob );
|
---|
246 | anothertestJob->setId(2);
|
---|
247 | anothertestJob->outputfile = std::string("do something else");
|
---|
248 |
|
---|
249 | #ifndef NDEBUG
|
---|
250 | CPPUNIT_ASSERT_NO_THROW( queue->pushJob(testJob) );
|
---|
251 | CPPUNIT_ASSERT_NO_THROW( queue->pushJob(anothertestJob) );
|
---|
252 | #else
|
---|
253 | queue->pushJob(testJob);
|
---|
254 | queue->pushJob(anothertestJob);
|
---|
255 | #endif
|
---|
256 |
|
---|
257 | // check that no results are returned.
|
---|
258 | {
|
---|
259 | const std::vector<FragmentResult::ptr> results = queue->getAllResults();
|
---|
260 | CPPUNIT_ASSERT_EQUAL( (size_t)0, results.size() );
|
---|
261 | }
|
---|
262 |
|
---|
263 | // pop both as if some work was being done
|
---|
264 | #ifndef NDEBUG
|
---|
265 | CPPUNIT_ASSERT_NO_THROW( queue->popJob() );
|
---|
266 | CPPUNIT_ASSERT_NO_THROW( queue->popJob() );
|
---|
267 | #else
|
---|
268 | queue->popJob();
|
---|
269 | queue->popJob();
|
---|
270 | #endif
|
---|
271 |
|
---|
272 | // prepare a result
|
---|
273 | FragmentResult::ptr testResult( new FragmentResult(1) );
|
---|
274 | FragmentResult::ptr anothertestResult( new FragmentResult(2) );
|
---|
275 |
|
---|
276 | // push correct result
|
---|
277 | #ifndef NDEBUG
|
---|
278 | CPPUNIT_ASSERT_NO_THROW( queue->pushResult(testResult) );
|
---|
279 | CPPUNIT_ASSERT_NO_THROW( queue->pushResult(anothertestResult) );
|
---|
280 | #else
|
---|
281 | queue->pushResult(testResult);
|
---|
282 | queue->pushResult(anothertestResult);
|
---|
283 | #endif
|
---|
284 |
|
---|
285 | // check that two results are returned.
|
---|
286 | {
|
---|
287 | const std::vector<FragmentResult::ptr> results = queue->getAllResults();
|
---|
288 | CPPUNIT_ASSERT_EQUAL( (size_t)2, results.size() );
|
---|
289 | }
|
---|
290 | }
|
---|
291 |
|
---|