📄 priorityeventtest.cpp
字号:
//
// PriorityEventTest.cpp
//
// $Id: //poco/1.3/Foundation/testsuite/src/PriorityEventTest.cpp#1 $
//
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// Permission is hereby granted, free of charge, to any person or organization
// obtaining a copy of the software and accompanying documentation covered by
// this license (the "Software") to use, reproduce, display, distribute,
// execute, and transmit the Software, and to prepare derivative works of the
// Software, and to permit third-parties to whom the Software is furnished to
// do so, all subject to the following:
//
// The copyright notices in the Software and this entire statement, including
// the above license grant, this restriction and the following disclaimer,
// must be included in all copies of the Software, in whole or in part, and
// all derivative works of the Software, unless such copies or derivative
// works are solely in the form of machine-executable object code generated by
// a source language processor.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
#include "PriorityEventTest.h"
#include "DummyDelegate.h"
#include "CppUnit/TestCaller.h"
#include "CppUnit/TestSuite.h"
#include "Poco/PriorityDelegate.h"
#include "Poco/PriorityExpire.h"
#include "Poco/Thread.h"
#include "Poco/Exception.h"
using namespace Poco;
#define LARGEINC 100
PriorityEventTest::PriorityEventTest(const std::string& name): CppUnit::TestCase(name)
{
}
PriorityEventTest::~PriorityEventTest()
{
}
void PriorityEventTest::testNoDelegate()
{
int tmp = 0;
EventArgs args;
assert (_count == 0);
Simple.notify(this, tmp);
assert (_count == 0);
Simple += PriorityDelegate<PriorityEventTest, int>(this, &PriorityEventTest::onSimple, 0);
Simple -= PriorityDelegate<PriorityEventTest, int>(this, &PriorityEventTest::onSimple, 0);
Simple.notify(this, tmp);
assert (_count == 0);
ConstSimple += PriorityDelegate<PriorityEventTest, const int>(this, &PriorityEventTest::onConstSimple, 0);
ConstSimple -= PriorityDelegate<PriorityEventTest, const int>(this, &PriorityEventTest::onConstSimple, 0);
ConstSimple.notify(this, tmp);
assert (_count == 0);
//Note: passing &args will not work due to &
EventArgs* pArgs = &args;
Complex += PriorityDelegate<PriorityEventTest, Poco::EventArgs*>(this, &PriorityEventTest::onComplex, 0);
Complex -= PriorityDelegate<PriorityEventTest, Poco::EventArgs*>(this, &PriorityEventTest::onComplex, 0);
Complex.notify(this, pArgs);
assert (_count == 0);
Complex2 += PriorityDelegate<PriorityEventTest, Poco::EventArgs>(this, &PriorityEventTest::onComplex2, 0);
Complex2 -= PriorityDelegate<PriorityEventTest, Poco::EventArgs>(this, &PriorityEventTest::onComplex2, 0);
Complex2.notify(this, args);
assert (_count == 0);
const EventArgs* pCArgs = &args;
ConstComplex += PriorityDelegate<PriorityEventTest, const Poco::EventArgs*>(this, &PriorityEventTest::onConstComplex, 0);
ConstComplex -= PriorityDelegate<PriorityEventTest, const Poco::EventArgs*>(this, &PriorityEventTest::onConstComplex, 0);
ConstComplex.notify(this, pCArgs);
assert (_count == 0);
Const2Complex += PriorityDelegate<PriorityEventTest, const Poco::EventArgs* const>(this, &PriorityEventTest::onConst2Complex, 0);
Const2Complex -= PriorityDelegate<PriorityEventTest, const Poco::EventArgs* const>(this, &PriorityEventTest::onConst2Complex, 0);
Const2Complex.notify(this, pArgs);
assert (_count == 0);
}
void PriorityEventTest::testSingleDelegate()
{
int tmp = 0;
EventArgs args;
assert (_count == 0);
Simple += PriorityDelegate<PriorityEventTest, int>(this, &PriorityEventTest::onSimple, 0);
// unregistering with a different priority --> different observer, is ignored
Simple -= PriorityDelegate<PriorityEventTest, int>(this, &PriorityEventTest::onSimple, 3);
Simple.notify(this, tmp);
assert (_count == 1);
ConstSimple += PriorityDelegate<PriorityEventTest, const int>(this, &PriorityEventTest::onConstSimple, 0);
ConstSimple -= PriorityDelegate<PriorityEventTest, const int>(this, &PriorityEventTest::onConstSimple, 3);
ConstSimple.notify(this, tmp);
assert (_count == 2);
EventArgs* pArgs = &args;
Complex += PriorityDelegate<PriorityEventTest, Poco::EventArgs*>(this, &PriorityEventTest::onComplex, 0);
Complex -= PriorityDelegate<PriorityEventTest, Poco::EventArgs*>(this, &PriorityEventTest::onComplex, 3);
Complex.notify(this, pArgs);
assert (_count == 3);
Complex2 += PriorityDelegate<PriorityEventTest, Poco::EventArgs>(this, &PriorityEventTest::onComplex2, 0);
Complex2 -= PriorityDelegate<PriorityEventTest, Poco::EventArgs>(this, &PriorityEventTest::onComplex2, 3);
Complex2.notify(this, args);
assert (_count == 4);
const EventArgs* pCArgs = &args;
ConstComplex += PriorityDelegate<PriorityEventTest, const Poco::EventArgs*>(this, &PriorityEventTest::onConstComplex, 0);
ConstComplex -= PriorityDelegate<PriorityEventTest, const Poco::EventArgs*>(this, &PriorityEventTest::onConstComplex, 3);
ConstComplex.notify(this, pCArgs);
assert (_count == 5);
Const2Complex += PriorityDelegate<PriorityEventTest, const Poco::EventArgs* const>(this, &PriorityEventTest::onConst2Complex, 0);
Const2Complex -= PriorityDelegate<PriorityEventTest, const Poco::EventArgs* const>(this, &PriorityEventTest::onConst2Complex, 3);
Const2Complex.notify(this, pArgs);
assert (_count == 6);
// check if 2nd notify also works
Const2Complex.notify(this, pArgs);
assert (_count == 7);
}
void PriorityEventTest::testDuplicateRegister()
{
int tmp = 0;
assert (_count == 0);
Simple += PriorityDelegate<PriorityEventTest, int>(this, &PriorityEventTest::onSimple, 0);
Simple += PriorityDelegate<PriorityEventTest, int>(this, &PriorityEventTest::onSimple, 0);
Simple.notify(this, tmp);
assert (_count == 1);
Simple -= PriorityDelegate<PriorityEventTest, int>(this, &PriorityEventTest::onSimple, 0);
Simple.notify(this, tmp);
assert (_count == 1);
Simple += PriorityDelegate<PriorityEventTest, int>(this, &PriorityEventTest::onSimple, 0);
Simple += PriorityDelegate<PriorityEventTest, int>(this, &PriorityEventTest::onSimpleOther, 1);
Simple.notify(this, tmp);
assert (_count == 2 + LARGEINC);
Simple -= PriorityDelegate<PriorityEventTest, int>(this, &PriorityEventTest::onSimpleOther, 1);
Simple.notify(this, tmp);
assert (_count == 3 + LARGEINC);
}
void PriorityEventTest::testDuplicateUnregister()
{
// duplicate unregister shouldn't give an error,
int tmp = 0;
assert (_count == 0);
Simple -= PriorityDelegate<PriorityEventTest, int>(this, &PriorityEventTest::onSimple, 0); // should work
Simple.notify(this, tmp);
assert (_count == 0);
Simple += PriorityDelegate<PriorityEventTest, int>(this, &PriorityEventTest::onSimple, 0);
Simple.notify(this, tmp);
assert (_count == 1);
Simple -= PriorityDelegate<PriorityEventTest, int>(this, &PriorityEventTest::onSimple, 0);
Simple.notify(this, tmp);
assert (_count == 1);
Simple -= PriorityDelegate<PriorityEventTest, int>(this, &PriorityEventTest::onSimple, 0);
Simple.notify(this, tmp);
assert (_count == 1);
}
void PriorityEventTest::testDisabling()
{
int tmp = 0;
assert (_count == 0);
Simple += PriorityDelegate<PriorityEventTest, int>(this, &PriorityEventTest::onSimple, 0);
Simple.disable();
Simple.notify(this, tmp);
assert (_count == 0);
Simple.enable();
Simple.notify(this, tmp);
assert (_count == 1);
// unregister should also work with disabled event
Simple.disable();
Simple -= PriorityDelegate<PriorityEventTest, int>(this, &PriorityEventTest::onSimple, 0);
Simple.enable();
Simple.notify(this, tmp);
assert (_count == 1);
}
void PriorityEventTest::testPriorityOrder()
{
DummyDelegate o1;
DummyDelegate o2;
assert (_count == 0);
Simple += PriorityDelegate<DummyDelegate, int>(&o2, &DummyDelegate::onSimple2, 1);
Simple += PriorityDelegate<DummyDelegate, int>(&o1, &DummyDelegate::onSimple, 0);
int tmp = 0;
Simple.notify(this, tmp);
assert (tmp == 2);
Simple -= PriorityDelegate<DummyDelegate, int>(&o1, &DummyDelegate::onSimple, 0);
Simple -= PriorityDelegate<DummyDelegate, int>(&o2, &DummyDelegate::onSimple2, 1);
// now try with the wrong order
Simple += PriorityDelegate<DummyDelegate, int>(&o2, &DummyDelegate::onSimple2, 0);
Simple += PriorityDelegate<DummyDelegate, int>(&o1, &DummyDelegate::onSimple, 1);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -