1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_sal.hxx"
31 //------------------------------------------------------------------------
33 //------------------------------------------------------------------------
34 #include <osl_Condition_Const.h>
40 //------------------------------------------------------------------------
41 // helper functions and classes
42 //------------------------------------------------------------------------
44 /** print Boolean value.
46 inline void printBool( sal_Bool bOk
)
48 t_print("#printBool# " );
49 ( sal_True
== bOk
) ? t_print("TRUE!\n" ): t_print("FALSE!\n" );
52 /** print a UNI_CODE String.
54 inline void printUString( const ::rtl::OUString
& str
)
58 t_print("#printUString_u# " );
59 aString
= ::rtl::OUStringToOString( str
, RTL_TEXTENCODING_ASCII_US
);
60 t_print("%s\n", aString
.getStr( ) );
63 /** wait _nSec seconds.
65 void thread_sleep( sal_Int32 _nSec
)
67 /// print statement in thread process must use fflush() to force display.
68 t_print("# wait %d seconds. ", _nSec
);
72 Sleep( _nSec
* 1000 );
74 #if ( defined UNX ) || ( defined OS2 ) //Unix
88 /** thread for testing Condition.
90 class ConditionThread
: public Thread
93 //get the Condition to operate
94 ConditionThread( ::osl::Condition
& Con
, ConditionType tType
): m_MyCon( Con
), m_MyType( tType
) { }
98 // LLA: do not throw in DTors!
99 // LLA: CPPUNIT_ASSERT_MESSAGE( "#ConditionThread does not shutdown properly.\n", sal_False == this -> isRunning( ) );
102 ::osl::Condition
& m_MyCon
;
103 ConditionType m_MyType
;
109 case thread_type_wait
:
110 m_MyCon
.wait(); break;
111 case thread_type_set
:
112 m_MyCon
.set(); break;
113 case thread_type_reset
:
114 m_MyCon
.reset(); break;
122 //------------------------------------------------------------------------
123 // test code start here
124 //------------------------------------------------------------------------
126 namespace osl_Condition
129 /** testing the method:
132 class ctors
: public CppUnit::TestFixture
135 sal_Bool bRes
, bRes1
;
139 ::osl::Condition aCond
;
140 bRes
= aCond
.check( );
142 CPPUNIT_ASSERT_MESSAGE( "#test comment#: create a condition its initial check state should be sal_False.",
148 ::osl::Condition aCond
;
150 bRes
= aCond
.check( );
152 CPPUNIT_ASSERT_MESSAGE( "#test comment#: create a condition and set it.",
156 CPPUNIT_TEST_SUITE( ctors
);
157 CPPUNIT_TEST( ctors_001
);
158 CPPUNIT_TEST( ctors_002
);
159 CPPUNIT_TEST_SUITE_END( );
163 /** testing the method:
166 class set
: public CppUnit::TestFixture
169 sal_Bool bRes
, bRes1
, bRes2
;
173 ::osl::Condition aCond
;
175 bRes
= aCond
.check( );
177 CPPUNIT_ASSERT_MESSAGE( "#test comment#: check state should be sal_True after set.",
183 ::osl::Condition aCond
;
184 ConditionThread
myThread1( aCond
, thread_type_wait
);
186 bRes
= myThread1
.isRunning( );
188 ConditionThread
myThread2( aCond
, thread_type_set
);
191 bRes1
= myThread1
.isRunning( );
192 bRes2
= aCond
.check( );
197 CPPUNIT_ASSERT_MESSAGE( "#test comment#: use one thread to set the condition in order to release another thread.",
198 sal_True
== bRes
&& sal_False
== bRes1
&& sal_True
== bRes2
);
202 CPPUNIT_TEST_SUITE( set
);
203 CPPUNIT_TEST( set_001
);
204 CPPUNIT_TEST( set_002
);
205 CPPUNIT_TEST_SUITE_END( );
209 /** testing the method:
212 class reset
: public CppUnit::TestFixture
215 sal_Bool bRes
, bRes1
, bRes2
;
219 ::osl::Condition aCond
;
222 ConditionThread
myThread( aCond
, thread_type_wait
);
224 bRes
= myThread
.isRunning( );
225 bRes2
= aCond
.check( );
229 bRes1
= myThread
.isRunning( );
231 CPPUNIT_ASSERT_MESSAGE( "#test comment#: wait will cause a reset thread block, use set to release it.",
232 sal_True
== bRes
&& sal_False
== bRes1
&& sal_False
== bRes2
);
237 ::osl::Condition aCond
;
239 bRes
= aCond
.check( );
241 bRes1
= aCond
.check( );
243 CPPUNIT_ASSERT_MESSAGE( "#test comment#: create a condition and reset/set it.",
244 ( sal_False
== bRes
&& sal_True
== bRes1
) );
247 CPPUNIT_TEST_SUITE( reset
);
248 CPPUNIT_TEST( reset_001
);
249 CPPUNIT_TEST( reset_002
);
250 CPPUNIT_TEST_SUITE_END( );
254 /** testing the method:
255 Result wait(const TimeValue *pTimeout = 0)
257 class wait
: public CppUnit::TestFixture
260 sal_Bool bRes
, bRes1
, bRes2
;
265 tv1
= (TimeValue
*)malloc(sizeof(TimeValue
));
278 ::osl::Condition cond1
;
279 ::osl::Condition cond2
;
280 ::osl::Condition cond3
;
285 osl::Condition::Result r1
=cond1
.wait(tv1
);
286 osl::Condition::Result r2
=cond2
.wait();
287 osl::Condition::Result r3
=cond3
.wait(tv1
);
288 fprintf(stderr
,"%d %d %d\n",r1
,r2
,r3
);
289 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test three types of wait.",
290 (cond1
.wait(tv1
) == ::osl::Condition::result_ok
) &&
291 (cond2
.wait() == ::osl::Condition::result_ok
) &&
292 (cond3
.wait(tv1
) == ::osl::Condition::result_timeout
) );
298 ::osl::Condition aCond
;
299 ::osl::Condition::Result wRes
, wRes1
;
302 bRes
= aCond
.check( );
303 wRes
= aCond
.wait( tv1
);
306 wRes1
= aCond
.wait( tv1
);
307 bRes1
= aCond
.check( );
309 CPPUNIT_ASSERT_MESSAGE( "#test comment#: wait a condition after set/reset.",
310 ( sal_False
== bRes
) && ( sal_True
== bRes1
) &&
311 ( ::osl::Condition::result_timeout
== wRes
) &&
312 ( ::osl::Condition::result_ok
== wRes1
) );
315 CPPUNIT_TEST_SUITE( wait
);
316 CPPUNIT_TEST( wait_001
);
317 CPPUNIT_TEST( wait_002
);
318 CPPUNIT_TEST_SUITE_END( );
322 /** testing the method:
325 class check
: public CppUnit::TestFixture
328 sal_Bool bRes
, bRes1
, bRes2
;
332 ::osl::Condition aCond
;
335 bRes
= aCond
.check( );
337 bRes1
= aCond
.check( );
339 CPPUNIT_ASSERT_MESSAGE( "#test comment#: check the condition states.",
340 ( sal_False
== bRes
&& sal_True
== bRes1
) );
345 ::osl::Condition aCond
;
348 ConditionThread
myThread( aCond
, thread_type_set
);
351 bRes
= aCond
.check( );
353 ConditionThread
myThread1( aCond
, thread_type_reset
);
356 bRes1
= aCond
.check( );
358 CPPUNIT_ASSERT_MESSAGE( "#test comment#: use threads to set/reset Condition and check it in main routine.",
359 ( sal_True
== bRes
&& sal_False
== bRes1
) );
362 CPPUNIT_TEST_SUITE( check
);
363 CPPUNIT_TEST( check_001
);
364 CPPUNIT_TEST( check_002
);
365 CPPUNIT_TEST_SUITE_END( );
369 // -----------------------------------------------------------------------------
370 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Condition::ctors
, "osl_Condition");
371 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Condition::set
, "osl_Condition");
372 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Condition::reset
, "osl_Condition");
373 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Condition::wait
, "osl_Condition");
374 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Condition::check
, "osl_Condition");
375 // -----------------------------------------------------------------------------
377 } // namespace osl_Condition
380 // -----------------------------------------------------------------------------
382 // this macro creates an empty function, which will called by the RegisterAllFunctions()
383 // to let the user the possibility to also register some functions by hand.