| 1 | /*
 | 
|---|
| 2 |  * Project: MoleCuilder
 | 
|---|
| 3 |  * Description: creates and alters molecular systems
 | 
|---|
| 4 |  * Copyright (C)  2012 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 |  * AtomObserverUnitTest.cpp
 | 
|---|
| 26 |  *
 | 
|---|
| 27 |  *  Created on: Dec 19, 2011
 | 
|---|
| 28 |  *      Author: heber
 | 
|---|
| 29 |  */
 | 
|---|
| 30 | 
 | 
|---|
| 31 | // include config.h
 | 
|---|
| 32 | #ifdef HAVE_CONFIG_H
 | 
|---|
| 33 | #include <config.h>
 | 
|---|
| 34 | #endif
 | 
|---|
| 35 | 
 | 
|---|
| 36 | #include "AtomObserverUnitTest.hpp"
 | 
|---|
| 37 | 
 | 
|---|
| 38 | #include <cppunit/CompilerOutputter.h>
 | 
|---|
| 39 | #include <cppunit/extensions/TestFactoryRegistry.h>
 | 
|---|
| 40 | #include <cppunit/ui/text/TestRunner.h>
 | 
|---|
| 41 | #include <iostream>
 | 
|---|
| 42 | 
 | 
|---|
| 43 | #include "CodePatterns/Log.hpp"
 | 
|---|
| 44 | 
 | 
|---|
| 45 | #include "World.hpp"
 | 
|---|
| 46 | #include "Atom/atom.hpp"
 | 
|---|
| 47 | #include "Atom/AtomObserver.hpp"
 | 
|---|
| 48 | #include "Element/element.hpp"
 | 
|---|
| 49 | #include "Element/periodentafel.hpp"
 | 
|---|
| 50 | 
 | 
|---|
| 51 | #include "stubs/ObserverStub.hpp"
 | 
|---|
| 52 | 
 | 
|---|
| 53 | #ifdef HAVE_TESTRUNNER
 | 
|---|
| 54 | #include "UnitTestMain.hpp"
 | 
|---|
| 55 | #endif /*HAVE_TESTRUNNER*/
 | 
|---|
| 56 | 
 | 
|---|
| 57 | /********************************************** Test classes **************************************/
 | 
|---|
| 58 | // Registers the fixture into the 'registry'
 | 
|---|
| 59 | CPPUNIT_TEST_SUITE_REGISTRATION( AtomObserverTest );
 | 
|---|
| 60 | 
 | 
|---|
| 61 | // set up and tear down
 | 
|---|
| 62 | void AtomObserverTest::setUp(){
 | 
|---|
| 63 |   World::getInstance();
 | 
|---|
| 64 |   AtomObserver::getInstance();
 | 
|---|
| 65 | 
 | 
|---|
| 66 |   setVerbosity(3);
 | 
|---|
| 67 | 
 | 
|---|
| 68 |   // observer checks that we don't receive general updates ...
 | 
|---|
| 69 |   observer = new UpdateCountObserver;
 | 
|---|
| 70 |   AtomObserver::getInstance().signOn(observer);
 | 
|---|
| 71 | 
 | 
|---|
| 72 |   // but only specific notifications in others observers
 | 
|---|
| 73 |   for (int i=0;i<AtomObservable::NotificationType_MAX; ++i) {
 | 
|---|
| 74 |     specific_observers.push_back(new NotificationObserver(
 | 
|---|
| 75 |           AtomObserver::getInstance().getChannel((enum AtomObservable::NotificationType)i)
 | 
|---|
| 76 |           ));
 | 
|---|
| 77 |     AtomObserver::getInstance().signOn(
 | 
|---|
| 78 |         specific_observers.back(), (enum AtomObservable::NotificationType)i);
 | 
|---|
| 79 |   }
 | 
|---|
| 80 | 
 | 
|---|
| 81 | 
 | 
|---|
| 82 |   for(int i=0;i<ATOM_COUNT;++i){
 | 
|---|
| 83 |     atoms[i]= World::getInstance().createAtom();
 | 
|---|
| 84 |     atomIds[i]= atoms[i]->getId();
 | 
|---|
| 85 |   }
 | 
|---|
| 86 | }
 | 
|---|
| 87 | 
 | 
|---|
| 88 | void AtomObserverTest::tearDown()
 | 
|---|
| 89 | {
 | 
|---|
| 90 |   AtomObserver::getInstance().signOff(observer);
 | 
|---|
| 91 |   for (int i=0;i<AtomObservable::NotificationType_MAX; ++i) {
 | 
|---|
| 92 |     AtomObserver::getInstance().signOff(specific_observers[i], (enum AtomObservable::NotificationType)i);
 | 
|---|
| 93 |     delete specific_observers[i];
 | 
|---|
| 94 |   }
 | 
|---|
| 95 |   specific_observers.clear();
 | 
|---|
| 96 | 
 | 
|---|
| 97 |   delete observer;
 | 
|---|
| 98 | 
 | 
|---|
| 99 |   World::purgeInstance();
 | 
|---|
| 100 |   AtomObserver::purgeInstance();
 | 
|---|
| 101 | }
 | 
|---|
| 102 | 
 | 
|---|
| 103 | /** Unit test on whether adding/removing atoms informs relay and onward.
 | 
|---|
| 104 |  *
 | 
|---|
| 105 |  */
 | 
|---|
| 106 | void AtomObserverTest::AtomAddTest()
 | 
|---|
| 107 | {
 | 
|---|
| 108 |   // check for zero update
 | 
|---|
| 109 |   CPPUNIT_ASSERT_EQUAL( (int)0, observer->updates );
 | 
|---|
| 110 |   for (int i=0;i<AtomObservable::NotificationType_MAX; ++i) {
 | 
|---|
| 111 | 
 | 
|---|
| 112 |     CPPUNIT_ASSERT_EQUAL( false, specific_observers[i]->wasNotified );
 | 
|---|
| 113 |   }
 | 
|---|
| 114 | }
 | 
|---|
| 115 | 
 | 
|---|
| 116 | /** Unit test on whether moving atoms informs relay and onward.
 | 
|---|
| 117 |  *
 | 
|---|
| 118 |  */
 | 
|---|
| 119 | void AtomObserverTest::AtomMoveTest()
 | 
|---|
| 120 | {
 | 
|---|
| 121 |   // move an atom
 | 
|---|
| 122 |   atoms[0]->setPosition(Vector(1.,0.,0.));
 | 
|---|
| 123 | 
 | 
|---|
| 124 |   // check for update
 | 
|---|
| 125 |   for (int i=0;i<AtomObservable::NotificationType_MAX; ++i) {
 | 
|---|
| 126 |     CPPUNIT_ASSERT_EQUAL( (enum AtomObservable::NotificationType)i == AtomObservable::PositionChanged,
 | 
|---|
| 127 |         specific_observers[i]->wasNotified );
 | 
|---|
| 128 |   }
 | 
|---|
| 129 |   CPPUNIT_ASSERT_EQUAL( (int)0, observer->updates );
 | 
|---|
| 130 | }
 | 
|---|
| 131 | 
 | 
|---|
| 132 | /** Unit test on whether changing atom's element informs relay and onward.
 | 
|---|
| 133 |  *
 | 
|---|
| 134 |  */
 | 
|---|
| 135 | void AtomObserverTest::AtomElementTest()
 | 
|---|
| 136 | {
 | 
|---|
| 137 |   CPPUNIT_ASSERT( atoms[0]->getType()->getAtomicNumber() != 2 );
 | 
|---|
| 138 |   atoms[0]->setType(2);
 | 
|---|
| 139 | 
 | 
|---|
| 140 |   // check for update
 | 
|---|
| 141 |   for (int i=0;i<AtomObservable::NotificationType_MAX; ++i) {
 | 
|---|
| 142 |     CPPUNIT_ASSERT_EQUAL( (enum AtomObservable::NotificationType)i == AtomObservable::ElementChanged,
 | 
|---|
| 143 |         specific_observers[i]->wasNotified );
 | 
|---|
| 144 |   }
 | 
|---|
| 145 |   CPPUNIT_ASSERT_EQUAL( (int)0, observer->updates );
 | 
|---|
| 146 | }
 | 
|---|