1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: osl_Condition.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sal.hxx"
34 //------------------------------------------------------------------------
36 //------------------------------------------------------------------------
37 #include <osl_Condition_Const.h>
43 //------------------------------------------------------------------------
44 // helper functions and classes
45 //------------------------------------------------------------------------
47 /** print Boolean value.
49 inline void printBool( sal_Bool bOk
)
51 t_print("#printBool# " );
52 ( sal_True
== bOk
) ? t_print("TRUE!\n" ): t_print("FALSE!\n" );
55 /** print a UNI_CODE String.
57 inline void printUString( const ::rtl::OUString
& str
)
61 t_print("#printUString_u# " );
62 aString
= ::rtl::OUStringToOString( str
, RTL_TEXTENCODING_ASCII_US
);
63 t_print("%s\n", aString
.getStr( ) );
66 /** wait _nSec seconds.
68 void thread_sleep( sal_Int32 _nSec
)
70 /// print statement in thread process must use fflush() to force display.
71 t_print("# wait %d seconds. ", _nSec
);
75 Sleep( _nSec
* 1000 );
77 #if ( defined UNX ) || ( defined OS2 ) //Unix
91 /** thread for testing Condition.
93 class ConditionThread
: public Thread
96 //get the Condition to operate
97 ConditionThread( ::osl::Condition
& Con
, ConditionType tType
): m_MyCon( Con
), m_MyType( tType
) { }
101 // LLA: do not throw in DTors!
102 // LLA: CPPUNIT_ASSERT_MESSAGE( "#ConditionThread does not shutdown properly.\n", sal_False == this -> isRunning( ) );
105 ::osl::Condition
& m_MyCon
;
106 ConditionType m_MyType
;
112 case thread_type_wait
:
113 m_MyCon
.wait(); break;
114 case thread_type_set
:
115 m_MyCon
.set(); break;
116 case thread_type_reset
:
117 m_MyCon
.reset(); break;
125 //------------------------------------------------------------------------
126 // test code start here
127 //------------------------------------------------------------------------
129 namespace osl_Condition
132 /** testing the method:
135 class ctors
: public CppUnit::TestFixture
138 sal_Bool bRes
, bRes1
;
142 ::osl::Condition aCond
;
143 bRes
= aCond
.check( );
145 CPPUNIT_ASSERT_MESSAGE( "#test comment#: create a condition its initial check state should be sal_False.",
151 ::osl::Condition aCond
;
153 bRes
= aCond
.check( );
155 CPPUNIT_ASSERT_MESSAGE( "#test comment#: create a condition and set it.",
159 CPPUNIT_TEST_SUITE( ctors
);
160 CPPUNIT_TEST( ctors_001
);
161 CPPUNIT_TEST( ctors_002
);
162 CPPUNIT_TEST_SUITE_END( );
166 /** testing the method:
169 class set
: public CppUnit::TestFixture
172 sal_Bool bRes
, bRes1
, bRes2
;
176 ::osl::Condition aCond
;
178 bRes
= aCond
.check( );
180 CPPUNIT_ASSERT_MESSAGE( "#test comment#: check state should be sal_True after set.",
186 ::osl::Condition aCond
;
187 ConditionThread
myThread1( aCond
, thread_type_wait
);
189 bRes
= myThread1
.isRunning( );
191 ConditionThread
myThread2( aCond
, thread_type_set
);
194 bRes1
= myThread1
.isRunning( );
195 bRes2
= aCond
.check( );
200 CPPUNIT_ASSERT_MESSAGE( "#test comment#: use one thread to set the condition in order to release another thread.",
201 sal_True
== bRes
&& sal_False
== bRes1
&& sal_True
== bRes2
);
205 CPPUNIT_TEST_SUITE( set
);
206 CPPUNIT_TEST( set_001
);
207 CPPUNIT_TEST( set_002
);
208 CPPUNIT_TEST_SUITE_END( );
212 /** testing the method:
215 class reset
: public CppUnit::TestFixture
218 sal_Bool bRes
, bRes1
, bRes2
;
222 ::osl::Condition aCond
;
225 ConditionThread
myThread( aCond
, thread_type_wait
);
227 bRes
= myThread
.isRunning( );
228 bRes2
= aCond
.check( );
232 bRes1
= myThread
.isRunning( );
234 CPPUNIT_ASSERT_MESSAGE( "#test comment#: wait will cause a reset thread block, use set to release it.",
235 sal_True
== bRes
&& sal_False
== bRes1
&& sal_False
== bRes2
);
240 ::osl::Condition aCond
;
242 bRes
= aCond
.check( );
244 bRes1
= aCond
.check( );
246 CPPUNIT_ASSERT_MESSAGE( "#test comment#: create a condition and reset/set it.",
247 ( sal_False
== bRes
&& sal_True
== bRes1
) );
250 CPPUNIT_TEST_SUITE( reset
);
251 CPPUNIT_TEST( reset_001
);
252 CPPUNIT_TEST( reset_002
);
253 CPPUNIT_TEST_SUITE_END( );
257 /** testing the method:
258 Result wait(const TimeValue *pTimeout = 0)
260 class wait
: public CppUnit::TestFixture
263 sal_Bool bRes
, bRes1
, bRes2
;
268 tv1
= (TimeValue
*)malloc(sizeof(TimeValue
));
281 ::osl::Condition cond1
;
282 ::osl::Condition cond2
;
283 ::osl::Condition cond3
;
288 osl::Condition::Result r1
=cond1
.wait(tv1
);
289 osl::Condition::Result r2
=cond2
.wait();
290 osl::Condition::Result r3
=cond3
.wait(tv1
);
291 fprintf(stderr
,"%d %d %d\n",r1
,r2
,r3
);
292 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test three types of wait.",
293 (cond1
.wait(tv1
) == ::osl::Condition::result_ok
) &&
294 (cond2
.wait() == ::osl::Condition::result_ok
) &&
295 (cond3
.wait(tv1
) == ::osl::Condition::result_timeout
) );
301 ::osl::Condition aCond
;
302 ::osl::Condition::Result wRes
, wRes1
;
305 bRes
= aCond
.check( );
306 wRes
= aCond
.wait( tv1
);
309 wRes1
= aCond
.wait( tv1
);
310 bRes1
= aCond
.check( );
312 CPPUNIT_ASSERT_MESSAGE( "#test comment#: wait a condition after set/reset.",
313 ( sal_False
== bRes
) && ( sal_True
== bRes1
) &&
314 ( ::osl::Condition::result_timeout
== wRes
) &&
315 ( ::osl::Condition::result_ok
== wRes1
) );
318 CPPUNIT_TEST_SUITE( wait
);
319 CPPUNIT_TEST( wait_001
);
320 CPPUNIT_TEST( wait_002
);
321 CPPUNIT_TEST_SUITE_END( );
325 /** testing the method:
328 class check
: public CppUnit::TestFixture
331 sal_Bool bRes
, bRes1
, bRes2
;
335 ::osl::Condition aCond
;
338 bRes
= aCond
.check( );
340 bRes1
= aCond
.check( );
342 CPPUNIT_ASSERT_MESSAGE( "#test comment#: check the condition states.",
343 ( sal_False
== bRes
&& sal_True
== bRes1
) );
348 ::osl::Condition aCond
;
351 ConditionThread
myThread( aCond
, thread_type_set
);
354 bRes
= aCond
.check( );
356 ConditionThread
myThread1( aCond
, thread_type_reset
);
359 bRes1
= aCond
.check( );
361 CPPUNIT_ASSERT_MESSAGE( "#test comment#: use threads to set/reset Condition and check it in main routine.",
362 ( sal_True
== bRes
&& sal_False
== bRes1
) );
365 CPPUNIT_TEST_SUITE( check
);
366 CPPUNIT_TEST( check_001
);
367 CPPUNIT_TEST( check_002
);
368 CPPUNIT_TEST_SUITE_END( );
372 // -----------------------------------------------------------------------------
373 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Condition::ctors
, "osl_Condition");
374 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Condition::set
, "osl_Condition");
375 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Condition::reset
, "osl_Condition");
376 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Condition::wait
, "osl_Condition");
377 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Condition::check
, "osl_Condition");
378 // -----------------------------------------------------------------------------
380 } // namespace osl_Condition
383 // -----------------------------------------------------------------------------
385 // this macro creates an empty function, which will called by the RegisterAllFunctions()
386 // to let the user the possibility to also register some functions by hand.