[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
|
---|
| 181 | #ifndef NDEBUG
|
---|
| 182 | CPPUNIT_ASSERT_NO_THROW( queue->popJob() );
|
---|
| 183 | #else
|
---|
| 184 | queue->popJob();
|
---|
| 185 | #endif
|
---|
| 186 |
|
---|
[02f346] | 187 |
|
---|
| 188 | // prepare a result
|
---|
[35f587] | 189 | FragmentResult::ptr testResult( new FragmentResult(1) );
|
---|
| 190 | FragmentResult::ptr wrongIdResult( new FragmentResult(2) );
|
---|
[02f346] | 191 |
|
---|
[b0b64c] | 192 | // check that none are present and we can't get result yet
|
---|
[8ee5ac] | 193 | CPPUNIT_ASSERT_EQUAL( (size_t)0, queue->getDoneJobs() );
|
---|
[02f346] | 194 | CPPUNIT_ASSERT( !queue->isResultPresent(1) );
|
---|
| 195 | CPPUNIT_ASSERT( !queue->isResultPresent(2) );
|
---|
| 196 | #ifndef NDEBUG
|
---|
| 197 | std::cout << "The following assertion is intended and does not indicate a failure." << std::endl;
|
---|
| 198 | CPPUNIT_ASSERT_THROW( queue->getResult(1), Assert::AssertionFailure );
|
---|
| 199 | #endif
|
---|
| 200 |
|
---|
[b0b64c] | 201 | /// check for admonishing wrong id
|
---|
[02f346] | 202 | #ifndef NDEBUG
|
---|
| 203 | std::cout << "The following assertion is intended and does not indicate a failure." << std::endl;
|
---|
| 204 | CPPUNIT_ASSERT_THROW( queue->pushResult(wrongIdResult), Assert::AssertionFailure );
|
---|
| 205 | #endif
|
---|
[b0b64c] | 206 |
|
---|
| 207 | // push correct result
|
---|
[02f346] | 208 | #ifndef NDEBUG
|
---|
| 209 | CPPUNIT_ASSERT_NO_THROW( queue->pushResult(testResult) );
|
---|
| 210 | #else
|
---|
| 211 | queue->pushResult(testResult);
|
---|
| 212 | #endif
|
---|
| 213 |
|
---|
| 214 | // check presence again
|
---|
| 215 | CPPUNIT_ASSERT( queue->isResultPresent(1) );
|
---|
[8ee5ac] | 216 | CPPUNIT_ASSERT_EQUAL( (size_t)1, queue->getDoneJobs() );
|
---|
[02f346] | 217 |
|
---|
[b0b64c] | 218 | // obtain result again
|
---|
[02f346] | 219 | #ifndef NDEBUG
|
---|
| 220 | CPPUNIT_ASSERT_NO_THROW( queue->getResult(1) );
|
---|
| 221 | #else
|
---|
| 222 | queue->getResult(1);
|
---|
| 223 | #endif
|
---|
| 224 |
|
---|
| 225 | // check presence one more time
|
---|
[8ee5ac] | 226 | CPPUNIT_ASSERT_EQUAL( (size_t)0, queue->getDoneJobs() );
|
---|
[02f346] | 227 | CPPUNIT_ASSERT( !queue->isResultPresent(1) );
|
---|
| 228 | CPPUNIT_ASSERT( !queue->isResultPresent(2) );
|
---|
| 229 | #ifndef NDEBUG
|
---|
| 230 | std::cout << "The following assertion is intended and does not indicate a failure." << std::endl;
|
---|
| 231 | CPPUNIT_ASSERT_THROW( queue->getResult(1), Assert::AssertionFailure );
|
---|
| 232 | #endif
|
---|
[b5ebb5] | 233 | }
|
---|
[b9c486] | 234 |
|
---|
| 235 | /** UnitTest for working ResultMap
|
---|
| 236 | */
|
---|
| 237 | void FragmentQueueTest::AllResultsTest()
|
---|
| 238 | {
|
---|
| 239 | // prepare a job
|
---|
[d920b9] | 240 | FragmentJob::ptr testJob( new FragmentJobStub(JobId::IllegalJob) );
|
---|
| 241 | testJob->setId((JobId_t)1);
|
---|
| 242 | FragmentJob::ptr anothertestJob( new FragmentJobStub(JobId::IllegalJob) );
|
---|
| 243 | anothertestJob->setId((JobId_t)2);
|
---|
[b9c486] | 244 |
|
---|
| 245 | #ifndef NDEBUG
|
---|
| 246 | CPPUNIT_ASSERT_NO_THROW( queue->pushJob(testJob) );
|
---|
| 247 | CPPUNIT_ASSERT_NO_THROW( queue->pushJob(anothertestJob) );
|
---|
| 248 | #else
|
---|
| 249 | queue->pushJob(testJob);
|
---|
| 250 | queue->pushJob(anothertestJob);
|
---|
| 251 | #endif
|
---|
| 252 |
|
---|
| 253 | // check that no results are returned.
|
---|
| 254 | {
|
---|
[35f587] | 255 | const std::vector<FragmentResult::ptr> results = queue->getAllResults();
|
---|
[b9c486] | 256 | CPPUNIT_ASSERT_EQUAL( (size_t)0, results.size() );
|
---|
| 257 | }
|
---|
| 258 |
|
---|
| 259 | // pop both as if some work was being done
|
---|
| 260 | #ifndef NDEBUG
|
---|
| 261 | CPPUNIT_ASSERT_NO_THROW( queue->popJob() );
|
---|
| 262 | CPPUNIT_ASSERT_NO_THROW( queue->popJob() );
|
---|
| 263 | #else
|
---|
| 264 | queue->popJob();
|
---|
| 265 | queue->popJob();
|
---|
| 266 | #endif
|
---|
| 267 |
|
---|
| 268 | // prepare a result
|
---|
[35f587] | 269 | FragmentResult::ptr testResult( new FragmentResult(1) );
|
---|
| 270 | FragmentResult::ptr anothertestResult( new FragmentResult(2) );
|
---|
[b9c486] | 271 |
|
---|
| 272 | // push correct result
|
---|
| 273 | #ifndef NDEBUG
|
---|
| 274 | CPPUNIT_ASSERT_NO_THROW( queue->pushResult(testResult) );
|
---|
| 275 | CPPUNIT_ASSERT_NO_THROW( queue->pushResult(anothertestResult) );
|
---|
| 276 | #else
|
---|
| 277 | queue->pushResult(testResult);
|
---|
| 278 | queue->pushResult(anothertestResult);
|
---|
| 279 | #endif
|
---|
| 280 |
|
---|
| 281 | // check that two results are returned.
|
---|
| 282 | {
|
---|
[35f587] | 283 | const std::vector<FragmentResult::ptr> results = queue->getAllResults();
|
---|
[b9c486] | 284 | CPPUNIT_ASSERT_EQUAL( (size_t)2, results.size() );
|
---|
| 285 | }
|
---|
| 286 | }
|
---|
| 287 |
|
---|