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