187d2adbcSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
387d2adbcSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
487d2adbcSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
587d2adbcSAndrew Rist  * distributed with this work for additional information
687d2adbcSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
787d2adbcSAndrew Rist  * to you under the Apache License, Version 2.0 (the
887d2adbcSAndrew Rist  * "License"); you may not use this file except in compliance
987d2adbcSAndrew Rist  * with the License.  You may obtain a copy of the License at
1087d2adbcSAndrew Rist  *
1187d2adbcSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
1287d2adbcSAndrew Rist  *
1387d2adbcSAndrew Rist  * Unless required by applicable law or agreed to in writing,
1487d2adbcSAndrew Rist  * software distributed under the License is distributed on an
1587d2adbcSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
1687d2adbcSAndrew Rist  * KIND, either express or implied.  See the License for the
1787d2adbcSAndrew Rist  * specific language governing permissions and limitations
1887d2adbcSAndrew Rist  * under the License.
1987d2adbcSAndrew Rist  *
2087d2adbcSAndrew Rist  *************************************************************/
2187d2adbcSAndrew Rist 
2287d2adbcSAndrew 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 
325c3a7755SJuergen Schmidt #ifdef WNT
335c3a7755SJuergen Schmidt #include <Windows.h>
345c3a7755SJuergen Schmidt #endif
355c3a7755SJuergen Schmidt 
361c576ccbSJuergen Schmidt #include "gtest/gtest.h"
371c576ccbSJuergen Schmidt 
38cdf0e10cSrcweir using namespace	osl;
39cdf0e10cSrcweir using namespace	rtl;
40cdf0e10cSrcweir 
41cdf0e10cSrcweir 
42cdf0e10cSrcweir //------------------------------------------------------------------------
43cdf0e10cSrcweir // helper functions and classes
44cdf0e10cSrcweir //------------------------------------------------------------------------
45cdf0e10cSrcweir 
46cdf0e10cSrcweir /** print Boolean value.
47cdf0e10cSrcweir */
printBool(sal_Bool bOk)48cdf0e10cSrcweir inline void printBool( sal_Bool bOk )
49cdf0e10cSrcweir {
501c576ccbSJuergen Schmidt 	printf("#printBool# " );
511c576ccbSJuergen Schmidt 	( sal_True == bOk ) ? printf("TRUE!\n" ): printf("FALSE!\n" );
52cdf0e10cSrcweir }
53cdf0e10cSrcweir 
54cdf0e10cSrcweir /** print a UNI_CODE String.
55cdf0e10cSrcweir */
printUString(const::rtl::OUString & str)56cdf0e10cSrcweir inline void printUString( const ::rtl::OUString & str )
57cdf0e10cSrcweir {
58cdf0e10cSrcweir 	rtl::OString aString;
59cdf0e10cSrcweir 
601c576ccbSJuergen Schmidt 	printf("#printUString_u# " );
61cdf0e10cSrcweir 	aString = ::rtl::OUStringToOString( str, RTL_TEXTENCODING_ASCII_US );
621c576ccbSJuergen Schmidt 	printf("%s\n", aString.getStr( ) );
63cdf0e10cSrcweir }
64cdf0e10cSrcweir 
65cdf0e10cSrcweir /** wait _nSec seconds.
66cdf0e10cSrcweir */
thread_sleep(sal_Int32 _nSec)67cdf0e10cSrcweir void thread_sleep( sal_Int32 _nSec )
68cdf0e10cSrcweir {
69cdf0e10cSrcweir 	/// print statement in thread process must use fflush() to force display.
701c576ccbSJuergen Schmidt 	printf("# wait %d seconds. ", _nSec );
71cdf0e10cSrcweir 	fflush( stdout );
72cdf0e10cSrcweir 
73cdf0e10cSrcweir #ifdef WNT                               //Windows
74cdf0e10cSrcweir 	Sleep( _nSec * 1000 );
75cdf0e10cSrcweir #endif
76cdf0e10cSrcweir #if ( defined UNX ) || ( defined OS2 )   //Unix
77cdf0e10cSrcweir 	sleep( _nSec );
78cdf0e10cSrcweir #endif
791c576ccbSJuergen Schmidt 	printf("# done\n" );
80cdf0e10cSrcweir }
81cdf0e10cSrcweir 
82cdf0e10cSrcweir enum ConditionType
83cdf0e10cSrcweir {
84cdf0e10cSrcweir 	thread_type_set,
85cdf0e10cSrcweir 	thread_type_reset,
86cdf0e10cSrcweir 	thread_type_wait,
87cdf0e10cSrcweir 	thread_type_check
88cdf0e10cSrcweir };
89cdf0e10cSrcweir 
90cdf0e10cSrcweir /** thread for testing Condition.
91cdf0e10cSrcweir  */
92cdf0e10cSrcweir class ConditionThread : public Thread
93cdf0e10cSrcweir {
94cdf0e10cSrcweir public:
95cdf0e10cSrcweir 	//get the Condition to operate
ConditionThread(::osl::Condition & Con,ConditionType tType)96cdf0e10cSrcweir 	ConditionThread( ::osl::Condition& Con, ConditionType tType): m_MyCon( Con ), m_MyType( tType ) { }
97cdf0e10cSrcweir 
~ConditionThread()98cdf0e10cSrcweir 	~ConditionThread( )
99cdf0e10cSrcweir 	{
100*65cc118fSDamjan Jovanovic 		EXPECT_TRUE( sal_False == this -> isRunning( ) ) << "#ConditionThread does not shutdown properly.\n";
101cdf0e10cSrcweir 	}
102cdf0e10cSrcweir protected:
103cdf0e10cSrcweir 	::osl::Condition& m_MyCon;
104cdf0e10cSrcweir 	ConditionType m_MyType;
105cdf0e10cSrcweir 
run()106cdf0e10cSrcweir 	void SAL_CALL run()
107cdf0e10cSrcweir 	{
108cdf0e10cSrcweir 		switch ( m_MyType )
109cdf0e10cSrcweir 		{
110cdf0e10cSrcweir 			case thread_type_wait:
111cdf0e10cSrcweir 				m_MyCon.wait(); break;
112cdf0e10cSrcweir 			case thread_type_set:
113cdf0e10cSrcweir 				m_MyCon.set(); break;
114cdf0e10cSrcweir 			case thread_type_reset:
115cdf0e10cSrcweir 				m_MyCon.reset(); break;
116cdf0e10cSrcweir 			default:
117cdf0e10cSrcweir 				break;
118cdf0e10cSrcweir 		}
119cdf0e10cSrcweir 	}
120cdf0e10cSrcweir };
1211c576ccbSJuergen Schmidt 
122cdf0e10cSrcweir 
123cdf0e10cSrcweir //------------------------------------------------------------------------
124cdf0e10cSrcweir // test code start here
125cdf0e10cSrcweir //------------------------------------------------------------------------
126cdf0e10cSrcweir 
127cdf0e10cSrcweir namespace osl_Condition
128cdf0e10cSrcweir {
129cdf0e10cSrcweir 	/** testing the method:
130cdf0e10cSrcweir 		Condition()
131cdf0e10cSrcweir 	*/
TEST(Sal_Test_Condition,ctors_001)1321c576ccbSJuergen Schmidt     TEST(Sal_Test_Condition, ctors_001) {
1331c576ccbSJuergen Schmidt         ::osl::Condition aCond;
1341c576ccbSJuergen Schmidt         sal_Bool bRes = aCond.check( );
135cdf0e10cSrcweir 
1361c576ccbSJuergen Schmidt         // #test comment#: create a condition its initial check state should be sal_False.
1371c576ccbSJuergen Schmidt         ASSERT_TRUE( !bRes );
1381c576ccbSJuergen Schmidt     }
1391c576ccbSJuergen Schmidt 
TEST(Sal_Test_Condition,ctors_002)1401c576ccbSJuergen Schmidt     TEST(Sal_Test_Condition, ctors_002) {
1411c576ccbSJuergen Schmidt         ::osl::Condition aCond;
1421c576ccbSJuergen Schmidt         aCond.set( );
1431c576ccbSJuergen Schmidt         sal_Bool bRes = aCond.check( );
144cdf0e10cSrcweir 
1451c576ccbSJuergen Schmidt         // #test comment#: create a condition and set it.
1461c576ccbSJuergen Schmidt         ASSERT_TRUE( bRes );
1471c576ccbSJuergen Schmidt     }
148cdf0e10cSrcweir 
149cdf0e10cSrcweir 
150cdf0e10cSrcweir 	/** testing the method:
151cdf0e10cSrcweir 		void set()
152cdf0e10cSrcweir 	*/
TEST(Sal_Test_Condition,set_001)1531c576ccbSJuergen Schmidt     TEST(Sal_Test_Condition, set_001) {
1541c576ccbSJuergen Schmidt         ::osl::Condition aCond;
1551c576ccbSJuergen Schmidt         aCond.set( );
1561c576ccbSJuergen Schmidt         sal_Bool bRes = aCond.check( );
157cdf0e10cSrcweir 
1581c576ccbSJuergen Schmidt         // #test comment#: check state should be sal_True after set.
1591c576ccbSJuergen Schmidt         ASSERT_TRUE( bRes );
1601c576ccbSJuergen Schmidt     }
1611c576ccbSJuergen Schmidt 
TEST(Sal_Test_Condition,set_002)1621c576ccbSJuergen Schmidt     TEST(Sal_Test_Condition, set_002) {
1631c576ccbSJuergen Schmidt         ::osl::Condition aCond;
1641c576ccbSJuergen Schmidt         ConditionThread myThread1( aCond, thread_type_wait );
1651c576ccbSJuergen Schmidt         myThread1.create();
1661c576ccbSJuergen Schmidt         sal_Bool bRes = myThread1.isRunning( );
167cdf0e10cSrcweir 
1681c576ccbSJuergen Schmidt         ConditionThread myThread2( aCond, thread_type_set );
1691c576ccbSJuergen Schmidt         myThread2.create();
1701c576ccbSJuergen Schmidt         thread_sleep(1);
1711c576ccbSJuergen Schmidt 		sal_Bool bRes1 = myThread1.isRunning( );
1721c576ccbSJuergen Schmidt 		sal_Bool bRes2 = aCond.check( );
173cdf0e10cSrcweir 
1741c576ccbSJuergen Schmidt         myThread1.join( );
1751c576ccbSJuergen Schmidt         myThread2.join( );
176cdf0e10cSrcweir 
1771c576ccbSJuergen Schmidt         // #test comment#: use one thread to set the condition in order to release another thread."
1781c576ccbSJuergen Schmidt         ASSERT_TRUE( bRes && !bRes1 && bRes2 );
1791c576ccbSJuergen Schmidt     }
1801c576ccbSJuergen Schmidt 
1811c576ccbSJuergen Schmidt 
182cdf0e10cSrcweir 	/** testing the method:
183cdf0e10cSrcweir 		void reset()
184cdf0e10cSrcweir 	*/
TEST(Sal_Test_Condition,reset_001)1851c576ccbSJuergen Schmidt     TEST(Sal_Test_Condition, reset_001) {
1861c576ccbSJuergen Schmidt         ::osl::Condition aCond;
1871c576ccbSJuergen Schmidt         aCond.reset( );
188cdf0e10cSrcweir 
1891c576ccbSJuergen Schmidt         ConditionThread myThread( aCond, thread_type_wait );
1901c576ccbSJuergen Schmidt         myThread.create();
1911c576ccbSJuergen Schmidt         sal_Bool bRes = myThread.isRunning( );
1921c576ccbSJuergen Schmidt 		sal_Bool bRes2 = aCond.check( );
193cdf0e10cSrcweir 
1941c576ccbSJuergen Schmidt         aCond.set( );
1951c576ccbSJuergen Schmidt         myThread.join( );
1961c576ccbSJuergen Schmidt         sal_Bool bRes1 = myThread.isRunning( );
197cdf0e10cSrcweir 
1981c576ccbSJuergen Schmidt         // #test comment#: wait will cause a reset thread block, use set to release it.
1991c576ccbSJuergen Schmidt         ASSERT_TRUE( bRes && !bRes1 && !bRes2 );
2001c576ccbSJuergen Schmidt     }
2011c576ccbSJuergen Schmidt 
TEST(Sal_Test_Condition,reset_002)2021c576ccbSJuergen Schmidt     TEST(Sal_Test_Condition, reset_002) {
2031c576ccbSJuergen Schmidt         ::osl::Condition aCond;
2041c576ccbSJuergen Schmidt         aCond.reset( );
2051c576ccbSJuergen Schmidt         sal_Bool bRes = aCond.check( );
2061c576ccbSJuergen Schmidt         aCond.set( );
2071c576ccbSJuergen Schmidt         sal_Bool bRes1 = aCond.check( );
2081c576ccbSJuergen Schmidt 
2091c576ccbSJuergen Schmidt         // #test comment#: create a condition and reset/set it.
2101c576ccbSJuergen Schmidt         ASSERT_TRUE( sal_False == bRes && sal_True == bRes1 );
2111c576ccbSJuergen Schmidt     }
212cdf0e10cSrcweir 
213cdf0e10cSrcweir 
214cdf0e10cSrcweir 	/** testing the method:
215cdf0e10cSrcweir 		Result wait(const TimeValue *pTimeout = 0)
216cdf0e10cSrcweir 	*/
TEST(Sal_Test_Condition,wait_001)2171c576ccbSJuergen Schmidt     TEST(Sal_Test_Condition, wait_001) {
2184a7db6b2SHerbert Dürr         TimeValue tv1 = {1,0};
2194a7db6b2SHerbert Dürr 
2201c576ccbSJuergen Schmidt         ::osl::Condition cond1;
2211c576ccbSJuergen Schmidt         ::osl::Condition cond2;
2221c576ccbSJuergen Schmidt         ::osl::Condition cond3;
2234a7db6b2SHerbert Dürr 
2241c576ccbSJuergen Schmidt         cond1.set();
2251c576ccbSJuergen Schmidt         cond2.set();
2264a7db6b2SHerbert Dürr 
2271c576ccbSJuergen Schmidt         osl::Condition::Result r1=cond1.wait(&tv1);
2281c576ccbSJuergen Schmidt         osl::Condition::Result r2=cond2.wait();
2291c576ccbSJuergen Schmidt         osl::Condition::Result r3=cond3.wait(&tv1);
2301c576ccbSJuergen Schmidt         fprintf(stderr,"%d %d %d\n",r1,r2,r3);
2311c576ccbSJuergen Schmidt 
2321c576ccbSJuergen Schmidt         // #test comment#: test three types of wait.
2334a7db6b2SHerbert Dürr         ASSERT_TRUE( cond1.wait(&tv1) == ::osl::Condition::result_ok );
2344a7db6b2SHerbert Dürr         ASSERT_TRUE( cond2.wait() == ::osl::Condition::result_ok );
2354a7db6b2SHerbert Dürr         ASSERT_TRUE( cond3.wait(&tv1) == ::osl::Condition::result_timeout );
2361c576ccbSJuergen Schmidt     }
2371c576ccbSJuergen Schmidt 
TEST(Sal_Test_Condition,wait_002)2381c576ccbSJuergen Schmidt     TEST(Sal_Test_Condition, wait_002) {
2394a7db6b2SHerbert Dürr         TimeValue tv1 = {1,0};
2404a7db6b2SHerbert Dürr 
2411c576ccbSJuergen Schmidt         ::osl::Condition aCond;
2421c576ccbSJuergen Schmidt         ::osl::Condition::Result wRes, wRes1;
2434a7db6b2SHerbert Dürr 
2441c576ccbSJuergen Schmidt         aCond.reset( );
2451c576ccbSJuergen Schmidt         sal_Bool bRes = aCond.check( );
2461c576ccbSJuergen Schmidt         wRes = aCond.wait( &tv1 );
2474a7db6b2SHerbert Dürr 
2481c576ccbSJuergen Schmidt         aCond.set( );
2491c576ccbSJuergen Schmidt         wRes1 = aCond.wait( &tv1 );
2501c576ccbSJuergen Schmidt         sal_Bool bRes1 = aCond.check( );
251cdf0e10cSrcweir 
2521c576ccbSJuergen Schmidt         // #test comment#: wait a condition after set/reset.
2534a7db6b2SHerbert Dürr         ASSERT_TRUE( !bRes );
2544a7db6b2SHerbert Dürr         ASSERT_TRUE( bRes1 );
2554a7db6b2SHerbert Dürr         ASSERT_TRUE( ::osl::Condition::result_timeout == wRes );
2564a7db6b2SHerbert Dürr         ASSERT_TRUE( ::osl::Condition::result_ok == wRes1 );
2571c576ccbSJuergen Schmidt     }
258cdf0e10cSrcweir 
259cdf0e10cSrcweir 
260cdf0e10cSrcweir 	/** testing the method:
261cdf0e10cSrcweir 		sal_Bool check()
262cdf0e10cSrcweir 	*/
TEST(Sal_Test_Condition,check_001)2631c576ccbSJuergen Schmidt     TEST(Sal_Test_Condition, check_001) {
2641c576ccbSJuergen Schmidt         ::osl::Condition aCond;
2651c576ccbSJuergen Schmidt         aCond.reset( );
2661c576ccbSJuergen Schmidt 		sal_Bool bRes = aCond.check( );
2671c576ccbSJuergen Schmidt         aCond.set( );
2681c576ccbSJuergen Schmidt 		sal_Bool bRes1 = aCond.check( );
2691c576ccbSJuergen Schmidt 
2701c576ccbSJuergen Schmidt 		// #test comment#: check the condition states.
2711c576ccbSJuergen Schmidt         ASSERT_TRUE( !bRes && bRes1 );
2721c576ccbSJuergen Schmidt     }
2731c576ccbSJuergen Schmidt 
TEST(Sal_Test_Condition,check_002)2741c576ccbSJuergen Schmidt     TEST(Sal_Test_Condition, check_002) {
2751c576ccbSJuergen Schmidt         ::osl::Condition aCond;
2761c576ccbSJuergen Schmidt         aCond.reset( );
277cdf0e10cSrcweir 
2781c576ccbSJuergen Schmidt         ConditionThread myThread( aCond, thread_type_set );
2791c576ccbSJuergen Schmidt         myThread.create( );
2801c576ccbSJuergen Schmidt         myThread.join( );
2811c576ccbSJuergen Schmidt         sal_Bool bRes = aCond.check( );
282cdf0e10cSrcweir 
2831c576ccbSJuergen Schmidt         ConditionThread myThread1( aCond, thread_type_reset );
2841c576ccbSJuergen Schmidt         myThread1.create( );
2851c576ccbSJuergen Schmidt         myThread1.join( );
2861c576ccbSJuergen Schmidt         sal_Bool bRes1 = aCond.check( );
287cdf0e10cSrcweir 
2881c576ccbSJuergen Schmidt         // #test comment#: use threads to set/reset Condition and check it in main routine.
2894a7db6b2SHerbert Dürr         ASSERT_TRUE( bRes );
2904a7db6b2SHerbert Dürr         ASSERT_TRUE( !bRes1 );
2911c576ccbSJuergen Schmidt     }
292cdf0e10cSrcweir 
293cdf0e10cSrcweir } // namespace osl_Condition
294cdf0e10cSrcweir 
295cdf0e10cSrcweir 
main(int argc,char ** argv)2961c576ccbSJuergen Schmidt int main(int argc, char **argv)
2971c576ccbSJuergen Schmidt {
2981c576ccbSJuergen Schmidt     ::testing::InitGoogleTest(&argc, argv);
2991c576ccbSJuergen Schmidt     return RUN_ALL_TESTS();
3001c576ccbSJuergen Schmidt }
301