| [084729c] | 1 | /* | 
|---|
|  | 2 | * Project: CodePatterns | 
|---|
|  | 3 | * Copyright (C)  2013 Frederik Heber. All rights reserved. | 
|---|
|  | 4 | * Please see the LICENSE file or "Copyright notice" in builder.cpp for details. | 
|---|
|  | 5 | */ | 
|---|
|  | 6 |  | 
|---|
|  | 7 | /* | 
|---|
|  | 8 | * GraveyardUnitTest.cpp | 
|---|
|  | 9 | * | 
|---|
|  | 10 | *  Created on: Sep 05, 2013 | 
|---|
|  | 11 | *      Author: heber | 
|---|
|  | 12 | */ | 
|---|
|  | 13 |  | 
|---|
|  | 14 | // include config.h | 
|---|
|  | 15 | #ifdef HAVE_CONFIG_H | 
|---|
|  | 16 | #include <config.h> | 
|---|
|  | 17 | #endif | 
|---|
|  | 18 |  | 
|---|
|  | 19 | #include <cppunit/CompilerOutputter.h> | 
|---|
|  | 20 | #include <cppunit/extensions/TestFactoryRegistry.h> | 
|---|
|  | 21 | #include <cppunit/ui/text/TestRunner.h> | 
|---|
|  | 22 | #include <set> | 
|---|
|  | 23 |  | 
|---|
|  | 24 | #include "CodePatterns/Assert.hpp" | 
|---|
|  | 25 |  | 
|---|
|  | 26 | #include <iostream> | 
|---|
|  | 27 |  | 
|---|
|  | 28 | #include "stubs/ObserverStub.hpp" | 
|---|
|  | 29 | #include "CodePatterns/Observer/Graveyard.hpp" | 
|---|
|  | 30 | #include "CodePatterns/Observer/Notification.hpp" | 
|---|
|  | 31 | #include "CodePatterns/Observer/ObserverLog.hpp" | 
|---|
|  | 32 |  | 
|---|
|  | 33 | #include "GraveyardUnitTest.hpp" | 
|---|
|  | 34 |  | 
|---|
|  | 35 | #ifdef HAVE_TESTRUNNER | 
|---|
|  | 36 | #include "UnitTestMain.hpp" | 
|---|
|  | 37 | #endif /*HAVE_TESTRUNNER*/ | 
|---|
|  | 38 |  | 
|---|
|  | 39 | // Registers the fixture into the 'registry' | 
|---|
|  | 40 | CPPUNIT_TEST_SUITE_REGISTRATION( GraveyardUnitTest ); | 
|---|
|  | 41 |  | 
|---|
|  | 42 | /******************* Test stubs ************************/ | 
|---|
|  | 43 |  | 
|---|
|  | 44 |  | 
|---|
|  | 45 | /******************* actuall tests ***************/ | 
|---|
|  | 46 |  | 
|---|
|  | 47 | void GraveyardUnitTest::setUp() { | 
|---|
|  | 48 | ASSERT_DO(Assert::Throw); | 
|---|
|  | 49 | #ifdef LOG_OBSERVER | 
|---|
|  | 50 | ObserverLog::getInstance().enableLogging(); | 
|---|
|  | 51 | #endif | 
|---|
|  | 52 |  | 
|---|
|  | 53 | graveyard = new Graveyard; | 
|---|
|  | 54 | observer1 = new UpdateCountObserver(); | 
|---|
|  | 55 | observer2 = new UpdateCountObserver(); | 
|---|
|  | 56 | observer3 = new UpdateCountObserver(); | 
|---|
|  | 57 | observer4 = new UpdateCountObserver(); | 
|---|
|  | 58 | } | 
|---|
|  | 59 |  | 
|---|
|  | 60 | void GraveyardUnitTest::tearDown() { | 
|---|
|  | 61 | delete observer1; | 
|---|
|  | 62 | delete observer2; | 
|---|
|  | 63 | delete observer3; | 
|---|
|  | 64 | delete observer4; | 
|---|
|  | 65 | #ifdef LOG_OBSERVER | 
|---|
|  | 66 | ObserverLog::getInstance().disableLogging(); | 
|---|
|  | 67 | #endif | 
|---|
|  | 68 | delete graveyard; | 
|---|
|  | 69 | } | 
|---|
|  | 70 |  | 
|---|
|  | 71 | void GraveyardUnitTest::Zombie_soloTest() | 
|---|
|  | 72 | { | 
|---|
|  | 73 | observerLog().enableLogging(); | 
|---|
|  | 74 |  | 
|---|
|  | 75 | // test with unobserved Observable | 
|---|
|  | 76 | Observable *simpleObservable1 = new SimpleObservable(); | 
|---|
|  | 77 |  | 
|---|
|  | 78 | graveyard->turnZombie( simpleObservable1); | 
|---|
|  | 79 | CPPUNIT_ASSERT( graveyard->graveyard.size() == 0); | 
|---|
|  | 80 |  | 
|---|
|  | 81 | observerLog().disableLogging(); | 
|---|
|  | 82 | } | 
|---|
|  | 83 |  | 
|---|
|  | 84 | void GraveyardUnitTest::Zombie_oneTest() | 
|---|
|  | 85 | { | 
|---|
|  | 86 | observerLog().enableLogging(); | 
|---|
|  | 87 |  | 
|---|
|  | 88 | // test with one Observer | 
|---|
|  | 89 | SimpleObservable *simpleObservable1 = new SimpleObservable(); | 
|---|
|  | 90 | Observable *_observable = simpleObservable1; | 
|---|
|  | 91 |  | 
|---|
|  | 92 | simpleObservable1->signOn(observer1); | 
|---|
|  | 93 | graveyard->turnZombie(_observable); | 
|---|
|  | 94 | simpleObservable1->signOff(observer1); | 
|---|
|  | 95 | CPPUNIT_ASSERT( graveyard->graveyard.size() == 0); | 
|---|
|  | 96 |  | 
|---|
|  | 97 | observerLog().disableLogging(); | 
|---|
|  | 98 | } | 
|---|
|  | 99 |  | 
|---|
|  | 100 | void GraveyardUnitTest::Zombie_manyTest() | 
|---|
|  | 101 | { | 
|---|
|  | 102 | observerLog().enableLogging(); | 
|---|
|  | 103 |  | 
|---|
|  | 104 | // test with some more Observers | 
|---|
|  | 105 | SimpleObservable *simpleObservable1 = new SimpleObservable(); | 
|---|
|  | 106 | Observable *_observable = simpleObservable1; | 
|---|
|  | 107 |  | 
|---|
|  | 108 | // create some observer noise | 
|---|
|  | 109 | simpleObservable1->signOn(observer1); | 
|---|
|  | 110 | simpleObservable1->signOn(observer2); | 
|---|
|  | 111 | simpleObservable1->signOn(observer3); | 
|---|
|  | 112 | simpleObservable1->signOn(observer4); | 
|---|
|  | 113 |  | 
|---|
|  | 114 | // turn zombie | 
|---|
|  | 115 | graveyard->turnZombie(_observable); | 
|---|
|  | 116 |  | 
|---|
|  | 117 | // remove the noise again (in mixed order) | 
|---|
|  | 118 | CPPUNIT_ASSERT( graveyard->graveyard.size() == 1); | 
|---|
|  | 119 | CPPUNIT_ASSERT_EQUAL( (size_t)4, simpleObservable1->getNumberOfObservers()); | 
|---|
|  | 120 | simpleObservable1->signOff(observer3); | 
|---|
|  | 121 | CPPUNIT_ASSERT( graveyard->graveyard.size() == 1); | 
|---|
|  | 122 | CPPUNIT_ASSERT_EQUAL( (size_t)3, simpleObservable1->getNumberOfObservers()); | 
|---|
|  | 123 | simpleObservable1->signOff(observer2); | 
|---|
|  | 124 | CPPUNIT_ASSERT( graveyard->graveyard.size() == 1); | 
|---|
|  | 125 | CPPUNIT_ASSERT_EQUAL( (size_t)2, simpleObservable1->getNumberOfObservers()); | 
|---|
|  | 126 | simpleObservable1->signOff(observer4); | 
|---|
|  | 127 | CPPUNIT_ASSERT( graveyard->graveyard.size() == 1); | 
|---|
|  | 128 | CPPUNIT_ASSERT_EQUAL( (size_t)1, simpleObservable1->getNumberOfObservers()); | 
|---|
|  | 129 | simpleObservable1->signOff(observer1); | 
|---|
|  | 130 | CPPUNIT_ASSERT( graveyard->graveyard.size() == 0); | 
|---|
|  | 131 | CPPUNIT_ASSERT_EQUAL( (size_t)0, simpleObservable1->getNumberOfObservers()); | 
|---|
|  | 132 |  | 
|---|
|  | 133 | observerLog().disableLogging(); | 
|---|
|  | 134 | } | 
|---|
|  | 135 |  | 
|---|
|  | 136 | void GraveyardUnitTest::Zombie_notificationsTest() | 
|---|
|  | 137 | { | 
|---|
|  | 138 | observerLog().enableLogging(); | 
|---|
|  | 139 |  | 
|---|
|  | 140 | // test with an observable with notifications | 
|---|
|  | 141 | NotificationObservable *notificationObservable = new NotificationObservable(); | 
|---|
|  | 142 | Observable *_observable = notificationObservable; | 
|---|
|  | 143 |  | 
|---|
|  | 144 | // create some observer noise | 
|---|
|  | 145 | notificationObservable->signOn(observer1); | 
|---|
|  | 146 | notificationObservable->signOn(observer2); | 
|---|
|  | 147 | notificationObservable->signOn(observer2, NotificationObservable::Operation1Notify); | 
|---|
|  | 148 | notificationObservable->signOn(observer3, NotificationObservable::Operation1Notify); | 
|---|
|  | 149 | notificationObservable->signOn(observer4, NotificationObservable::Operation2Notify); | 
|---|
|  | 150 |  | 
|---|
|  | 151 | // turn zombie | 
|---|
|  | 152 | graveyard->turnZombie(_observable); | 
|---|
|  | 153 |  | 
|---|
|  | 154 | // remove the noise again (in mixed order) | 
|---|
|  | 155 | CPPUNIT_ASSERT( graveyard->graveyard.size() == 1); | 
|---|
|  | 156 | CPPUNIT_ASSERT_EQUAL( (size_t)5, notificationObservable->getNumberOfObservers()); | 
|---|
|  | 157 | notificationObservable->signOff(observer3, NotificationObservable::Operation1Notify); | 
|---|
|  | 158 | CPPUNIT_ASSERT( graveyard->graveyard.size() == 1); | 
|---|
|  | 159 | CPPUNIT_ASSERT_EQUAL( (size_t)4, notificationObservable->getNumberOfObservers()); | 
|---|
|  | 160 | notificationObservable->signOff(observer2, NotificationObservable::Operation1Notify); | 
|---|
|  | 161 | CPPUNIT_ASSERT( graveyard->graveyard.size() == 1); | 
|---|
|  | 162 | CPPUNIT_ASSERT_EQUAL( (size_t)3, notificationObservable->getNumberOfObservers()); | 
|---|
|  | 163 | notificationObservable->signOff(observer2); | 
|---|
|  | 164 | CPPUNIT_ASSERT( graveyard->graveyard.size() == 1); | 
|---|
|  | 165 | CPPUNIT_ASSERT_EQUAL( (size_t)2, notificationObservable->getNumberOfObservers()); | 
|---|
|  | 166 | notificationObservable->signOff(observer4, NotificationObservable::Operation2Notify); | 
|---|
|  | 167 | CPPUNIT_ASSERT( graveyard->graveyard.size() == 1); | 
|---|
|  | 168 | CPPUNIT_ASSERT_EQUAL( (size_t)1, notificationObservable->getNumberOfObservers()); | 
|---|
|  | 169 | notificationObservable->signOff(observer1); | 
|---|
|  | 170 | CPPUNIT_ASSERT( graveyard->graveyard.size() == 0); | 
|---|
|  | 171 | CPPUNIT_ASSERT_EQUAL( (size_t)0, notificationObservable->getNumberOfObservers()); | 
|---|
|  | 172 |  | 
|---|
|  | 173 | observerLog().disableLogging(); | 
|---|
|  | 174 | } | 
|---|