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