Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : :
30 : : //------------------------------------------------------------------------
31 : : // include files
32 : : //------------------------------------------------------------------------
33 : : #include <osl_Condition_Const.h>
34 : : #include <stdlib.h>
35 : :
36 : : using namespace osl;
37 : : using namespace rtl;
38 : :
39 : :
40 : : //------------------------------------------------------------------------
41 : : // helper functions and classes
42 : : //------------------------------------------------------------------------
43 : :
44 : : /** print Boolean value.
45 : : */
46 : : inline void printBool( sal_Bool bOk )
47 : : {
48 : : printf("#printBool# " );
49 : : ( sal_True == bOk ) ? printf("TRUE!\n" ): printf("FALSE!\n" );
50 : : }
51 : :
52 : : /** print a UNI_CODE String.
53 : : */
54 : : inline void printUString( const ::rtl::OUString & str )
55 : : {
56 : : rtl::OString aString;
57 : :
58 : : printf("#printUString_u# " );
59 : : aString = ::rtl::OUStringToOString( str, RTL_TEXTENCODING_ASCII_US );
60 : : printf("%s\n", aString.getStr( ) );
61 : : }
62 : :
63 : : enum ConditionType
64 : : {
65 : : thread_type_set,
66 : : thread_type_reset,
67 : : thread_type_wait,
68 : : thread_type_check
69 : : };
70 : :
71 : : /** thread for testing Condition.
72 : : */
73 : : class ConditionThread : public Thread
74 : : {
75 : : public:
76 : : //get the Condition to operate
77 : 25 : ConditionThread( ::osl::Condition& Con, ConditionType tType): m_MyCon( Con ), m_MyType( tType ) { }
78 : :
79 : 25 : ~ConditionThread( )
80 : 25 : {
81 : : // LLA: do not throw in DTors!
82 : : // LLA: CPPUNIT_ASSERT_MESSAGE( "#ConditionThread does not shutdown properly.\n", sal_False == this -> isRunning( ) );
83 [ - + ]: 25 : }
84 : : protected:
85 : : ::osl::Condition& m_MyCon;
86 : : ConditionType m_MyType;
87 : :
88 : 25 : void SAL_CALL run()
89 : : {
90 [ + + + - ]: 25 : switch ( m_MyType )
91 : : {
92 : : case thread_type_wait:
93 : 10 : m_MyCon.wait(); break;
94 : : case thread_type_set:
95 : 10 : m_MyCon.set(); break;
96 : : case thread_type_reset:
97 : 5 : m_MyCon.reset(); break;
98 : : default:
99 : 0 : break;
100 : : }
101 : 25 : }
102 : : };
103 : :
104 : :
105 : : //------------------------------------------------------------------------
106 : : // test code start here
107 : : //------------------------------------------------------------------------
108 : :
109 : : namespace osl_Condition
110 : : {
111 : :
112 : : /** testing the method:
113 : : Condition()
114 : : */
115 [ - + ]: 30 : class ctors : public CppUnit::TestFixture
116 : : {
117 : : public:
118 : : sal_Bool bRes, bRes1;
119 : :
120 : 5 : void ctors_001( )
121 : : {
122 [ + - ]: 5 : ::osl::Condition aCond;
123 [ + - ]: 5 : bRes = aCond.check( );
124 : :
125 [ + - ][ + - ]: 10 : CPPUNIT_ASSERT_MESSAGE( "#test comment#: create a condition its initial check state should be sal_False.",
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
126 [ + - + - ]: 10 : sal_False == bRes );
127 : 5 : }
128 : :
129 : 5 : void ctors_002( )
130 : : {
131 [ + - ]: 5 : ::osl::Condition aCond;
132 [ + - ]: 5 : aCond.set( );
133 [ + - ]: 5 : bRes = aCond.check( );
134 : :
135 [ + - ][ + - ]: 10 : CPPUNIT_ASSERT_MESSAGE( "#test comment#: create a condition and set it.",
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
136 [ + - + - ]: 10 : sal_True == bRes );
137 : 5 : }
138 : :
139 [ + - ][ + - ]: 10 : CPPUNIT_TEST_SUITE( ctors );
[ + - ][ + - ]
[ # # ]
140 [ + - ][ + - ]: 5 : CPPUNIT_TEST( ctors_001 );
[ + - ][ + - ]
[ + - ][ + - ]
141 [ + - ][ + - ]: 5 : CPPUNIT_TEST( ctors_002 );
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
142 [ + - ][ + - ]: 10 : CPPUNIT_TEST_SUITE_END( );
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
143 : : }; // class ctors
144 : :
145 : :
146 : : /** testing the method:
147 : : void set()
148 : : */
149 [ - + ]: 30 : class set : public CppUnit::TestFixture
150 : : {
151 : : public:
152 : : sal_Bool bRes, bRes1, bRes2;
153 : :
154 : 5 : void set_001( )
155 : : {
156 [ + - ]: 5 : ::osl::Condition aCond;
157 [ + - ]: 5 : aCond.set( );
158 [ + - ]: 5 : bRes = aCond.check( );
159 : :
160 [ + - ][ + - ]: 10 : CPPUNIT_ASSERT_MESSAGE( "#test comment#: check state should be sal_True after set.",
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
161 [ + - + - ]: 10 : sal_True == bRes );
162 : 5 : }
163 : :
164 : 5 : void set_002( )
165 : : {
166 [ + - ]: 5 : ::osl::Condition aCond;
167 [ + - ]: 5 : ConditionThread myThread1( aCond, thread_type_wait );
168 [ + - ]: 5 : myThread1.create();
169 [ + - ]: 5 : bRes = myThread1.isRunning( );
170 : :
171 [ + - ]: 5 : ConditionThread myThread2( aCond, thread_type_set );
172 [ + - ]: 5 : myThread2.create();
173 : :
174 [ + - ]: 5 : myThread1.join( );
175 [ + - ]: 5 : bRes1 = myThread1.isRunning( );
176 [ + - ]: 5 : bRes2 = aCond.check( );
177 [ + - ]: 5 : myThread2.join( );
178 : :
179 [ + - ][ + - ]: 10 : CPPUNIT_ASSERT_MESSAGE( "#test comment#: use one thread to set the condition in order to release another thread.",
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ - + ][ + - ]
[ + - ]
180 [ + - + - ]: 10 : sal_True == bRes && sal_False == bRes1 && sal_True == bRes2 );
[ + - ][ + - ]
181 : 5 : }
182 : :
183 : :
184 [ + - ][ + - ]: 10 : CPPUNIT_TEST_SUITE( set );
[ + - ][ + - ]
[ # # ]
185 [ + - ][ + - ]: 5 : CPPUNIT_TEST( set_001 );
[ + - ][ + - ]
[ + - ][ + - ]
186 [ + - ][ + - ]: 5 : CPPUNIT_TEST( set_002 );
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
187 [ + - ][ + - ]: 10 : CPPUNIT_TEST_SUITE_END( );
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
188 : : }; // class set
189 : :
190 : :
191 : : /** testing the method:
192 : : void reset()
193 : : */
194 [ - + ]: 30 : class reset : public CppUnit::TestFixture
195 : : {
196 : : public:
197 : : sal_Bool bRes, bRes1, bRes2;
198 : :
199 : 5 : void reset_001( )
200 : : {
201 [ + - ]: 5 : ::osl::Condition aCond;
202 [ + - ]: 5 : aCond.reset( );
203 : :
204 [ + - ]: 5 : ConditionThread myThread( aCond, thread_type_wait );
205 [ + - ]: 5 : myThread.create();
206 [ + - ]: 5 : bRes = myThread.isRunning( );
207 [ + - ]: 5 : bRes2 = aCond.check( );
208 : :
209 [ + - ]: 5 : aCond.set( );
210 [ + - ]: 5 : myThread.join( );
211 [ + - ]: 5 : bRes1 = myThread.isRunning( );
212 : :
213 [ + - ][ + - ]: 10 : CPPUNIT_ASSERT_MESSAGE( "#test comment#: wait will cause a reset thread block, use set to release it.",
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ - + ][ + - ]
[ + - ]
214 [ + - + - ]: 10 : sal_True == bRes && sal_False == bRes1 && sal_False == bRes2 );
[ + - ]
215 : 5 : }
216 : :
217 : 5 : void reset_002( )
218 : : {
219 [ + - ]: 5 : ::osl::Condition aCond;
220 [ + - ]: 5 : aCond.reset( );
221 [ + - ]: 5 : bRes = aCond.check( );
222 [ + - ]: 5 : aCond.set( );
223 [ + - ]: 5 : bRes1 = aCond.check( );
224 : :
225 [ + - ][ + - ]: 10 : CPPUNIT_ASSERT_MESSAGE( "#test comment#: create a condition and reset/set it.",
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ - + ]
[ + - ][ + - ]
226 [ + - + - ]: 10 : ( sal_False == bRes && sal_True == bRes1 ) );
227 : 5 : }
228 : :
229 [ + - ][ + - ]: 10 : CPPUNIT_TEST_SUITE( reset );
[ + - ][ + - ]
[ # # ]
230 [ + - ][ + - ]: 5 : CPPUNIT_TEST( reset_001 );
[ + - ][ + - ]
[ + - ][ + - ]
231 [ + - ][ + - ]: 5 : CPPUNIT_TEST( reset_002 );
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
232 [ + - ][ + - ]: 10 : CPPUNIT_TEST_SUITE_END( );
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
233 : : }; // class reset
234 : :
235 : :
236 : : /** testing the method:
237 : : Result wait(const TimeValue *pTimeout = 0)
238 : : */
239 [ - + ]: 30 : class wait : public CppUnit::TestFixture
240 : : {
241 : : public:
242 : : sal_Bool bRes, bRes1, bRes2;
243 : : TimeValue *tv1;
244 : :
245 : 10 : void setUp( )
246 : : {
247 : 10 : tv1 = new TimeValue;
248 : 10 : tv1->Seconds = 1;
249 : 10 : tv1->Nanosec = 0;
250 : :
251 : 10 : }
252 : :
253 : 10 : void tearDown( )
254 : : {
255 : 10 : delete tv1;
256 : 10 : }
257 : :
258 : :
259 : 5 : void wait_001( )
260 : : {
261 [ + - ]: 5 : ::osl::Condition cond1;
262 [ + - ]: 5 : ::osl::Condition cond2;
263 [ + - ]: 5 : ::osl::Condition cond3;
264 : :
265 [ + - ]: 5 : cond1.set();
266 [ + - ]: 5 : cond2.set();
267 : :
268 [ + - ]: 5 : osl::Condition::Result r1=cond1.wait(tv1);
269 [ + - ]: 5 : osl::Condition::Result r2=cond2.wait();
270 [ + - ]: 5 : osl::Condition::Result r3=cond3.wait(tv1);
271 : :
272 [ + - ][ + - ]: 10 : CPPUNIT_ASSERT_MESSAGE( "#test comment#: test three types of wait.",
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ - + ][ + - ]
[ + - ]
273 : : (r1 == ::osl::Condition::result_ok) &&
274 : : (r2 == ::osl::Condition::result_ok) &&
275 [ + - + - ]: 10 : (r3 == ::osl::Condition::result_timeout) );
[ + - ][ + - ]
276 : 5 : }
277 : :
278 : 5 : void wait_002( )
279 : : {
280 [ + - ]: 5 : ::osl::Condition aCond;
281 : : ::osl::Condition::Result wRes, wRes1;
282 : :
283 [ + - ]: 5 : aCond.reset( );
284 [ + - ]: 5 : bRes = aCond.check( );
285 [ + - ]: 5 : wRes = aCond.wait( tv1 );
286 : :
287 [ + - ]: 5 : aCond.set( );
288 [ + - ]: 5 : wRes1 = aCond.wait( tv1 );
289 [ + - ]: 5 : bRes1 = aCond.check( );
290 : :
291 [ + - ][ + - ]: 10 : CPPUNIT_ASSERT_MESSAGE( "#test comment#: wait a condition after set/reset.",
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ - + ]
[ + - ][ + - ]
292 : : ( sal_False == bRes ) && ( sal_True == bRes1 ) &&
293 : : ( ::osl::Condition::result_timeout == wRes ) &&
294 [ + - + - ]: 10 : ( ::osl::Condition::result_ok == wRes1 ) );
295 : 5 : }
296 : :
297 [ + - ][ + - ]: 10 : CPPUNIT_TEST_SUITE( wait );
[ + - ][ + - ]
[ # # ]
298 [ + - ][ + - ]: 5 : CPPUNIT_TEST( wait_001 );
[ + - ][ + - ]
[ + - ][ + - ]
299 [ + - ][ + - ]: 5 : CPPUNIT_TEST( wait_002 );
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
300 [ + - ][ + - ]: 10 : CPPUNIT_TEST_SUITE_END( );
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
301 : : }; // class wait
302 : :
303 : :
304 : : /** testing the method:
305 : : sal_Bool check()
306 : : */
307 [ - + ]: 30 : class check : public CppUnit::TestFixture
308 : : {
309 : : public:
310 : : sal_Bool bRes, bRes1, bRes2;
311 : :
312 : 5 : void check_001( )
313 : : {
314 [ + - ]: 5 : ::osl::Condition aCond;
315 : :
316 [ + - ]: 5 : aCond.reset( );
317 [ + - ]: 5 : bRes = aCond.check( );
318 [ + - ]: 5 : aCond.set( );
319 [ + - ]: 5 : bRes1 = aCond.check( );
320 : :
321 [ + - ][ + - ]: 10 : CPPUNIT_ASSERT_MESSAGE( "#test comment#: check the condition states.",
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ - + ]
[ + - ][ + - ]
322 [ + - + - ]: 10 : ( sal_False == bRes && sal_True == bRes1 ) );
323 : 5 : }
324 : :
325 : 5 : void check_002( )
326 : : {
327 [ + - ]: 5 : ::osl::Condition aCond;
328 [ + - ]: 5 : aCond.reset( );
329 : :
330 [ + - ]: 5 : ConditionThread myThread( aCond, thread_type_set );
331 [ + - ]: 5 : myThread.create( );
332 [ + - ]: 5 : myThread.join( );
333 [ + - ]: 5 : bRes = aCond.check( );
334 : :
335 [ + - ]: 5 : ConditionThread myThread1( aCond, thread_type_reset );
336 [ + - ]: 5 : myThread1.create( );
337 [ + - ]: 5 : myThread1.join( );
338 [ + - ]: 5 : bRes1 = aCond.check( );
339 : :
340 [ + - ][ + - ]: 10 : CPPUNIT_ASSERT_MESSAGE( "#test comment#: use threads to set/reset Condition and check it in main routine.",
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ - + ]
[ + - ][ + - ]
341 [ + - + - ]: 10 : ( sal_True == bRes && sal_False == bRes1 ) );
[ + - ][ + - ]
342 : 5 : }
343 : :
344 [ + - ][ + - ]: 10 : CPPUNIT_TEST_SUITE( check );
[ + - ][ + - ]
[ # # ]
345 [ + - ][ + - ]: 5 : CPPUNIT_TEST( check_001 );
[ + - ][ + - ]
[ + - ][ + - ]
346 [ + - ][ + - ]: 5 : CPPUNIT_TEST( check_002 );
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
347 [ + - ][ + - ]: 10 : CPPUNIT_TEST_SUITE_END( );
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
348 : : }; // class check
349 : :
350 : :
351 : : // -----------------------------------------------------------------------------
352 : 5 : CPPUNIT_TEST_SUITE_REGISTRATION(osl_Condition::ctors);
353 : 5 : CPPUNIT_TEST_SUITE_REGISTRATION(osl_Condition::set);
354 : 5 : CPPUNIT_TEST_SUITE_REGISTRATION(osl_Condition::reset);
355 : 5 : CPPUNIT_TEST_SUITE_REGISTRATION(osl_Condition::wait);
356 : 5 : CPPUNIT_TEST_SUITE_REGISTRATION(osl_Condition::check);
357 : : // -----------------------------------------------------------------------------
358 : :
359 : : } // namespace osl_Condition
360 : :
361 : :
362 : : // -----------------------------------------------------------------------------
363 : :
364 [ + - ][ + - ]: 20 : CPPUNIT_PLUGIN_IMPLEMENT();
[ + - ][ + - ]
[ + - ][ # # ]
365 : :
366 : :
367 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|