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 "osl_Condition_Const.h"
34 /** thread for testing Condition.
36 class ConditionThread
: public Thread
39 //get the Condition to operate
40 ConditionThread( ::osl::Condition
& Con
, ConditionType tType
): m_MyCon( Con
), m_MyType( tType
) { }
43 ::osl::Condition
& m_MyCon
;
44 ConditionType m_MyType
;
46 void SAL_CALL
run() override
50 case thread_type_wait
:
51 m_MyCon
.wait(); break;
54 case thread_type_reset
:
55 m_MyCon
.reset(); break;
64 namespace osl_Condition
66 /** testing the method:
69 class ctors
: public CppUnit::TestFixture
76 ::osl::Condition aCond
;
79 CPPUNIT_ASSERT_MESSAGE("#test comment#: create a condition its initial check state should be sal_False.",
83 void ctors_createAndSet()
85 ::osl::Condition aCond
;
89 CPPUNIT_ASSERT_MESSAGE("#test comment#: create a condition and set it.",
93 CPPUNIT_TEST_SUITE(ctors
);
94 CPPUNIT_TEST(ctors_create
);
95 CPPUNIT_TEST(ctors_createAndSet
);
96 CPPUNIT_TEST_SUITE_END();
99 /** testing the method:
102 class set
: public CppUnit::TestFixture
105 bool bRes
, bRes1
, bRes2
;
107 void set_createAndSet()
109 ::osl::Condition aCond
;
111 bRes
= aCond
.check();
113 CPPUNIT_ASSERT_MESSAGE("#test comment#: check state should be sal_True after set.",
117 void set_threadWaitRelease()
119 ::osl::Condition aCond
;
120 ConditionThread
myThread1(aCond
, thread_type_wait
);
122 bRes
= myThread1
.isRunning();
124 ConditionThread
myThread2(aCond
, thread_type_set
);
128 bRes1
= myThread1
.isRunning();
129 bRes2
= aCond
.check();
132 CPPUNIT_ASSERT_MESSAGE("#test comment#: use one thread to set the condition in order to release another thread.",
134 CPPUNIT_ASSERT_MESSAGE("#test comment#: use one thread to set the condition in order to release another thread.",
136 CPPUNIT_ASSERT_MESSAGE( "#test comment#: use one thread to set the condition in order to release another thread.",
140 CPPUNIT_TEST_SUITE(set
);
141 CPPUNIT_TEST(set_createAndSet
);
142 CPPUNIT_TEST(set_threadWaitRelease
);
143 CPPUNIT_TEST_SUITE_END( );
146 /** testing the method:
149 class reset
: public CppUnit::TestFixture
152 bool bRes
, bRes1
, bRes2
;
154 void reset_resetWaitAndSet()
156 ::osl::Condition aCond
;
159 ConditionThread
myThread(aCond
, thread_type_wait
);
161 bRes
= myThread
.isRunning();
162 bRes2
= aCond
.check();
166 bRes1
= myThread
.isRunning();
168 CPPUNIT_ASSERT_MESSAGE("#test comment#: wait will cause a reset thread block, use set to release it.",
170 CPPUNIT_ASSERT_MESSAGE("#test comment#: wait will cause a reset thread block, use set to release it.",
172 CPPUNIT_ASSERT_MESSAGE("#test comment#: wait will cause a reset thread block, use set to release it.",
176 void reset_resetAndSet()
178 ::osl::Condition aCond
;
180 bRes
= aCond
.check();
182 bRes1
= aCond
.check();
184 CPPUNIT_ASSERT_MESSAGE("#test comment#: create a condition and reset/set it.",
186 CPPUNIT_ASSERT_MESSAGE("#test comment#: create a condition and reset/set it.",
190 CPPUNIT_TEST_SUITE(reset
);
191 CPPUNIT_TEST(reset_resetWaitAndSet
);
192 CPPUNIT_TEST(reset_resetAndSet
);
193 CPPUNIT_TEST_SUITE_END();
196 /** testing the method:
197 Result wait(const TimeValue *pTimeout = 0)
199 class wait
: public CppUnit::TestFixture
202 bool bRes
, bRes1
, bRes2
;
203 std::unique_ptr
<TimeValue
> tv1
;
205 void setUp() override
207 tv1
.reset(new TimeValue
);
212 void tearDown() override
217 void wait_testAllCombos( )
219 ::osl::Condition cond1
;
220 ::osl::Condition cond2
;
221 ::osl::Condition cond3
;
226 osl::Condition::Result r1
=cond1
.wait(tv1
.get());
227 osl::Condition::Result r2
=cond2
.wait();
228 osl::Condition::Result r3
=cond3
.wait(tv1
.get());
230 CPPUNIT_ASSERT_EQUAL_MESSAGE( "#test comment#: test three types of wait.",
231 ::osl::Condition::result_ok
, r1
);
232 CPPUNIT_ASSERT_EQUAL_MESSAGE( "#test comment#: test three types of wait.",
233 ::osl::Condition::result_ok
, r2
);
234 CPPUNIT_ASSERT_EQUAL_MESSAGE( "#test comment#: test three types of wait.",
235 ::osl::Condition::result_timeout
, r3
);
238 void wait_timeoutWaits()
240 ::osl::Condition aCond
;
241 ::osl::Condition::Result wRes
, wRes1
;
244 bRes
= aCond
.check();
245 wRes
= aCond
.wait(tv1
.get());
248 wRes1
= aCond
.wait(tv1
.get());
249 bRes1
= aCond
.check();
251 CPPUNIT_ASSERT_MESSAGE("#test comment#: wait a condition after set/reset.",
253 CPPUNIT_ASSERT_MESSAGE("#test comment#: wait a condition after set/reset.",
255 CPPUNIT_ASSERT_EQUAL_MESSAGE("#test comment#: wait a condition after set/reset.",
256 ::osl::Condition::result_timeout
, wRes
);
257 CPPUNIT_ASSERT_EQUAL_MESSAGE("#test comment#: wait a condition after set/reset.",
258 ::osl::Condition::result_ok
, wRes1
);
261 CPPUNIT_TEST_SUITE(wait
);
262 CPPUNIT_TEST(wait_testAllCombos
);
263 CPPUNIT_TEST(wait_timeoutWaits
);
264 CPPUNIT_TEST_SUITE_END();
267 /** testing the method:
270 class check
: public CppUnit::TestFixture
273 bool bRes
, bRes1
, bRes2
;
275 void check_checkStates()
277 ::osl::Condition aCond
;
280 bRes
= aCond
.check();
282 bRes1
= aCond
.check();
284 CPPUNIT_ASSERT_MESSAGE("#test comment#: check the condition states.",
286 CPPUNIT_ASSERT_MESSAGE("#test comment#: check the condition states.",
290 void check_threadedCheckStates( )
292 ::osl::Condition aCond
;
295 ConditionThread
myThread(aCond
, thread_type_set
);
298 bRes
= aCond
.check();
300 ConditionThread
myThread1(aCond
, thread_type_reset
);
303 bRes1
= aCond
.check();
305 CPPUNIT_ASSERT_MESSAGE("#test comment#: use threads to set/reset Condition and check it in main routine.",
307 CPPUNIT_ASSERT_MESSAGE("#test comment#: use threads to set/reset Condition and check it in main routine.",
311 CPPUNIT_TEST_SUITE(check
);
312 CPPUNIT_TEST(check_checkStates
);
313 CPPUNIT_TEST(check_threadedCheckStates
);
314 CPPUNIT_TEST_SUITE_END();
317 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Condition::ctors
);
318 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Condition::set
);
319 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Condition::reset
);
320 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Condition::wait
);
321 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Condition::check
);
323 } // namespace osl_Condition
325 CPPUNIT_PLUGIN_IMPLEMENT();
327 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */