LanguageTool: don't crash if REST protocol isn't set
[LibreOffice.git] / sal / qa / osl / condition / osl_Condition.cxx
blob05b7fc661f6a99bec4a66b7cacfd52025bf6a68c
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 .
20 #include "osl_Condition_Const.h"
21 #include <stdlib.h>
23 using namespace osl;
25 namespace {
27 enum ConditionType
29 thread_type_set,
30 thread_type_reset,
31 thread_type_wait
34 /** thread for testing Condition.
36 class ConditionThread : public Thread
38 public:
39 //get the Condition to operate
40 ConditionThread( ::osl::Condition& Con, ConditionType tType): m_MyCon( Con ), m_MyType( tType ) { }
42 protected:
43 ::osl::Condition& m_MyCon;
44 ConditionType m_MyType;
46 void SAL_CALL run() override
48 switch ( m_MyType )
50 case thread_type_wait:
51 m_MyCon.wait(); break;
52 case thread_type_set:
53 m_MyCon.set(); break;
54 case thread_type_reset:
55 m_MyCon.reset(); break;
56 default:
57 break;
64 namespace osl_Condition
66 /** testing the method:
67 Condition()
69 class ctors : public CppUnit::TestFixture
71 public:
72 bool bRes, bRes1;
74 void ctors_create()
76 ::osl::Condition aCond;
77 bRes = aCond.check();
79 CPPUNIT_ASSERT_MESSAGE("#test comment#: create a condition its initial check state should be sal_False.",
80 !bRes );
83 void ctors_createAndSet()
85 ::osl::Condition aCond;
86 aCond.set();
87 bRes = aCond.check();
89 CPPUNIT_ASSERT_MESSAGE("#test comment#: create a condition and set it.",
90 bRes );
93 CPPUNIT_TEST_SUITE(ctors);
94 CPPUNIT_TEST(ctors_create);
95 CPPUNIT_TEST(ctors_createAndSet);
96 CPPUNIT_TEST_SUITE_END();
99 /** testing the method:
100 void set()
102 class set : public CppUnit::TestFixture
104 public:
105 bool bRes, bRes1, bRes2;
107 void set_createAndSet()
109 ::osl::Condition aCond;
110 aCond.set();
111 bRes = aCond.check();
113 CPPUNIT_ASSERT_MESSAGE("#test comment#: check state should be sal_True after set.",
114 bRes );
117 void set_threadWaitRelease()
119 ::osl::Condition aCond;
120 ConditionThread myThread1(aCond, thread_type_wait);
121 myThread1.create();
122 bRes = myThread1.isRunning();
124 ConditionThread myThread2(aCond, thread_type_set);
125 myThread2.create();
127 myThread1.join();
128 bRes1 = myThread1.isRunning();
129 bRes2 = aCond.check();
130 myThread2.join();
132 CPPUNIT_ASSERT_MESSAGE("#test comment#: use one thread to set the condition in order to release another thread.",
133 bRes);
134 CPPUNIT_ASSERT_MESSAGE("#test comment#: use one thread to set the condition in order to release another thread.",
135 !bRes1);
136 CPPUNIT_ASSERT_MESSAGE( "#test comment#: use one thread to set the condition in order to release another thread.",
137 bRes2);
140 CPPUNIT_TEST_SUITE(set);
141 CPPUNIT_TEST(set_createAndSet);
142 CPPUNIT_TEST(set_threadWaitRelease);
143 CPPUNIT_TEST_SUITE_END( );
146 /** testing the method:
147 void reset()
149 class reset : public CppUnit::TestFixture
151 public:
152 bool bRes, bRes1, bRes2;
154 void reset_resetWaitAndSet()
156 ::osl::Condition aCond;
157 aCond.reset();
159 ConditionThread myThread(aCond, thread_type_wait);
160 myThread.create();
161 bRes = myThread.isRunning();
162 bRes2 = aCond.check();
164 aCond.set();
165 myThread.join();
166 bRes1 = myThread.isRunning();
168 CPPUNIT_ASSERT_MESSAGE("#test comment#: wait will cause a reset thread block, use set to release it.",
169 bRes);
170 CPPUNIT_ASSERT_MESSAGE("#test comment#: wait will cause a reset thread block, use set to release it.",
171 !bRes1);
172 CPPUNIT_ASSERT_MESSAGE("#test comment#: wait will cause a reset thread block, use set to release it.",
173 !bRes2);
176 void reset_resetAndSet()
178 ::osl::Condition aCond;
179 aCond.reset();
180 bRes = aCond.check();
181 aCond.set();
182 bRes1 = aCond.check();
184 CPPUNIT_ASSERT_MESSAGE("#test comment#: create a condition and reset/set it.",
185 !bRes );
186 CPPUNIT_ASSERT_MESSAGE("#test comment#: create a condition and reset/set it.",
187 bRes1 );
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
201 public:
202 bool bRes, bRes1, bRes2;
203 std::unique_ptr<TimeValue> tv1;
205 void setUp() override
207 tv1.reset(new TimeValue);
208 tv1->Seconds = 1;
209 tv1->Nanosec = 0;
212 void tearDown() override
214 tv1.reset();
217 void wait_testAllCombos( )
219 ::osl::Condition cond1;
220 ::osl::Condition cond2;
221 ::osl::Condition cond3;
223 cond1.set();
224 cond2.set();
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;
243 aCond.reset();
244 bRes = aCond.check();
245 wRes = aCond.wait(tv1.get());
247 aCond.set();
248 wRes1 = aCond.wait(tv1.get());
249 bRes1 = aCond.check();
251 CPPUNIT_ASSERT_MESSAGE("#test comment#: wait a condition after set/reset.",
252 !bRes );
253 CPPUNIT_ASSERT_MESSAGE("#test comment#: wait a condition after set/reset.",
254 bRes1 );
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:
268 sal_Bool check()
270 class check : public CppUnit::TestFixture
272 public:
273 bool bRes, bRes1, bRes2;
275 void check_checkStates()
277 ::osl::Condition aCond;
279 aCond.reset();
280 bRes = aCond.check();
281 aCond.set();
282 bRes1 = aCond.check();
284 CPPUNIT_ASSERT_MESSAGE("#test comment#: check the condition states.",
285 !bRes );
286 CPPUNIT_ASSERT_MESSAGE("#test comment#: check the condition states.",
287 bRes1 );
290 void check_threadedCheckStates( )
292 ::osl::Condition aCond;
293 aCond.reset();
295 ConditionThread myThread(aCond, thread_type_set);
296 myThread.create();
297 myThread.join();
298 bRes = aCond.check();
300 ConditionThread myThread1(aCond, thread_type_reset);
301 myThread1.create();
302 myThread1.join();
303 bRes1 = aCond.check();
305 CPPUNIT_ASSERT_MESSAGE("#test comment#: use threads to set/reset Condition and check it in main routine.",
306 bRes );
307 CPPUNIT_ASSERT_MESSAGE("#test comment#: use threads to set/reset Condition and check it in main routine.",
308 !bRes1 );
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: */