| [b5ebb5] | 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 |
|
|---|
| [9875cc] | 26 | #include <vector>
|
|---|
| 27 |
|
|---|
| [02f346] | 28 | #include "CodePatterns/Assert.hpp"
|
|---|
| [9875cc] | 29 | #include "FragmentQueue.hpp"
|
|---|
| 30 |
|
|---|
| [02f346] | 31 |
|
|---|
| [b5ebb5] | 32 | #ifdef HAVE_TESTRUNNER
|
|---|
| 33 | #include "UnitTestMain.hpp"
|
|---|
| 34 | #endif /*HAVE_TESTRUNNER*/
|
|---|
| 35 |
|
|---|
| 36 | /********************************************** Test classes **************************************/
|
|---|
| 37 |
|
|---|
| [d920b9] | 38 | #include "stubs/FragmentJobStub.hpp"
|
|---|
| 39 |
|
|---|
| [b5ebb5] | 40 | // Registers the fixture into the 'registry'
|
|---|
| 41 | CPPUNIT_TEST_SUITE_REGISTRATION( FragmentQueueTest );
|
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 | void FragmentQueueTest::setUp()
|
|---|
| 45 | {
|
|---|
| [02f346] | 46 | // Throw assertions
|
|---|
| 47 | ASSERT_DO(Assert::Throw);
|
|---|
| 48 |
|
|---|
| [b5ebb5] | 49 | queue = new FragmentQueue();
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 | void FragmentQueueTest::tearDown()
|
|---|
| 54 | {
|
|---|
| 55 | delete queue;
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | /** UnitTest for working JobQueue
|
|---|
| 59 | */
|
|---|
| [9875cc] | 60 | void FragmentQueueTest::JobTest()
|
|---|
| [b5ebb5] | 61 | {
|
|---|
| [d920b9] | 62 | FragmentJob::ptr testJob(new FragmentJobStub(JobId::IllegalJob));
|
|---|
| [02f346] | 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
|
|---|
| [78ad7d] | 69 | testJob->setId(1);
|
|---|
| [02f346] | 70 |
|
|---|
| [12d15a] | 71 | CPPUNIT_ASSERT_EQUAL(false, queue->isJobPresent() );
|
|---|
| 72 |
|
|---|
| [02f346] | 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());
|
|---|
| [12d15a] | 80 | CPPUNIT_ASSERT_EQUAL(true, queue->isJobPresent() );
|
|---|
| 81 | CPPUNIT_ASSERT_EQUAL((size_t)1, queue->results.size());
|
|---|
| [9875cc] | 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 | }
|
|---|
| [02f346] | 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());
|
|---|
| [12d15a] | 95 | CPPUNIT_ASSERT_EQUAL((size_t)1, queue->results.size());
|
|---|
| 96 |
|
|---|
| [78ad7d] | 97 | FragmentJob::ptr poppedJob;
|
|---|
| [12d15a] | 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());
|
|---|
| [9875cc] | 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 | }
|
|---|
| [12d15a] | 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
|
|---|
| [b5ebb5] | 118 | }
|
|---|
| 119 |
|
|---|
| [9875cc] | 120 | /** UnitTest for adding multiple jobs at a time.
|
|---|
| 121 | */
|
|---|
| 122 | void FragmentQueueTest::JobsTest()
|
|---|
| 123 | {
|
|---|
| 124 | // prepare some jobs
|
|---|
| [d920b9] | 125 | FragmentJob::ptr testJob(new FragmentJobStub(JobId::IllegalJob));
|
|---|
| [78ad7d] | 126 | testJob->setId((JobId_t)1);
|
|---|
| [d920b9] | 127 | FragmentJob::ptr anothertestJob(new FragmentJobStub(JobId::IllegalJob));
|
|---|
| [78ad7d] | 128 | anothertestJob->setId((JobId_t)2);
|
|---|
| [9875cc] | 129 |
|
|---|
| 130 | // prepare a vector of them
|
|---|
| [78ad7d] | 131 | std::vector<FragmentJob::ptr> testjobs;
|
|---|
| [9875cc] | 132 | testjobs.push_back(testJob);
|
|---|
| 133 | testjobs.push_back(anothertestJob);
|
|---|
| 134 |
|
|---|
| 135 | // prepare another vector of them
|
|---|
| [78ad7d] | 136 | std::vector<FragmentJob::ptr> sametestjobs;
|
|---|
| [9875cc] | 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 |
|
|---|
| [b5ebb5] | 169 | /** UnitTest for working ResultMap
|
|---|
| 170 | */
|
|---|
| 171 | void FragmentQueueTest::ResultsTest()
|
|---|
| 172 | {
|
|---|
| [02f346] | 173 | // prepare a job
|
|---|
| [d920b9] | 174 | FragmentJob::ptr testJob(new FragmentJobStub(JobId::IllegalJob));
|
|---|
| [78ad7d] | 175 | testJob->setId(1);
|
|---|
| [12d15a] | 176 | #ifndef NDEBUG
|
|---|
| 177 | CPPUNIT_ASSERT_NO_THROW( queue->pushJob(testJob) );
|
|---|
| 178 | #else
|
|---|
| [02f346] | 179 | queue->pushJob(testJob);
|
|---|
| [12d15a] | 180 | #endif
|
|---|
| [bf56f6] | 181 |
|
|---|
| 182 | // check for present job to do
|
|---|
| 183 | CPPUNIT_ASSERT_EQUAL( (size_t)1, queue->getPresentJobs() );
|
|---|
| 184 |
|
|---|
| [12d15a] | 185 | #ifndef NDEBUG
|
|---|
| 186 | CPPUNIT_ASSERT_NO_THROW( queue->popJob() );
|
|---|
| 187 | #else
|
|---|
| 188 | queue->popJob();
|
|---|
| 189 | #endif
|
|---|
| 190 |
|
|---|
| [bf56f6] | 191 | // check for present job to do
|
|---|
| 192 | CPPUNIT_ASSERT_EQUAL( (size_t)0, queue->getPresentJobs() );
|
|---|
| [02f346] | 193 |
|
|---|
| 194 | // prepare a result
|
|---|
| [35f587] | 195 | FragmentResult::ptr testResult( new FragmentResult(1) );
|
|---|
| 196 | FragmentResult::ptr wrongIdResult( new FragmentResult(2) );
|
|---|
| [02f346] | 197 |
|
|---|
| [b0b64c] | 198 | // check that none are present and we can't get result yet
|
|---|
| [8ee5ac] | 199 | CPPUNIT_ASSERT_EQUAL( (size_t)0, queue->getDoneJobs() );
|
|---|
| [02f346] | 200 | CPPUNIT_ASSERT( !queue->isResultPresent(1) );
|
|---|
| 201 | CPPUNIT_ASSERT( !queue->isResultPresent(2) );
|
|---|
| 202 | #ifndef NDEBUG
|
|---|
| 203 | std::cout << "The following assertion is intended and does not indicate a failure." << std::endl;
|
|---|
| 204 | CPPUNIT_ASSERT_THROW( queue->getResult(1), Assert::AssertionFailure );
|
|---|
| 205 | #endif
|
|---|
| 206 |
|
|---|
| [b0b64c] | 207 | /// check for admonishing wrong id
|
|---|
| [02f346] | 208 | #ifndef NDEBUG
|
|---|
| 209 | std::cout << "The following assertion is intended and does not indicate a failure." << std::endl;
|
|---|
| 210 | CPPUNIT_ASSERT_THROW( queue->pushResult(wrongIdResult), Assert::AssertionFailure );
|
|---|
| 211 | #endif
|
|---|
| [b0b64c] | 212 |
|
|---|
| 213 | // push correct result
|
|---|
| [02f346] | 214 | #ifndef NDEBUG
|
|---|
| 215 | CPPUNIT_ASSERT_NO_THROW( queue->pushResult(testResult) );
|
|---|
| 216 | #else
|
|---|
| 217 | queue->pushResult(testResult);
|
|---|
| 218 | #endif
|
|---|
| 219 |
|
|---|
| 220 | // check presence again
|
|---|
| 221 | CPPUNIT_ASSERT( queue->isResultPresent(1) );
|
|---|
| [8ee5ac] | 222 | CPPUNIT_ASSERT_EQUAL( (size_t)1, queue->getDoneJobs() );
|
|---|
| [bf56f6] | 223 | CPPUNIT_ASSERT_EQUAL( (size_t)0, queue->getPresentJobs() );
|
|---|
| [02f346] | 224 |
|
|---|
| [b0b64c] | 225 | // obtain result again
|
|---|
| [02f346] | 226 | #ifndef NDEBUG
|
|---|
| 227 | CPPUNIT_ASSERT_NO_THROW( queue->getResult(1) );
|
|---|
| 228 | #else
|
|---|
| 229 | queue->getResult(1);
|
|---|
| 230 | #endif
|
|---|
| 231 |
|
|---|
| 232 | // check presence one more time
|
|---|
| [8ee5ac] | 233 | CPPUNIT_ASSERT_EQUAL( (size_t)0, queue->getDoneJobs() );
|
|---|
| [bf56f6] | 234 | CPPUNIT_ASSERT_EQUAL( (size_t)0, queue->getPresentJobs() );
|
|---|
| [02f346] | 235 | CPPUNIT_ASSERT( !queue->isResultPresent(1) );
|
|---|
| 236 | CPPUNIT_ASSERT( !queue->isResultPresent(2) );
|
|---|
| 237 | #ifndef NDEBUG
|
|---|
| 238 | std::cout << "The following assertion is intended and does not indicate a failure." << std::endl;
|
|---|
| 239 | CPPUNIT_ASSERT_THROW( queue->getResult(1), Assert::AssertionFailure );
|
|---|
| 240 | #endif
|
|---|
| [b5ebb5] | 241 | }
|
|---|
| [b9c486] | 242 |
|
|---|
| 243 | /** UnitTest for working ResultMap
|
|---|
| 244 | */
|
|---|
| 245 | void FragmentQueueTest::AllResultsTest()
|
|---|
| 246 | {
|
|---|
| 247 | // prepare a job
|
|---|
| [d920b9] | 248 | FragmentJob::ptr testJob( new FragmentJobStub(JobId::IllegalJob) );
|
|---|
| 249 | testJob->setId((JobId_t)1);
|
|---|
| 250 | FragmentJob::ptr anothertestJob( new FragmentJobStub(JobId::IllegalJob) );
|
|---|
| 251 | anothertestJob->setId((JobId_t)2);
|
|---|
| [b9c486] | 252 |
|
|---|
| 253 | #ifndef NDEBUG
|
|---|
| 254 | CPPUNIT_ASSERT_NO_THROW( queue->pushJob(testJob) );
|
|---|
| 255 | CPPUNIT_ASSERT_NO_THROW( queue->pushJob(anothertestJob) );
|
|---|
| 256 | #else
|
|---|
| 257 | queue->pushJob(testJob);
|
|---|
| 258 | queue->pushJob(anothertestJob);
|
|---|
| 259 | #endif
|
|---|
| 260 |
|
|---|
| 261 | // check that no results are returned.
|
|---|
| 262 | {
|
|---|
| [35f587] | 263 | const std::vector<FragmentResult::ptr> results = queue->getAllResults();
|
|---|
| [b9c486] | 264 | CPPUNIT_ASSERT_EQUAL( (size_t)0, results.size() );
|
|---|
| 265 | }
|
|---|
| 266 |
|
|---|
| 267 | // pop both as if some work was being done
|
|---|
| 268 | #ifndef NDEBUG
|
|---|
| 269 | CPPUNIT_ASSERT_NO_THROW( queue->popJob() );
|
|---|
| 270 | CPPUNIT_ASSERT_NO_THROW( queue->popJob() );
|
|---|
| 271 | #else
|
|---|
| 272 | queue->popJob();
|
|---|
| 273 | queue->popJob();
|
|---|
| 274 | #endif
|
|---|
| 275 |
|
|---|
| 276 | // prepare a result
|
|---|
| [35f587] | 277 | FragmentResult::ptr testResult( new FragmentResult(1) );
|
|---|
| 278 | FragmentResult::ptr anothertestResult( new FragmentResult(2) );
|
|---|
| [b9c486] | 279 |
|
|---|
| 280 | // push correct result
|
|---|
| 281 | #ifndef NDEBUG
|
|---|
| 282 | CPPUNIT_ASSERT_NO_THROW( queue->pushResult(testResult) );
|
|---|
| 283 | CPPUNIT_ASSERT_NO_THROW( queue->pushResult(anothertestResult) );
|
|---|
| 284 | #else
|
|---|
| 285 | queue->pushResult(testResult);
|
|---|
| 286 | queue->pushResult(anothertestResult);
|
|---|
| 287 | #endif
|
|---|
| 288 |
|
|---|
| 289 | // check that two results are returned.
|
|---|
| 290 | {
|
|---|
| [35f587] | 291 | const std::vector<FragmentResult::ptr> results = queue->getAllResults();
|
|---|
| [b9c486] | 292 | CPPUNIT_ASSERT_EQUAL( (size_t)2, results.size() );
|
|---|
| 293 | }
|
|---|
| 294 | }
|
|---|
| 295 |
|
|---|
| [fe95b7] | 296 | /** Unit test for resubmitJob().
|
|---|
| 297 | *
|
|---|
| 298 | */
|
|---|
| 299 | void FragmentQueueTest::resubmitTest()
|
|---|
| 300 | {
|
|---|
| 301 | FragmentJob::ptr testJob(new FragmentJobStub(1));
|
|---|
| 302 |
|
|---|
| 303 | // push a Job into queue
|
|---|
| 304 | #ifndef NDEBUG
|
|---|
| 305 | CPPUNIT_ASSERT_NO_THROW( queue->pushJob(testJob) );
|
|---|
| 306 | #else
|
|---|
| 307 | queue->pushJob(testJob);
|
|---|
| 308 | #endif
|
|---|
| 309 | CPPUNIT_ASSERT_EQUAL((size_t)1, queue->jobs.size());
|
|---|
| 310 | CPPUNIT_ASSERT_EQUAL((size_t)1, queue->results.size());
|
|---|
| 311 |
|
|---|
| 312 | // pop the job
|
|---|
| 313 | // pop both as if some work was being done
|
|---|
| 314 | #ifndef NDEBUG
|
|---|
| 315 | CPPUNIT_ASSERT_NO_THROW( queue->popJob() );
|
|---|
| 316 | #else
|
|---|
| 317 | queue->popJob();
|
|---|
| 318 | #endif
|
|---|
| 319 | CPPUNIT_ASSERT_EQUAL((size_t)0, queue->jobs.size());
|
|---|
| 320 | CPPUNIT_ASSERT_EQUAL((size_t)1, queue->results.size());
|
|---|
| 321 |
|
|---|
| 322 | // resubmit
|
|---|
| 323 | #ifndef NDEBUG
|
|---|
| 324 | CPPUNIT_ASSERT_NO_THROW( queue->resubmitJob((JobId_t)1) );
|
|---|
| 325 | #else
|
|---|
| 326 | queue->resubmitJob((JobId_t)1);
|
|---|
| 327 | #endif
|
|---|
| 328 |
|
|---|
| 329 | // check whethers it's present again
|
|---|
| 330 | CPPUNIT_ASSERT_EQUAL((size_t)1, queue->jobs.size());
|
|---|
| 331 | CPPUNIT_ASSERT_EQUAL((size_t)1, queue->results.size());
|
|---|
| 332 | }
|
|---|
| 333 |
|
|---|