Version 4.2.0.1, tag libreoffice-4.2.0.1
[LibreOffice.git] / sal / qa / osl / condition / osl_Condition.cxx
blobab51d83ff586d52a910344f1d8b7a15b2432bc3a
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 //------------------------------------------------------------------------
22 // include files
23 //------------------------------------------------------------------------
24 #include <osl_Condition_Const.h>
25 #include <stdlib.h>
27 using namespace osl;
28 using namespace rtl;
31 //------------------------------------------------------------------------
32 // helper functions and classes
33 //------------------------------------------------------------------------
35 /** print Boolean value.
37 inline void printBool( sal_Bool bOk )
39 printf("#printBool# " );
40 ( sal_True == bOk ) ? printf("TRUE!\n" ): printf("FALSE!\n" );
43 /** print a UNI_CODE String.
45 inline void printUString( const ::rtl::OUString & str )
47 rtl::OString aString;
49 printf("#printUString_u# " );
50 aString = ::rtl::OUStringToOString( str, RTL_TEXTENCODING_ASCII_US );
51 printf("%s\n", aString.getStr( ) );
54 enum ConditionType
56 thread_type_set,
57 thread_type_reset,
58 thread_type_wait,
59 thread_type_check
62 /** thread for testing Condition.
64 class ConditionThread : public Thread
66 public:
67 //get the Condition to operate
68 ConditionThread( ::osl::Condition& Con, ConditionType tType): m_MyCon( Con ), m_MyType( tType ) { }
70 ~ConditionThread( )
72 // LLA: do not throw in DTors!
73 // LLA: CPPUNIT_ASSERT_MESSAGE( "#ConditionThread does not shutdown properly.\n", sal_False == this -> isRunning( ) );
75 protected:
76 ::osl::Condition& m_MyCon;
77 ConditionType m_MyType;
79 void SAL_CALL run()
81 switch ( m_MyType )
83 case thread_type_wait:
84 m_MyCon.wait(); break;
85 case thread_type_set:
86 m_MyCon.set(); break;
87 case thread_type_reset:
88 m_MyCon.reset(); break;
89 default:
90 break;
96 //------------------------------------------------------------------------
97 // test code start here
98 //------------------------------------------------------------------------
100 namespace osl_Condition
103 /** testing the method:
104 Condition()
106 class ctors : public CppUnit::TestFixture
108 public:
109 sal_Bool bRes, bRes1;
111 void ctors_001( )
113 ::osl::Condition aCond;
114 bRes = aCond.check( );
116 CPPUNIT_ASSERT_MESSAGE( "#test comment#: create a condition its initial check state should be sal_False.",
117 sal_False == bRes );
120 void ctors_002( )
122 ::osl::Condition aCond;
123 aCond.set( );
124 bRes = aCond.check( );
126 CPPUNIT_ASSERT_MESSAGE( "#test comment#: create a condition and set it.",
127 sal_True == bRes );
130 CPPUNIT_TEST_SUITE( ctors );
131 CPPUNIT_TEST( ctors_001 );
132 CPPUNIT_TEST( ctors_002 );
133 CPPUNIT_TEST_SUITE_END( );
134 }; // class ctors
137 /** testing the method:
138 void set()
140 class set : public CppUnit::TestFixture
142 public:
143 sal_Bool bRes, bRes1, bRes2;
145 void set_001( )
147 ::osl::Condition aCond;
148 aCond.set( );
149 bRes = aCond.check( );
151 CPPUNIT_ASSERT_MESSAGE( "#test comment#: check state should be sal_True after set.",
152 sal_True == bRes );
155 void set_002( )
157 ::osl::Condition aCond;
158 ConditionThread myThread1( aCond, thread_type_wait );
159 myThread1.create();
160 bRes = myThread1.isRunning( );
162 ConditionThread myThread2( aCond, thread_type_set );
163 myThread2.create();
165 myThread1.join( );
166 bRes1 = myThread1.isRunning( );
167 bRes2 = aCond.check( );
168 myThread2.join( );
170 CPPUNIT_ASSERT_MESSAGE( "#test comment#: use one thread to set the condition in order to release another thread.",
171 sal_True == bRes && sal_False == bRes1 && sal_True == bRes2 );
175 CPPUNIT_TEST_SUITE( set );
176 CPPUNIT_TEST( set_001 );
177 CPPUNIT_TEST( set_002 );
178 CPPUNIT_TEST_SUITE_END( );
179 }; // class set
182 /** testing the method:
183 void reset()
185 class reset : public CppUnit::TestFixture
187 public:
188 sal_Bool bRes, bRes1, bRes2;
190 void reset_001( )
192 ::osl::Condition aCond;
193 aCond.reset( );
195 ConditionThread myThread( aCond, thread_type_wait );
196 myThread.create();
197 bRes = myThread.isRunning( );
198 bRes2 = aCond.check( );
200 aCond.set( );
201 myThread.join( );
202 bRes1 = myThread.isRunning( );
204 CPPUNIT_ASSERT_MESSAGE( "#test comment#: wait will cause a reset thread block, use set to release it.",
205 sal_True == bRes && sal_False == bRes1 && sal_False == bRes2 );
208 void reset_002( )
210 ::osl::Condition aCond;
211 aCond.reset( );
212 bRes = aCond.check( );
213 aCond.set( );
214 bRes1 = aCond.check( );
216 CPPUNIT_ASSERT_MESSAGE( "#test comment#: create a condition and reset/set it.",
217 ( sal_False == bRes && sal_True == bRes1 ) );
220 CPPUNIT_TEST_SUITE( reset );
221 CPPUNIT_TEST( reset_001 );
222 CPPUNIT_TEST( reset_002 );
223 CPPUNIT_TEST_SUITE_END( );
224 }; // class reset
227 /** testing the method:
228 Result wait(const TimeValue *pTimeout = 0)
230 class wait : public CppUnit::TestFixture
232 public:
233 sal_Bool bRes, bRes1, bRes2;
234 TimeValue *tv1;
236 void setUp( )
238 tv1 = new TimeValue;
239 tv1->Seconds = 1;
240 tv1->Nanosec = 0;
244 void tearDown( )
246 delete tv1;
250 void wait_001( )
252 ::osl::Condition cond1;
253 ::osl::Condition cond2;
254 ::osl::Condition cond3;
256 cond1.set();
257 cond2.set();
259 osl::Condition::Result r1=cond1.wait(tv1);
260 osl::Condition::Result r2=cond2.wait();
261 osl::Condition::Result r3=cond3.wait(tv1);
263 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test three types of wait.",
264 (r1 == ::osl::Condition::result_ok) &&
265 (r2 == ::osl::Condition::result_ok) &&
266 (r3 == ::osl::Condition::result_timeout) );
269 void wait_002( )
271 ::osl::Condition aCond;
272 ::osl::Condition::Result wRes, wRes1;
274 aCond.reset( );
275 bRes = aCond.check( );
276 wRes = aCond.wait( tv1 );
278 aCond.set( );
279 wRes1 = aCond.wait( tv1 );
280 bRes1 = aCond.check( );
282 CPPUNIT_ASSERT_MESSAGE( "#test comment#: wait a condition after set/reset.",
283 ( sal_False == bRes ) && ( sal_True == bRes1 ) &&
284 ( ::osl::Condition::result_timeout == wRes ) &&
285 ( ::osl::Condition::result_ok == wRes1 ) );
288 CPPUNIT_TEST_SUITE( wait );
289 CPPUNIT_TEST( wait_001 );
290 CPPUNIT_TEST( wait_002 );
291 CPPUNIT_TEST_SUITE_END( );
292 }; // class wait
295 /** testing the method:
296 sal_Bool check()
298 class check : public CppUnit::TestFixture
300 public:
301 sal_Bool bRes, bRes1, bRes2;
303 void check_001( )
305 ::osl::Condition aCond;
307 aCond.reset( );
308 bRes = aCond.check( );
309 aCond.set( );
310 bRes1 = aCond.check( );
312 CPPUNIT_ASSERT_MESSAGE( "#test comment#: check the condition states.",
313 ( sal_False == bRes && sal_True == bRes1 ) );
316 void check_002( )
318 ::osl::Condition aCond;
319 aCond.reset( );
321 ConditionThread myThread( aCond, thread_type_set );
322 myThread.create( );
323 myThread.join( );
324 bRes = aCond.check( );
326 ConditionThread myThread1( aCond, thread_type_reset );
327 myThread1.create( );
328 myThread1.join( );
329 bRes1 = aCond.check( );
331 CPPUNIT_ASSERT_MESSAGE( "#test comment#: use threads to set/reset Condition and check it in main routine.",
332 ( sal_True == bRes && sal_False == bRes1 ) );
335 CPPUNIT_TEST_SUITE( check );
336 CPPUNIT_TEST( check_001 );
337 CPPUNIT_TEST( check_002 );
338 CPPUNIT_TEST_SUITE_END( );
339 }; // class check
342 // -----------------------------------------------------------------------------
343 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Condition::ctors);
344 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Condition::set);
345 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Condition::reset);
346 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Condition::wait);
347 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Condition::check);
348 // -----------------------------------------------------------------------------
350 } // namespace osl_Condition
353 // -----------------------------------------------------------------------------
355 CPPUNIT_PLUGIN_IMPLEMENT();
358 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */