merge the formfield patch from ooo-build
[ooovba.git] / sal / qa / osl / condition / osl_Condition.cxx
blob867df04f213b6f1df47342b786b3992cf4887553
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: osl_Condition.cxx,v $
10 * $Revision: 1.8 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sal.hxx"
34 //------------------------------------------------------------------------
35 // include files
36 //------------------------------------------------------------------------
37 #include <osl_Condition_Const.h>
39 using namespace osl;
40 using namespace rtl;
43 //------------------------------------------------------------------------
44 // helper functions and classes
45 //------------------------------------------------------------------------
47 /** print Boolean value.
49 inline void printBool( sal_Bool bOk )
51 t_print("#printBool# " );
52 ( sal_True == bOk ) ? t_print("TRUE!\n" ): t_print("FALSE!\n" );
55 /** print a UNI_CODE String.
57 inline void printUString( const ::rtl::OUString & str )
59 rtl::OString aString;
61 t_print("#printUString_u# " );
62 aString = ::rtl::OUStringToOString( str, RTL_TEXTENCODING_ASCII_US );
63 t_print("%s\n", aString.getStr( ) );
66 /** wait _nSec seconds.
68 void thread_sleep( sal_Int32 _nSec )
70 /// print statement in thread process must use fflush() to force display.
71 t_print("# wait %d seconds. ", _nSec );
72 fflush( stdout );
74 #ifdef WNT //Windows
75 Sleep( _nSec * 1000 );
76 #endif
77 #if ( defined UNX ) || ( defined OS2 ) //Unix
78 sleep( _nSec );
79 #endif
80 t_print("# done\n" );
83 enum ConditionType
85 thread_type_set,
86 thread_type_reset,
87 thread_type_wait,
88 thread_type_check
91 /** thread for testing Condition.
93 class ConditionThread : public Thread
95 public:
96 //get the Condition to operate
97 ConditionThread( ::osl::Condition& Con, ConditionType tType): m_MyCon( Con ), m_MyType( tType ) { }
99 ~ConditionThread( )
101 // LLA: do not throw in DTors!
102 // LLA: CPPUNIT_ASSERT_MESSAGE( "#ConditionThread does not shutdown properly.\n", sal_False == this -> isRunning( ) );
104 protected:
105 ::osl::Condition& m_MyCon;
106 ConditionType m_MyType;
108 void SAL_CALL run()
110 switch ( m_MyType )
112 case thread_type_wait:
113 m_MyCon.wait(); break;
114 case thread_type_set:
115 m_MyCon.set(); break;
116 case thread_type_reset:
117 m_MyCon.reset(); break;
118 default:
119 break;
125 //------------------------------------------------------------------------
126 // test code start here
127 //------------------------------------------------------------------------
129 namespace osl_Condition
132 /** testing the method:
133 Condition()
135 class ctors : public CppUnit::TestFixture
137 public:
138 sal_Bool bRes, bRes1;
140 void ctors_001( )
142 ::osl::Condition aCond;
143 bRes = aCond.check( );
145 CPPUNIT_ASSERT_MESSAGE( "#test comment#: create a condition its initial check state should be sal_False.",
146 sal_False == bRes );
149 void ctors_002( )
151 ::osl::Condition aCond;
152 aCond.set( );
153 bRes = aCond.check( );
155 CPPUNIT_ASSERT_MESSAGE( "#test comment#: create a condition and set it.",
156 sal_True == bRes );
159 CPPUNIT_TEST_SUITE( ctors );
160 CPPUNIT_TEST( ctors_001 );
161 CPPUNIT_TEST( ctors_002 );
162 CPPUNIT_TEST_SUITE_END( );
163 }; // class ctors
166 /** testing the method:
167 void set()
169 class set : public CppUnit::TestFixture
171 public:
172 sal_Bool bRes, bRes1, bRes2;
174 void set_001( )
176 ::osl::Condition aCond;
177 aCond.set( );
178 bRes = aCond.check( );
180 CPPUNIT_ASSERT_MESSAGE( "#test comment#: check state should be sal_True after set.",
181 sal_True == bRes );
184 void set_002( )
186 ::osl::Condition aCond;
187 ConditionThread myThread1( aCond, thread_type_wait );
188 myThread1.create();
189 bRes = myThread1.isRunning( );
191 ConditionThread myThread2( aCond, thread_type_set );
192 myThread2.create();
193 thread_sleep(1);
194 bRes1 = myThread1.isRunning( );
195 bRes2 = aCond.check( );
197 myThread1.join( );
198 myThread2.join( );
200 CPPUNIT_ASSERT_MESSAGE( "#test comment#: use one thread to set the condition in order to release another thread.",
201 sal_True == bRes && sal_False == bRes1 && sal_True == bRes2 );
205 CPPUNIT_TEST_SUITE( set );
206 CPPUNIT_TEST( set_001 );
207 CPPUNIT_TEST( set_002 );
208 CPPUNIT_TEST_SUITE_END( );
209 }; // class set
212 /** testing the method:
213 void reset()
215 class reset : public CppUnit::TestFixture
217 public:
218 sal_Bool bRes, bRes1, bRes2;
220 void reset_001( )
222 ::osl::Condition aCond;
223 aCond.reset( );
225 ConditionThread myThread( aCond, thread_type_wait );
226 myThread.create();
227 bRes = myThread.isRunning( );
228 bRes2 = aCond.check( );
230 aCond.set( );
231 myThread.join( );
232 bRes1 = myThread.isRunning( );
234 CPPUNIT_ASSERT_MESSAGE( "#test comment#: wait will cause a reset thread block, use set to release it.",
235 sal_True == bRes && sal_False == bRes1 && sal_False == bRes2 );
238 void reset_002( )
240 ::osl::Condition aCond;
241 aCond.reset( );
242 bRes = aCond.check( );
243 aCond.set( );
244 bRes1 = aCond.check( );
246 CPPUNIT_ASSERT_MESSAGE( "#test comment#: create a condition and reset/set it.",
247 ( sal_False == bRes && sal_True == bRes1 ) );
250 CPPUNIT_TEST_SUITE( reset );
251 CPPUNIT_TEST( reset_001 );
252 CPPUNIT_TEST( reset_002 );
253 CPPUNIT_TEST_SUITE_END( );
254 }; // class reset
257 /** testing the method:
258 Result wait(const TimeValue *pTimeout = 0)
260 class wait : public CppUnit::TestFixture
262 public:
263 sal_Bool bRes, bRes1, bRes2;
264 TimeValue *tv1;
266 void setUp( )
268 tv1 = (TimeValue*)malloc(sizeof(TimeValue));
269 tv1->Seconds = 1;
273 void tearDown( )
275 free( tv1 );
279 void wait_001( )
281 ::osl::Condition cond1;
282 ::osl::Condition cond2;
283 ::osl::Condition cond3;
285 cond1.set();
286 cond2.set();
288 osl::Condition::Result r1=cond1.wait(tv1);
289 osl::Condition::Result r2=cond2.wait();
290 osl::Condition::Result r3=cond3.wait(tv1);
291 fprintf(stderr,"%d %d %d\n",r1,r2,r3);
292 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test three types of wait.",
293 (cond1.wait(tv1) == ::osl::Condition::result_ok) &&
294 (cond2.wait() == ::osl::Condition::result_ok) &&
295 (cond3.wait(tv1) == ::osl::Condition::result_timeout) );
299 void wait_002( )
301 ::osl::Condition aCond;
302 ::osl::Condition::Result wRes, wRes1;
304 aCond.reset( );
305 bRes = aCond.check( );
306 wRes = aCond.wait( tv1 );
308 aCond.set( );
309 wRes1 = aCond.wait( tv1 );
310 bRes1 = aCond.check( );
312 CPPUNIT_ASSERT_MESSAGE( "#test comment#: wait a condition after set/reset.",
313 ( sal_False == bRes ) && ( sal_True == bRes1 ) &&
314 ( ::osl::Condition::result_timeout == wRes ) &&
315 ( ::osl::Condition::result_ok == wRes1 ) );
318 CPPUNIT_TEST_SUITE( wait );
319 CPPUNIT_TEST( wait_001 );
320 CPPUNIT_TEST( wait_002 );
321 CPPUNIT_TEST_SUITE_END( );
322 }; // class wait
325 /** testing the method:
326 sal_Bool check()
328 class check : public CppUnit::TestFixture
330 public:
331 sal_Bool bRes, bRes1, bRes2;
333 void check_001( )
335 ::osl::Condition aCond;
337 aCond.reset( );
338 bRes = aCond.check( );
339 aCond.set( );
340 bRes1 = aCond.check( );
342 CPPUNIT_ASSERT_MESSAGE( "#test comment#: check the condition states.",
343 ( sal_False == bRes && sal_True == bRes1 ) );
346 void check_002( )
348 ::osl::Condition aCond;
349 aCond.reset( );
351 ConditionThread myThread( aCond, thread_type_set );
352 myThread.create( );
353 myThread.join( );
354 bRes = aCond.check( );
356 ConditionThread myThread1( aCond, thread_type_reset );
357 myThread1.create( );
358 myThread1.join( );
359 bRes1 = aCond.check( );
361 CPPUNIT_ASSERT_MESSAGE( "#test comment#: use threads to set/reset Condition and check it in main routine.",
362 ( sal_True == bRes && sal_False == bRes1 ) );
365 CPPUNIT_TEST_SUITE( check );
366 CPPUNIT_TEST( check_001 );
367 CPPUNIT_TEST( check_002 );
368 CPPUNIT_TEST_SUITE_END( );
369 }; // class check
372 // -----------------------------------------------------------------------------
373 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Condition::ctors, "osl_Condition");
374 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Condition::set, "osl_Condition");
375 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Condition::reset, "osl_Condition");
376 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Condition::wait, "osl_Condition");
377 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Condition::check, "osl_Condition");
378 // -----------------------------------------------------------------------------
380 } // namespace osl_Condition
383 // -----------------------------------------------------------------------------
385 // this macro creates an empty function, which will called by the RegisterAllFunctions()
386 // to let the user the possibility to also register some functions by hand.
387 NOADDITIONAL;