| 1 | /*
 | 
|---|
| 2 |  * Project: MoleCuilder
 | 
|---|
| 3 |  * Description: creates and alters molecular systems
 | 
|---|
| 4 |  * Copyright (C)  2012 University of Bonn. All rights reserved.
 | 
|---|
| 5 |  * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
 | 
|---|
| 6 |  */
 | 
|---|
| 7 | 
 | 
|---|
| 8 | /*
 | 
|---|
| 9 |  * Box_BoundaryConditionsUnitTest.cpp
 | 
|---|
| 10 |  *
 | 
|---|
| 11 |  *  Created on: Jan 2, 2012
 | 
|---|
| 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 <sstream>
 | 
|---|
| 25 | 
 | 
|---|
| 26 | #include "Box_BoundaryConditions.hpp"
 | 
|---|
| 27 | #include "CodePatterns/Assert.hpp"
 | 
|---|
| 28 | #include "LinearAlgebra/defs.hpp"
 | 
|---|
| 29 | 
 | 
|---|
| 30 | #include "Box_BoundaryConditionsUnitTest.hpp"
 | 
|---|
| 31 | 
 | 
|---|
| 32 | #ifdef HAVE_TESTRUNNER
 | 
|---|
| 33 | #include "UnitTestMain.hpp"
 | 
|---|
| 34 | #endif /*HAVE_TESTRUNNER*/
 | 
|---|
| 35 | 
 | 
|---|
| 36 | /********************************************** Test classes **************************************/
 | 
|---|
| 37 | 
 | 
|---|
| 38 | // Registers the fixture into the 'registry'
 | 
|---|
| 39 | CPPUNIT_TEST_SUITE_REGISTRATION( Box_BoundaryConditionsTest );
 | 
|---|
| 40 | 
 | 
|---|
| 41 | void Box_BoundaryConditionsTest::setUp(){
 | 
|---|
| 42 |   // failing asserts should be thrown
 | 
|---|
| 43 |   ASSERT_DO(Assert::Throw);
 | 
|---|
| 44 | 
 | 
|---|
| 45 |   CPPUNIT_ASSERT_NO_THROW( bc = new BoundaryConditions::BCContainer );
 | 
|---|
| 46 | }
 | 
|---|
| 47 | 
 | 
|---|
| 48 | void Box_BoundaryConditionsTest::tearDown()
 | 
|---|
| 49 | {
 | 
|---|
| 50 |   delete bc;
 | 
|---|
| 51 | }
 | 
|---|
| 52 | 
 | 
|---|
| 53 | void Box_BoundaryConditionsTest::getTest()
 | 
|---|
| 54 | {
 | 
|---|
| 55 |   // check default values
 | 
|---|
| 56 |   for (size_t index = 0; index < NDIM; ++index)
 | 
|---|
| 57 |     CPPUNIT_ASSERT_EQUAL( BoundaryConditions::Wrap, bc->get(index));
 | 
|---|
| 58 | 
 | 
|---|
| 59 |   // check throw
 | 
|---|
| 60 | #ifndef NDEBUG
 | 
|---|
| 61 |   std::cout << "The following assertions are intended and do not represent a failure of the test.\n";
 | 
|---|
| 62 |   CPPUNIT_ASSERT_THROW( bc->get(4), Assert::AssertionFailure );
 | 
|---|
| 63 |   CPPUNIT_ASSERT_THROW( bc->get(-1), Assert::AssertionFailure );
 | 
|---|
| 64 | #endif
 | 
|---|
| 65 | }
 | 
|---|
| 66 | 
 | 
|---|
| 67 | void Box_BoundaryConditionsTest::setTest()
 | 
|---|
| 68 | {
 | 
|---|
| 69 |   // set to other values
 | 
|---|
| 70 |   bc->set(1, BoundaryConditions::Ignore);
 | 
|---|
| 71 |   bc->set(2, BoundaryConditions::Bounce);
 | 
|---|
| 72 | 
 | 
|---|
| 73 |   // check that is not Wrap anymore
 | 
|---|
| 74 |   CPPUNIT_ASSERT_EQUAL( BoundaryConditions::Ignore, bc->get(1) );
 | 
|---|
| 75 |   CPPUNIT_ASSERT_EQUAL( BoundaryConditions::Bounce, bc->get(2) );
 | 
|---|
| 76 | 
 | 
|---|
| 77 |   // check throw
 | 
|---|
| 78 | #ifndef NDEBUG
 | 
|---|
| 79 |   std::cout << "The following assertions are intended and do not represent a failure of the test.\n";
 | 
|---|
| 80 |   CPPUNIT_ASSERT_THROW( bc->set(4, BoundaryConditions::Wrap), Assert::AssertionFailure );
 | 
|---|
| 81 |   CPPUNIT_ASSERT_THROW( bc->set(-1, BoundaryConditions::Wrap), Assert::AssertionFailure );
 | 
|---|
| 82 | #endif
 | 
|---|
| 83 | 
 | 
|---|
| 84 |   // set all at once
 | 
|---|
| 85 |   BoundaryConditions::Conditions_t conditions(3, BoundaryConditions::Wrap);
 | 
|---|
| 86 | 
 | 
|---|
| 87 |   // check throw
 | 
|---|
| 88 |   conditions.resize(NDIM+1, BoundaryConditions::Wrap);
 | 
|---|
| 89 | #ifndef NDEBUG
 | 
|---|
| 90 |   std::cout << "The following assertion is intended and does not represent a failure of the test.\n";
 | 
|---|
| 91 |   CPPUNIT_ASSERT_THROW( bc->set(conditions), Assert::AssertionFailure );
 | 
|---|
| 92 | #endif
 | 
|---|
| 93 |   conditions.resize(NDIM-1, BoundaryConditions::Wrap);
 | 
|---|
| 94 | #ifndef NDEBUG
 | 
|---|
| 95 |   std::cout << "The following assertion is intended and does not represent a failure of the test.\n";
 | 
|---|
| 96 |   CPPUNIT_ASSERT_THROW( bc->set(conditions), Assert::AssertionFailure );
 | 
|---|
| 97 | #endif
 | 
|---|
| 98 | }
 | 
|---|
| 99 | 
 | 
|---|
| 100 | void Box_BoundaryConditionsTest::outputTest()
 | 
|---|
| 101 | {
 | 
|---|
| 102 |   std::stringstream outstream;
 | 
|---|
| 103 |   outstream << *bc;
 | 
|---|
| 104 |   CPPUNIT_ASSERT( outstream.str() == std::string("Wrap Wrap Wrap"));
 | 
|---|
| 105 | }
 | 
|---|
| 106 | 
 | 
|---|
| 107 | void Box_BoundaryConditionsTest::converterTest()
 | 
|---|
| 108 | {
 | 
|---|
| 109 |   // enum to string
 | 
|---|
| 110 |   for (size_t index = (size_t)0; index < (size_t)BoundaryConditions::MAX_BoundaryCondition_t;index++)
 | 
|---|
| 111 |     CPPUNIT_ASSERT_EQUAL(
 | 
|---|
| 112 |         bc->ConverterBiMap[(BoundaryConditions::BoundaryCondition_t)index],
 | 
|---|
| 113 |         bc->getName((BoundaryConditions::BoundaryCondition_t)index) );
 | 
|---|
| 114 |   // check throw
 | 
|---|
| 115 | #ifndef NDEBUG
 | 
|---|
| 116 |   std::cout << "The following assertion is intended and does not represent a failure of the test.\n";
 | 
|---|
| 117 |   CPPUNIT_ASSERT_THROW( bc->getName(BoundaryConditions::MAX_BoundaryCondition_t), Assert::AssertionFailure );
 | 
|---|
| 118 | #endif
 | 
|---|
| 119 | 
 | 
|---|
| 120 |   // enum to string
 | 
|---|
| 121 |   CPPUNIT_ASSERT_EQUAL( BoundaryConditions::Wrap, bc->getEnum(std::string("Wrap")) );
 | 
|---|
| 122 |   CPPUNIT_ASSERT_EQUAL( BoundaryConditions::Bounce, bc->getEnum(std::string("Bounce")) );
 | 
|---|
| 123 |   CPPUNIT_ASSERT_EQUAL( BoundaryConditions::Ignore, bc->getEnum(std::string("Ignore")) );
 | 
|---|
| 124 |   // check throw
 | 
|---|
| 125 | #ifndef NDEBUG
 | 
|---|
| 126 |   std::cout << "The following assertion is intended and does not represent a failure of the test.\n";
 | 
|---|
| 127 |   CPPUNIT_ASSERT_THROW( bc->getEnum("Unknown"), Assert::AssertionFailure );
 | 
|---|
| 128 | #endif
 | 
|---|
| 129 | }
 | 
|---|
| 130 | 
 | 
|---|
| 131 | void Box_BoundaryConditionsTest::inputTest()
 | 
|---|
| 132 | {
 | 
|---|
| 133 |   // set by string
 | 
|---|
| 134 |   std::stringstream inputstream(" Bounce,Wrap,Ignore");
 | 
|---|
| 135 |   std::stringstream otherinputstream(" Bounce Wrap Ignore");
 | 
|---|
| 136 |   std::stringstream anotherinputstream(" Bounce, Wrap, Ignore");
 | 
|---|
| 137 | 
 | 
|---|
| 138 |   // check
 | 
|---|
| 139 |   CPPUNIT_ASSERT_NO_THROW( inputstream >> *bc );
 | 
|---|
| 140 |   CPPUNIT_ASSERT_EQUAL( BoundaryConditions::Bounce, bc->get(0) );
 | 
|---|
| 141 |   CPPUNIT_ASSERT_EQUAL( BoundaryConditions::Wrap, bc->get(1) );
 | 
|---|
| 142 |   CPPUNIT_ASSERT_EQUAL( BoundaryConditions::Ignore, bc->get(2) );
 | 
|---|
| 143 | 
 | 
|---|
| 144 |   // check
 | 
|---|
| 145 |   CPPUNIT_ASSERT_NO_THROW( otherinputstream >> *bc );
 | 
|---|
| 146 |   CPPUNIT_ASSERT_EQUAL( BoundaryConditions::Bounce, bc->get(0) );
 | 
|---|
| 147 |   CPPUNIT_ASSERT_EQUAL( BoundaryConditions::Wrap, bc->get(1) );
 | 
|---|
| 148 |   CPPUNIT_ASSERT_EQUAL( BoundaryConditions::Ignore, bc->get(2) );
 | 
|---|
| 149 | 
 | 
|---|
| 150 |   // check
 | 
|---|
| 151 |   CPPUNIT_ASSERT_NO_THROW( anotherinputstream >> *bc );
 | 
|---|
| 152 |   CPPUNIT_ASSERT_EQUAL( BoundaryConditions::Bounce, bc->get(0) );
 | 
|---|
| 153 |   CPPUNIT_ASSERT_EQUAL( BoundaryConditions::Wrap, bc->get(1) );
 | 
|---|
| 154 |   CPPUNIT_ASSERT_EQUAL( BoundaryConditions::Ignore, bc->get(2) );
 | 
|---|
| 155 | 
 | 
|---|
| 156 |   // check throw
 | 
|---|
| 157 |   std::stringstream invalidstream1("Bounce");
 | 
|---|
| 158 |   std::stringstream invalidstream2("Bounce, Bounce, Wrap, Ignore");
 | 
|---|
| 159 | #ifndef NDEBUG
 | 
|---|
| 160 |   std::cout << "The following assertions are intended and do not represent a failure of the test.\n";
 | 
|---|
| 161 |   CPPUNIT_ASSERT_THROW( invalidstream1 >> *bc, Assert::AssertionFailure );
 | 
|---|
| 162 |   CPPUNIT_ASSERT_THROW( invalidstream2 >> *bc, Assert::AssertionFailure );
 | 
|---|
| 163 | #endif
 | 
|---|
| 164 | 
 | 
|---|
| 165 |   // check that all is still the same
 | 
|---|
| 166 |   CPPUNIT_ASSERT_EQUAL( BoundaryConditions::Bounce, bc->get(0) );
 | 
|---|
| 167 |   CPPUNIT_ASSERT_EQUAL( BoundaryConditions::Wrap, bc->get(1) );
 | 
|---|
| 168 |   CPPUNIT_ASSERT_EQUAL( BoundaryConditions::Ignore, bc->get(2) );
 | 
|---|
| 169 | }
 | 
|---|
| 170 | 
 | 
|---|