| [dd067a] | 1 | /*
 | 
|---|
 | 2 |  * Project: MoleCuilder
 | 
|---|
 | 3 |  * Description: creates and alters molecular systems
 | 
|---|
 | 4 |  * Copyright (C)  2012 University of Bonn. All rights reserved.
 | 
|---|
| [94d5ac6] | 5 |  * 
 | 
|---|
 | 6 |  *
 | 
|---|
 | 7 |  *   This file is part of MoleCuilder.
 | 
|---|
 | 8 |  *
 | 
|---|
 | 9 |  *    MoleCuilder is free software: you can redistribute it and/or modify
 | 
|---|
 | 10 |  *    it under the terms of the GNU General Public License as published by
 | 
|---|
 | 11 |  *    the Free Software Foundation, either version 2 of the License, or
 | 
|---|
 | 12 |  *    (at your option) any later version.
 | 
|---|
 | 13 |  *
 | 
|---|
 | 14 |  *    MoleCuilder is distributed in the hope that it will be useful,
 | 
|---|
 | 15 |  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
|---|
 | 16 |  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
|---|
 | 17 |  *    GNU General Public License for more details.
 | 
|---|
 | 18 |  *
 | 
|---|
 | 19 |  *    You should have received a copy of the GNU General Public License
 | 
|---|
 | 20 |  *    along with MoleCuilder.  If not, see <http://www.gnu.org/licenses/>.
 | 
|---|
| [dd067a] | 21 |  */
 | 
|---|
 | 22 | 
 | 
|---|
 | 23 | /*
 | 
|---|
 | 24 |  * Box_BoundaryConditionsUnitTest.cpp
 | 
|---|
 | 25 |  *
 | 
|---|
 | 26 |  *  Created on: Jan 2, 2012
 | 
|---|
 | 27 |  *      Author: heber
 | 
|---|
 | 28 |  */
 | 
|---|
 | 29 | 
 | 
|---|
 | 30 | // include config.h
 | 
|---|
 | 31 | #ifdef HAVE_CONFIG_H
 | 
|---|
 | 32 | #include <config.h>
 | 
|---|
 | 33 | #endif
 | 
|---|
 | 34 | 
 | 
|---|
 | 35 | #include <cppunit/CompilerOutputter.h>
 | 
|---|
 | 36 | #include <cppunit/extensions/TestFactoryRegistry.h>
 | 
|---|
 | 37 | #include <cppunit/ui/text/TestRunner.h>
 | 
|---|
 | 38 | 
 | 
|---|
 | 39 | #include <sstream>
 | 
|---|
 | 40 | 
 | 
|---|
 | 41 | #include "Box_BoundaryConditions.hpp"
 | 
|---|
 | 42 | #include "CodePatterns/Assert.hpp"
 | 
|---|
 | 43 | #include "LinearAlgebra/defs.hpp"
 | 
|---|
 | 44 | 
 | 
|---|
 | 45 | #include "Box_BoundaryConditionsUnitTest.hpp"
 | 
|---|
 | 46 | 
 | 
|---|
 | 47 | #ifdef HAVE_TESTRUNNER
 | 
|---|
 | 48 | #include "UnitTestMain.hpp"
 | 
|---|
 | 49 | #endif /*HAVE_TESTRUNNER*/
 | 
|---|
 | 50 | 
 | 
|---|
 | 51 | /********************************************** Test classes **************************************/
 | 
|---|
 | 52 | 
 | 
|---|
 | 53 | // Registers the fixture into the 'registry'
 | 
|---|
 | 54 | CPPUNIT_TEST_SUITE_REGISTRATION( Box_BoundaryConditionsTest );
 | 
|---|
 | 55 | 
 | 
|---|
 | 56 | void Box_BoundaryConditionsTest::setUp(){
 | 
|---|
 | 57 |   // failing asserts should be thrown
 | 
|---|
 | 58 |   ASSERT_DO(Assert::Throw);
 | 
|---|
 | 59 | 
 | 
|---|
 | 60 |   CPPUNIT_ASSERT_NO_THROW( bc = new BoundaryConditions::BCContainer );
 | 
|---|
 | 61 | }
 | 
|---|
 | 62 | 
 | 
|---|
 | 63 | void Box_BoundaryConditionsTest::tearDown()
 | 
|---|
 | 64 | {
 | 
|---|
 | 65 |   delete bc;
 | 
|---|
 | 66 | }
 | 
|---|
 | 67 | 
 | 
|---|
 | 68 | void Box_BoundaryConditionsTest::getTest()
 | 
|---|
 | 69 | {
 | 
|---|
 | 70 |   // check default values
 | 
|---|
 | 71 |   for (size_t index = 0; index < NDIM; ++index)
 | 
|---|
 | 72 |     CPPUNIT_ASSERT_EQUAL( BoundaryConditions::Wrap, bc->get(index));
 | 
|---|
 | 73 | 
 | 
|---|
 | 74 |   // check throw
 | 
|---|
 | 75 | #ifndef NDEBUG
 | 
|---|
 | 76 |   std::cout << "The following assertions are intended and do not represent a failure of the test.\n";
 | 
|---|
 | 77 |   CPPUNIT_ASSERT_THROW( bc->get(4), Assert::AssertionFailure );
 | 
|---|
 | 78 |   CPPUNIT_ASSERT_THROW( bc->get(-1), Assert::AssertionFailure );
 | 
|---|
 | 79 | #endif
 | 
|---|
 | 80 | }
 | 
|---|
 | 81 | 
 | 
|---|
 | 82 | void Box_BoundaryConditionsTest::setTest()
 | 
|---|
 | 83 | {
 | 
|---|
 | 84 |   // set to other values
 | 
|---|
 | 85 |   bc->set(1, BoundaryConditions::Ignore);
 | 
|---|
 | 86 |   bc->set(2, BoundaryConditions::Bounce);
 | 
|---|
 | 87 | 
 | 
|---|
 | 88 |   // check that is not Wrap anymore
 | 
|---|
 | 89 |   CPPUNIT_ASSERT_EQUAL( BoundaryConditions::Ignore, bc->get(1) );
 | 
|---|
 | 90 |   CPPUNIT_ASSERT_EQUAL( BoundaryConditions::Bounce, bc->get(2) );
 | 
|---|
 | 91 | 
 | 
|---|
 | 92 |   // check throw
 | 
|---|
 | 93 | #ifndef NDEBUG
 | 
|---|
 | 94 |   std::cout << "The following assertions are intended and do not represent a failure of the test.\n";
 | 
|---|
 | 95 |   CPPUNIT_ASSERT_THROW( bc->set(4, BoundaryConditions::Wrap), Assert::AssertionFailure );
 | 
|---|
 | 96 |   CPPUNIT_ASSERT_THROW( bc->set(-1, BoundaryConditions::Wrap), Assert::AssertionFailure );
 | 
|---|
 | 97 | #endif
 | 
|---|
 | 98 | 
 | 
|---|
 | 99 |   // set all at once
 | 
|---|
 | 100 |   BoundaryConditions::Conditions_t conditions(3, BoundaryConditions::Wrap);
 | 
|---|
 | 101 | 
 | 
|---|
 | 102 |   // check throw
 | 
|---|
 | 103 |   conditions.resize(NDIM+1, BoundaryConditions::Wrap);
 | 
|---|
 | 104 | #ifndef NDEBUG
 | 
|---|
 | 105 |   std::cout << "The following assertion is intended and does not represent a failure of the test.\n";
 | 
|---|
 | 106 |   CPPUNIT_ASSERT_THROW( bc->set(conditions), Assert::AssertionFailure );
 | 
|---|
 | 107 | #endif
 | 
|---|
 | 108 |   conditions.resize(NDIM-1, BoundaryConditions::Wrap);
 | 
|---|
 | 109 | #ifndef NDEBUG
 | 
|---|
 | 110 |   std::cout << "The following assertion is intended and does not represent a failure of the test.\n";
 | 
|---|
 | 111 |   CPPUNIT_ASSERT_THROW( bc->set(conditions), Assert::AssertionFailure );
 | 
|---|
 | 112 | #endif
 | 
|---|
 | 113 | }
 | 
|---|
 | 114 | 
 | 
|---|
 | 115 | void Box_BoundaryConditionsTest::outputTest()
 | 
|---|
 | 116 | {
 | 
|---|
 | 117 |   std::stringstream outstream;
 | 
|---|
 | 118 |   outstream << *bc;
 | 
|---|
| [97f9b9] | 119 |   CPPUNIT_ASSERT( outstream.str() == std::string("Wrap Wrap Wrap"));
 | 
|---|
| [dd067a] | 120 | }
 | 
|---|
 | 121 | 
 | 
|---|
 | 122 | void Box_BoundaryConditionsTest::converterTest()
 | 
|---|
 | 123 | {
 | 
|---|
 | 124 |   // enum to string
 | 
|---|
 | 125 |   for (size_t index = (size_t)0; index < (size_t)BoundaryConditions::MAX_BoundaryCondition_t;index++)
 | 
|---|
 | 126 |     CPPUNIT_ASSERT_EQUAL(
 | 
|---|
 | 127 |         bc->ConverterBiMap[(BoundaryConditions::BoundaryCondition_t)index],
 | 
|---|
 | 128 |         bc->getName((BoundaryConditions::BoundaryCondition_t)index) );
 | 
|---|
 | 129 |   // check throw
 | 
|---|
 | 130 | #ifndef NDEBUG
 | 
|---|
 | 131 |   std::cout << "The following assertion is intended and does not represent a failure of the test.\n";
 | 
|---|
 | 132 |   CPPUNIT_ASSERT_THROW( bc->getName(BoundaryConditions::MAX_BoundaryCondition_t), Assert::AssertionFailure );
 | 
|---|
 | 133 | #endif
 | 
|---|
 | 134 | 
 | 
|---|
 | 135 |   // enum to string
 | 
|---|
 | 136 |   CPPUNIT_ASSERT_EQUAL( BoundaryConditions::Wrap, bc->getEnum(std::string("Wrap")) );
 | 
|---|
 | 137 |   CPPUNIT_ASSERT_EQUAL( BoundaryConditions::Bounce, bc->getEnum(std::string("Bounce")) );
 | 
|---|
 | 138 |   CPPUNIT_ASSERT_EQUAL( BoundaryConditions::Ignore, bc->getEnum(std::string("Ignore")) );
 | 
|---|
 | 139 |   // check throw
 | 
|---|
 | 140 | #ifndef NDEBUG
 | 
|---|
 | 141 |   std::cout << "The following assertion is intended and does not represent a failure of the test.\n";
 | 
|---|
 | 142 |   CPPUNIT_ASSERT_THROW( bc->getEnum("Unknown"), Assert::AssertionFailure );
 | 
|---|
 | 143 | #endif
 | 
|---|
 | 144 | }
 | 
|---|
 | 145 | 
 | 
|---|
 | 146 | void Box_BoundaryConditionsTest::inputTest()
 | 
|---|
 | 147 | {
 | 
|---|
 | 148 |   // set by string
 | 
|---|
| [97f9b9] | 149 |   std::stringstream inputstream(" Bounce,Wrap,Ignore");
 | 
|---|
 | 150 |   std::stringstream otherinputstream(" Bounce Wrap Ignore");
 | 
|---|
 | 151 |   std::stringstream anotherinputstream(" Bounce, Wrap, Ignore");
 | 
|---|
 | 152 | 
 | 
|---|
 | 153 |   // check
 | 
|---|
| [dd067a] | 154 |   CPPUNIT_ASSERT_NO_THROW( inputstream >> *bc );
 | 
|---|
| [97f9b9] | 155 |   CPPUNIT_ASSERT_EQUAL( BoundaryConditions::Bounce, bc->get(0) );
 | 
|---|
 | 156 |   CPPUNIT_ASSERT_EQUAL( BoundaryConditions::Wrap, bc->get(1) );
 | 
|---|
 | 157 |   CPPUNIT_ASSERT_EQUAL( BoundaryConditions::Ignore, bc->get(2) );
 | 
|---|
 | 158 | 
 | 
|---|
 | 159 |   // check
 | 
|---|
 | 160 |   CPPUNIT_ASSERT_NO_THROW( otherinputstream >> *bc );
 | 
|---|
 | 161 |   CPPUNIT_ASSERT_EQUAL( BoundaryConditions::Bounce, bc->get(0) );
 | 
|---|
 | 162 |   CPPUNIT_ASSERT_EQUAL( BoundaryConditions::Wrap, bc->get(1) );
 | 
|---|
 | 163 |   CPPUNIT_ASSERT_EQUAL( BoundaryConditions::Ignore, bc->get(2) );
 | 
|---|
| [dd067a] | 164 | 
 | 
|---|
 | 165 |   // check
 | 
|---|
| [97f9b9] | 166 |   CPPUNIT_ASSERT_NO_THROW( anotherinputstream >> *bc );
 | 
|---|
| [dd067a] | 167 |   CPPUNIT_ASSERT_EQUAL( BoundaryConditions::Bounce, bc->get(0) );
 | 
|---|
 | 168 |   CPPUNIT_ASSERT_EQUAL( BoundaryConditions::Wrap, bc->get(1) );
 | 
|---|
 | 169 |   CPPUNIT_ASSERT_EQUAL( BoundaryConditions::Ignore, bc->get(2) );
 | 
|---|
 | 170 | 
 | 
|---|
 | 171 |   // check throw
 | 
|---|
 | 172 |   std::stringstream invalidstream1("Bounce");
 | 
|---|
 | 173 |   std::stringstream invalidstream2("Bounce, Bounce, Wrap, Ignore");
 | 
|---|
 | 174 | #ifndef NDEBUG
 | 
|---|
 | 175 |   std::cout << "The following assertions are intended and do not represent a failure of the test.\n";
 | 
|---|
 | 176 |   CPPUNIT_ASSERT_THROW( invalidstream1 >> *bc, Assert::AssertionFailure );
 | 
|---|
 | 177 |   CPPUNIT_ASSERT_THROW( invalidstream2 >> *bc, Assert::AssertionFailure );
 | 
|---|
 | 178 | #endif
 | 
|---|
 | 179 | 
 | 
|---|
 | 180 |   // check that all is still the same
 | 
|---|
 | 181 |   CPPUNIT_ASSERT_EQUAL( BoundaryConditions::Bounce, bc->get(0) );
 | 
|---|
 | 182 |   CPPUNIT_ASSERT_EQUAL( BoundaryConditions::Wrap, bc->get(1) );
 | 
|---|
 | 183 |   CPPUNIT_ASSERT_EQUAL( BoundaryConditions::Ignore, bc->get(2) );
 | 
|---|
 | 184 | }
 | 
|---|
 | 185 | 
 | 
|---|