[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 |
|
---|
[d6b12c] | 31 | #include "CodePatterns/Observer/Channels.hpp"
|
---|
[02f346] | 32 |
|
---|
[b5ebb5] | 33 | #ifdef HAVE_TESTRUNNER
|
---|
| 34 | #include "UnitTestMain.hpp"
|
---|
| 35 | #endif /*HAVE_TESTRUNNER*/
|
---|
| 36 |
|
---|
| 37 | /********************************************** Test classes **************************************/
|
---|
| 38 |
|
---|
[d920b9] | 39 | #include "stubs/FragmentJobStub.hpp"
|
---|
[d6b12c] | 40 | #include "unittests/stubs/ObserverStub.hpp"
|
---|
[d920b9] | 41 |
|
---|
[b5ebb5] | 42 | // Registers the fixture into the 'registry'
|
---|
| 43 | CPPUNIT_TEST_SUITE_REGISTRATION( FragmentQueueTest );
|
---|
| 44 |
|
---|
| 45 |
|
---|
| 46 | void FragmentQueueTest::setUp()
|
---|
| 47 | {
|
---|
[02f346] | 48 | // Throw assertions
|
---|
| 49 | ASSERT_DO(Assert::Throw);
|
---|
| 50 |
|
---|
[b5ebb5] | 51 | queue = new FragmentQueue();
|
---|
[d6b12c] | 52 | addobserver = new NotificationObserver(queue->getChannel(FragmentQueue::JobAdded));
|
---|
| 53 |
|
---|
| 54 | // and sign on
|
---|
| 55 | queue->signOn(addobserver, FragmentQueue::JobAdded);
|
---|
[b5ebb5] | 56 | }
|
---|
| 57 |
|
---|
| 58 |
|
---|
| 59 | void FragmentQueueTest::tearDown()
|
---|
| 60 | {
|
---|
[d6b12c] | 61 | queue->signOff(addobserver, FragmentQueue::JobAdded);
|
---|
| 62 |
|
---|
[b5ebb5] | 63 | delete queue;
|
---|
[d6b12c] | 64 | delete addobserver;
|
---|
[b5ebb5] | 65 | }
|
---|
| 66 |
|
---|
| 67 | /** UnitTest for working JobQueue
|
---|
| 68 | */
|
---|
[9875cc] | 69 | void FragmentQueueTest::JobTest()
|
---|
[b5ebb5] | 70 | {
|
---|
[d920b9] | 71 | FragmentJob::ptr testJob(new FragmentJobStub(JobId::IllegalJob));
|
---|
[02f346] | 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
|
---|
[78ad7d] | 78 | testJob->setId(1);
|
---|
[02f346] | 79 |
|
---|
[12d15a] | 80 | CPPUNIT_ASSERT_EQUAL(false, queue->isJobPresent() );
|
---|
| 81 |
|
---|
[02f346] | 82 | #ifndef NDEBUG
|
---|
| 83 | CPPUNIT_ASSERT_NO_THROW( queue->pushJob(testJob) );
|
---|
| 84 | #else
|
---|
| 85 | queue->pushJob(testJob);
|
---|
| 86 | #endif
|
---|
[d6b12c] | 87 | CPPUNIT_ASSERT( addobserver->wasNotified );
|
---|
| 88 | addobserver->wasNotified = false;
|
---|
[02f346] | 89 |
|
---|
| 90 | CPPUNIT_ASSERT_EQUAL((size_t)1, queue->jobs.size());
|
---|
[12d15a] | 91 | CPPUNIT_ASSERT_EQUAL(true, queue->isJobPresent() );
|
---|
| 92 | CPPUNIT_ASSERT_EQUAL((size_t)1, queue->results.size());
|
---|
[9875cc] | 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 | }
|
---|
[02f346] | 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());
|
---|
[12d15a] | 106 | CPPUNIT_ASSERT_EQUAL((size_t)1, queue->results.size());
|
---|
| 107 |
|
---|
[78ad7d] | 108 | FragmentJob::ptr poppedJob;
|
---|
[12d15a] | 109 | #ifndef NDEBUG
|
---|
| 110 | CPPUNIT_ASSERT_NO_THROW( poppedJob = queue->popJob() );
|
---|
| 111 | #else
|
---|
| 112 | poppedJob = queue->popJob();
|
---|
| 113 | #endif
|
---|
[d6b12c] | 114 | CPPUNIT_ASSERT( !addobserver->wasNotified );
|
---|
[12d15a] | 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());
|
---|
[9875cc] | 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 | }
|
---|
[12d15a] | 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
|
---|
[b5ebb5] | 130 | }
|
---|
| 131 |
|
---|
[9875cc] | 132 | /** UnitTest for adding multiple jobs at a time.
|
---|
| 133 | */
|
---|
| 134 | void FragmentQueueTest::JobsTest()
|
---|
| 135 | {
|
---|
| 136 | // prepare some jobs
|
---|
[d920b9] | 137 | FragmentJob::ptr testJob(new FragmentJobStub(JobId::IllegalJob));
|
---|
[78ad7d] | 138 | testJob->setId((JobId_t)1);
|
---|
[d920b9] | 139 | FragmentJob::ptr anothertestJob(new FragmentJobStub(JobId::IllegalJob));
|
---|
[78ad7d] | 140 | anothertestJob->setId((JobId_t)2);
|
---|
[9875cc] | 141 |
|
---|
| 142 | // prepare a vector of them
|
---|
[78ad7d] | 143 | std::vector<FragmentJob::ptr> testjobs;
|
---|
[9875cc] | 144 | testjobs.push_back(testJob);
|
---|
| 145 | testjobs.push_back(anothertestJob);
|
---|
| 146 |
|
---|
| 147 | // prepare another vector of them
|
---|
[78ad7d] | 148 | std::vector<FragmentJob::ptr> sametestjobs;
|
---|
[9875cc] | 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 |
|
---|
[b5ebb5] | 181 | /** UnitTest for working ResultMap
|
---|
| 182 | */
|
---|
| 183 | void FragmentQueueTest::ResultsTest()
|
---|
| 184 | {
|
---|
[02f346] | 185 | // prepare a job
|
---|
[d920b9] | 186 | FragmentJob::ptr testJob(new FragmentJobStub(JobId::IllegalJob));
|
---|
[78ad7d] | 187 | testJob->setId(1);
|
---|
[12d15a] | 188 | #ifndef NDEBUG
|
---|
| 189 | CPPUNIT_ASSERT_NO_THROW( queue->pushJob(testJob) );
|
---|
| 190 | #else
|
---|
[02f346] | 191 | queue->pushJob(testJob);
|
---|
[12d15a] | 192 | #endif
|
---|
[bf56f6] | 193 |
|
---|
| 194 | // check for present job to do
|
---|
| 195 | CPPUNIT_ASSERT_EQUAL( (size_t)1, queue->getPresentJobs() );
|
---|
| 196 |
|
---|
[12d15a] | 197 | #ifndef NDEBUG
|
---|
| 198 | CPPUNIT_ASSERT_NO_THROW( queue->popJob() );
|
---|
| 199 | #else
|
---|
| 200 | queue->popJob();
|
---|
| 201 | #endif
|
---|
| 202 |
|
---|
[bf56f6] | 203 | // check for present job to do
|
---|
| 204 | CPPUNIT_ASSERT_EQUAL( (size_t)0, queue->getPresentJobs() );
|
---|
[02f346] | 205 |
|
---|
| 206 | // prepare a result
|
---|
[35f587] | 207 | FragmentResult::ptr testResult( new FragmentResult(1) );
|
---|
| 208 | FragmentResult::ptr wrongIdResult( new FragmentResult(2) );
|
---|
[02f346] | 209 |
|
---|
[b0b64c] | 210 | // check that none are present and we can't get result yet
|
---|
[8ee5ac] | 211 | CPPUNIT_ASSERT_EQUAL( (size_t)0, queue->getDoneJobs() );
|
---|
[02f346] | 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 |
|
---|
[b0b64c] | 219 | /// check for admonishing wrong id
|
---|
[02f346] | 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
|
---|
[b0b64c] | 224 |
|
---|
| 225 | // push correct result
|
---|
[02f346] | 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) );
|
---|
[8ee5ac] | 234 | CPPUNIT_ASSERT_EQUAL( (size_t)1, queue->getDoneJobs() );
|
---|
[bf56f6] | 235 | CPPUNIT_ASSERT_EQUAL( (size_t)0, queue->getPresentJobs() );
|
---|
[02f346] | 236 |
|
---|
[b0b64c] | 237 | // obtain result again
|
---|
[02f346] | 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
|
---|
[8ee5ac] | 245 | CPPUNIT_ASSERT_EQUAL( (size_t)0, queue->getDoneJobs() );
|
---|
[bf56f6] | 246 | CPPUNIT_ASSERT_EQUAL( (size_t)0, queue->getPresentJobs() );
|
---|
[02f346] | 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
|
---|
[b5ebb5] | 253 | }
|
---|
[b9c486] | 254 |
|
---|
| 255 | /** UnitTest for working ResultMap
|
---|
| 256 | */
|
---|
| 257 | void FragmentQueueTest::AllResultsTest()
|
---|
| 258 | {
|
---|
| 259 | // prepare a job
|
---|
[d920b9] | 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);
|
---|
[b9c486] | 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 | {
|
---|
[35f587] | 275 | const std::vector<FragmentResult::ptr> results = queue->getAllResults();
|
---|
[b9c486] | 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
|
---|
[35f587] | 289 | FragmentResult::ptr testResult( new FragmentResult(1) );
|
---|
| 290 | FragmentResult::ptr anothertestResult( new FragmentResult(2) );
|
---|
[b9c486] | 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 | {
|
---|
[35f587] | 303 | const std::vector<FragmentResult::ptr> results = queue->getAllResults();
|
---|
[b9c486] | 304 | CPPUNIT_ASSERT_EQUAL( (size_t)2, results.size() );
|
---|
| 305 | }
|
---|
| 306 | }
|
---|
| 307 |
|
---|
[fe95b7] | 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 |
|
---|