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