Yingdi Yu | fe14015 | 2014-07-24 10:02:40 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Spyridon Mastorakis | 429634f | 2015-02-19 17:35:33 -0800 | [diff] [blame] | 3 | * Copyright (c) 2013-2015 Regents of the University of California, |
| 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology |
Yingdi Yu | fe14015 | 2014-07-24 10:02:40 -0700 | [diff] [blame] | 9 | * |
| 10 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 11 | * |
| 12 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 13 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 14 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 15 | * |
| 16 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 17 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 18 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 19 | * |
| 20 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 21 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 22 | * <http://www.gnu.org/licenses/>. |
| 23 | * |
| 24 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
Yingdi Yu | fe14015 | 2014-07-24 10:02:40 -0700 | [diff] [blame] | 25 | */ |
Yingdi Yu | fe14015 | 2014-07-24 10:02:40 -0700 | [diff] [blame] | 26 | |
| 27 | #include "util/event-emitter.hpp" |
| 28 | |
| 29 | #include "boost-test.hpp" |
| 30 | |
| 31 | namespace ndn { |
Spyridon Mastorakis | 429634f | 2015-02-19 17:35:33 -0800 | [diff] [blame] | 32 | namespace util { |
Yingdi Yu | fe14015 | 2014-07-24 10:02:40 -0700 | [diff] [blame] | 33 | namespace tests { |
| 34 | |
Yingdi Yu | fe14015 | 2014-07-24 10:02:40 -0700 | [diff] [blame] | 35 | BOOST_AUTO_TEST_SUITE(UtilEventEmitter) |
| 36 | |
| 37 | class EventEmitterTester : noncopyable |
| 38 | { |
| 39 | public: |
| 40 | EventEmitterTester(); |
| 41 | |
| 42 | int m_hit; |
| 43 | int m_a1; |
| 44 | int m_a2; |
| 45 | int m_a3; |
| 46 | int m_a4; |
| 47 | |
| 48 | void |
| 49 | clear(); |
| 50 | |
| 51 | void |
| 52 | f0(); |
| 53 | |
| 54 | void |
| 55 | f1(int a1); |
| 56 | |
| 57 | void |
| 58 | f2(int a1, int a2); |
| 59 | |
| 60 | void |
| 61 | f3(int a1, int a2, int a3); |
| 62 | |
| 63 | void |
| 64 | f4(int a1, int a2, int a3, int a4); |
| 65 | }; |
| 66 | |
| 67 | EventEmitterTester::EventEmitterTester() |
| 68 | { |
| 69 | this->clear(); |
| 70 | } |
| 71 | |
| 72 | void |
| 73 | EventEmitterTester::clear() |
| 74 | { |
| 75 | m_hit = 0; |
| 76 | m_a1 = 0; |
| 77 | m_a2 = 0; |
| 78 | m_a3 = 0; |
| 79 | m_a4 = 0; |
| 80 | } |
| 81 | |
| 82 | void |
| 83 | EventEmitterTester::f0() |
| 84 | { |
| 85 | ++m_hit; |
| 86 | } |
| 87 | |
| 88 | void |
| 89 | EventEmitterTester::f1(int a1) |
| 90 | { |
| 91 | ++m_hit; |
| 92 | m_a1 = a1; |
| 93 | } |
| 94 | |
| 95 | void |
| 96 | EventEmitterTester::f2(int a1, int a2) |
| 97 | { |
| 98 | ++m_hit; |
| 99 | m_a1 = a1; |
| 100 | m_a2 = a2; |
| 101 | } |
| 102 | |
| 103 | void |
| 104 | EventEmitterTester::f3(int a1, int a2, int a3) |
| 105 | { |
| 106 | ++m_hit; |
| 107 | m_a1 = a1; |
| 108 | m_a2 = a2; |
| 109 | m_a3 = a3; |
| 110 | } |
| 111 | |
| 112 | void |
| 113 | EventEmitterTester::f4(int a1, int a2, int a3, int a4) |
| 114 | { |
| 115 | ++m_hit; |
| 116 | m_a1 = a1; |
| 117 | m_a2 = a2; |
| 118 | m_a3 = a3; |
| 119 | m_a4 = a4; |
| 120 | } |
| 121 | |
| 122 | static int g_EventEmitterTest_RefObject_copyCount; |
| 123 | |
| 124 | class EventEmitterTest_RefObject |
| 125 | { |
| 126 | public: |
| 127 | EventEmitterTest_RefObject() {} |
| 128 | |
| 129 | EventEmitterTest_RefObject(const EventEmitterTest_RefObject& other); |
| 130 | }; |
| 131 | |
| 132 | EventEmitterTest_RefObject::EventEmitterTest_RefObject(const EventEmitterTest_RefObject& other) |
| 133 | { |
| 134 | ++g_EventEmitterTest_RefObject_copyCount; |
| 135 | } |
| 136 | |
| 137 | void |
| 138 | EventEmitterTest_RefObject_byVal(EventEmitterTest_RefObject a1) {} |
| 139 | |
| 140 | void |
| 141 | EventEmitterTest_RefObject_byRef(const EventEmitterTest_RefObject& a1) {} |
| 142 | |
| 143 | |
| 144 | BOOST_AUTO_TEST_CASE(ZeroListener) |
| 145 | { |
| 146 | EventEmitter<> ee; |
| 147 | BOOST_CHECK_NO_THROW(ee()); |
| 148 | } |
| 149 | |
| 150 | BOOST_AUTO_TEST_CASE(TwoListeners) |
| 151 | { |
| 152 | EventEmitterTester eet1; |
| 153 | EventEmitterTester eet2; |
| 154 | EventEmitter<> ee; |
| 155 | ee += bind(&EventEmitterTester::f0, &eet1); |
| 156 | ee += bind(&EventEmitterTester::f0, &eet2); |
| 157 | ee(); |
| 158 | |
| 159 | BOOST_CHECK_EQUAL(eet1.m_hit, 1); |
| 160 | BOOST_CHECK_EQUAL(eet2.m_hit, 1); |
| 161 | } |
| 162 | |
| 163 | BOOST_AUTO_TEST_CASE(ZeroArgument) |
| 164 | { |
| 165 | EventEmitterTester eet; |
| 166 | EventEmitter<> ee; |
| 167 | ee += bind(&EventEmitterTester::f0, &eet); |
| 168 | ee(); |
| 169 | |
| 170 | BOOST_CHECK_EQUAL(eet.m_hit, 1); |
| 171 | } |
| 172 | |
| 173 | BOOST_AUTO_TEST_CASE(OneArgument) |
| 174 | { |
| 175 | EventEmitterTester eet; |
| 176 | EventEmitter<int> ee; |
| 177 | ee += bind(&EventEmitterTester::f1, &eet, _1); |
| 178 | ee(11); |
| 179 | |
| 180 | BOOST_CHECK_EQUAL(eet.m_hit, 1); |
| 181 | BOOST_CHECK_EQUAL(eet.m_a1, 11); |
| 182 | } |
| 183 | |
| 184 | BOOST_AUTO_TEST_CASE(TwoArguments) |
| 185 | { |
| 186 | EventEmitterTester eet; |
| 187 | EventEmitter<int,int> ee; |
| 188 | ee += bind(&EventEmitterTester::f2, &eet, _1, _2); |
| 189 | ee(21, 22); |
| 190 | |
| 191 | BOOST_CHECK_EQUAL(eet.m_hit, 1); |
| 192 | BOOST_CHECK_EQUAL(eet.m_a1, 21); |
| 193 | BOOST_CHECK_EQUAL(eet.m_a2, 22); |
| 194 | } |
| 195 | |
| 196 | BOOST_AUTO_TEST_CASE(ThreeArguments) |
| 197 | { |
| 198 | EventEmitterTester eet; |
| 199 | EventEmitter<int,int,int> ee; |
| 200 | ee += bind(&EventEmitterTester::f3, &eet, _1, _2, _3); |
| 201 | ee(31, 32, 33); |
| 202 | |
| 203 | BOOST_CHECK_EQUAL(eet.m_hit, 1); |
| 204 | BOOST_CHECK_EQUAL(eet.m_a1, 31); |
| 205 | BOOST_CHECK_EQUAL(eet.m_a2, 32); |
| 206 | BOOST_CHECK_EQUAL(eet.m_a3, 33); |
| 207 | } |
| 208 | |
| 209 | BOOST_AUTO_TEST_CASE(FourArguments) |
| 210 | { |
| 211 | EventEmitterTester eet; |
| 212 | EventEmitter<int,int,int,int> ee; |
| 213 | ee += bind(&EventEmitterTester::f4, &eet, _1, _2, _3, _4); |
| 214 | ee(41, 42, 43, 44); |
| 215 | |
| 216 | BOOST_CHECK_EQUAL(eet.m_hit, 1); |
| 217 | BOOST_CHECK_EQUAL(eet.m_a1, 41); |
| 218 | BOOST_CHECK_EQUAL(eet.m_a2, 42); |
| 219 | BOOST_CHECK_EQUAL(eet.m_a3, 43); |
| 220 | BOOST_CHECK_EQUAL(eet.m_a4, 44); |
| 221 | } |
| 222 | |
| 223 | // EventEmitter passes arguments by reference, |
| 224 | // but it also allows a handler that accept arguments by value |
| 225 | BOOST_AUTO_TEST_CASE(HandlerByVal) |
| 226 | { |
| 227 | EventEmitterTest_RefObject refObject; |
| 228 | g_EventEmitterTest_RefObject_copyCount = 0; |
| 229 | |
| 230 | EventEmitter<EventEmitterTest_RefObject> ee; |
| 231 | ee += &EventEmitterTest_RefObject_byVal; |
| 232 | ee(refObject); |
| 233 | |
| 234 | BOOST_CHECK_EQUAL(g_EventEmitterTest_RefObject_copyCount, 1); |
| 235 | } |
| 236 | |
| 237 | // EventEmitter passes arguments by reference, and no copying |
| 238 | // is necessary when handler accepts arguments by reference |
| 239 | BOOST_AUTO_TEST_CASE(HandlerByRef) |
| 240 | { |
| 241 | EventEmitterTest_RefObject refObject; |
| 242 | g_EventEmitterTest_RefObject_copyCount = 0; |
| 243 | |
| 244 | EventEmitter<EventEmitterTest_RefObject> ee; |
| 245 | ee += &EventEmitterTest_RefObject_byRef; |
| 246 | ee(refObject); |
| 247 | |
| 248 | BOOST_CHECK_EQUAL(g_EventEmitterTest_RefObject_copyCount, 0); |
| 249 | } |
| 250 | |
| 251 | BOOST_AUTO_TEST_SUITE_END() |
| 252 | |
| 253 | } // namespace tests |
Spyridon Mastorakis | 429634f | 2015-02-19 17:35:33 -0800 | [diff] [blame] | 254 | } // namespace util |
Yingdi Yu | fe14015 | 2014-07-24 10:02:40 -0700 | [diff] [blame] | 255 | } // namespace ndn |