Main Page
Namespaces
Classes
Files
File List
File Members
src
lib
coil
tests
Mutex
MutexTests.cpp
Go to the documentation of this file.
1
// -*- C++ -*-
12
/*
13
* $Log$
14
*
15
*/
16
17
#ifndef Mutex_cpp
18
#define Mutex_cpp
19
20
#include <iostream>
21
#include <iomanip>
22
#include <string>
23
#include <stdio.h>
24
//#include <unistd.h>
25
#include <time.h>
26
#include <cppunit/ui/text/TestRunner.h>
27
#include <cppunit/TextOutputter.h>
28
#include <cppunit/extensions/TestFactoryRegistry.h>
29
#include <cppunit/extensions/HelperMacros.h>
30
#include <cppunit/TestAssert.h>
31
32
#include <coil/Mutex.h>
33
#include <coil/Task.h>
34
#include <coil/Time.h>
35
36
41
namespace
Mutex
42
{
43
class
MutexTests
44
:
public
CppUnit::TestFixture
45
{
46
CPPUNIT_TEST_SUITE
(
MutexTests
);
47
// CPPUNIT_TEST(test_case0);
48
CPPUNIT_TEST
(
test_lock
);
49
CPPUNIT_TEST
(
test_trylock
);
50
CPPUNIT_TEST_SUITE_END
();
51
52
private
:
53
public
:
54
class
TestTaskLock
55
:
public
coil::Task
56
{
57
public
:
58
static
long
ShareCount
;
59
static
coil::Mutex
mtx
;
60
int
svc
(
void
)
61
{
62
clock_t start, end;
63
long
lc;
64
long
lbuf;
65
for
(lc=0; lc<100; lc++)
66
{
67
mtx.
lock
();
68
lbuf =
ShareCount
;
69
// usleep(1);
70
start = clock();
71
for
(;;)
72
{
73
end = clock();
74
if
((
double
)(end-start)/CLOCKS_PER_SEC>0.000001)
75
{
76
break
;
77
}
78
}
79
lbuf++;
80
ShareCount = lbuf;
81
mtx.
unlock
();
82
}
83
return
0;
84
}
85
86
long
getShareCount
()
87
{
88
return
ShareCount
;
89
}
90
void
setShareCount
(
long
lc)
91
{
92
mtx.
lock
();
93
ShareCount = lc;
94
mtx.
unlock
();
95
}
96
};
97
class
TestTaskTrylock
98
:
public
coil::Task
99
{
100
public
:
101
static
long
ShareCount
;
102
static
coil::Mutex
mtx
;
103
int
svc
(
void
)
104
{
105
clock_t start, end;
106
long
lc;
107
long
lbuf;
108
int
iret;
109
for
(lc=0; lc<100; lc++)
110
{
111
for
(;;)
112
{
113
iret = mtx.
trylock
();
114
if
(iret ==
false
)
115
{
116
lbuf =
ShareCount
;
117
// usleep(1);
118
start = clock();
119
for
(;;)
120
{
121
end = clock();
122
if
((
double
)(end-start)/CLOCKS_PER_SEC>0.000001)
123
{
124
break
;
125
}
126
}
127
lbuf++;
128
ShareCount = lbuf;
129
break
;
130
}
131
}
132
mtx.
unlock
();
133
}
134
return
0;
135
}
136
137
long
getShareCount
()
138
{
139
return
ShareCount
;
140
}
141
void
setShareCount
(
long
lc)
142
{
143
mtx.
lock
();
144
ShareCount = lc;
145
mtx.
unlock
();
146
}
147
148
};
152
MutexTests
()
153
{
154
}
155
159
~MutexTests
()
160
{
161
}
162
166
virtual
void
setUp
()
167
{
168
}
169
173
virtual
void
tearDown
()
174
{
175
}
176
177
/* test case */
178
/*
179
---------------------------------------------------------------------------
180
This function tests the Mutex::lock function.
181
Check exclusive control,when two threads access static variable.
182
---------------------------------------------------------------------------
183
*/
184
void
test_lock
()
185
{
186
long
lc;
187
char
cstr[256];
188
MutexTests::TestTaskLock
tka;
189
MutexTests::TestTaskLock
tkb;
190
tka.
setShareCount
(0);
191
192
tka.
activate
();
193
tkb.
activate
();
194
tka.
wait
();
195
tkb.
wait
();
196
lc = tka.
getShareCount
();
197
sprintf
( cstr,
"count %ld"
, lc );
198
// std::cout<<cstr<<std::endl;
199
CPPUNIT_ASSERT_MESSAGE(cstr , (lc == 200) );
200
201
}
202
/*
203
---------------------------------------------------------------------------
204
This function tests the Mutex::trylock function.
205
Check exclusive control,when two threads access static variable.
206
---------------------------------------------------------------------------
207
*/
208
void
test_trylock
()
209
{
210
long
lc;
211
char
cstr[256];
212
MutexTests::TestTaskTrylock
tka;
213
MutexTests::TestTaskTrylock
tkb;
214
tka.
setShareCount
(0);
215
216
tka.
activate
();
217
tkb.
activate
();
218
tka.
wait
();
219
tkb.
wait
();
220
lc = tka.
getShareCount
();
221
sprintf
( cstr,
"count %ld"
, lc );
222
// std::cout<<cstr<<std::endl;
223
CPPUNIT_ASSERT_MESSAGE(cstr , (lc == 200) );
224
225
}
226
};
227
long
MutexTests::TestTaskLock::ShareCount
= 0L;
228
coil::Mutex
MutexTests::TestTaskLock::mtx
;
229
long
MutexTests::TestTaskTrylock::ShareCount
= 0L;
230
coil::Mutex
MutexTests::TestTaskTrylock::mtx
;
231
};
// namespace Mutex
232
233
/*
234
* Register test suite
235
*/
236
CPPUNIT_TEST_SUITE_REGISTRATION
(
Mutex::MutexTests
);
237
238
#ifdef LOCAL_MAIN
239
int
main
(
int
argc,
char
* argv[])
240
{
241
CppUnit::TextUi::TestRunner runner;
242
runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
243
CppUnit::Outputter* outputter =
244
new
CppUnit::TextOutputter(&runner.result(), std::cout);
245
runner.setOutputter(outputter);
246
bool
retcode
= runner.run();
247
return
!
retcode
;
248
}
249
#endif // MAIN
250
#endif // Mutex_cpp
main
int main(int argc, char **argv)
Definition:
AutoTestInComp.cpp:74
Mutex::MutexTests::test_lock
void test_lock()
Definition:
MutexTests.cpp:184
coil::Mutex
Mutex class.
Definition:
ace/coil/Mutex.h:36
Mutex::MutexTests::TestTaskTrylock::svc
int svc(void)
Execute thread.
Definition:
MutexTests.cpp:103
Mutex::MutexTests::CPPUNIT_TEST
CPPUNIT_TEST(test_lock)
Mutex::MutexTests::test_trylock
void test_trylock()
Definition:
MutexTests.cpp:208
coil::Mutex::lock
void lock()
Definition:
ace/coil/Mutex.h:49
Mutex
Definition:
MutexTests.cpp:41
Mutex::MutexTests::TestTaskLock
Definition:
MutexTests.cpp:54
Mutex::MutexTests::TestTaskLock::setShareCount
void setShareCount(long lc)
Definition:
MutexTests.cpp:90
coil::Task::wait
virtual int wait(void)
Waiting for the thread terminate.
Definition:
posix/coil/Task.cpp:111
Mutex::MutexTests::tearDown
virtual void tearDown()
Test finalization.
Definition:
MutexTests.cpp:173
coil::Mutex::unlock
void unlock()
Definition:
ace/coil/Mutex.h:59
CPPUNIT_TEST_SUITE_REGISTRATION
CPPUNIT_TEST_SUITE_REGISTRATION(Mutex::MutexTests)
Mutex::MutexTests::MutexTests
MutexTests()
Constructor.
Definition:
MutexTests.cpp:152
coil::Mutex::trylock
bool trylock()
Definition:
ace/coil/Mutex.h:54
Mutex::MutexTests::TestTaskLock::getShareCount
long getShareCount()
Definition:
MutexTests.cpp:86
Mutex::MutexTests::TestTaskLock::mtx
static coil::Mutex mtx
Definition:
MutexTests.cpp:59
Mutex::MutexTests::TestTaskLock::svc
int svc(void)
Execute thread.
Definition:
MutexTests.cpp:60
Mutex::MutexTests::TestTaskTrylock::setShareCount
void setShareCount(long lc)
Definition:
MutexTests.cpp:141
Mutex::MutexTests::~MutexTests
~MutexTests()
Destructor.
Definition:
MutexTests.cpp:159
Mutex::MutexTests::CPPUNIT_TEST_SUITE
CPPUNIT_TEST_SUITE(MutexTests)
Mutex::MutexTests::CPPUNIT_TEST_SUITE_END
CPPUNIT_TEST_SUITE_END()
coil::sprintf
std::string sprintf(char const *__restrict fmt,...)
Convert it into a format given with an argumen.
Definition:
stringutil.cpp:593
coil::Task
Task class.
Definition:
posix/coil/Task.h:39
Mutex::MutexTests::TestTaskTrylock
Definition:
MutexTests.cpp:97
AddRemoveRTCTest.retcode
retcode
Definition:
AddRemoveRTCTest.py:73
Mutex::MutexTests
Definition:
MutexTests.cpp:43
Mutex::MutexTests::setUp
virtual void setUp()
Test initialization.
Definition:
MutexTests.cpp:166
Mutex::MutexTests::TestTaskLock::ShareCount
static long ShareCount
Definition:
MutexTests.cpp:58
Mutex::MutexTests::TestTaskTrylock::mtx
static coil::Mutex mtx
Definition:
MutexTests.cpp:102
Mutex::MutexTests::TestTaskTrylock::ShareCount
static long ShareCount
Definition:
MutexTests.cpp:101
coil::Task::activate
virtual void activate()
Create a thread.
Definition:
posix/coil/Task.cpp:92
Mutex::MutexTests::TestTaskTrylock::getShareCount
long getShareCount()
Definition:
MutexTests.cpp:137
openrtm_aist
Author(s): Noriaki Ando
autogenerated on Thu Jun 6 2019 19:25:59