| 1 | /*
 | 
|---|
| 2 |  * Project: MoleCuilder
 | 
|---|
| 3 |  * Description: creates and alters molecular systems
 | 
|---|
| 4 |  * Copyright (C)  2010-2012 University of Bonn. All rights reserved.
 | 
|---|
| 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/>.
 | 
|---|
| 21 |  */
 | 
|---|
| 22 | 
 | 
|---|
| 23 | /*
 | 
|---|
| 24 |  * Ops_FillPredicateUnitTest.cpp
 | 
|---|
| 25 |  *
 | 
|---|
| 26 |  *  Created on: Jan 19, 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 "CodePatterns/Assert.hpp"
 | 
|---|
| 40 | 
 | 
|---|
| 41 | #include "Atom/atom.hpp"
 | 
|---|
| 42 | #include "Box.hpp"
 | 
|---|
| 43 | #include "Filling/Predicates/IsValidInDomain_FillPredicate.hpp"
 | 
|---|
| 44 | #include "Filling/Predicates/IsVoidNode_FillPredicate.hpp"
 | 
|---|
| 45 | #include "Filling/Predicates/Ops_FillPredicate.hpp"
 | 
|---|
| 46 | #include "LinearAlgebra/RealSpaceMatrix.hpp"
 | 
|---|
| 47 | #include "Filling/NodeTypes.hpp"
 | 
|---|
| 48 | #include "Shapes/BaseShapes.hpp"
 | 
|---|
| 49 | #include "World.hpp"
 | 
|---|
| 50 | 
 | 
|---|
| 51 | #include "Ops_FillPredicateUnitTest.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( Ops_FillPredicateTest );
 | 
|---|
| 61 | 
 | 
|---|
| 62 | void DoSomething(atom * const _atom)
 | 
|---|
| 63 | {
 | 
|---|
| 64 |   _atom->setPosition( Vector((double)_atom->getId(), 0., 0.) );
 | 
|---|
| 65 | }
 | 
|---|
| 66 | 
 | 
|---|
| 67 | void Ops_FillPredicateTest::setUp()
 | 
|---|
| 68 | {
 | 
|---|
| 69 |   // failing asserts should be thrown
 | 
|---|
| 70 |   ASSERT_DO(Assert::Throw);
 | 
|---|
| 71 | 
 | 
|---|
| 72 |   // set default BCs to ignore
 | 
|---|
| 73 |   World::getInstance().getDomain().setConditions(
 | 
|---|
| 74 |       BoundaryConditions::Conditions_t(3, BoundaryConditions::Ignore)
 | 
|---|
| 75 |   );
 | 
|---|
| 76 | 
 | 
|---|
| 77 |   Domainpredicate = new FillPredicate(IsValidInDomain_FillPredicate(World::getInstance().getDomain()));
 | 
|---|
| 78 | 
 | 
|---|
| 79 |   // create some atoms as "neighbours"
 | 
|---|
| 80 |   atoms.resize((size_t)5, NULL);
 | 
|---|
| 81 |   std::generate_n(atoms.begin(), (size_t)5, boost::bind(&World::createAtom, World::getPointer()) );
 | 
|---|
| 82 | 
 | 
|---|
| 83 |   // position them
 | 
|---|
| 84 |   std::for_each(atoms.begin(), atoms.end(), &DoSomething);
 | 
|---|
| 85 | 
 | 
|---|
| 86 |   Voidpredicate = new FillPredicate(IsVoidNode_FillPredicate(Sphere()));
 | 
|---|
| 87 | }
 | 
|---|
| 88 | 
 | 
|---|
| 89 | 
 | 
|---|
| 90 | void Ops_FillPredicateTest::tearDown()
 | 
|---|
| 91 | {
 | 
|---|
| 92 |   delete Domainpredicate;
 | 
|---|
| 93 |   delete Voidpredicate;
 | 
|---|
| 94 | 
 | 
|---|
| 95 |   World::purgeInstance();
 | 
|---|
| 96 | }
 | 
|---|
| 97 | 
 | 
|---|
| 98 | /** Test whether operator() returns as desired
 | 
|---|
| 99 |  *
 | 
|---|
| 100 |  */
 | 
|---|
| 101 | void Ops_FillPredicateTest::AndTest()
 | 
|---|
| 102 | {
 | 
|---|
| 103 |         FillPredicate Andpredicate = (*Domainpredicate) && (*Voidpredicate);
 | 
|---|
| 104 | 
 | 
|---|
| 105 |         CPPUNIT_ASSERT( !Andpredicate( Vector(-1.,1.,0.)) );
 | 
|---|
| 106 |   CPPUNIT_ASSERT( !Andpredicate( Vector(-2.,0.,0.)) );
 | 
|---|
| 107 |         CPPUNIT_ASSERT( Andpredicate( Vector(5.,1.,0.)) );
 | 
|---|
| 108 |   CPPUNIT_ASSERT( Andpredicate( Vector(5.,0.,1.)) );
 | 
|---|
| 109 |   for (double i = 0; i < 5.; ++i) {
 | 
|---|
| 110 |     CPPUNIT_ASSERT( !Andpredicate( Vector(i, 0., 0.)) );
 | 
|---|
| 111 |     CPPUNIT_ASSERT( !Andpredicate( Vector(i, 1., 0.)) );
 | 
|---|
| 112 |     CPPUNIT_ASSERT( !Andpredicate( Vector(i, -1., 0.)) );
 | 
|---|
| 113 |     CPPUNIT_ASSERT( !Andpredicate( Vector(i, 0., 1.)) );
 | 
|---|
| 114 |     CPPUNIT_ASSERT( !Andpredicate( Vector(i, 0., -1.)) );
 | 
|---|
| 115 |   }
 | 
|---|
| 116 |   CPPUNIT_ASSERT( !Andpredicate( Vector(5.,-1.,0.)) );
 | 
|---|
| 117 |   CPPUNIT_ASSERT( Andpredicate( Vector(5.,0.,1.)) );
 | 
|---|
| 118 |   CPPUNIT_ASSERT( !Andpredicate( Vector(5.,0.,-1.)) );
 | 
|---|
| 119 |   CPPUNIT_ASSERT( Andpredicate( Vector(6.,0.,0.)) );
 | 
|---|
| 120 | }
 | 
|---|
| 121 | /** Test whether operator() returns as desired
 | 
|---|
| 122 |  *
 | 
|---|
| 123 |  */
 | 
|---|
| 124 | void Ops_FillPredicateTest::OrTest()
 | 
|---|
| 125 | {
 | 
|---|
| 126 |   FillPredicate Orpredicate = (*Domainpredicate) || (*Voidpredicate);
 | 
|---|
| 127 | 
 | 
|---|
| 128 |   // in OR case: Voidpredicate will always throw because it internally checks
 | 
|---|
| 129 |   // whether Linked Cell structure gets correct index to cell, which fails under
 | 
|---|
| 130 |   // Ignore boundary conditions
 | 
|---|
| 131 | #ifndef NDEBUG
 | 
|---|
| 132 |   std::cout << "The following Assertion is intended and does not represent a failure of the test." << std::endl;
 | 
|---|
| 133 |   CPPUNIT_ASSERT_THROW( Orpredicate( Vector(-1.,1.,0.)), Assert::AssertionFailure );
 | 
|---|
| 134 | #else
 | 
|---|
| 135 |   CPPUNIT_ASSERT( Orpredicate( Vector(-1.,1.,0.)) );
 | 
|---|
| 136 | #endif
 | 
|---|
| 137 |         CPPUNIT_ASSERT( Orpredicate( Vector(5.,1.,0.)) );
 | 
|---|
| 138 | #ifndef NDEBUG
 | 
|---|
| 139 |   std::cout << "The following Assertion is intended and does not represent a failure of the test." << std::endl;
 | 
|---|
| 140 |   CPPUNIT_ASSERT_THROW( Orpredicate( Vector(-2.,0.,0.)), Assert::AssertionFailure );
 | 
|---|
| 141 | #else
 | 
|---|
| 142 |   CPPUNIT_ASSERT( Orpredicate( Vector(-2.,0.,0.)) );
 | 
|---|
| 143 | #endif
 | 
|---|
| 144 |   for (double i = 0; i < 5.; ++i) {
 | 
|---|
| 145 |     CPPUNIT_ASSERT( Orpredicate( Vector(i, 0., 0.)) );
 | 
|---|
| 146 |     CPPUNIT_ASSERT( Orpredicate( Vector(i, 1., 0.)) );
 | 
|---|
| 147 | #ifndef NDEBUG
 | 
|---|
| 148 |   std::cout << "The following Assertion is intended and does not represent a failure of the test." << std::endl;
 | 
|---|
| 149 |   CPPUNIT_ASSERT_THROW( Orpredicate( Vector(i, -1., 0.)), Assert::AssertionFailure );
 | 
|---|
| 150 | #else
 | 
|---|
| 151 |   CPPUNIT_ASSERT( !Orpredicate( Vector(i, -1., 0.)) );
 | 
|---|
| 152 | #endif
 | 
|---|
| 153 |     CPPUNIT_ASSERT( Orpredicate( Vector(i, 0., 1.)) );
 | 
|---|
| 154 | #ifndef NDEBUG
 | 
|---|
| 155 |   std::cout << "The following Assertion is intended and does not represent a failure of the test." << std::endl;
 | 
|---|
| 156 |   CPPUNIT_ASSERT_THROW( Orpredicate( Vector(i, 0., -1.)), Assert::AssertionFailure );
 | 
|---|
| 157 | #else
 | 
|---|
| 158 |   CPPUNIT_ASSERT( !Orpredicate( Vector(i, 0., -1.)) );
 | 
|---|
| 159 | #endif
 | 
|---|
| 160 |   }
 | 
|---|
| 161 |   CPPUNIT_ASSERT( Orpredicate( Vector(5.,1.,0.)) );
 | 
|---|
| 162 | #ifndef NDEBUG
 | 
|---|
| 163 |   std::cout << "The following Assertion is intended and does not represent a failure of the test." << std::endl;
 | 
|---|
| 164 |   CPPUNIT_ASSERT_THROW( Orpredicate( Vector(5.,-1.,0.)), Assert::AssertionFailure );
 | 
|---|
| 165 | #else
 | 
|---|
| 166 |   CPPUNIT_ASSERT( Orpredicate( Vector(5.,-1.,0.)) );
 | 
|---|
| 167 | #endif
 | 
|---|
| 168 |   CPPUNIT_ASSERT( Orpredicate( Vector(5.,0.,1.)) );
 | 
|---|
| 169 | #ifndef NDEBUG
 | 
|---|
| 170 |   std::cout << "The following Assertion is intended and does not represent a failure of the test." << std::endl;
 | 
|---|
| 171 |   CPPUNIT_ASSERT_THROW( Orpredicate( Vector(5.,0.,-1.)), Assert::AssertionFailure );
 | 
|---|
| 172 | #else
 | 
|---|
| 173 |   CPPUNIT_ASSERT( Orpredicate( Vector(5.,0.,-1.)) );
 | 
|---|
| 174 | #endif
 | 
|---|
| 175 |   CPPUNIT_ASSERT( Orpredicate( Vector(6.,0.,0.)) );
 | 
|---|
| 176 | }
 | 
|---|
| 177 | 
 | 
|---|
| 178 | /** Test whether operator!() returns as desired
 | 
|---|
| 179 |  *
 | 
|---|
| 180 |  */
 | 
|---|
| 181 | void Ops_FillPredicateTest::NotTest()
 | 
|---|
| 182 | {
 | 
|---|
| 183 |   Box &domain = World::getInstance().getDomain();
 | 
|---|
| 184 |   Node origin(0.,0.,0.);
 | 
|---|
| 185 |   Node center(10.,10.,10.);
 | 
|---|
| 186 |   Node corner(20.,20.,20.);
 | 
|---|
| 187 |   Node outside(30.,30.,30.);
 | 
|---|
| 188 | 
 | 
|---|
| 189 |   FillPredicate Notpredicate = !(*Domainpredicate);
 | 
|---|
| 190 |   FillPredicate Notpredicate2 = !(*Voidpredicate);
 | 
|---|
| 191 | 
 | 
|---|
| 192 |   domain.setConditions(
 | 
|---|
| 193 |       BoundaryConditions::Conditions_t(3, BoundaryConditions::Wrap)
 | 
|---|
| 194 |   );
 | 
|---|
| 195 |   // boundary conditions: Wrap
 | 
|---|
| 196 |   {
 | 
|---|
| 197 |     // origin is inside
 | 
|---|
| 198 |     CPPUNIT_ASSERT( !Notpredicate(origin) );
 | 
|---|
| 199 | 
 | 
|---|
| 200 |     // center is inside
 | 
|---|
| 201 |     CPPUNIT_ASSERT( !Notpredicate(center) );
 | 
|---|
| 202 | 
 | 
|---|
| 203 |     // corner is inside
 | 
|---|
| 204 |     CPPUNIT_ASSERT( !Notpredicate(corner) );
 | 
|---|
| 205 | 
 | 
|---|
| 206 |     // outside is outside
 | 
|---|
| 207 |     CPPUNIT_ASSERT( !Notpredicate(outside) );
 | 
|---|
| 208 |   }
 | 
|---|
| 209 | 
 | 
|---|
| 210 |   // boundary conditions: Bounce
 | 
|---|
| 211 |   {
 | 
|---|
| 212 |     domain.setCondition(0, BoundaryConditions::Bounce);
 | 
|---|
| 213 |     // origin is inside
 | 
|---|
| 214 |     CPPUNIT_ASSERT( !Notpredicate(origin) );
 | 
|---|
| 215 | 
 | 
|---|
| 216 |     // center is inside
 | 
|---|
| 217 |     CPPUNIT_ASSERT( !Notpredicate(center) );
 | 
|---|
| 218 | 
 | 
|---|
| 219 |     // corner is inside
 | 
|---|
| 220 |     CPPUNIT_ASSERT( !Notpredicate(corner) );
 | 
|---|
| 221 | 
 | 
|---|
| 222 |     // outside is outside
 | 
|---|
| 223 |     CPPUNIT_ASSERT( !Notpredicate(outside) );
 | 
|---|
| 224 |     domain.setCondition(0, BoundaryConditions::Wrap);
 | 
|---|
| 225 |   }
 | 
|---|
| 226 | 
 | 
|---|
| 227 |   // boundary conditions: Ignore
 | 
|---|
| 228 |   {
 | 
|---|
| 229 |     domain.setCondition(0, BoundaryConditions::Ignore);
 | 
|---|
| 230 |     // origin is inside
 | 
|---|
| 231 |     CPPUNIT_ASSERT( !Notpredicate(origin) );
 | 
|---|
| 232 | 
 | 
|---|
| 233 |     // center is inside
 | 
|---|
| 234 |     CPPUNIT_ASSERT( !Notpredicate(center) );
 | 
|---|
| 235 | 
 | 
|---|
| 236 |     // corner is inside
 | 
|---|
| 237 |     CPPUNIT_ASSERT( !Notpredicate(corner) );
 | 
|---|
| 238 | 
 | 
|---|
| 239 |     // outside is outside
 | 
|---|
| 240 |     CPPUNIT_ASSERT( Notpredicate(outside) );
 | 
|---|
| 241 |     domain.setCondition(0, BoundaryConditions::Wrap);
 | 
|---|
| 242 |   }
 | 
|---|
| 243 | 
 | 
|---|
| 244 |   // set BCs back to default Ignore
 | 
|---|
| 245 |   domain.setConditions(
 | 
|---|
| 246 |       BoundaryConditions::Conditions_t(3, BoundaryConditions::Ignore)
 | 
|---|
| 247 |   );
 | 
|---|
| 248 |         // check on not void
 | 
|---|
| 249 | #ifndef NDEBUG
 | 
|---|
| 250 |   std::cout << "The following Assertion is intended and does not represent a failure of the test." << std::endl;
 | 
|---|
| 251 |   CPPUNIT_ASSERT_THROW( Notpredicate2( Vector(-2.,0.,0.)), Assert::AssertionFailure );
 | 
|---|
| 252 | #else
 | 
|---|
| 253 |   CPPUNIT_ASSERT( !Notpredicate2( Vector(-2.,0.,0.)) );
 | 
|---|
| 254 | #endif
 | 
|---|
| 255 |   for (double i = 0; i < 5.; ++i) {
 | 
|---|
| 256 |     CPPUNIT_ASSERT( Notpredicate2( Vector(i, 0., 0.)) );
 | 
|---|
| 257 |     CPPUNIT_ASSERT( Notpredicate2( Vector(i, 1., 0.)) );
 | 
|---|
| 258 | #ifndef NDEBUG
 | 
|---|
| 259 |   std::cout << "The following Assertion is intended and does not represent a failure of the test." << std::endl;
 | 
|---|
| 260 |   CPPUNIT_ASSERT_THROW( Notpredicate2( Vector(i, -1., 0.)), Assert::AssertionFailure );
 | 
|---|
| 261 | #else
 | 
|---|
| 262 |   CPPUNIT_ASSERT( Notpredicate2( Vector(i, -1., 0.)) );
 | 
|---|
| 263 | #endif
 | 
|---|
| 264 |     CPPUNIT_ASSERT( Notpredicate2( Vector(i, 0., 1.)) );
 | 
|---|
| 265 | #ifndef NDEBUG
 | 
|---|
| 266 |   std::cout << "The following Assertion is intended and does not represent a failure of the test." << std::endl;
 | 
|---|
| 267 |   CPPUNIT_ASSERT_THROW( Notpredicate2( Vector(i, 0., -1.)), Assert::AssertionFailure );
 | 
|---|
| 268 | #else
 | 
|---|
| 269 |   CPPUNIT_ASSERT( Notpredicate2( Vector(i, 0., -1.)) );
 | 
|---|
| 270 | #endif
 | 
|---|
| 271 |   }
 | 
|---|
| 272 |   CPPUNIT_ASSERT( !Notpredicate2( Vector(5.,1.,0.)) );
 | 
|---|
| 273 | #ifndef NDEBUG
 | 
|---|
| 274 |   std::cout << "The following Assertion is intended and does not represent a failure of the test." << std::endl;
 | 
|---|
| 275 |   CPPUNIT_ASSERT_THROW( Notpredicate2( Vector(5.,-1.,0.)), Assert::AssertionFailure );
 | 
|---|
| 276 | #else
 | 
|---|
| 277 |   CPPUNIT_ASSERT( !Notpredicate2( Vector(5.,-1.,0.)) );
 | 
|---|
| 278 | #endif
 | 
|---|
| 279 |   CPPUNIT_ASSERT( !Notpredicate2( Vector(5.,0.,1.)) );
 | 
|---|
| 280 | #ifndef NDEBUG
 | 
|---|
| 281 |   std::cout << "The following Assertion is intended and does not represent a failure of the test." << std::endl;
 | 
|---|
| 282 |   CPPUNIT_ASSERT_THROW( Notpredicate2( Vector(5.,0.,-1.)), Assert::AssertionFailure );
 | 
|---|
| 283 | #else
 | 
|---|
| 284 |   CPPUNIT_ASSERT( !Notpredicate2( Vector(5.,0.,-1.)) );
 | 
|---|
| 285 | #endif
 | 
|---|
| 286 |   CPPUNIT_ASSERT( !Notpredicate2( Vector(6.,0.,0.)) );
 | 
|---|
| 287 | }
 | 
|---|