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