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