1  /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
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.
14  *
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).
20  *
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.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_sal.hxx"
30 
31 //------------------------------------------------------------------------
32 // include files
33 //------------------------------------------------------------------------
34 #include <osl_Condition_Const.h>
35 
36 using namespace	osl;
37 using namespace	rtl;
38 
39 
40 //------------------------------------------------------------------------
41 // helper functions and classes
42 //------------------------------------------------------------------------
43 
44 /** print Boolean value.
45 */
46 inline void printBool( sal_Bool bOk )
47 {
48 	t_print("#printBool# " );
49 	( sal_True == bOk ) ? t_print("TRUE!\n" ): t_print("FALSE!\n" );
50 }
51 
52 /** print a UNI_CODE String.
53 */
54 inline void printUString( const ::rtl::OUString & str )
55 {
56 	rtl::OString aString;
57 
58 	t_print("#printUString_u# " );
59 	aString = ::rtl::OUStringToOString( str, RTL_TEXTENCODING_ASCII_US );
60 	t_print("%s\n", aString.getStr( ) );
61 }
62 
63 /** wait _nSec seconds.
64 */
65 void thread_sleep( sal_Int32 _nSec )
66 {
67 	/// print statement in thread process must use fflush() to force display.
68 	t_print("# wait %d seconds. ", _nSec );
69 	fflush( stdout );
70 
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" );
78 }
79 
80 enum ConditionType
81 {
82 	thread_type_set,
83 	thread_type_reset,
84 	thread_type_wait,
85 	thread_type_check
86 };
87 
88 /** thread for testing Condition.
89  */
90 class ConditionThread : public Thread
91 {
92 public:
93 	//get the Condition to operate
94 	ConditionThread( ::osl::Condition& Con, ConditionType tType): m_MyCon( Con ), m_MyType( tType ) { }
95 
96 	~ConditionThread( )
97 	{
98         // LLA: do not throw in DTors!
99 		// LLA: CPPUNIT_ASSERT_MESSAGE( "#ConditionThread does not shutdown properly.\n", sal_False == this -> isRunning( ) );
100 	}
101 protected:
102 	::osl::Condition& m_MyCon;
103 	ConditionType m_MyType;
104 
105 	void SAL_CALL run()
106 	{
107 		switch ( m_MyType )
108 		{
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;
117 		}
118 	}
119 };
120 
121 
122 //------------------------------------------------------------------------
123 // test code start here
124 //------------------------------------------------------------------------
125 
126 namespace osl_Condition
127 {
128 
129 	/** testing the method:
130 		Condition()
131 	*/
132 	class ctors : public CppUnit::TestFixture
133 	{
134 	public:
135 		sal_Bool bRes, bRes1;
136 
137 		void ctors_001( )
138 		{
139 			::osl::Condition aCond;
140 			bRes = aCond.check( );
141 
142 			CPPUNIT_ASSERT_MESSAGE( "#test comment#: create a condition its initial check state should be sal_False.",
143 									sal_False == bRes );
144 		}
145 
146 		void ctors_002( )
147 		{
148 			::osl::Condition aCond;
149 			aCond.set( );
150 			bRes = aCond.check( );
151 
152 			CPPUNIT_ASSERT_MESSAGE( "#test comment#: create a condition and set it.",
153 									sal_True == bRes );
154 		}
155 
156 		CPPUNIT_TEST_SUITE( ctors );
157 		CPPUNIT_TEST( ctors_001 );
158 		CPPUNIT_TEST( ctors_002 );
159 		CPPUNIT_TEST_SUITE_END( );
160 	}; // class ctors
161 
162 
163 	/** testing the method:
164 		void set()
165 	*/
166 	class set : public CppUnit::TestFixture
167 	{
168 	public:
169 		sal_Bool bRes, bRes1, bRes2;
170 
171 		void set_001( )
172 		{
173 			::osl::Condition aCond;
174 			aCond.set( );
175 			bRes = aCond.check( );
176 
177 			CPPUNIT_ASSERT_MESSAGE( "#test comment#: check state should be sal_True after set.",
178 									sal_True == bRes );
179 		}
180 
181 		void set_002( )
182 		{
183 			::osl::Condition aCond;
184 			ConditionThread myThread1( aCond, thread_type_wait );
185 			myThread1.create();
186 			bRes = myThread1.isRunning( );
187 
188 			ConditionThread myThread2( aCond, thread_type_set );
189 			myThread2.create();
190 			thread_sleep(1);
191 			bRes1 = myThread1.isRunning( );
192 			bRes2 = aCond.check( );
193 
194 			myThread1.join( );
195 			myThread2.join( );
196 
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 );
199 		}
200 
201 
202 		CPPUNIT_TEST_SUITE( set );
203 		CPPUNIT_TEST( set_001 );
204 		CPPUNIT_TEST( set_002 );
205 		CPPUNIT_TEST_SUITE_END( );
206 	}; // class set
207 
208 
209 	/** testing the method:
210 		void reset()
211 	*/
212 	class reset : public CppUnit::TestFixture
213 	{
214 	public:
215 		sal_Bool bRes, bRes1, bRes2;
216 
217 		void reset_001( )
218 		{
219 			::osl::Condition aCond;
220 			aCond.reset( );
221 
222 			ConditionThread myThread( aCond, thread_type_wait );
223 			myThread.create();
224 			bRes = myThread.isRunning( );
225 			bRes2 = aCond.check( );
226 
227 			aCond.set( );
228 			myThread.join( );
229 			bRes1 = myThread.isRunning( );
230 
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 );
233 		}
234 
235 		void reset_002( )
236 		{
237 			::osl::Condition aCond;
238 			aCond.reset( );
239 			bRes = aCond.check( );
240 			aCond.set( );
241 			bRes1 = aCond.check( );
242 
243 			CPPUNIT_ASSERT_MESSAGE( "#test comment#: create a condition and reset/set it.",
244 									( sal_False == bRes && sal_True == bRes1 ) );
245 		}
246 
247 		CPPUNIT_TEST_SUITE( reset );
248 		CPPUNIT_TEST( reset_001 );
249 		CPPUNIT_TEST( reset_002 );
250 		CPPUNIT_TEST_SUITE_END( );
251 	}; // class reset
252 
253 
254 	/** testing the method:
255 		Result wait(const TimeValue *pTimeout = 0)
256 	*/
257 	class wait : public CppUnit::TestFixture
258 	{
259 	public:
260 		sal_Bool bRes, bRes1, bRes2;
261 		TimeValue *tv1;
262 
263 		void setUp( )
264 		{
265 			tv1 = (TimeValue*)malloc(sizeof(TimeValue));
266 			tv1->Seconds = 1;
267 
268 		}
269 
270 		void tearDown( )
271 		{
272 			free( tv1 );
273 		}
274 
275 
276 		void wait_001( )
277 		{
278 			::osl::Condition cond1;
279 			::osl::Condition cond2;
280 			::osl::Condition cond3;
281 
282 			cond1.set();
283 			cond2.set();
284 
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) );
293 
294 		}
295 
296 		void wait_002( )
297 		{
298 			::osl::Condition aCond;
299 			::osl::Condition::Result wRes, wRes1;
300 
301 			aCond.reset( );
302 			bRes = aCond.check( );
303 			wRes = aCond.wait( tv1 );
304 
305 			aCond.set( );
306 			wRes1 = aCond.wait( tv1 );
307 			bRes1 = aCond.check( );
308 
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 ) );
313 		}
314 
315 		CPPUNIT_TEST_SUITE( wait );
316 		CPPUNIT_TEST( wait_001 );
317 		CPPUNIT_TEST( wait_002 );
318 		CPPUNIT_TEST_SUITE_END( );
319 	}; // class wait
320 
321 
322 	/** testing the method:
323 		sal_Bool check()
324 	*/
325 	class check : public CppUnit::TestFixture
326 	{
327 	public:
328 		sal_Bool bRes, bRes1, bRes2;
329 
330 		void check_001( )
331 		{
332 			::osl::Condition aCond;
333 
334 			aCond.reset( );
335 			bRes = aCond.check( );
336 			aCond.set( );
337 			bRes1 = aCond.check( );
338 
339 			CPPUNIT_ASSERT_MESSAGE( "#test comment#: check the condition states.",
340 									( sal_False == bRes && sal_True == bRes1 ) );
341 		}
342 
343 		void check_002( )
344 		{
345 			::osl::Condition aCond;
346 			aCond.reset( );
347 
348 			ConditionThread myThread( aCond, thread_type_set );
349 			myThread.create( );
350 			myThread.join( );
351 			bRes = aCond.check( );
352 
353 			ConditionThread myThread1( aCond, thread_type_reset );
354 			myThread1.create( );
355 			myThread1.join( );
356 			bRes1 = aCond.check( );
357 
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 ) );
360 		}
361 
362 		CPPUNIT_TEST_SUITE( check );
363 		CPPUNIT_TEST( check_001 );
364 		CPPUNIT_TEST( check_002 );
365 		CPPUNIT_TEST_SUITE_END( );
366 	}; // class check
367 
368 
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 // -----------------------------------------------------------------------------
376 
377 } // namespace osl_Condition
378 
379 
380 // -----------------------------------------------------------------------------
381 
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;
385