~ubuntu-branches/ubuntu/gutsy/poco/gutsy

« back to all changes in this revision

Viewing changes to Foundation/testsuite/src/BasicEventTest.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Krzysztof Burghardt
  • Date: 2007-04-27 18:33:48 UTC
  • Revision ID: james.westby@ubuntu.com-20070427183348-xgnpct0qd6a2ip34
Tags: upstream-1.2.9
ImportĀ upstreamĀ versionĀ 1.2.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// BasicEventTest.cpp
 
3
//
 
4
// $Id: //poco/1.2/Foundation/testsuite/src/BasicEventTest.cpp#2 $
 
5
//
 
6
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
 
7
// and Contributors.
 
8
//
 
9
// Permission is hereby granted, free of charge, to any person or organization
 
10
// obtaining a copy of the software and accompanying documentation covered by
 
11
// this license (the "Software") to use, reproduce, display, distribute,
 
12
// execute, and transmit the Software, and to prepare derivative works of the
 
13
// Software, and to permit third-parties to whom the Software is furnished to
 
14
// do so, all subject to the following:
 
15
// 
 
16
// The copyright notices in the Software and this entire statement, including
 
17
// the above license grant, this restriction and the following disclaimer,
 
18
// must be included in all copies of the Software, in whole or in part, and
 
19
// all derivative works of the Software, unless such copies or derivative
 
20
// works are solely in the form of machine-executable object code generated by
 
21
// a source language processor.
 
22
// 
 
23
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
24
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
25
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
 
26
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
 
27
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
 
28
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 
29
// DEALINGS IN THE SOFTWARE.
 
30
//
 
31
 
 
32
 
 
33
#include "BasicEventTest.h"
 
34
#include "DummyDelegate.h"
 
35
#include "CppUnit/TestCaller.h"
 
36
#include "CppUnit/TestSuite.h"
 
37
#include "Poco/Delegate.h"
 
38
#include "Poco/Expire.h"
 
39
#include "Poco/Thread.h"
 
40
#include "Poco/Exception.h"
 
41
 
 
42
 
 
43
using namespace Poco;
 
44
 
 
45
 
 
46
#define LARGEINC 100
 
47
 
 
48
 
 
49
BasicEventTest::BasicEventTest(const std::string& name): CppUnit::TestCase(name)
 
50
{
 
51
}
 
52
 
 
53
 
 
54
BasicEventTest::~BasicEventTest()
 
55
{
 
56
}
 
57
 
 
58
void BasicEventTest::testNoDelegate()
 
59
{
 
60
        int tmp = 0;
 
61
        EventArgs args;
 
62
 
 
63
        assert (_count == 0);
 
64
        Simple.notify(this, tmp);
 
65
        assert (_count == 0);
 
66
 
 
67
        Simple += Delegate<BasicEventTest, int>(this, &BasicEventTest::onSimple);
 
68
        Simple -= Delegate<BasicEventTest, int>(this, &BasicEventTest::onSimple);
 
69
        Simple.notify(this, tmp);
 
70
        assert (_count == 0);
 
71
        
 
72
        ConstSimple += Delegate<BasicEventTest, const int>(this, &BasicEventTest::onConstSimple);
 
73
        ConstSimple -= Delegate<BasicEventTest, const int>(this, &BasicEventTest::onConstSimple);
 
74
        ConstSimple.notify(this, tmp);
 
75
        assert (_count == 0);
 
76
        
 
77
        //Note: passing &args will not work due to &
 
78
        EventArgs* pArgs = &args;
 
79
        Complex += Delegate<BasicEventTest, Poco::EventArgs*>(this, &BasicEventTest::onComplex);
 
80
        Complex -= Delegate<BasicEventTest, Poco::EventArgs*>(this, &BasicEventTest::onComplex);
 
81
        Complex.notify(this, pArgs);
 
82
        assert (_count == 0);
 
83
 
 
84
        Complex2 += Delegate<BasicEventTest, Poco::EventArgs>(this, &BasicEventTest::onComplex2);
 
85
        Complex2 -= Delegate<BasicEventTest, Poco::EventArgs>(this, &BasicEventTest::onComplex2);
 
86
        Complex2.notify(this, args);
 
87
        assert (_count == 0);
 
88
 
 
89
        const EventArgs* pCArgs = &args;
 
90
        ConstComplex += Delegate<BasicEventTest, const Poco::EventArgs*>(this, &BasicEventTest::onConstComplex);
 
91
        ConstComplex -= Delegate<BasicEventTest, const Poco::EventArgs*>(this, &BasicEventTest::onConstComplex);
 
92
        ConstComplex.notify(this, pCArgs);
 
93
        assert (_count == 0);
 
94
 
 
95
        Const2Complex += Delegate<BasicEventTest, const Poco::EventArgs* const>(this, &BasicEventTest::onConst2Complex);
 
96
        Const2Complex -= Delegate<BasicEventTest, const Poco::EventArgs* const>(this, &BasicEventTest::onConst2Complex);
 
97
        Const2Complex.notify(this, pArgs);
 
98
        assert (_count == 0);
 
99
}
 
100
 
 
101
void BasicEventTest::testSingleDelegate()
 
102
{
 
103
        int tmp = 0;
 
104
        EventArgs args;
 
105
 
 
106
        assert (_count == 0);
 
107
 
 
108
        Simple += Delegate<BasicEventTest, int>(this, &BasicEventTest::onSimple);
 
109
        Simple.notify(this, tmp);
 
110
        assert (_count == 1);
 
111
        
 
112
        ConstSimple += Delegate<BasicEventTest, const int>(this, &BasicEventTest::onConstSimple);
 
113
        ConstSimple.notify(this, tmp);
 
114
        assert (_count == 2);
 
115
        
 
116
        EventArgs* pArgs = &args;
 
117
        Complex += Delegate<BasicEventTest, Poco::EventArgs*>(this, &BasicEventTest::onComplex);
 
118
        Complex.notify(this, pArgs);
 
119
        assert (_count == 3);
 
120
 
 
121
        Complex2 += Delegate<BasicEventTest, Poco::EventArgs>(this, &BasicEventTest::onComplex2);
 
122
        Complex2.notify(this, args);
 
123
        assert (_count == 4);
 
124
 
 
125
        const EventArgs* pCArgs = &args;
 
126
        ConstComplex += Delegate<BasicEventTest, const Poco::EventArgs*>(this, &BasicEventTest::onConstComplex);
 
127
        ConstComplex.notify(this, pCArgs);
 
128
        assert (_count == 5);
 
129
 
 
130
        Const2Complex += Delegate<BasicEventTest, const Poco::EventArgs* const>(this, &BasicEventTest::onConst2Complex);
 
131
        Const2Complex.notify(this, pArgs);
 
132
        assert (_count == 6);
 
133
        // check if 2nd notify also works
 
134
        Const2Complex.notify(this, pArgs);
 
135
        assert (_count == 7);
 
136
        
 
137
}
 
138
 
 
139
void BasicEventTest::testDuplicateRegister()
 
140
{
 
141
        int tmp = 0;
 
142
        
 
143
        assert (_count == 0);
 
144
 
 
145
        Simple += Delegate<BasicEventTest, int>(this, &BasicEventTest::onSimple);
 
146
        Simple += Delegate<BasicEventTest, int>(this, &BasicEventTest::onSimple);
 
147
        Simple.notify(this, tmp);
 
148
        assert (_count == 1);
 
149
        Simple -= Delegate<BasicEventTest, int>(this, &BasicEventTest::onSimple);
 
150
        Simple.notify(this, tmp);
 
151
        assert (_count == 1);
 
152
}
 
153
 
 
154
void BasicEventTest::testDuplicateUnregister()
 
155
{
 
156
        // duplicate unregister shouldn't give an error,
 
157
        int tmp = 0;
 
158
        
 
159
        assert (_count == 0);
 
160
 
 
161
        Simple -= Delegate<BasicEventTest, int>(this, &BasicEventTest::onSimple); // should work
 
162
        Simple.notify(this, tmp);
 
163
        assert (_count == 0);
 
164
 
 
165
        Simple += Delegate<BasicEventTest, int>(this, &BasicEventTest::onSimple);
 
166
        Simple.notify(this, tmp);
 
167
        assert (_count == 1);
 
168
 
 
169
        Simple -= Delegate<BasicEventTest, int>(this, &BasicEventTest::onSimple);
 
170
        Simple.notify(this, tmp);
 
171
        assert (_count == 1);
 
172
 
 
173
        Simple -= Delegate<BasicEventTest, int>(this, &BasicEventTest::onSimple);
 
174
        Simple.notify(this, tmp);
 
175
        assert (_count == 1);
 
176
}
 
177
 
 
178
void BasicEventTest::testDisabling()
 
179
{
 
180
        int tmp = 0;
 
181
        
 
182
        assert (_count == 0);
 
183
 
 
184
        Simple += Delegate<BasicEventTest, int>(this, &BasicEventTest::onSimple);
 
185
        Simple.disable();
 
186
        Simple.notify(this, tmp);
 
187
        assert (_count == 0);
 
188
        Simple.enable();
 
189
        Simple.notify(this, tmp);
 
190
        assert (_count == 1);
 
191
 
 
192
        // unregister should also work with disabled event
 
193
        Simple.disable();
 
194
        Simple -= Delegate<BasicEventTest, int>(this, &BasicEventTest::onSimple);
 
195
        Simple.enable();
 
196
        Simple.notify(this, tmp);
 
197
        assert (_count == 1);
 
198
}
 
199
 
 
200
void BasicEventTest::testExpire()
 
201
{
 
202
        int tmp = 0;
 
203
        
 
204
        assert (_count == 0);
 
205
 
 
206
        Simple += Expire<int>(Delegate<BasicEventTest, int>(this, &BasicEventTest::onSimple), 500);
 
207
        Simple.notify(this, tmp);
 
208
        assert (_count == 1);
 
209
        Poco::Thread::sleep(700);
 
210
        Simple.notify(this, tmp);
 
211
        assert (_count == 1);
 
212
}
 
213
 
 
214
void BasicEventTest::testExpireReRegister()
 
215
{
 
216
        int tmp = 0;
 
217
        
 
218
        assert (_count == 0);
 
219
 
 
220
        Simple += Expire<int>(Delegate<BasicEventTest, int>(this, &BasicEventTest::onSimple), 500);
 
221
        Simple.notify(this, tmp);
 
222
        assert (_count == 1);
 
223
        Poco::Thread::sleep(200);
 
224
        Simple.notify(this, tmp);
 
225
        assert (_count == 2);
 
226
        // renew registration
 
227
        Simple += Expire<int>(Delegate<BasicEventTest, int>(this, &BasicEventTest::onSimple), 600);
 
228
        Poco::Thread::sleep(400);
 
229
        Simple.notify(this, tmp);
 
230
        assert (_count == 3);
 
231
        Poco::Thread::sleep(300);
 
232
        Simple.notify(this, tmp);
 
233
        assert (_count == 3);
 
234
}
 
235
 
 
236
void BasicEventTest::testReturnParams()
 
237
{
 
238
        DummyDelegate o1;
 
239
        Simple += Delegate<DummyDelegate, int>(&o1, &DummyDelegate::onSimple);
 
240
 
 
241
        int tmp = 0;
 
242
        Simple.notify(this, tmp);
 
243
        assert (tmp == 1);
 
244
}
 
245
 
 
246
void BasicEventTest::testOverwriteDelegate()
 
247
{
 
248
        DummyDelegate o1;
 
249
        Simple += Delegate<DummyDelegate, int>(&o1, &DummyDelegate::onSimple2);
 
250
        // o1 can only have one entry, thus the next line will replace the entry
 
251
        Simple += Delegate<DummyDelegate, int>(&o1, &DummyDelegate::onSimple);
 
252
 
 
253
        int tmp = 0; // onsimple requires 0 as input
 
254
        Simple.notify(this, tmp);
 
255
        assert (tmp == 1);
 
256
        // now overwrite with onsimple2 with requires as input tmp = 1
 
257
        Simple += Expire<int>(Delegate<DummyDelegate, int>(&o1, &DummyDelegate::onSimple2), 23000);
 
258
        Simple.notify(this, tmp);
 
259
        assert (tmp == 2);
 
260
}
 
261
 
 
262
void BasicEventTest::testAsyncNotify()
 
263
{
 
264
        Poco::BasicEvent<int>* pSimple= new Poco::BasicEvent<int>();
 
265
        (*pSimple) += Delegate<BasicEventTest, int>(this, &BasicEventTest::onAsync);
 
266
        assert (_count == 0);
 
267
        int tmp = 0;
 
268
        Poco::ActiveResult<int>retArg = pSimple->notifyAsync(this, tmp);
 
269
        delete pSimple; // must work even when the event got deleted!
 
270
        pSimple = NULL;
 
271
        assert (_count == 0);
 
272
        retArg.wait();
 
273
        assert (retArg.data() == tmp);
 
274
        assert (_count == LARGEINC);
 
275
}
 
276
 
 
277
void BasicEventTest::onSimple(const void* pSender, int& i)
 
278
{
 
279
        _count++;
 
280
}
 
281
 
 
282
void BasicEventTest::onSimpleOther(const void* pSender, int& i)
 
283
{
 
284
        _count+=100;
 
285
}
 
286
 
 
287
void BasicEventTest::onConstSimple(const void* pSender, const int& i)
 
288
{
 
289
        _count++;
 
290
}
 
291
 
 
292
void BasicEventTest::onComplex(const void* pSender, Poco::EventArgs* & i)
 
293
{
 
294
        _count++;
 
295
}
 
296
 
 
297
void BasicEventTest::onComplex2(const void* pSender, Poco::EventArgs & i)
 
298
{
 
299
        _count++;
 
300
}
 
301
 
 
302
void BasicEventTest::onConstComplex(const void* pSender, const Poco::EventArgs*& i)
 
303
{
 
304
        _count++;
 
305
}
 
306
 
 
307
void BasicEventTest::onConst2Complex(const void* pSender, const Poco::EventArgs * const & i)
 
308
{
 
309
        _count++;
 
310
}
 
311
 
 
312
void BasicEventTest::onAsync(const void* pSender, int& i)
 
313
{
 
314
        Poco::Thread::sleep(700);
 
315
        _count += LARGEINC ;
 
316
}
 
317
 
 
318
int BasicEventTest::getCount() const
 
319
{
 
320
        return _count;
 
321
}
 
322
 
 
323
void BasicEventTest::setUp()
 
324
{
 
325
        _count = 0;
 
326
        // must clear events, otherwise repeating test executions will fail
 
327
        // because tests are only created once, only setup is called before 
 
328
        // each test run
 
329
        Simple.clear();
 
330
        ConstSimple.clear();
 
331
        Complex.clear();
 
332
        Complex2.clear();
 
333
        ConstComplex.clear();
 
334
        Const2Complex.clear();
 
335
}
 
336
 
 
337
 
 
338
void BasicEventTest::tearDown()
 
339
{
 
340
}
 
341
 
 
342
 
 
343
CppUnit::Test* BasicEventTest::suite()
 
344
{
 
345
        CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("BasicEventTest");
 
346
 
 
347
        CppUnit_addTest(pSuite, BasicEventTest, testNoDelegate);
 
348
        CppUnit_addTest(pSuite, BasicEventTest, testSingleDelegate);
 
349
        CppUnit_addTest(pSuite, BasicEventTest, testReturnParams);
 
350
        CppUnit_addTest(pSuite, BasicEventTest, testDuplicateRegister);
 
351
        CppUnit_addTest(pSuite, BasicEventTest, testDuplicateUnregister);
 
352
        CppUnit_addTest(pSuite, BasicEventTest, testDisabling);
 
353
        CppUnit_addTest(pSuite, BasicEventTest, testExpire);
 
354
        CppUnit_addTest(pSuite, BasicEventTest, testExpireReRegister);
 
355
        CppUnit_addTest(pSuite, BasicEventTest, testOverwriteDelegate);
 
356
        CppUnit_addTest(pSuite, BasicEventTest, testAsyncNotify);
 
357
        return pSuite;
 
358
}