1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 //------------------------------------------------------------------------
23 //------------------------------------------------------------------------
24 #include <osl_Condition_Const.h>
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
)
49 printf("#printUString_u# " );
50 aString
= ::rtl::OUStringToOString( str
, RTL_TEXTENCODING_ASCII_US
);
51 printf("%s\n", aString
.getStr( ) );
62 /** thread for testing Condition.
64 class ConditionThread
: public Thread
67 //get the Condition to operate
68 ConditionThread( ::osl::Condition
& Con
, ConditionType tType
): m_MyCon( Con
), m_MyType( tType
) { }
72 // LLA: do not throw in DTors!
73 // LLA: CPPUNIT_ASSERT_MESSAGE( "#ConditionThread does not shutdown properly.\n", sal_False == this -> isRunning( ) );
76 ::osl::Condition
& m_MyCon
;
77 ConditionType m_MyType
;
83 case thread_type_wait
:
84 m_MyCon
.wait(); break;
87 case thread_type_reset
:
88 m_MyCon
.reset(); break;
96 //------------------------------------------------------------------------
97 // test code start here
98 //------------------------------------------------------------------------
100 namespace osl_Condition
103 /** testing the method:
106 class ctors
: public CppUnit::TestFixture
109 sal_Bool bRes
, bRes1
;
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.",
122 ::osl::Condition aCond
;
124 bRes
= aCond
.check( );
126 CPPUNIT_ASSERT_MESSAGE( "#test comment#: create a condition and set it.",
130 CPPUNIT_TEST_SUITE( ctors
);
131 CPPUNIT_TEST( ctors_001
);
132 CPPUNIT_TEST( ctors_002
);
133 CPPUNIT_TEST_SUITE_END( );
137 /** testing the method:
140 class set
: public CppUnit::TestFixture
143 sal_Bool bRes
, bRes1
, bRes2
;
147 ::osl::Condition aCond
;
149 bRes
= aCond
.check( );
151 CPPUNIT_ASSERT_MESSAGE( "#test comment#: check state should be sal_True after set.",
157 ::osl::Condition aCond
;
158 ConditionThread
myThread1( aCond
, thread_type_wait
);
160 bRes
= myThread1
.isRunning( );
162 ConditionThread
myThread2( aCond
, thread_type_set
);
166 bRes1
= myThread1
.isRunning( );
167 bRes2
= aCond
.check( );
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( );
182 /** testing the method:
185 class reset
: public CppUnit::TestFixture
188 sal_Bool bRes
, bRes1
, bRes2
;
192 ::osl::Condition aCond
;
195 ConditionThread
myThread( aCond
, thread_type_wait
);
197 bRes
= myThread
.isRunning( );
198 bRes2
= aCond
.check( );
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
);
210 ::osl::Condition aCond
;
212 bRes
= aCond
.check( );
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( );
227 /** testing the method:
228 Result wait(const TimeValue *pTimeout = 0)
230 class wait
: public CppUnit::TestFixture
233 sal_Bool bRes
, bRes1
, bRes2
;
252 ::osl::Condition cond1
;
253 ::osl::Condition cond2
;
254 ::osl::Condition cond3
;
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
) );
271 ::osl::Condition aCond
;
272 ::osl::Condition::Result wRes
, wRes1
;
275 bRes
= aCond
.check( );
276 wRes
= aCond
.wait( tv1
);
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( );
295 /** testing the method:
298 class check
: public CppUnit::TestFixture
301 sal_Bool bRes
, bRes1
, bRes2
;
305 ::osl::Condition aCond
;
308 bRes
= aCond
.check( );
310 bRes1
= aCond
.check( );
312 CPPUNIT_ASSERT_MESSAGE( "#test comment#: check the condition states.",
313 ( sal_False
== bRes
&& sal_True
== bRes1
) );
318 ::osl::Condition aCond
;
321 ConditionThread
myThread( aCond
, thread_type_set
);
324 bRes
= aCond
.check( );
326 ConditionThread
myThread1( aCond
, thread_type_reset
);
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( );
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: */