| 1 | /*
 | 
|---|
| 2 |  * Project: MoleCuilder
 | 
|---|
| 3 |  * Description: creates and alters molecular systems
 | 
|---|
| 4 |  * Copyright (C)  2013 University of Bonn. All rights reserved.
 | 
|---|
| 5 |  * Copyright (C)  2013 Frederik Heber. All rights reserved.
 | 
|---|
| 6 |  * 
 | 
|---|
| 7 |  *
 | 
|---|
| 8 |  *   This file is part of MoleCuilder.
 | 
|---|
| 9 |  *
 | 
|---|
| 10 |  *    MoleCuilder is free software: you can redistribute it and/or modify
 | 
|---|
| 11 |  *    it under the terms of the GNU General Public License as published by
 | 
|---|
| 12 |  *    the Free Software Foundation, either version 2 of the License, or
 | 
|---|
| 13 |  *    (at your option) any later version.
 | 
|---|
| 14 |  *
 | 
|---|
| 15 |  *    MoleCuilder is distributed in the hope that it will be useful,
 | 
|---|
| 16 |  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
|---|
| 17 |  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
|---|
| 18 |  *    GNU General Public License for more details.
 | 
|---|
| 19 |  *
 | 
|---|
| 20 |  *    You should have received a copy of the GNU General Public License
 | 
|---|
| 21 |  *    along with MoleCuilder.  If not, see <http://www.gnu.org/licenses/>.
 | 
|---|
| 22 |  */
 | 
|---|
| 23 | 
 | 
|---|
| 24 | /*
 | 
|---|
| 25 |  * HydrogenPoolUnitTest.cpp
 | 
|---|
| 26 |  *
 | 
|---|
| 27 |  *  Created on: Mar 03, 2013
 | 
|---|
| 28 |  *      Author: heber
 | 
|---|
| 29 |  */
 | 
|---|
| 30 | 
 | 
|---|
| 31 | // include config.h
 | 
|---|
| 32 | #ifdef HAVE_CONFIG_H
 | 
|---|
| 33 | #include <config.h>
 | 
|---|
| 34 | #endif
 | 
|---|
| 35 | 
 | 
|---|
| 36 | using namespace std;
 | 
|---|
| 37 | 
 | 
|---|
| 38 | #include <cppunit/CompilerOutputter.h>
 | 
|---|
| 39 | #include <cppunit/extensions/TestFactoryRegistry.h>
 | 
|---|
| 40 | #include <cppunit/ui/text/TestRunner.h>
 | 
|---|
| 41 | 
 | 
|---|
| 42 | // include headers that implement a archive in simple text format
 | 
|---|
| 43 | #include <boost/archive/text_oarchive.hpp>
 | 
|---|
| 44 | #include <boost/archive/text_iarchive.hpp>
 | 
|---|
| 45 | 
 | 
|---|
| 46 | #include "HydrogenPoolUnitTest.hpp"
 | 
|---|
| 47 | 
 | 
|---|
| 48 | #include "CodePatterns/Assert.hpp"
 | 
|---|
| 49 | 
 | 
|---|
| 50 | #include "Fragmentation/Exporters/HydrogenPool.hpp"
 | 
|---|
| 51 | #include "World.hpp"
 | 
|---|
| 52 | 
 | 
|---|
| 53 | #ifdef HAVE_TESTRUNNER
 | 
|---|
| 54 | #include "UnitTestMain.hpp"
 | 
|---|
| 55 | #endif /*HAVE_TESTRUNNER*/
 | 
|---|
| 56 | 
 | 
|---|
| 57 | /********************************************** Test classes **************************************/
 | 
|---|
| 58 | 
 | 
|---|
| 59 | // Registers the fixture into the 'registry'
 | 
|---|
| 60 | CPPUNIT_TEST_SUITE_REGISTRATION( HydrogenPoolTest );
 | 
|---|
| 61 | 
 | 
|---|
| 62 | 
 | 
|---|
| 63 | void HydrogenPoolTest::setUp()
 | 
|---|
| 64 | {
 | 
|---|
| 65 |   // failing asserts should be thrown
 | 
|---|
| 66 |   ASSERT_DO(Assert::Throw);
 | 
|---|
| 67 | 
 | 
|---|
| 68 |   pool = new HydrogenPool();
 | 
|---|
| 69 | }
 | 
|---|
| 70 | 
 | 
|---|
| 71 | 
 | 
|---|
| 72 | void HydrogenPoolTest::tearDown()
 | 
|---|
| 73 | {
 | 
|---|
| 74 |   delete pool;
 | 
|---|
| 75 | 
 | 
|---|
| 76 |   World::purgeInstance();
 | 
|---|
| 77 | }
 | 
|---|
| 78 | 
 | 
|---|
| 79 | /** UnitTest for leaseHydrogen() and releaseHydrogen.
 | 
|---|
| 80 |  */
 | 
|---|
| 81 | void HydrogenPoolTest::lease_releaseHydrogen_Test()
 | 
|---|
| 82 | {
 | 
|---|
| 83 |   // check simple test
 | 
|---|
| 84 |   {
 | 
|---|
| 85 |     CPPUNIT_ASSERT_EQUAL( 0, World::getInstance().numAtoms() );
 | 
|---|
| 86 |     atom * const Walker = pool->leaseHydrogen();
 | 
|---|
| 87 |     CPPUNIT_ASSERT( pool->HydrogenQueue.empty() );
 | 
|---|
| 88 |     CPPUNIT_ASSERT( !pool->HydrogenInUse.empty() );
 | 
|---|
| 89 |     CPPUNIT_ASSERT_EQUAL( 1, World::getInstance().numAtoms() );
 | 
|---|
| 90 |     pool->releaseHydrogen(Walker);
 | 
|---|
| 91 |     CPPUNIT_ASSERT( !pool->HydrogenQueue.empty() );
 | 
|---|
| 92 |     CPPUNIT_ASSERT( pool->HydrogenInUse.empty() );
 | 
|---|
| 93 |     pool->cleanup();
 | 
|---|
| 94 |     CPPUNIT_ASSERT_EQUAL( 0, World::getInstance().numAtoms() );
 | 
|---|
| 95 |   }
 | 
|---|
| 96 |   // check via id test
 | 
|---|
| 97 |   {
 | 
|---|
| 98 |     CPPUNIT_ASSERT_EQUAL( 0, World::getInstance().numAtoms() );
 | 
|---|
| 99 |     atom * const Walker = pool->leaseHydrogen();
 | 
|---|
| 100 |     CPPUNIT_ASSERT( pool->HydrogenQueue.empty() );
 | 
|---|
| 101 |     CPPUNIT_ASSERT( !pool->HydrogenInUse.empty() );
 | 
|---|
| 102 |     CPPUNIT_ASSERT_EQUAL( 1, World::getInstance().numAtoms() );
 | 
|---|
| 103 |     pool->releaseHydrogen(Walker->getId());
 | 
|---|
| 104 |     CPPUNIT_ASSERT( !pool->HydrogenQueue.empty() );
 | 
|---|
| 105 |     CPPUNIT_ASSERT( pool->HydrogenInUse.empty() );
 | 
|---|
| 106 |     pool->cleanup();
 | 
|---|
| 107 |     CPPUNIT_ASSERT_EQUAL( 0, World::getInstance().numAtoms() );
 | 
|---|
| 108 |   }
 | 
|---|
| 109 |   // check that can't cleanup with leased atoms
 | 
|---|
| 110 |   {
 | 
|---|
| 111 |     CPPUNIT_ASSERT_EQUAL( 0, World::getInstance().numAtoms() );
 | 
|---|
| 112 |     atom * const Walker = pool->leaseHydrogen();
 | 
|---|
| 113 |     std::cerr << "The following assertion is intended and does not entail a failure of the test." << std::endl;
 | 
|---|
| 114 | #ifndef NDEBUG
 | 
|---|
| 115 |     CPPUNIT_ASSERT_THROW(pool->cleanup(), Assert::AssertionFailure );
 | 
|---|
| 116 | #else
 | 
|---|
| 117 |     pool->cleanup();
 | 
|---|
| 118 | #endif
 | 
|---|
| 119 |     CPPUNIT_ASSERT_EQUAL( 1, World::getInstance().numAtoms() );
 | 
|---|
| 120 |     pool->releaseHydrogen(Walker);
 | 
|---|
| 121 |     pool->cleanup();
 | 
|---|
| 122 |     CPPUNIT_ASSERT_EQUAL( 0, World::getInstance().numAtoms() );
 | 
|---|
| 123 |   }
 | 
|---|
| 124 |   // check returning from atom
 | 
|---|
| 125 |   {
 | 
|---|
| 126 |     CPPUNIT_ASSERT_EQUAL( 0, World::getInstance().numAtoms() );
 | 
|---|
| 127 |     atom * const Walker = pool->leaseHydrogen();
 | 
|---|
| 128 |     CPPUNIT_ASSERT_EQUAL( 1, World::getInstance().numAtoms() );
 | 
|---|
| 129 |     atom * const InvalidWalker = NULL;
 | 
|---|
| 130 |     std::cerr << "The following assertion is intended and does not entail a failure of the test." << std::endl;
 | 
|---|
| 131 | #ifndef NDEBUG
 | 
|---|
| 132 |     CPPUNIT_ASSERT_THROW(pool->releaseHydrogen(InvalidWalker), Assert::AssertionFailure );
 | 
|---|
| 133 | #else
 | 
|---|
| 134 |     pool->releaseHydrogen(InvalidWalker);
 | 
|---|
| 135 | #endif
 | 
|---|
| 136 |     CPPUNIT_ASSERT_EQUAL( 1, World::getInstance().numAtoms() );
 | 
|---|
| 137 |     pool->releaseHydrogen(Walker);
 | 
|---|
| 138 |     pool->cleanup();
 | 
|---|
| 139 |     CPPUNIT_ASSERT_EQUAL( 0, World::getInstance().numAtoms() );
 | 
|---|
| 140 |   }
 | 
|---|
| 141 | }
 | 
|---|
| 142 | 
 | 
|---|
| 143 | /** UnitTest for requestHydrogen().
 | 
|---|
| 144 |  */
 | 
|---|
| 145 | void HydrogenPoolTest::requestHydrogen_Test()
 | 
|---|
| 146 | {
 | 
|---|
| 147 |   CPPUNIT_ASSERT_EQUAL( 0, World::getInstance().numAtoms() );
 | 
|---|
| 148 |   pool->requestHydrogenIntoPool();
 | 
|---|
| 149 |   CPPUNIT_ASSERT( !pool->HydrogenQueue.empty() );
 | 
|---|
| 150 |   CPPUNIT_ASSERT_EQUAL( 1, World::getInstance().numAtoms() );
 | 
|---|
| 151 |   pool->cleanup();
 | 
|---|
| 152 |   CPPUNIT_ASSERT_EQUAL( 0, World::getInstance().numAtoms() );
 | 
|---|
| 153 | }
 | 
|---|