merged tag ooo/DEV300_m102
[LibreOffice.git] / sal / qa / osl / condition / osl_Condition.cxx
blob59cfcce9d77ae3f1cfbc0eed5878f5539847b3d3
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_sal.hxx"
31 //------------------------------------------------------------------------
32 // include files
33 //------------------------------------------------------------------------
34 #include <osl_Condition_Const.h>
36 using namespace osl;
37 using namespace rtl;
40 //------------------------------------------------------------------------
41 // helper functions and classes
42 //------------------------------------------------------------------------
44 /** print Boolean value.
46 inline void printBool( sal_Bool bOk )
48 t_print("#printBool# " );
49 ( sal_True == bOk ) ? t_print("TRUE!\n" ): t_print("FALSE!\n" );
52 /** print a UNI_CODE String.
54 inline void printUString( const ::rtl::OUString & str )
56 rtl::OString aString;
58 t_print("#printUString_u# " );
59 aString = ::rtl::OUStringToOString( str, RTL_TEXTENCODING_ASCII_US );
60 t_print("%s\n", aString.getStr( ) );
63 /** wait _nSec seconds.
65 void thread_sleep( sal_Int32 _nSec )
67 /// print statement in thread process must use fflush() to force display.
68 t_print("# wait %d seconds. ", _nSec );
69 fflush( stdout );
71 #ifdef WNT //Windows
72 Sleep( _nSec * 1000 );
73 #endif
74 #if ( defined UNX ) || ( defined OS2 ) //Unix
75 sleep( _nSec );
76 #endif
77 t_print("# done\n" );
80 enum ConditionType
82 thread_type_set,
83 thread_type_reset,
84 thread_type_wait,
85 thread_type_check
88 /** thread for testing Condition.
90 class ConditionThread : public Thread
92 public:
93 //get the Condition to operate
94 ConditionThread( ::osl::Condition& Con, ConditionType tType): m_MyCon( Con ), m_MyType( tType ) { }
96 ~ConditionThread( )
98 // LLA: do not throw in DTors!
99 // LLA: CPPUNIT_ASSERT_MESSAGE( "#ConditionThread does not shutdown properly.\n", sal_False == this -> isRunning( ) );
101 protected:
102 ::osl::Condition& m_MyCon;
103 ConditionType m_MyType;
105 void SAL_CALL run()
107 switch ( m_MyType )
109 case thread_type_wait:
110 m_MyCon.wait(); break;
111 case thread_type_set:
112 m_MyCon.set(); break;
113 case thread_type_reset:
114 m_MyCon.reset(); break;
115 default:
116 break;
122 //------------------------------------------------------------------------
123 // test code start here
124 //------------------------------------------------------------------------
126 namespace osl_Condition
129 /** testing the method:
130 Condition()
132 class ctors : public CppUnit::TestFixture
134 public:
135 sal_Bool bRes, bRes1;
137 void ctors_001( )
139 ::osl::Condition aCond;
140 bRes = aCond.check( );
142 CPPUNIT_ASSERT_MESSAGE( "#test comment#: create a condition its initial check state should be sal_False.",
143 sal_False == bRes );
146 void ctors_002( )
148 ::osl::Condition aCond;
149 aCond.set( );
150 bRes = aCond.check( );
152 CPPUNIT_ASSERT_MESSAGE( "#test comment#: create a condition and set it.",
153 sal_True == bRes );
156 CPPUNIT_TEST_SUITE( ctors );
157 CPPUNIT_TEST( ctors_001 );
158 CPPUNIT_TEST( ctors_002 );
159 CPPUNIT_TEST_SUITE_END( );
160 }; // class ctors
163 /** testing the method:
164 void set()
166 class set : public CppUnit::TestFixture
168 public:
169 sal_Bool bRes, bRes1, bRes2;
171 void set_001( )
173 ::osl::Condition aCond;
174 aCond.set( );
175 bRes = aCond.check( );
177 CPPUNIT_ASSERT_MESSAGE( "#test comment#: check state should be sal_True after set.",
178 sal_True == bRes );
181 void set_002( )
183 ::osl::Condition aCond;
184 ConditionThread myThread1( aCond, thread_type_wait );
185 myThread1.create();
186 bRes = myThread1.isRunning( );
188 ConditionThread myThread2( aCond, thread_type_set );
189 myThread2.create();
190 thread_sleep(1);
191 bRes1 = myThread1.isRunning( );
192 bRes2 = aCond.check( );
194 myThread1.join( );
195 myThread2.join( );
197 CPPUNIT_ASSERT_MESSAGE( "#test comment#: use one thread to set the condition in order to release another thread.",
198 sal_True == bRes && sal_False == bRes1 && sal_True == bRes2 );
202 CPPUNIT_TEST_SUITE( set );
203 CPPUNIT_TEST( set_001 );
204 CPPUNIT_TEST( set_002 );
205 CPPUNIT_TEST_SUITE_END( );
206 }; // class set
209 /** testing the method:
210 void reset()
212 class reset : public CppUnit::TestFixture
214 public:
215 sal_Bool bRes, bRes1, bRes2;
217 void reset_001( )
219 ::osl::Condition aCond;
220 aCond.reset( );
222 ConditionThread myThread( aCond, thread_type_wait );
223 myThread.create();
224 bRes = myThread.isRunning( );
225 bRes2 = aCond.check( );
227 aCond.set( );
228 myThread.join( );
229 bRes1 = myThread.isRunning( );
231 CPPUNIT_ASSERT_MESSAGE( "#test comment#: wait will cause a reset thread block, use set to release it.",
232 sal_True == bRes && sal_False == bRes1 && sal_False == bRes2 );
235 void reset_002( )
237 ::osl::Condition aCond;
238 aCond.reset( );
239 bRes = aCond.check( );
240 aCond.set( );
241 bRes1 = aCond.check( );
243 CPPUNIT_ASSERT_MESSAGE( "#test comment#: create a condition and reset/set it.",
244 ( sal_False == bRes && sal_True == bRes1 ) );
247 CPPUNIT_TEST_SUITE( reset );
248 CPPUNIT_TEST( reset_001 );
249 CPPUNIT_TEST( reset_002 );
250 CPPUNIT_TEST_SUITE_END( );
251 }; // class reset
254 /** testing the method:
255 Result wait(const TimeValue *pTimeout = 0)
257 class wait : public CppUnit::TestFixture
259 public:
260 sal_Bool bRes, bRes1, bRes2;
261 TimeValue *tv1;
263 void setUp( )
265 tv1 = (TimeValue*)malloc(sizeof(TimeValue));
266 tv1->Seconds = 1;
270 void tearDown( )
272 free( tv1 );
276 void wait_001( )
278 ::osl::Condition cond1;
279 ::osl::Condition cond2;
280 ::osl::Condition cond3;
282 cond1.set();
283 cond2.set();
285 osl::Condition::Result r1=cond1.wait(tv1);
286 osl::Condition::Result r2=cond2.wait();
287 osl::Condition::Result r3=cond3.wait(tv1);
288 fprintf(stderr,"%d %d %d\n",r1,r2,r3);
289 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test three types of wait.",
290 (cond1.wait(tv1) == ::osl::Condition::result_ok) &&
291 (cond2.wait() == ::osl::Condition::result_ok) &&
292 (cond3.wait(tv1) == ::osl::Condition::result_timeout) );
296 void wait_002( )
298 ::osl::Condition aCond;
299 ::osl::Condition::Result wRes, wRes1;
301 aCond.reset( );
302 bRes = aCond.check( );
303 wRes = aCond.wait( tv1 );
305 aCond.set( );
306 wRes1 = aCond.wait( tv1 );
307 bRes1 = aCond.check( );
309 CPPUNIT_ASSERT_MESSAGE( "#test comment#: wait a condition after set/reset.",
310 ( sal_False == bRes ) && ( sal_True == bRes1 ) &&
311 ( ::osl::Condition::result_timeout == wRes ) &&
312 ( ::osl::Condition::result_ok == wRes1 ) );
315 CPPUNIT_TEST_SUITE( wait );
316 CPPUNIT_TEST( wait_001 );
317 CPPUNIT_TEST( wait_002 );
318 CPPUNIT_TEST_SUITE_END( );
319 }; // class wait
322 /** testing the method:
323 sal_Bool check()
325 class check : public CppUnit::TestFixture
327 public:
328 sal_Bool bRes, bRes1, bRes2;
330 void check_001( )
332 ::osl::Condition aCond;
334 aCond.reset( );
335 bRes = aCond.check( );
336 aCond.set( );
337 bRes1 = aCond.check( );
339 CPPUNIT_ASSERT_MESSAGE( "#test comment#: check the condition states.",
340 ( sal_False == bRes && sal_True == bRes1 ) );
343 void check_002( )
345 ::osl::Condition aCond;
346 aCond.reset( );
348 ConditionThread myThread( aCond, thread_type_set );
349 myThread.create( );
350 myThread.join( );
351 bRes = aCond.check( );
353 ConditionThread myThread1( aCond, thread_type_reset );
354 myThread1.create( );
355 myThread1.join( );
356 bRes1 = aCond.check( );
358 CPPUNIT_ASSERT_MESSAGE( "#test comment#: use threads to set/reset Condition and check it in main routine.",
359 ( sal_True == bRes && sal_False == bRes1 ) );
362 CPPUNIT_TEST_SUITE( check );
363 CPPUNIT_TEST( check_001 );
364 CPPUNIT_TEST( check_002 );
365 CPPUNIT_TEST_SUITE_END( );
366 }; // class check
369 // -----------------------------------------------------------------------------
370 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Condition::ctors, "osl_Condition");
371 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Condition::set, "osl_Condition");
372 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Condition::reset, "osl_Condition");
373 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Condition::wait, "osl_Condition");
374 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Condition::check, "osl_Condition");
375 // -----------------------------------------------------------------------------
377 } // namespace osl_Condition
380 // -----------------------------------------------------------------------------
382 // this macro creates an empty function, which will called by the RegisterAllFunctions()
383 // to let the user the possibility to also register some functions by hand.
384 NOADDITIONAL;