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