1 |
schoenebeck |
63 |
#ifndef __LS_CONDITIONTEST_H__ |
2 |
|
|
#define __LS_CONDITIONTEST_H__ |
3 |
|
|
|
4 |
|
|
#include <cppunit/TestFixture.h> |
5 |
|
|
#include <cppunit/extensions/HelperMacros.h> |
6 |
|
|
|
7 |
|
|
// needed for sleep() and usleep() calls |
8 |
|
|
#include <unistd.h> |
9 |
|
|
|
10 |
|
|
// the Condition class we want to test |
11 |
|
|
#include "../common/Condition.h" |
12 |
|
|
|
13 |
|
|
// we need an additional thread to test the Mutex class |
14 |
|
|
#include "../common/Thread.h" |
15 |
|
|
|
16 |
|
|
class ConditionTest : public CppUnit::TestFixture { |
17 |
|
|
|
18 |
|
|
CPPUNIT_TEST_SUITE(ConditionTest); |
19 |
|
|
CPPUNIT_TEST(testDoesntBlockOnDesiredCondtion); |
20 |
|
|
CPPUNIT_TEST(testBlocksIfNotDesiredCondition); |
21 |
|
|
CPPUNIT_TEST(testBlocksUntilDesiredCondition); |
22 |
|
|
CPPUNIT_TEST(testWaitIFBlocksConcurrentThread); |
23 |
|
|
CPPUNIT_TEST_SUITE_END(); |
24 |
|
|
|
25 |
|
|
private: |
26 |
|
|
// simulates a thread waiting for a condition |
27 |
|
|
class ConditionChecker : public Thread { |
28 |
|
|
public: |
29 |
|
|
int resource; |
30 |
|
|
Condition condition; |
31 |
|
|
|
32 |
|
|
ConditionChecker(bool waitFor); |
33 |
|
|
int Main(); |
34 |
|
|
protected: |
35 |
|
|
bool waitFor; |
36 |
|
|
}; |
37 |
|
|
|
38 |
|
|
// simulates a thread which sets the condition |
39 |
|
|
class ConditionSetter : public Thread { |
40 |
|
|
public: |
41 |
|
|
int resource; |
42 |
|
|
Condition* condition; |
43 |
|
|
|
44 |
|
|
ConditionSetter(Condition* condition, bool toSet); |
45 |
|
|
int Main(); |
46 |
|
|
private: |
47 |
|
|
bool toSet; |
48 |
|
|
}; |
49 |
|
|
|
50 |
|
|
// same as 'ConditionChecker' except that this one uses WaitIf() instead of WaitAndUnlockIf() |
51 |
|
|
class ConditionCheckerLocking : public ConditionChecker { |
52 |
|
|
public: |
53 |
|
|
bool doUnlock; |
54 |
|
|
static Condition staticcondition; |
55 |
|
|
|
56 |
|
|
ConditionCheckerLocking(bool waitFor); |
57 |
|
|
int Main(); |
58 |
|
|
}; |
59 |
|
|
public: |
60 |
|
|
void setUp(); |
61 |
|
|
void tearDown(); |
62 |
|
|
|
63 |
|
|
void testDoesntBlockOnDesiredCondtion(); |
64 |
|
|
void testBlocksIfNotDesiredCondition(); |
65 |
|
|
void testBlocksUntilDesiredCondition(); |
66 |
|
|
void testWaitIFBlocksConcurrentThread(); |
67 |
|
|
}; |
68 |
|
|
|
69 |
|
|
#endif // __LS_CONDITIONTEST_H__ |