/* * Project: MoleCuilder * Description: creates and alters molecular systems * Copyright (C) 2011 University of Bonn. All rights reserved. * Please see the LICENSE file or "Copyright notice" in builder.cpp for details. */ /* * FragmentQueueUnitTest.cpp * * Created on: Oct 23, 2011 * Author: heber */ // include config.h #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include "FragmentQueueUnitTest.hpp" #include #include "CodePatterns/Assert.hpp" #include "FragmentQueue.hpp" #ifdef HAVE_TESTRUNNER #include "UnitTestMain.hpp" #endif /*HAVE_TESTRUNNER*/ /********************************************** Test classes **************************************/ // Registers the fixture into the 'registry' CPPUNIT_TEST_SUITE_REGISTRATION( FragmentQueueTest ); void FragmentQueueTest::setUp() { // Throw assertions ASSERT_DO(Assert::Throw); queue = new FragmentQueue(); } void FragmentQueueTest::tearDown() { delete queue; } /** UnitTest for working JobQueue */ void FragmentQueueTest::JobTest() { FragmentJob::ptr testJob(new FragmentJob); /// check for illegal id #ifndef NDEBUG std::cout << "The following assertion is intended and does not indicate a failure." << std::endl; CPPUNIT_ASSERT_THROW( queue->pushJob(testJob), Assert::AssertionFailure ); #endif // set to valid id testJob->setId(1); testJob->outputfile = std::string("do something"); CPPUNIT_ASSERT_EQUAL(false, queue->isJobPresent() ); #ifndef NDEBUG CPPUNIT_ASSERT_NO_THROW( queue->pushJob(testJob) ); #else queue->pushJob(testJob); #endif CPPUNIT_ASSERT_EQUAL((size_t)1, queue->jobs.size()); CPPUNIT_ASSERT_EQUAL(true, queue->isJobPresent() ); CPPUNIT_ASSERT_EQUAL((size_t)1, queue->results.size()); { FragmentQueue::ResultMap::const_iterator iter = queue->results.find((JobId_t)1); CPPUNIT_ASSERT( iter != queue->results.end() ); CPPUNIT_ASSERT( FragmentQueue::NoResult == iter->second ); } // push same id again #ifndef NDEBUG std::cout << "The following assertion is intended and does not indicate a failure." << std::endl; CPPUNIT_ASSERT_THROW( queue->pushJob(testJob), Assert::AssertionFailure ); #endif CPPUNIT_ASSERT_EQUAL((size_t)1, queue->jobs.size()); CPPUNIT_ASSERT_EQUAL((size_t)1, queue->results.size()); FragmentJob::ptr poppedJob; #ifndef NDEBUG CPPUNIT_ASSERT_NO_THROW( poppedJob = queue->popJob() ); #else poppedJob = queue->popJob(); #endif CPPUNIT_ASSERT( poppedJob == testJob ); CPPUNIT_ASSERT_EQUAL((size_t)0, queue->jobs.size()); CPPUNIT_ASSERT_EQUAL(false, queue->isJobPresent() ); CPPUNIT_ASSERT_EQUAL((size_t)1, queue->results.size()); { FragmentQueue::ResultMap::const_iterator iter = queue->results.find((JobId_t)1); CPPUNIT_ASSERT( iter != queue->results.end() ); CPPUNIT_ASSERT( FragmentQueue::NoResultQueued == iter->second ); } #ifndef NDEBUG std::cout << "The following assertion is intended and does not indicate a failure." << std::endl; CPPUNIT_ASSERT_THROW( queue->popJob(), Assert::AssertionFailure ); #endif } /** UnitTest for adding multiple jobs at a time. */ void FragmentQueueTest::JobsTest() { // prepare some jobs FragmentJob::ptr testJob(new FragmentJob); testJob->setId((JobId_t)1); testJob->outputfile = std::string("do something"); FragmentJob::ptr anothertestJob(new FragmentJob); anothertestJob->setId((JobId_t)2); anothertestJob->outputfile = std::string("do something else"); // prepare a vector of them std::vector testjobs; testjobs.push_back(testJob); testjobs.push_back(anothertestJob); // prepare another vector of them std::vector sametestjobs; sametestjobs.push_back(testJob); sametestjobs.push_back(anothertestJob); // push the vector CPPUNIT_ASSERT_EQUAL( (size_t)0, queue->jobs.size() ); #ifndef NDEBUG CPPUNIT_ASSERT_NO_THROW( queue->pushJobs(testjobs) ); #else queue->pushJobs(testjobs); #endif CPPUNIT_ASSERT_EQUAL( (size_t)2, queue->jobs.size() ); CPPUNIT_ASSERT_EQUAL((size_t)2, queue->results.size()); { FragmentQueue::ResultMap::const_iterator iter = queue->results.find((JobId_t)1); CPPUNIT_ASSERT( iter != queue->results.end() ); CPPUNIT_ASSERT( FragmentQueue::NoResult == iter->second ); } { FragmentQueue::ResultMap::const_iterator iter = queue->results.find((JobId_t)2); CPPUNIT_ASSERT( iter != queue->results.end() ); CPPUNIT_ASSERT( FragmentQueue::NoResult == iter->second ); } // push again #ifndef NDEBUG std::cout << "The following assertion is intended and does not indicate a failure." << std::endl; CPPUNIT_ASSERT_THROW( queue->pushJobs(testjobs), Assert::AssertionFailure ); #endif CPPUNIT_ASSERT_EQUAL( (size_t)2, queue->jobs.size() ); CPPUNIT_ASSERT_EQUAL((size_t)2, queue->results.size()); } /** UnitTest for working ResultMap */ void FragmentQueueTest::ResultsTest() { // prepare a job FragmentJob::ptr testJob(new FragmentJob); testJob->setId(1); testJob->outputfile = std::string("do something"); #ifndef NDEBUG CPPUNIT_ASSERT_NO_THROW( queue->pushJob(testJob) ); #else queue->pushJob(testJob); #endif #ifndef NDEBUG CPPUNIT_ASSERT_NO_THROW( queue->popJob() ); #else queue->popJob(); #endif // prepare a result FragmentResult::ptr testResult( new FragmentResult(1) ); FragmentResult::ptr wrongIdResult( new FragmentResult(2) ); // check that none are present and we can't get result yet CPPUNIT_ASSERT_EQUAL( (size_t)0, queue->getDoneJobs() ); CPPUNIT_ASSERT( !queue->isResultPresent(1) ); CPPUNIT_ASSERT( !queue->isResultPresent(2) ); #ifndef NDEBUG std::cout << "The following assertion is intended and does not indicate a failure." << std::endl; CPPUNIT_ASSERT_THROW( queue->getResult(1), Assert::AssertionFailure ); #endif /// check for admonishing wrong id #ifndef NDEBUG std::cout << "The following assertion is intended and does not indicate a failure." << std::endl; CPPUNIT_ASSERT_THROW( queue->pushResult(wrongIdResult), Assert::AssertionFailure ); #endif // push correct result #ifndef NDEBUG CPPUNIT_ASSERT_NO_THROW( queue->pushResult(testResult) ); #else queue->pushResult(testResult); #endif // check presence again CPPUNIT_ASSERT( queue->isResultPresent(1) ); CPPUNIT_ASSERT_EQUAL( (size_t)1, queue->getDoneJobs() ); // obtain result again #ifndef NDEBUG CPPUNIT_ASSERT_NO_THROW( queue->getResult(1) ); #else queue->getResult(1); #endif // check presence one more time CPPUNIT_ASSERT_EQUAL( (size_t)0, queue->getDoneJobs() ); CPPUNIT_ASSERT( !queue->isResultPresent(1) ); CPPUNIT_ASSERT( !queue->isResultPresent(2) ); #ifndef NDEBUG std::cout << "The following assertion is intended and does not indicate a failure." << std::endl; CPPUNIT_ASSERT_THROW( queue->getResult(1), Assert::AssertionFailure ); #endif } /** UnitTest for working ResultMap */ void FragmentQueueTest::AllResultsTest() { // prepare a job FragmentJob::ptr testJob( new FragmentJob ); testJob->setId(1); testJob->outputfile = std::string("do something"); FragmentJob::ptr anothertestJob( new FragmentJob ); anothertestJob->setId(2); anothertestJob->outputfile = std::string("do something else"); #ifndef NDEBUG CPPUNIT_ASSERT_NO_THROW( queue->pushJob(testJob) ); CPPUNIT_ASSERT_NO_THROW( queue->pushJob(anothertestJob) ); #else queue->pushJob(testJob); queue->pushJob(anothertestJob); #endif // check that no results are returned. { const std::vector results = queue->getAllResults(); CPPUNIT_ASSERT_EQUAL( (size_t)0, results.size() ); } // pop both as if some work was being done #ifndef NDEBUG CPPUNIT_ASSERT_NO_THROW( queue->popJob() ); CPPUNIT_ASSERT_NO_THROW( queue->popJob() ); #else queue->popJob(); queue->popJob(); #endif // prepare a result FragmentResult::ptr testResult( new FragmentResult(1) ); FragmentResult::ptr anothertestResult( new FragmentResult(2) ); // push correct result #ifndef NDEBUG CPPUNIT_ASSERT_NO_THROW( queue->pushResult(testResult) ); CPPUNIT_ASSERT_NO_THROW( queue->pushResult(anothertestResult) ); #else queue->pushResult(testResult); queue->pushResult(anothertestResult); #endif // check that two results are returned. { const std::vector results = queue->getAllResults(); CPPUNIT_ASSERT_EQUAL( (size_t)2, results.size() ); } }