Junxiao Shi | 1a2a856 | 2014-01-23 10:07:59 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (C) 2014 Named Data Networking Project |
| 4 | * See COPYING for copyright and distribution information. |
| 5 | */ |
| 6 | |
Junxiao Shi | 7f02c1e | 2014-01-29 22:57:01 -0700 | [diff] [blame] | 7 | #include "core/event-emitter.hpp" |
Alexander Afanasyev | b927a3a | 2014-01-24 10:41:47 -0800 | [diff] [blame] | 8 | |
Junxiao Shi | 7f02c1e | 2014-01-29 22:57:01 -0700 | [diff] [blame] | 9 | #include <boost/test/unit_test.hpp> |
Alexander Afanasyev | b927a3a | 2014-01-24 10:41:47 -0800 | [diff] [blame] | 10 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 11 | namespace nfd { |
Junxiao Shi | 1a2a856 | 2014-01-23 10:07:59 -0700 | [diff] [blame] | 12 | |
Alexander Afanasyev | b927a3a | 2014-01-24 10:41:47 -0800 | [diff] [blame] | 13 | class EventEmitterTester : noncopyable |
Junxiao Shi | 1a2a856 | 2014-01-23 10:07:59 -0700 | [diff] [blame] | 14 | { |
| 15 | public: |
| 16 | EventEmitterTester(); |
| 17 | |
| 18 | int m_hit; |
| 19 | int m_a1; |
| 20 | int m_a2; |
| 21 | int m_a3; |
| 22 | int m_a4; |
| 23 | |
| 24 | void |
| 25 | clear(); |
| 26 | |
| 27 | void |
| 28 | f0(); |
| 29 | |
| 30 | void |
| 31 | f1(int a1); |
| 32 | |
| 33 | void |
| 34 | f2(int a1, int a2); |
| 35 | |
| 36 | void |
| 37 | f3(int a1, int a2, int a3); |
| 38 | |
| 39 | void |
| 40 | f4(int a1, int a2, int a3, int a4); |
| 41 | }; |
| 42 | |
| 43 | EventEmitterTester::EventEmitterTester() |
| 44 | { |
| 45 | this->clear(); |
| 46 | } |
| 47 | |
| 48 | void |
| 49 | EventEmitterTester::clear() |
| 50 | { |
| 51 | m_hit = 0; |
| 52 | m_a1 = 0; |
| 53 | m_a2 = 0; |
| 54 | m_a3 = 0; |
| 55 | m_a4 = 0; |
| 56 | } |
| 57 | |
| 58 | void |
| 59 | EventEmitterTester::f0() |
| 60 | { |
| 61 | ++m_hit; |
| 62 | } |
| 63 | |
| 64 | void |
| 65 | EventEmitterTester::f1(int a1) |
| 66 | { |
| 67 | ++m_hit; |
| 68 | m_a1 = a1; |
| 69 | } |
| 70 | |
| 71 | void |
| 72 | EventEmitterTester::f2(int a1, int a2) |
| 73 | { |
| 74 | ++m_hit; |
| 75 | m_a1 = a1; |
| 76 | m_a2 = a2; |
| 77 | } |
| 78 | |
| 79 | void |
| 80 | EventEmitterTester::f3(int a1, int a2, int a3) |
| 81 | { |
| 82 | ++m_hit; |
| 83 | m_a1 = a1; |
| 84 | m_a2 = a2; |
| 85 | m_a3 = a3; |
| 86 | } |
| 87 | |
| 88 | void |
| 89 | EventEmitterTester::f4(int a1, int a2, int a3, int a4) |
| 90 | { |
| 91 | ++m_hit; |
| 92 | m_a1 = a1; |
| 93 | m_a2 = a2; |
| 94 | m_a3 = a3; |
| 95 | m_a4 = a4; |
| 96 | } |
| 97 | |
| 98 | static int g_EventEmitterTest_RefObject_copyCount; |
| 99 | |
| 100 | class EventEmitterTest_RefObject |
| 101 | { |
| 102 | public: |
| 103 | EventEmitterTest_RefObject() {} |
| 104 | |
| 105 | EventEmitterTest_RefObject(const EventEmitterTest_RefObject& other); |
| 106 | }; |
| 107 | |
| 108 | EventEmitterTest_RefObject::EventEmitterTest_RefObject(const EventEmitterTest_RefObject& other) |
| 109 | { |
| 110 | ++g_EventEmitterTest_RefObject_copyCount; |
| 111 | } |
| 112 | |
| 113 | void |
| 114 | EventEmitterTest_RefObject_byVal(EventEmitterTest_RefObject a1) {} |
| 115 | |
| 116 | void |
| 117 | EventEmitterTest_RefObject_byRef(const EventEmitterTest_RefObject& a1) {} |
| 118 | |
| 119 | |
| 120 | BOOST_AUTO_TEST_SUITE(UtilEventEmitter) |
| 121 | |
| 122 | BOOST_AUTO_TEST_CASE(ZeroListener) |
| 123 | { |
| 124 | EventEmitter<> ee; |
| 125 | BOOST_CHECK_NO_THROW(ee()); |
| 126 | } |
| 127 | |
| 128 | BOOST_AUTO_TEST_CASE(TwoListeners) |
| 129 | { |
| 130 | EventEmitterTester eet1; |
| 131 | EventEmitterTester eet2; |
| 132 | EventEmitter<> ee; |
Alexander Afanasyev | b927a3a | 2014-01-24 10:41:47 -0800 | [diff] [blame] | 133 | ee += bind(&EventEmitterTester::f0, &eet1); |
| 134 | ee += bind(&EventEmitterTester::f0, &eet2); |
Junxiao Shi | 1a2a856 | 2014-01-23 10:07:59 -0700 | [diff] [blame] | 135 | ee(); |
| 136 | |
| 137 | BOOST_CHECK_EQUAL(eet1.m_hit, 1); |
| 138 | BOOST_CHECK_EQUAL(eet2.m_hit, 1); |
| 139 | } |
| 140 | |
| 141 | BOOST_AUTO_TEST_CASE(ZeroArgument) |
| 142 | { |
| 143 | EventEmitterTester eet; |
| 144 | EventEmitter<> ee; |
Alexander Afanasyev | b927a3a | 2014-01-24 10:41:47 -0800 | [diff] [blame] | 145 | ee += bind(&EventEmitterTester::f0, &eet); |
Junxiao Shi | 1a2a856 | 2014-01-23 10:07:59 -0700 | [diff] [blame] | 146 | ee(); |
| 147 | |
| 148 | BOOST_CHECK_EQUAL(eet.m_hit, 1); |
| 149 | } |
| 150 | |
| 151 | BOOST_AUTO_TEST_CASE(OneArgument) |
| 152 | { |
| 153 | EventEmitterTester eet; |
| 154 | EventEmitter<int> ee; |
Alexander Afanasyev | b927a3a | 2014-01-24 10:41:47 -0800 | [diff] [blame] | 155 | ee += bind(&EventEmitterTester::f1, &eet, _1); |
Junxiao Shi | 1a2a856 | 2014-01-23 10:07:59 -0700 | [diff] [blame] | 156 | ee(11); |
| 157 | |
| 158 | BOOST_CHECK_EQUAL(eet.m_hit, 1); |
| 159 | BOOST_CHECK_EQUAL(eet.m_a1, 11); |
| 160 | } |
| 161 | |
| 162 | BOOST_AUTO_TEST_CASE(TwoArguments) |
| 163 | { |
| 164 | EventEmitterTester eet; |
| 165 | EventEmitter<int,int> ee; |
Alexander Afanasyev | b927a3a | 2014-01-24 10:41:47 -0800 | [diff] [blame] | 166 | ee += bind(&EventEmitterTester::f2, &eet, _1, _2); |
Junxiao Shi | 1a2a856 | 2014-01-23 10:07:59 -0700 | [diff] [blame] | 167 | ee(21, 22); |
| 168 | |
| 169 | BOOST_CHECK_EQUAL(eet.m_hit, 1); |
| 170 | BOOST_CHECK_EQUAL(eet.m_a1, 21); |
| 171 | BOOST_CHECK_EQUAL(eet.m_a2, 22); |
| 172 | } |
| 173 | |
| 174 | BOOST_AUTO_TEST_CASE(ThreeArguments) |
| 175 | { |
| 176 | EventEmitterTester eet; |
| 177 | EventEmitter<int,int,int> ee; |
Alexander Afanasyev | b927a3a | 2014-01-24 10:41:47 -0800 | [diff] [blame] | 178 | ee += bind(&EventEmitterTester::f3, &eet, _1, _2, _3); |
Junxiao Shi | 1a2a856 | 2014-01-23 10:07:59 -0700 | [diff] [blame] | 179 | ee(31, 32, 33); |
| 180 | |
| 181 | BOOST_CHECK_EQUAL(eet.m_hit, 1); |
| 182 | BOOST_CHECK_EQUAL(eet.m_a1, 31); |
| 183 | BOOST_CHECK_EQUAL(eet.m_a2, 32); |
| 184 | BOOST_CHECK_EQUAL(eet.m_a3, 33); |
| 185 | } |
| 186 | |
| 187 | BOOST_AUTO_TEST_CASE(FourArguments) |
| 188 | { |
| 189 | EventEmitterTester eet; |
| 190 | EventEmitter<int,int,int,int> ee; |
Alexander Afanasyev | b927a3a | 2014-01-24 10:41:47 -0800 | [diff] [blame] | 191 | ee += bind(&EventEmitterTester::f4, &eet, _1, _2, _3, _4); |
Junxiao Shi | 1a2a856 | 2014-01-23 10:07:59 -0700 | [diff] [blame] | 192 | ee(41, 42, 43, 44); |
| 193 | |
| 194 | BOOST_CHECK_EQUAL(eet.m_hit, 1); |
| 195 | BOOST_CHECK_EQUAL(eet.m_a1, 41); |
| 196 | BOOST_CHECK_EQUAL(eet.m_a2, 42); |
| 197 | BOOST_CHECK_EQUAL(eet.m_a3, 43); |
| 198 | BOOST_CHECK_EQUAL(eet.m_a4, 44); |
| 199 | } |
| 200 | |
| 201 | // EventEmitter passes arguments by reference, |
| 202 | // but it also allows a handler that accept arguments by value |
| 203 | BOOST_AUTO_TEST_CASE(HandlerByVal) |
| 204 | { |
| 205 | EventEmitterTest_RefObject refObject; |
| 206 | g_EventEmitterTest_RefObject_copyCount = 0; |
| 207 | |
| 208 | EventEmitter<EventEmitterTest_RefObject> ee; |
| 209 | ee += &EventEmitterTest_RefObject_byVal; |
| 210 | ee(refObject); |
| 211 | |
| 212 | BOOST_CHECK_EQUAL(g_EventEmitterTest_RefObject_copyCount, 1); |
| 213 | } |
| 214 | |
| 215 | // EventEmitter passes arguments by reference, and no copying |
| 216 | // is necessary when handler accepts arguments by reference |
| 217 | BOOST_AUTO_TEST_CASE(HandlerByRef) |
| 218 | { |
| 219 | EventEmitterTest_RefObject refObject; |
| 220 | g_EventEmitterTest_RefObject_copyCount = 0; |
| 221 | |
| 222 | EventEmitter<EventEmitterTest_RefObject> ee; |
| 223 | ee += &EventEmitterTest_RefObject_byRef; |
| 224 | ee(refObject); |
| 225 | |
| 226 | BOOST_CHECK_EQUAL(g_EventEmitterTest_RefObject_copyCount, 0); |
| 227 | } |
| 228 | |
| 229 | BOOST_AUTO_TEST_SUITE_END() |
Alexander Afanasyev | b927a3a | 2014-01-24 10:41:47 -0800 | [diff] [blame] | 230 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 231 | } // namespace nfd |