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