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