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