1*87d2adbcSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*87d2adbcSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*87d2adbcSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*87d2adbcSAndrew Rist  * distributed with this work for additional information
6*87d2adbcSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*87d2adbcSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*87d2adbcSAndrew Rist  * "License"); you may not use this file except in compliance
9*87d2adbcSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*87d2adbcSAndrew Rist  *
11*87d2adbcSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*87d2adbcSAndrew Rist  *
13*87d2adbcSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*87d2adbcSAndrew Rist  * software distributed under the License is distributed on an
15*87d2adbcSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*87d2adbcSAndrew Rist  * KIND, either express or implied.  See the License for the
17*87d2adbcSAndrew Rist  * specific language governing permissions and limitations
18*87d2adbcSAndrew Rist  * under the License.
19*87d2adbcSAndrew Rist  *
20*87d2adbcSAndrew Rist  *************************************************************/
21*87d2adbcSAndrew Rist 
22*87d2adbcSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_sal.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir //------------------------------------------------------------------------
28cdf0e10cSrcweir // include files
29cdf0e10cSrcweir //------------------------------------------------------------------------
30cdf0e10cSrcweir #include <osl_Condition_Const.h>
31cdf0e10cSrcweir 
32cdf0e10cSrcweir using namespace	osl;
33cdf0e10cSrcweir using namespace	rtl;
34cdf0e10cSrcweir 
35cdf0e10cSrcweir 
36cdf0e10cSrcweir //------------------------------------------------------------------------
37cdf0e10cSrcweir // helper functions and classes
38cdf0e10cSrcweir //------------------------------------------------------------------------
39cdf0e10cSrcweir 
40cdf0e10cSrcweir /** print Boolean value.
41cdf0e10cSrcweir */
printBool(sal_Bool bOk)42cdf0e10cSrcweir inline void printBool( sal_Bool bOk )
43cdf0e10cSrcweir {
44cdf0e10cSrcweir 	t_print("#printBool# " );
45cdf0e10cSrcweir 	( sal_True == bOk ) ? t_print("TRUE!\n" ): t_print("FALSE!\n" );
46cdf0e10cSrcweir }
47cdf0e10cSrcweir 
48cdf0e10cSrcweir /** print a UNI_CODE String.
49cdf0e10cSrcweir */
printUString(const::rtl::OUString & str)50cdf0e10cSrcweir inline void printUString( const ::rtl::OUString & str )
51cdf0e10cSrcweir {
52cdf0e10cSrcweir 	rtl::OString aString;
53cdf0e10cSrcweir 
54cdf0e10cSrcweir 	t_print("#printUString_u# " );
55cdf0e10cSrcweir 	aString = ::rtl::OUStringToOString( str, RTL_TEXTENCODING_ASCII_US );
56cdf0e10cSrcweir 	t_print("%s\n", aString.getStr( ) );
57cdf0e10cSrcweir }
58cdf0e10cSrcweir 
59cdf0e10cSrcweir /** wait _nSec seconds.
60cdf0e10cSrcweir */
thread_sleep(sal_Int32 _nSec)61cdf0e10cSrcweir void thread_sleep( sal_Int32 _nSec )
62cdf0e10cSrcweir {
63cdf0e10cSrcweir 	/// print statement in thread process must use fflush() to force display.
64cdf0e10cSrcweir 	t_print("# wait %d seconds. ", _nSec );
65cdf0e10cSrcweir 	fflush( stdout );
66cdf0e10cSrcweir 
67cdf0e10cSrcweir #ifdef WNT                               //Windows
68cdf0e10cSrcweir 	Sleep( _nSec * 1000 );
69cdf0e10cSrcweir #endif
70cdf0e10cSrcweir #if ( defined UNX ) || ( defined OS2 )   //Unix
71cdf0e10cSrcweir 	sleep( _nSec );
72cdf0e10cSrcweir #endif
73cdf0e10cSrcweir 	t_print("# done\n" );
74cdf0e10cSrcweir }
75cdf0e10cSrcweir 
76cdf0e10cSrcweir enum ConditionType
77cdf0e10cSrcweir {
78cdf0e10cSrcweir 	thread_type_set,
79cdf0e10cSrcweir 	thread_type_reset,
80cdf0e10cSrcweir 	thread_type_wait,
81cdf0e10cSrcweir 	thread_type_check
82cdf0e10cSrcweir };
83cdf0e10cSrcweir 
84cdf0e10cSrcweir /** thread for testing Condition.
85cdf0e10cSrcweir  */
86cdf0e10cSrcweir class ConditionThread : public Thread
87cdf0e10cSrcweir {
88cdf0e10cSrcweir public:
89cdf0e10cSrcweir 	//get the Condition to operate
ConditionThread(::osl::Condition & Con,ConditionType tType)90cdf0e10cSrcweir 	ConditionThread( ::osl::Condition& Con, ConditionType tType): m_MyCon( Con ), m_MyType( tType ) { }
91cdf0e10cSrcweir 
~ConditionThread()92cdf0e10cSrcweir 	~ConditionThread( )
93cdf0e10cSrcweir 	{
94cdf0e10cSrcweir         // LLA: do not throw in DTors!
95cdf0e10cSrcweir 		// LLA: CPPUNIT_ASSERT_MESSAGE( "#ConditionThread does not shutdown properly.\n", sal_False == this -> isRunning( ) );
96cdf0e10cSrcweir 	}
97cdf0e10cSrcweir protected:
98cdf0e10cSrcweir 	::osl::Condition& m_MyCon;
99cdf0e10cSrcweir 	ConditionType m_MyType;
100cdf0e10cSrcweir 
run()101cdf0e10cSrcweir 	void SAL_CALL run()
102cdf0e10cSrcweir 	{
103cdf0e10cSrcweir 		switch ( m_MyType )
104cdf0e10cSrcweir 		{
105cdf0e10cSrcweir 			case thread_type_wait:
106cdf0e10cSrcweir 				m_MyCon.wait(); break;
107cdf0e10cSrcweir 			case thread_type_set:
108cdf0e10cSrcweir 				m_MyCon.set(); break;
109cdf0e10cSrcweir 			case thread_type_reset:
110cdf0e10cSrcweir 				m_MyCon.reset(); break;
111cdf0e10cSrcweir 			default:
112cdf0e10cSrcweir 				break;
113cdf0e10cSrcweir 		}
114cdf0e10cSrcweir 	}
115cdf0e10cSrcweir };
116cdf0e10cSrcweir 
117cdf0e10cSrcweir 
118cdf0e10cSrcweir //------------------------------------------------------------------------
119cdf0e10cSrcweir // test code start here
120cdf0e10cSrcweir //------------------------------------------------------------------------
121cdf0e10cSrcweir 
122cdf0e10cSrcweir namespace osl_Condition
123cdf0e10cSrcweir {
124cdf0e10cSrcweir 
125cdf0e10cSrcweir 	/** testing the method:
126cdf0e10cSrcweir 		Condition()
127cdf0e10cSrcweir 	*/
128cdf0e10cSrcweir 	class ctors : public CppUnit::TestFixture
129cdf0e10cSrcweir 	{
130cdf0e10cSrcweir 	public:
131cdf0e10cSrcweir 		sal_Bool bRes, bRes1;
132cdf0e10cSrcweir 
ctors_001()133cdf0e10cSrcweir 		void ctors_001( )
134cdf0e10cSrcweir 		{
135cdf0e10cSrcweir 			::osl::Condition aCond;
136cdf0e10cSrcweir 			bRes = aCond.check( );
137cdf0e10cSrcweir 
138cdf0e10cSrcweir 			CPPUNIT_ASSERT_MESSAGE( "#test comment#: create a condition its initial check state should be sal_False.",
139cdf0e10cSrcweir 									sal_False == bRes );
140cdf0e10cSrcweir 		}
141cdf0e10cSrcweir 
ctors_002()142cdf0e10cSrcweir 		void ctors_002( )
143cdf0e10cSrcweir 		{
144cdf0e10cSrcweir 			::osl::Condition aCond;
145cdf0e10cSrcweir 			aCond.set( );
146cdf0e10cSrcweir 			bRes = aCond.check( );
147cdf0e10cSrcweir 
148cdf0e10cSrcweir 			CPPUNIT_ASSERT_MESSAGE( "#test comment#: create a condition and set it.",
149cdf0e10cSrcweir 									sal_True == bRes );
150cdf0e10cSrcweir 		}
151cdf0e10cSrcweir 
152cdf0e10cSrcweir 		CPPUNIT_TEST_SUITE( ctors );
153cdf0e10cSrcweir 		CPPUNIT_TEST( ctors_001 );
154cdf0e10cSrcweir 		CPPUNIT_TEST( ctors_002 );
155cdf0e10cSrcweir 		CPPUNIT_TEST_SUITE_END( );
156cdf0e10cSrcweir 	}; // class ctors
157cdf0e10cSrcweir 
158cdf0e10cSrcweir 
159cdf0e10cSrcweir 	/** testing the method:
160cdf0e10cSrcweir 		void set()
161cdf0e10cSrcweir 	*/
162cdf0e10cSrcweir 	class set : public CppUnit::TestFixture
163cdf0e10cSrcweir 	{
164cdf0e10cSrcweir 	public:
165cdf0e10cSrcweir 		sal_Bool bRes, bRes1, bRes2;
166cdf0e10cSrcweir 
set_001()167cdf0e10cSrcweir 		void set_001( )
168cdf0e10cSrcweir 		{
169cdf0e10cSrcweir 			::osl::Condition aCond;
170cdf0e10cSrcweir 			aCond.set( );
171cdf0e10cSrcweir 			bRes = aCond.check( );
172cdf0e10cSrcweir 
173cdf0e10cSrcweir 			CPPUNIT_ASSERT_MESSAGE( "#test comment#: check state should be sal_True after set.",
174cdf0e10cSrcweir 									sal_True == bRes );
175cdf0e10cSrcweir 		}
176cdf0e10cSrcweir 
set_002()177cdf0e10cSrcweir 		void set_002( )
178cdf0e10cSrcweir 		{
179cdf0e10cSrcweir 			::osl::Condition aCond;
180cdf0e10cSrcweir 			ConditionThread myThread1( aCond, thread_type_wait );
181cdf0e10cSrcweir 			myThread1.create();
182cdf0e10cSrcweir 			bRes = myThread1.isRunning( );
183cdf0e10cSrcweir 
184cdf0e10cSrcweir 			ConditionThread myThread2( aCond, thread_type_set );
185cdf0e10cSrcweir 			myThread2.create();
186cdf0e10cSrcweir 			thread_sleep(1);
187cdf0e10cSrcweir 			bRes1 = myThread1.isRunning( );
188cdf0e10cSrcweir 			bRes2 = aCond.check( );
189cdf0e10cSrcweir 
190cdf0e10cSrcweir 			myThread1.join( );
191cdf0e10cSrcweir 			myThread2.join( );
192cdf0e10cSrcweir 
193cdf0e10cSrcweir 			CPPUNIT_ASSERT_MESSAGE( "#test comment#: use one thread to set the condition in order to release another thread.",
194cdf0e10cSrcweir 									sal_True == bRes && sal_False == bRes1 && sal_True == bRes2 );
195cdf0e10cSrcweir 		}
196cdf0e10cSrcweir 
197cdf0e10cSrcweir 
198cdf0e10cSrcweir 		CPPUNIT_TEST_SUITE( set );
199cdf0e10cSrcweir 		CPPUNIT_TEST( set_001 );
200cdf0e10cSrcweir 		CPPUNIT_TEST( set_002 );
201cdf0e10cSrcweir 		CPPUNIT_TEST_SUITE_END( );
202cdf0e10cSrcweir 	}; // class set
203cdf0e10cSrcweir 
204cdf0e10cSrcweir 
205cdf0e10cSrcweir 	/** testing the method:
206cdf0e10cSrcweir 		void reset()
207cdf0e10cSrcweir 	*/
208cdf0e10cSrcweir 	class reset : public CppUnit::TestFixture
209cdf0e10cSrcweir 	{
210cdf0e10cSrcweir 	public:
211cdf0e10cSrcweir 		sal_Bool bRes, bRes1, bRes2;
212cdf0e10cSrcweir 
reset_001()213cdf0e10cSrcweir 		void reset_001( )
214cdf0e10cSrcweir 		{
215cdf0e10cSrcweir 			::osl::Condition aCond;
216cdf0e10cSrcweir 			aCond.reset( );
217cdf0e10cSrcweir 
218cdf0e10cSrcweir 			ConditionThread myThread( aCond, thread_type_wait );
219cdf0e10cSrcweir 			myThread.create();
220cdf0e10cSrcweir 			bRes = myThread.isRunning( );
221cdf0e10cSrcweir 			bRes2 = aCond.check( );
222cdf0e10cSrcweir 
223cdf0e10cSrcweir 			aCond.set( );
224cdf0e10cSrcweir 			myThread.join( );
225cdf0e10cSrcweir 			bRes1 = myThread.isRunning( );
226cdf0e10cSrcweir 
227cdf0e10cSrcweir 			CPPUNIT_ASSERT_MESSAGE( "#test comment#: wait will cause a reset thread block, use set to release it.",
228cdf0e10cSrcweir 									sal_True == bRes && sal_False == bRes1 && sal_False == bRes2 );
229cdf0e10cSrcweir 		}
230cdf0e10cSrcweir 
reset_002()231cdf0e10cSrcweir 		void reset_002( )
232cdf0e10cSrcweir 		{
233cdf0e10cSrcweir 			::osl::Condition aCond;
234cdf0e10cSrcweir 			aCond.reset( );
235cdf0e10cSrcweir 			bRes = aCond.check( );
236cdf0e10cSrcweir 			aCond.set( );
237cdf0e10cSrcweir 			bRes1 = aCond.check( );
238cdf0e10cSrcweir 
239cdf0e10cSrcweir 			CPPUNIT_ASSERT_MESSAGE( "#test comment#: create a condition and reset/set it.",
240cdf0e10cSrcweir 									( sal_False == bRes && sal_True == bRes1 ) );
241cdf0e10cSrcweir 		}
242cdf0e10cSrcweir 
243cdf0e10cSrcweir 		CPPUNIT_TEST_SUITE( reset );
244cdf0e10cSrcweir 		CPPUNIT_TEST( reset_001 );
245cdf0e10cSrcweir 		CPPUNIT_TEST( reset_002 );
246cdf0e10cSrcweir 		CPPUNIT_TEST_SUITE_END( );
247cdf0e10cSrcweir 	}; // class reset
248cdf0e10cSrcweir 
249cdf0e10cSrcweir 
250cdf0e10cSrcweir 	/** testing the method:
251cdf0e10cSrcweir 		Result wait(const TimeValue *pTimeout = 0)
252cdf0e10cSrcweir 	*/
253cdf0e10cSrcweir 	class wait : public CppUnit::TestFixture
254cdf0e10cSrcweir 	{
255cdf0e10cSrcweir 	public:
256cdf0e10cSrcweir 		sal_Bool bRes, bRes1, bRes2;
257cdf0e10cSrcweir 		TimeValue *tv1;
258cdf0e10cSrcweir 
setUp()259cdf0e10cSrcweir 		void setUp( )
260cdf0e10cSrcweir 		{
261cdf0e10cSrcweir 			tv1 = (TimeValue*)malloc(sizeof(TimeValue));
262cdf0e10cSrcweir 			tv1->Seconds = 1;
263cdf0e10cSrcweir 
264cdf0e10cSrcweir 		}
265cdf0e10cSrcweir 
tearDown()266cdf0e10cSrcweir 		void tearDown( )
267cdf0e10cSrcweir 		{
268cdf0e10cSrcweir 			free( tv1 );
269cdf0e10cSrcweir 		}
270cdf0e10cSrcweir 
271cdf0e10cSrcweir 
wait_001()272cdf0e10cSrcweir 		void wait_001( )
273cdf0e10cSrcweir 		{
274cdf0e10cSrcweir 			::osl::Condition cond1;
275cdf0e10cSrcweir 			::osl::Condition cond2;
276cdf0e10cSrcweir 			::osl::Condition cond3;
277cdf0e10cSrcweir 
278cdf0e10cSrcweir 			cond1.set();
279cdf0e10cSrcweir 			cond2.set();
280cdf0e10cSrcweir 
281cdf0e10cSrcweir osl::Condition::Result r1=cond1.wait(tv1);
282cdf0e10cSrcweir osl::Condition::Result r2=cond2.wait();
283cdf0e10cSrcweir osl::Condition::Result r3=cond3.wait(tv1);
284cdf0e10cSrcweir fprintf(stderr,"%d %d %d\n",r1,r2,r3);
285cdf0e10cSrcweir 			CPPUNIT_ASSERT_MESSAGE( "#test comment#: test three types of wait.",
286cdf0e10cSrcweir 									(cond1.wait(tv1) == ::osl::Condition::result_ok) &&
287cdf0e10cSrcweir 									(cond2.wait() == ::osl::Condition::result_ok) &&
288cdf0e10cSrcweir 									(cond3.wait(tv1) == ::osl::Condition::result_timeout) );
289cdf0e10cSrcweir 
290cdf0e10cSrcweir 		}
291cdf0e10cSrcweir 
wait_002()292cdf0e10cSrcweir 		void wait_002( )
293cdf0e10cSrcweir 		{
294cdf0e10cSrcweir 			::osl::Condition aCond;
295cdf0e10cSrcweir 			::osl::Condition::Result wRes, wRes1;
296cdf0e10cSrcweir 
297cdf0e10cSrcweir 			aCond.reset( );
298cdf0e10cSrcweir 			bRes = aCond.check( );
299cdf0e10cSrcweir 			wRes = aCond.wait( tv1 );
300cdf0e10cSrcweir 
301cdf0e10cSrcweir 			aCond.set( );
302cdf0e10cSrcweir 			wRes1 = aCond.wait( tv1 );
303cdf0e10cSrcweir 			bRes1 = aCond.check( );
304cdf0e10cSrcweir 
305cdf0e10cSrcweir 			CPPUNIT_ASSERT_MESSAGE( "#test comment#: wait a condition after set/reset.",
306cdf0e10cSrcweir 									( sal_False == bRes ) && ( sal_True == bRes1 ) &&
307cdf0e10cSrcweir 									( ::osl::Condition::result_timeout == wRes ) &&
308cdf0e10cSrcweir 									( ::osl::Condition::result_ok == wRes1 ) );
309cdf0e10cSrcweir 		}
310cdf0e10cSrcweir 
311cdf0e10cSrcweir 		CPPUNIT_TEST_SUITE( wait );
312cdf0e10cSrcweir 		CPPUNIT_TEST( wait_001 );
313cdf0e10cSrcweir 		CPPUNIT_TEST( wait_002 );
314cdf0e10cSrcweir 		CPPUNIT_TEST_SUITE_END( );
315cdf0e10cSrcweir 	}; // class wait
316cdf0e10cSrcweir 
317cdf0e10cSrcweir 
318cdf0e10cSrcweir 	/** testing the method:
319cdf0e10cSrcweir 		sal_Bool check()
320cdf0e10cSrcweir 	*/
321cdf0e10cSrcweir 	class check : public CppUnit::TestFixture
322cdf0e10cSrcweir 	{
323cdf0e10cSrcweir 	public:
324cdf0e10cSrcweir 		sal_Bool bRes, bRes1, bRes2;
325cdf0e10cSrcweir 
check_001()326cdf0e10cSrcweir 		void check_001( )
327cdf0e10cSrcweir 		{
328cdf0e10cSrcweir 			::osl::Condition aCond;
329cdf0e10cSrcweir 
330cdf0e10cSrcweir 			aCond.reset( );
331cdf0e10cSrcweir 			bRes = aCond.check( );
332cdf0e10cSrcweir 			aCond.set( );
333cdf0e10cSrcweir 			bRes1 = aCond.check( );
334cdf0e10cSrcweir 
335cdf0e10cSrcweir 			CPPUNIT_ASSERT_MESSAGE( "#test comment#: check the condition states.",
336cdf0e10cSrcweir 									( sal_False == bRes && sal_True == bRes1 ) );
337cdf0e10cSrcweir 		}
338cdf0e10cSrcweir 
check_002()339cdf0e10cSrcweir 		void check_002( )
340cdf0e10cSrcweir 		{
341cdf0e10cSrcweir 			::osl::Condition aCond;
342cdf0e10cSrcweir 			aCond.reset( );
343cdf0e10cSrcweir 
344cdf0e10cSrcweir 			ConditionThread myThread( aCond, thread_type_set );
345cdf0e10cSrcweir 			myThread.create( );
346cdf0e10cSrcweir 			myThread.join( );
347cdf0e10cSrcweir 			bRes = aCond.check( );
348cdf0e10cSrcweir 
349cdf0e10cSrcweir 			ConditionThread myThread1( aCond, thread_type_reset );
350cdf0e10cSrcweir 			myThread1.create( );
351cdf0e10cSrcweir 			myThread1.join( );
352cdf0e10cSrcweir 			bRes1 = aCond.check( );
353cdf0e10cSrcweir 
354cdf0e10cSrcweir 			CPPUNIT_ASSERT_MESSAGE( "#test comment#: use threads to set/reset Condition and check it in main routine.",
355cdf0e10cSrcweir 									( sal_True == bRes && sal_False == bRes1 ) );
356cdf0e10cSrcweir 		}
357cdf0e10cSrcweir 
358cdf0e10cSrcweir 		CPPUNIT_TEST_SUITE( check );
359cdf0e10cSrcweir 		CPPUNIT_TEST( check_001 );
360cdf0e10cSrcweir 		CPPUNIT_TEST( check_002 );
361cdf0e10cSrcweir 		CPPUNIT_TEST_SUITE_END( );
362cdf0e10cSrcweir 	}; // class check
363cdf0e10cSrcweir 
364cdf0e10cSrcweir 
365cdf0e10cSrcweir // -----------------------------------------------------------------------------
366cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Condition::ctors, "osl_Condition");
367cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Condition::set, "osl_Condition");
368cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Condition::reset, "osl_Condition");
369cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Condition::wait, "osl_Condition");
370cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Condition::check, "osl_Condition");
371cdf0e10cSrcweir // -----------------------------------------------------------------------------
372cdf0e10cSrcweir 
373cdf0e10cSrcweir } // namespace osl_Condition
374cdf0e10cSrcweir 
375cdf0e10cSrcweir 
376cdf0e10cSrcweir // -----------------------------------------------------------------------------
377cdf0e10cSrcweir 
378cdf0e10cSrcweir // this macro creates an empty function, which will called by the RegisterAllFunctions()
379cdf0e10cSrcweir // to let the user the possibility to also register some functions by hand.
380cdf0e10cSrcweir NOADDITIONAL;
381