fix indentation

This commit is contained in:
ale_bychuk
2015-03-08 22:04:29 +03:00
parent 662fbc861a
commit 3af35f548c
13 changed files with 1370 additions and 1367 deletions

View File

@@ -20,7 +20,6 @@
#include "Poco/Timestamp.h"
#include "Poco/Timespan.h"
#include "Poco/Environment.h"
//#include <iostream>
#if defined(__sun) && defined(__SVR4) && !defined(__EXTENSIONS__)
#define __EXTENSIONS__
#endif
@@ -37,45 +36,45 @@ using Poco::Event;
class MyRunnable: public Runnable
{
public:
MyRunnable(): _ran(false)
{
}
void run()
{
Thread* pThread = Thread::current();
if (pThread)
_threadName = pThread->name();
_ran = true;
_event.wait();
}
bool ran() const
{
return _ran;
}
const std::string& threadName() const
{
return _threadName;
}
void notify()
{
_event.set();
}
static void staticFunc()
{
++_staticVar;
}
MyRunnable(): _ran(false)
{
}
static int _staticVar;
void run()
{
Thread* pThread = Thread::current();
if (pThread)
_threadName = pThread->name();
_ran = true;
_event.wait();
}
bool ran() const
{
return _ran;
}
const std::string& threadName() const
{
return _threadName;
}
void notify()
{
_event.set();
}
static void staticFunc()
{
++_staticVar;
}
static int _staticVar;
private:
bool _ran;
std::string _threadName;
Event _event;
bool _ran;
std::string _threadName;
Event _event;
};
@@ -84,68 +83,68 @@ int MyRunnable::_staticVar = 0;
void freeFunc()
{
++MyRunnable::_staticVar;
++MyRunnable::_staticVar;
}
void freeFunc(void* pData)
{
MyRunnable::_staticVar += *reinterpret_cast<int*>(pData);
MyRunnable::_staticVar += *reinterpret_cast<int*>(pData);
}
class NonJoinRunnable : public Runnable
{
public:
NonJoinRunnable() : _finished(false)
{
}
void run()
{
_finished = true;
}
bool finished() const
{
return _finished;
}
NonJoinRunnable() : _finished(false)
{
}
void run()
{
_finished = true;
}
bool finished() const
{
return _finished;
}
private:
bool _finished;
bool _finished;
};
class TrySleepRunnable : public Runnable
{
public:
TrySleepRunnable() : _counter(0), _sleepy(true)
{
}
TrySleepRunnable() : _counter(0), _sleepy(true)
{
}
void run()
{
_sleepy = !Thread::trySleep(300000);
++_counter;
_sleepy = !Thread::trySleep(300000);
++_counter;
_sleepy = !Thread::trySleep(100);
++_counter;
}
void run()
{
_sleepy = !Thread::trySleep(300000);
++_counter;
_sleepy = !Thread::trySleep(300000);
++_counter;
_sleepy = !Thread::trySleep(100);
++_counter;
}
int counter() const
{
return _counter;
}
int counter() const
{
return _counter;
}
bool isSleepy() const
{
return _sleepy;
}
bool isSleepy() const
{
return _sleepy;
}
private:
int _counter;
bool _sleepy;
int _counter;
bool _sleepy;
};
@@ -161,244 +160,244 @@ ThreadTest::~ThreadTest()
void ThreadTest::testThread()
{
Thread thread;
MyRunnable r;
assert (!thread.isRunning());
thread.start(r);
Thread::sleep(200);
assert (thread.isRunning());
r.notify();
thread.join();
assert (!thread.isRunning());
assert (r.ran());
assert (!r.threadName().empty());
Thread thread;
MyRunnable r;
assert (!thread.isRunning());
thread.start(r);
Thread::sleep(200);
assert (thread.isRunning());
r.notify();
thread.join();
assert (!thread.isRunning());
assert (r.ran());
assert (!r.threadName().empty());
}
void ThreadTest::testNamedThread()
{
Thread thread("MyThread");
MyRunnable r;
thread.start(r);
r.notify();
thread.join();
assert (r.ran());
assert (r.threadName() == "MyThread");
Thread thread("MyThread");
MyRunnable r;
thread.start(r);
r.notify();
thread.join();
assert (r.ran());
assert (r.threadName() == "MyThread");
}
void ThreadTest::testCurrent()
{
assertNullPtr (Thread::current());
assertNullPtr (Thread::current());
}
void ThreadTest::testThreads()
{
Thread thread1("Thread1");
Thread thread2("Thread2");
Thread thread3("Thread3");
Thread thread4("Thread4");
Thread thread1("Thread1");
Thread thread2("Thread2");
Thread thread3("Thread3");
Thread thread4("Thread4");
MyRunnable r1;
MyRunnable r2;
MyRunnable r3;
MyRunnable r4;
assert (!thread1.isRunning());
assert (!thread2.isRunning());
assert (!thread3.isRunning());
assert (!thread4.isRunning());
thread1.start(r1);
Thread::sleep(200);
assert (thread1.isRunning());
assert (!thread2.isRunning());
assert (!thread3.isRunning());
assert (!thread4.isRunning());
thread2.start(r2);
thread3.start(r3);
thread4.start(r4);
Thread::sleep(200);
assert (thread1.isRunning());
assert (thread2.isRunning());
assert (thread3.isRunning());
assert (thread4.isRunning());
r4.notify();
thread4.join();
assert (!thread4.isRunning());
assert (thread1.isRunning());
assert (thread2.isRunning());
assert (thread3.isRunning());
r3.notify();
thread3.join();
assert (!thread3.isRunning());
r2.notify();
thread2.join();
assert (!thread2.isRunning());
r1.notify();
thread1.join();
assert (!thread1.isRunning());
assert (r1.ran());
assert (r1.threadName() == "Thread1");
assert (r2.ran());
assert (r2.threadName() == "Thread2");
assert (r3.ran());
assert (r3.threadName() == "Thread3");
assert (r4.ran());
assert (r4.threadName() == "Thread4");
MyRunnable r1;
MyRunnable r2;
MyRunnable r3;
MyRunnable r4;
assert (!thread1.isRunning());
assert (!thread2.isRunning());
assert (!thread3.isRunning());
assert (!thread4.isRunning());
thread1.start(r1);
Thread::sleep(200);
assert (thread1.isRunning());
assert (!thread2.isRunning());
assert (!thread3.isRunning());
assert (!thread4.isRunning());
thread2.start(r2);
thread3.start(r3);
thread4.start(r4);
Thread::sleep(200);
assert (thread1.isRunning());
assert (thread2.isRunning());
assert (thread3.isRunning());
assert (thread4.isRunning());
r4.notify();
thread4.join();
assert (!thread4.isRunning());
assert (thread1.isRunning());
assert (thread2.isRunning());
assert (thread3.isRunning());
r3.notify();
thread3.join();
assert (!thread3.isRunning());
r2.notify();
thread2.join();
assert (!thread2.isRunning());
r1.notify();
thread1.join();
assert (!thread1.isRunning());
assert (r1.ran());
assert (r1.threadName() == "Thread1");
assert (r2.ran());
assert (r2.threadName() == "Thread2");
assert (r3.ran());
assert (r3.threadName() == "Thread3");
assert (r4.ran());
assert (r4.threadName() == "Thread4");
}
void ThreadTest::testJoin()
{
Thread thread;
MyRunnable r;
assert (!thread.isRunning());
thread.start(r);
Thread::sleep(200);
assert (thread.isRunning());
assert (!thread.tryJoin(100));
r.notify();
assert (thread.tryJoin(500));
assert (!thread.isRunning());
Thread thread;
MyRunnable r;
assert (!thread.isRunning());
thread.start(r);
Thread::sleep(200);
assert (thread.isRunning());
assert (!thread.tryJoin(100));
r.notify();
assert (thread.tryJoin(500));
assert (!thread.isRunning());
}
void ThreadTest::testNotJoin()
{
Thread thread;
NonJoinRunnable r;
thread.start(r);
while (!r.finished())
{
Thread::sleep(10);
}
Thread::sleep(100);
assert (!thread.isRunning());
Thread thread;
NonJoinRunnable r;
thread.start(r);
while (!r.finished())
{
Thread::sleep(10);
}
Thread::sleep(100);
assert (!thread.isRunning());
}
void ThreadTest::testTrySleep()
{
Thread thread;
TrySleepRunnable r;
assert(r.isSleepy());
assert(!thread.isRunning());
assert(r.counter() == 0);
thread.start(r);
assert(thread.isRunning());
assert(r.counter() == 0);
assert(r.isSleepy());
Thread::sleep(100);
assert(r.counter() == 0);
assert(r.isSleepy());
thread.wakeUp(); Thread::sleep(10);
assert(r.counter() == 1);
assert(r.isSleepy());
Thread::sleep(100);
assert(r.counter() == 1);
thread.wakeUp(); Thread::sleep(10);
assert(r.counter() == 2);
assert(r.isSleepy());
Thread::sleep(200);
assert(r.counter() == 3);
assert(!r.isSleepy());
assert(!thread.isRunning());
thread.wakeUp();
assert(!thread.isRunning());
Thread thread;
TrySleepRunnable r;
assert(r.isSleepy());
assert(!thread.isRunning());
assert(r.counter() == 0);
thread.start(r);
assert(thread.isRunning());
assert(r.counter() == 0);
assert(r.isSleepy());
Thread::sleep(100);
assert(r.counter() == 0);
assert(r.isSleepy());
thread.wakeUp(); Thread::sleep(10);
assert(r.counter() == 1);
assert(r.isSleepy());
Thread::sleep(100);
assert(r.counter() == 1);
thread.wakeUp(); Thread::sleep(10);
assert(r.counter() == 2);
assert(r.isSleepy());
Thread::sleep(200);
assert(r.counter() == 3);
assert(!r.isSleepy());
assert(!thread.isRunning());
thread.wakeUp();
assert(!thread.isRunning());
}
void ThreadTest::testNotRun()
{
Thread thread;
Thread thread;
}
void ThreadTest::testNotRunJoin()
{
Thread thread;
thread.join();
Thread thread;
thread.join();
}
void ThreadTest::testThreadTarget()
{
ThreadTarget te(&MyRunnable::staticFunc);
Thread thread;
ThreadTarget te(&MyRunnable::staticFunc);
Thread thread;
assert (!thread.isRunning());
assert (!thread.isRunning());
int tmp = MyRunnable::_staticVar;
thread.start(te);
thread.join();
assert (tmp + 1 == MyRunnable::_staticVar);
int tmp = MyRunnable::_staticVar;
thread.start(te);
thread.join();
assert (tmp + 1 == MyRunnable::_staticVar);
ThreadTarget te1(freeFunc);
assert (!thread.isRunning());
ThreadTarget te1(freeFunc);
assert (!thread.isRunning());
tmp = MyRunnable::_staticVar;
thread.start(te1);
thread.join();
assert (tmp + 1 == MyRunnable::_staticVar);
tmp = MyRunnable::_staticVar;
thread.start(te1);
thread.join();
assert (tmp + 1 == MyRunnable::_staticVar);
}
void ThreadTest::testThreadFunction()
{
Thread thread;
Thread thread;
assert (!thread.isRunning());
assert (!thread.isRunning());
int tmp = MyRunnable::_staticVar;
thread.start(freeFunc, &tmp);
thread.join();
assert (tmp * 2 == MyRunnable::_staticVar);
int tmp = MyRunnable::_staticVar;
thread.start(freeFunc, &tmp);
thread.join();
assert (tmp * 2 == MyRunnable::_staticVar);
assert (!thread.isRunning());
assert (!thread.isRunning());
tmp = MyRunnable::_staticVar = 0;
thread.start(freeFunc, &tmp);
thread.join();
assert (0 == MyRunnable::_staticVar);
tmp = MyRunnable::_staticVar = 0;
thread.start(freeFunc, &tmp);
thread.join();
assert (0 == MyRunnable::_staticVar);
}
struct Functor
{
void operator () ()
{
++MyRunnable::_staticVar;
}
void operator () ()
{
++MyRunnable::_staticVar;
}
};
void ThreadTest::testThreadFunctor()
{
Thread thread;
Thread thread;
assert (!thread.isRunning());
assert (!thread.isRunning());
MyRunnable::_staticVar = 0;
thread.startFunc(Functor());
thread.join();
assert (1 == MyRunnable::_staticVar);
MyRunnable::_staticVar = 0;
thread.startFunc(Functor());
thread.join();
assert (1 == MyRunnable::_staticVar);
assert (!thread.isRunning());
assert (!thread.isRunning());
#if __cplusplus >= 201103L
Thread thread2;
Thread thread2;
assert (!thread2.isRunning());
assert (!thread2.isRunning());
MyRunnable::_staticVar = 0;
thread.startFunc([] () {MyRunnable::_staticVar++;});
thread.join();
assert (1 == MyRunnable::_staticVar);
MyRunnable::_staticVar = 0;
thread.startFunc([] () {MyRunnable::_staticVar++;});
thread.join();
assert (1 == MyRunnable::_staticVar);
assert (!thread2.isRunning());
assert (!thread2.isRunning());
#endif
}
@@ -406,110 +405,110 @@ void ThreadTest::testThreadFunctor()
void ThreadTest::testThreadStackSize()
{
int stackSize = 50000000;
int stackSize = 50000000;
Thread thread;
Thread thread;
assert (0 == thread.getStackSize());
thread.setStackSize(stackSize);
assert (stackSize <= thread.getStackSize());
int tmp = MyRunnable::_staticVar;
thread.start(freeFunc, &tmp);
thread.join();
assert (tmp * 2 == MyRunnable::_staticVar);
assert (0 == thread.getStackSize());
thread.setStackSize(stackSize);
assert (stackSize <= thread.getStackSize());
int tmp = MyRunnable::_staticVar;
thread.start(freeFunc, &tmp);
thread.join();
assert (tmp * 2 == MyRunnable::_staticVar);
stackSize = 1;
thread.setStackSize(stackSize);
stackSize = 1;
thread.setStackSize(stackSize);
#if !defined(POCO_OS_FAMILY_BSD) // on BSD family, stack size is rounded
#ifdef PTHREAD_STACK_MIN
assert (PTHREAD_STACK_MIN == thread.getStackSize());
#else
assert (stackSize >= thread.getStackSize());
#endif
#ifdef PTHREAD_STACK_MIN
assert (PTHREAD_STACK_MIN == thread.getStackSize());
#else
assert (stackSize >= thread.getStackSize());
#endif
#endif
tmp = MyRunnable::_staticVar;
thread.start(freeFunc, &tmp);
thread.join();
assert (tmp * 2 == MyRunnable::_staticVar);
tmp = MyRunnable::_staticVar;
thread.start(freeFunc, &tmp);
thread.join();
assert (tmp * 2 == MyRunnable::_staticVar);
thread.setStackSize(0);
assert (0 == thread.getStackSize());
tmp = MyRunnable::_staticVar;
thread.start(freeFunc, &tmp);
thread.join();
assert (tmp * 2 == MyRunnable::_staticVar);
thread.setStackSize(0);
assert (0 == thread.getStackSize());
tmp = MyRunnable::_staticVar;
thread.start(freeFunc, &tmp);
thread.join();
assert (tmp * 2 == MyRunnable::_staticVar);
}
void ThreadTest::testSleep()
{
Poco::Timestamp start;
Thread::sleep(200);
Poco::Timespan elapsed = start.elapsed();
assert (elapsed.totalMilliseconds() >= 190 && elapsed.totalMilliseconds() < 250);
Poco::Timestamp start;
Thread::sleep(200);
Poco::Timespan elapsed = start.elapsed();
assert (elapsed.totalMilliseconds() >= 190 && elapsed.totalMilliseconds() < 250);
}
void ThreadTest::testAffinity()
{
std::stringstream ss;
unsigned cpuCount = Poco::Environment::processorCount();
unsigned usedCpu = 0;
bool notImplemented = false;
std::vector<Thread *> threadList;
Thread *thread = NULL;
std::vector<MyRunnable *> runnableList;
MyRunnable *runbl = NULL;
std::stringstream ss;
unsigned cpuCount = Poco::Environment::processorCount();
unsigned usedCpu = 0;
bool notImplemented = false;
std::vector<Thread *> threadList;
Thread *thread = NULL;
std::vector<MyRunnable *> runnableList;
MyRunnable *runbl = NULL;
for (unsigned i = 0; i < cpuCount; i++)
{
ss.str("");
ss << "Thread" << i;
thread = new Thread(ss.str());
threadList.push_back(thread);
runbl = new MyRunnable();
runnableList.push_back(runbl);
}
for (unsigned i = 0; i < cpuCount; i++)
{
ss.str("");
ss << "Thread" << i;
thread = new Thread(ss.str());
threadList.push_back(thread);
runbl = new MyRunnable();
runnableList.push_back(runbl);
}
for (int i = 0; i < cpuCount; i++)
{
assert (!threadList[i]->isRunning());
}
for (int i = 0; i < cpuCount; i++)
{
threadList[i]->start(*runnableList[i]);
try
{
threadList[i]->setAffinity(i);
}
catch (Poco::NotImplementedException &niex)
{
notImplemented = true;
}
Thread::sleep(100);
try
{
usedCpu = threadList[i]->getAffinity();
}
catch (Poco::NotImplementedException &niex)
{
notImplemented = true;
}
if (!notImplemented)
{
assert (usedCpu == i);
}
}
for (int i = 0; i < cpuCount; i++)
{
assert (!threadList[i]->isRunning());
}
for (int i = 0; i < cpuCount; i++)
{
runnableList[i]->notify();
threadList[i]->join();
delete runnableList[i];
delete threadList[i];
}
for (int i = 0; i < cpuCount; i++)
{
threadList[i]->start(*runnableList[i]);
try
{
threadList[i]->setAffinity(i);
}
catch (Poco::NotImplementedException &niex)
{
notImplemented = true;
}
Thread::sleep(100);
try
{
usedCpu = threadList[i]->getAffinity();
}
catch (Poco::NotImplementedException &niex)
{
notImplemented = true;
}
if (!notImplemented)
{
assert (usedCpu == i);
}
}
for (int i = 0; i < cpuCount; i++)
{
runnableList[i]->notify();
threadList[i]->join();
delete runnableList[i];
delete threadList[i];
}
}
@@ -525,23 +524,23 @@ void ThreadTest::tearDown()
CppUnit::Test* ThreadTest::suite()
{
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("ThreadTest");
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("ThreadTest");
CppUnit_addTest(pSuite, ThreadTest, testThread);
CppUnit_addTest(pSuite, ThreadTest, testNamedThread);
CppUnit_addTest(pSuite, ThreadTest, testCurrent);
CppUnit_addTest(pSuite, ThreadTest, testThreads);
CppUnit_addTest(pSuite, ThreadTest, testJoin);
CppUnit_addTest(pSuite, ThreadTest, testNotJoin);
CppUnit_addTest(pSuite, ThreadTest, testNotRun);
CppUnit_addTest(pSuite, ThreadTest, testNotRunJoin);
CppUnit_addTest(pSuite, ThreadTest, testTrySleep);
CppUnit_addTest(pSuite, ThreadTest, testThreadTarget);
CppUnit_addTest(pSuite, ThreadTest, testThreadFunction);
CppUnit_addTest(pSuite, ThreadTest, testThreadFunctor);
CppUnit_addTest(pSuite, ThreadTest, testThreadStackSize);
CppUnit_addTest(pSuite, ThreadTest, testSleep);
CppUnit_addTest(pSuite, ThreadTest, testAffinity);
CppUnit_addTest(pSuite, ThreadTest, testThread);
CppUnit_addTest(pSuite, ThreadTest, testNamedThread);
CppUnit_addTest(pSuite, ThreadTest, testCurrent);
CppUnit_addTest(pSuite, ThreadTest, testThreads);
CppUnit_addTest(pSuite, ThreadTest, testJoin);
CppUnit_addTest(pSuite, ThreadTest, testNotJoin);
CppUnit_addTest(pSuite, ThreadTest, testNotRun);
CppUnit_addTest(pSuite, ThreadTest, testNotRunJoin);
CppUnit_addTest(pSuite, ThreadTest, testTrySleep);
CppUnit_addTest(pSuite, ThreadTest, testThreadTarget);
CppUnit_addTest(pSuite, ThreadTest, testThreadFunction);
CppUnit_addTest(pSuite, ThreadTest, testThreadFunctor);
CppUnit_addTest(pSuite, ThreadTest, testThreadStackSize);
CppUnit_addTest(pSuite, ThreadTest, testSleep);
CppUnit_addTest(pSuite, ThreadTest, testAffinity);
return pSuite;
return pSuite;
}

View File

@@ -23,29 +23,29 @@
class ThreadTest: public CppUnit::TestCase
{
public:
ThreadTest(const std::string& name);
~ThreadTest();
ThreadTest(const std::string& name);
~ThreadTest();
void testThread();
void testNamedThread();
void testCurrent();
void testThreads();
void testJoin();
void testNotJoin();
void testNotRun();
void testNotRunJoin();
void testTrySleep();
void testThreadTarget();
void testThreadFunction();
void testThreadFunctor();
void testThreadStackSize();
void testSleep();
void testAffinity();
void testThread();
void testNamedThread();
void testCurrent();
void testThreads();
void testJoin();
void testNotJoin();
void testNotRun();
void testNotRunJoin();
void testTrySleep();
void testThreadTarget();
void testThreadFunction();
void testThreadFunctor();
void testThreadStackSize();
void testSleep();
void testAffinity();
void setUp();
void tearDown();
void setUp();
void tearDown();
static CppUnit::Test* suite();
static CppUnit::Test* suite();
private:
};