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