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 .
20 #include <sal/types.h>
21 #include <osl/thread.hxx>
22 #include <osl/conditn.hxx>
25 #include <cppunit/TestFixture.h>
26 #include <cppunit/extensions/HelperMacros.h>
27 #include <cppunit/plugin/TestPlugIn.h>
40 /** thread for testing Condition.
42 class ConditionThread
: public Thread
45 //get the Condition to operate
46 ConditionThread( ::osl::Condition
& Con
, ConditionType tType
): m_MyCon( Con
), m_MyType( tType
) { }
49 ::osl::Condition
& m_MyCon
;
50 ConditionType m_MyType
;
52 void SAL_CALL
run() override
56 case thread_type_wait
:
57 m_MyCon
.wait(); break;
60 case thread_type_reset
:
61 m_MyCon
.reset(); break;
70 namespace osl_Condition
72 /** testing the method:
75 class ctors
: public CppUnit::TestFixture
82 ::osl::Condition aCond
;
85 CPPUNIT_ASSERT_MESSAGE("#test comment#: create a condition its initial check state should be sal_False.",
89 void ctors_createAndSet()
91 ::osl::Condition aCond
;
95 CPPUNIT_ASSERT_MESSAGE("#test comment#: create a condition and set it.",
99 CPPUNIT_TEST_SUITE(ctors
);
100 CPPUNIT_TEST(ctors_create
);
101 CPPUNIT_TEST(ctors_createAndSet
);
102 CPPUNIT_TEST_SUITE_END();
105 /** testing the method:
108 class set
: public CppUnit::TestFixture
111 bool bRes
, bRes1
, bRes2
;
113 void set_createAndSet()
115 ::osl::Condition aCond
;
117 bRes
= aCond
.check();
119 CPPUNIT_ASSERT_MESSAGE("#test comment#: check state should be sal_True after set.",
123 void set_threadWaitRelease()
125 ::osl::Condition aCond
;
126 ConditionThread
myThread1(aCond
, thread_type_wait
);
128 bRes
= myThread1
.isRunning();
130 ConditionThread
myThread2(aCond
, thread_type_set
);
134 bRes1
= myThread1
.isRunning();
135 bRes2
= aCond
.check();
138 CPPUNIT_ASSERT_MESSAGE("#test comment#: use one thread to set the condition in order to release another thread.",
140 CPPUNIT_ASSERT_MESSAGE("#test comment#: use one thread to set the condition in order to release another thread.",
142 CPPUNIT_ASSERT_MESSAGE( "#test comment#: use one thread to set the condition in order to release another thread.",
146 CPPUNIT_TEST_SUITE(set
);
147 CPPUNIT_TEST(set_createAndSet
);
148 CPPUNIT_TEST(set_threadWaitRelease
);
149 CPPUNIT_TEST_SUITE_END( );
152 /** testing the method:
155 class reset
: public CppUnit::TestFixture
158 bool bRes
, bRes1
, bRes2
;
160 void reset_resetWaitAndSet()
162 ::osl::Condition aCond
;
165 ConditionThread
myThread(aCond
, thread_type_wait
);
167 bRes
= myThread
.isRunning();
168 bRes2
= aCond
.check();
172 bRes1
= myThread
.isRunning();
174 CPPUNIT_ASSERT_MESSAGE("#test comment#: wait will cause a reset thread block, use set to release it.",
176 CPPUNIT_ASSERT_MESSAGE("#test comment#: wait will cause a reset thread block, use set to release it.",
178 CPPUNIT_ASSERT_MESSAGE("#test comment#: wait will cause a reset thread block, use set to release it.",
182 void reset_resetAndSet()
184 ::osl::Condition aCond
;
186 bRes
= aCond
.check();
188 bRes1
= aCond
.check();
190 CPPUNIT_ASSERT_MESSAGE("#test comment#: create a condition and reset/set it.",
192 CPPUNIT_ASSERT_MESSAGE("#test comment#: create a condition and reset/set it.",
196 CPPUNIT_TEST_SUITE(reset
);
197 CPPUNIT_TEST(reset_resetWaitAndSet
);
198 CPPUNIT_TEST(reset_resetAndSet
);
199 CPPUNIT_TEST_SUITE_END();
202 /** testing the method:
203 Result wait(const TimeValue *pTimeout = 0)
205 class wait
: public CppUnit::TestFixture
208 bool bRes
, bRes1
, bRes2
;
209 std::unique_ptr
<TimeValue
> tv1
;
211 void setUp() override
213 tv1
.reset(new TimeValue
);
218 void tearDown() override
223 void wait_testAllCombos( )
225 ::osl::Condition cond1
;
226 ::osl::Condition cond2
;
227 ::osl::Condition cond3
;
232 osl::Condition::Result r1
=cond1
.wait(tv1
.get());
233 osl::Condition::Result r2
=cond2
.wait();
234 osl::Condition::Result r3
=cond3
.wait(tv1
.get());
236 CPPUNIT_ASSERT_EQUAL_MESSAGE( "#test comment#: test three types of wait.",
237 ::osl::Condition::result_ok
, r1
);
238 CPPUNIT_ASSERT_EQUAL_MESSAGE( "#test comment#: test three types of wait.",
239 ::osl::Condition::result_ok
, r2
);
240 CPPUNIT_ASSERT_EQUAL_MESSAGE( "#test comment#: test three types of wait.",
241 ::osl::Condition::result_timeout
, r3
);
244 void wait_timeoutWaits()
246 ::osl::Condition aCond
;
247 ::osl::Condition::Result wRes
, wRes1
;
250 bRes
= aCond
.check();
251 wRes
= aCond
.wait(tv1
.get());
254 wRes1
= aCond
.wait(tv1
.get());
255 bRes1
= aCond
.check();
257 CPPUNIT_ASSERT_MESSAGE("#test comment#: wait a condition after set/reset.",
259 CPPUNIT_ASSERT_MESSAGE("#test comment#: wait a condition after set/reset.",
261 CPPUNIT_ASSERT_EQUAL_MESSAGE("#test comment#: wait a condition after set/reset.",
262 ::osl::Condition::result_timeout
, wRes
);
263 CPPUNIT_ASSERT_EQUAL_MESSAGE("#test comment#: wait a condition after set/reset.",
264 ::osl::Condition::result_ok
, wRes1
);
267 CPPUNIT_TEST_SUITE(wait
);
268 CPPUNIT_TEST(wait_testAllCombos
);
269 CPPUNIT_TEST(wait_timeoutWaits
);
270 CPPUNIT_TEST_SUITE_END();
273 /** testing the method:
276 class check
: public CppUnit::TestFixture
279 bool bRes
, bRes1
, bRes2
;
281 void check_checkStates()
283 ::osl::Condition aCond
;
286 bRes
= aCond
.check();
288 bRes1
= aCond
.check();
290 CPPUNIT_ASSERT_MESSAGE("#test comment#: check the condition states.",
292 CPPUNIT_ASSERT_MESSAGE("#test comment#: check the condition states.",
296 void check_threadedCheckStates( )
298 ::osl::Condition aCond
;
301 ConditionThread
myThread(aCond
, thread_type_set
);
304 bRes
= aCond
.check();
306 ConditionThread
myThread1(aCond
, thread_type_reset
);
309 bRes1
= aCond
.check();
311 CPPUNIT_ASSERT_MESSAGE("#test comment#: use threads to set/reset Condition and check it in main routine.",
313 CPPUNIT_ASSERT_MESSAGE("#test comment#: use threads to set/reset Condition and check it in main routine.",
317 CPPUNIT_TEST_SUITE(check
);
318 CPPUNIT_TEST(check_checkStates
);
319 CPPUNIT_TEST(check_threadedCheckStates
);
320 CPPUNIT_TEST_SUITE_END();
323 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Condition::ctors
);
324 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Condition::set
);
325 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Condition::reset
);
326 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Condition::wait
);
327 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Condition::check
);
329 } // namespace osl_Condition
331 CPPUNIT_PLUGIN_IMPLEMENT();
333 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */