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 | * AtomObserverUnitTest.cpp
|
---|
10 | *
|
---|
11 | * Created on: Dec 19, 2011
|
---|
12 | * Author: heber
|
---|
13 | */
|
---|
14 |
|
---|
15 | // include config.h
|
---|
16 | #ifdef HAVE_CONFIG_H
|
---|
17 | #include <config.h>
|
---|
18 | #endif
|
---|
19 |
|
---|
20 | #include "AtomObserverUnitTest.hpp"
|
---|
21 |
|
---|
22 | #include <cppunit/CompilerOutputter.h>
|
---|
23 | #include <cppunit/extensions/TestFactoryRegistry.h>
|
---|
24 | #include <cppunit/ui/text/TestRunner.h>
|
---|
25 | #include <iostream>
|
---|
26 |
|
---|
27 | #include "CodePatterns/Log.hpp"
|
---|
28 |
|
---|
29 | #include "World.hpp"
|
---|
30 | #include "Atom/atom.hpp"
|
---|
31 | #include "Atom/AtomObserver.hpp"
|
---|
32 | #include "Element/element.hpp"
|
---|
33 | #include "Element/periodentafel.hpp"
|
---|
34 |
|
---|
35 | #include "stubs/ObserverStub.hpp"
|
---|
36 |
|
---|
37 | #ifdef HAVE_TESTRUNNER
|
---|
38 | #include "UnitTestMain.hpp"
|
---|
39 | #endif /*HAVE_TESTRUNNER*/
|
---|
40 |
|
---|
41 | /********************************************** Test classes **************************************/
|
---|
42 | // Registers the fixture into the 'registry'
|
---|
43 | CPPUNIT_TEST_SUITE_REGISTRATION( AtomObserverTest );
|
---|
44 |
|
---|
45 | // set up and tear down
|
---|
46 | void AtomObserverTest::setUp(){
|
---|
47 | World::getInstance();
|
---|
48 | AtomObserver::getInstance();
|
---|
49 |
|
---|
50 | setVerbosity(3);
|
---|
51 |
|
---|
52 | // observer checks that we don't receive general updates ...
|
---|
53 | observer = new UpdateCountObserver;
|
---|
54 | // but only specific notifications in observer1 and observer2
|
---|
55 | observer1 = new NotificationObserver(
|
---|
56 | AtomObserver::getInstance().getChannel(AtomObservable::PositionChanged)
|
---|
57 | );
|
---|
58 | observer2 = new NotificationObserver(
|
---|
59 | AtomObserver::getInstance().getChannel(AtomObservable::ElementChanged)
|
---|
60 | );
|
---|
61 |
|
---|
62 | AtomObserver::getInstance().signOn(observer);
|
---|
63 | AtomObserver::getInstance().signOn(observer1, AtomObservable::PositionChanged);
|
---|
64 | AtomObserver::getInstance().signOn(observer2, AtomObservable::ElementChanged);
|
---|
65 |
|
---|
66 | for(int i=0;i<ATOM_COUNT;++i){
|
---|
67 | atoms[i]= World::getInstance().createAtom();
|
---|
68 | atomIds[i]= atoms[i]->getId();
|
---|
69 | }
|
---|
70 | }
|
---|
71 |
|
---|
72 | void AtomObserverTest::tearDown()
|
---|
73 | {
|
---|
74 | AtomObserver::getInstance().signOff(observer);
|
---|
75 | AtomObserver::getInstance().signOff(observer1, AtomObservable::PositionChanged);
|
---|
76 | AtomObserver::getInstance().signOff(observer2, AtomObservable::ElementChanged);
|
---|
77 |
|
---|
78 | delete observer;
|
---|
79 | delete observer1;
|
---|
80 | delete observer2;
|
---|
81 |
|
---|
82 | AtomObserver::purgeInstance();
|
---|
83 | World::purgeInstance();
|
---|
84 | }
|
---|
85 |
|
---|
86 | /** Unit test on whether adding/removing atoms informs relay and onward.
|
---|
87 | *
|
---|
88 | */
|
---|
89 | void AtomObserverTest::AtomAddTest()
|
---|
90 | {
|
---|
91 | // check for zero update
|
---|
92 | CPPUNIT_ASSERT_EQUAL( (int)0, observer->updates );
|
---|
93 | CPPUNIT_ASSERT_EQUAL( false, observer1->wasNotified );
|
---|
94 | CPPUNIT_ASSERT_EQUAL( false, observer2->wasNotified );
|
---|
95 | }
|
---|
96 |
|
---|
97 | /** Unit test on whether moving atoms informs relay and onward.
|
---|
98 | *
|
---|
99 | */
|
---|
100 | void AtomObserverTest::AtomMoveTest()
|
---|
101 | {
|
---|
102 | // move an atom
|
---|
103 | atoms[0]->setPosition(Vector(1.,0.,0.));
|
---|
104 |
|
---|
105 | // check for update
|
---|
106 | CPPUNIT_ASSERT_EQUAL( true, observer1->wasNotified );
|
---|
107 | CPPUNIT_ASSERT_EQUAL( false, observer2->wasNotified );
|
---|
108 | CPPUNIT_ASSERT_EQUAL( (int)0, observer->updates );
|
---|
109 | }
|
---|
110 |
|
---|
111 | /** Unit test on whether changing atom's element informs relay and onward.
|
---|
112 | *
|
---|
113 | */
|
---|
114 | void AtomObserverTest::AtomElementTest()
|
---|
115 | {
|
---|
116 | atoms[0]->setType(1);
|
---|
117 |
|
---|
118 | // check for update
|
---|
119 | CPPUNIT_ASSERT_EQUAL( false, observer1->wasNotified );
|
---|
120 | CPPUNIT_ASSERT_EQUAL( true, observer2->wasNotified );
|
---|
121 | CPPUNIT_ASSERT_EQUAL( (int)0, observer->updates );
|
---|
122 | }
|
---|