| 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 | 
 | 
|---|
| 182 |   // check for present job to do
 | 
|---|
| 183 |   CPPUNIT_ASSERT_EQUAL( (size_t)1, queue->getPresentJobs() );
 | 
|---|
| 184 | 
 | 
|---|
| 185 | #ifndef NDEBUG
 | 
|---|
| 186 |   CPPUNIT_ASSERT_NO_THROW( queue->popJob() );
 | 
|---|
| 187 | #else
 | 
|---|
| 188 |   queue->popJob();
 | 
|---|
| 189 | #endif
 | 
|---|
| 190 | 
 | 
|---|
| 191 |   // check for present job to do
 | 
|---|
| 192 |   CPPUNIT_ASSERT_EQUAL( (size_t)0, queue->getPresentJobs() );
 | 
|---|
| 193 | 
 | 
|---|
| 194 |   // prepare a result
 | 
|---|
| 195 |   FragmentResult::ptr testResult( new FragmentResult(1) );
 | 
|---|
| 196 |   FragmentResult::ptr wrongIdResult( new FragmentResult(2) );
 | 
|---|
| 197 | 
 | 
|---|
| 198 |   // check that none are present and we can't get result yet
 | 
|---|
| 199 |   CPPUNIT_ASSERT_EQUAL( (size_t)0, queue->getDoneJobs() );
 | 
|---|
| 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 | 
 | 
|---|
| 207 |   /// check for admonishing wrong id
 | 
|---|
| 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
 | 
|---|
| 212 | 
 | 
|---|
| 213 |   // push correct result
 | 
|---|
| 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) );
 | 
|---|
| 222 |   CPPUNIT_ASSERT_EQUAL( (size_t)1, queue->getDoneJobs() );
 | 
|---|
| 223 |   CPPUNIT_ASSERT_EQUAL( (size_t)0, queue->getPresentJobs() );
 | 
|---|
| 224 | 
 | 
|---|
| 225 |   // obtain result again
 | 
|---|
| 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
 | 
|---|
| 233 |   CPPUNIT_ASSERT_EQUAL( (size_t)0, queue->getDoneJobs() );
 | 
|---|
| 234 |   CPPUNIT_ASSERT_EQUAL( (size_t)0, queue->getPresentJobs() );
 | 
|---|
| 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
 | 
|---|
| 241 | }
 | 
|---|
| 242 | 
 | 
|---|
| 243 | /** UnitTest for working ResultMap
 | 
|---|
| 244 |  */
 | 
|---|
| 245 | void FragmentQueueTest::AllResultsTest()
 | 
|---|
| 246 | {
 | 
|---|
| 247 |   // prepare a job
 | 
|---|
| 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);
 | 
|---|
| 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 |   {
 | 
|---|
| 263 |     const std::vector<FragmentResult::ptr> results = queue->getAllResults();
 | 
|---|
| 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
 | 
|---|
| 277 |   FragmentResult::ptr testResult( new FragmentResult(1) );
 | 
|---|
| 278 |   FragmentResult::ptr anothertestResult( new FragmentResult(2) );
 | 
|---|
| 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 |   {
 | 
|---|
| 291 |     const std::vector<FragmentResult::ptr> results = queue->getAllResults();
 | 
|---|
| 292 |     CPPUNIT_ASSERT_EQUAL( (size_t)2, results.size() );
 | 
|---|
| 293 |   }
 | 
|---|
| 294 | }
 | 
|---|
| 295 | 
 | 
|---|