Fix GNU C++ version check
[LibreOffice.git] / sal / qa / osl / condition / osl_Condition.cxx
blob9bc15b92b6ef12d9cceb3d6c83a9f006ed48cc05
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 <sal/types.h>
21 #include <osl/thread.hxx>
22 #include <osl/conditn.hxx>
23 #include <osl/time.h>
25 #include <cppunit/TestFixture.h>
26 #include <cppunit/extensions/HelperMacros.h>
27 #include <cppunit/plugin/TestPlugIn.h>
29 using namespace osl;
31 namespace {
33 enum ConditionType
35 thread_type_set,
36 thread_type_reset,
37 thread_type_wait
40 /** thread for testing Condition.
42 class ConditionThread : public Thread
44 public:
45 //get the Condition to operate
46 ConditionThread( ::osl::Condition& Con, ConditionType tType): m_MyCon( Con ), m_MyType( tType ) { }
48 protected:
49 ::osl::Condition& m_MyCon;
50 ConditionType m_MyType;
52 void SAL_CALL run() override
54 switch ( m_MyType )
56 case thread_type_wait:
57 m_MyCon.wait(); break;
58 case thread_type_set:
59 m_MyCon.set(); break;
60 case thread_type_reset:
61 m_MyCon.reset(); break;
62 default:
63 break;
70 namespace osl_Condition
72 /** testing the method:
73 Condition()
75 class ctors : public CppUnit::TestFixture
77 public:
78 bool bRes, bRes1;
80 void ctors_create()
82 ::osl::Condition aCond;
83 bRes = aCond.check();
85 CPPUNIT_ASSERT_MESSAGE("#test comment#: create a condition its initial check state should be sal_False.",
86 !bRes );
89 void ctors_createAndSet()
91 ::osl::Condition aCond;
92 aCond.set();
93 bRes = aCond.check();
95 CPPUNIT_ASSERT_MESSAGE("#test comment#: create a condition and set it.",
96 bRes );
99 CPPUNIT_TEST_SUITE(ctors);
100 CPPUNIT_TEST(ctors_create);
101 CPPUNIT_TEST(ctors_createAndSet);
102 CPPUNIT_TEST_SUITE_END();
105 /** testing the method:
106 void set()
108 class set : public CppUnit::TestFixture
110 public:
111 bool bRes, bRes1, bRes2;
113 void set_createAndSet()
115 ::osl::Condition aCond;
116 aCond.set();
117 bRes = aCond.check();
119 CPPUNIT_ASSERT_MESSAGE("#test comment#: check state should be sal_True after set.",
120 bRes );
123 void set_threadWaitRelease()
125 ::osl::Condition aCond;
126 ConditionThread myThread1(aCond, thread_type_wait);
127 myThread1.create();
128 bRes = myThread1.isRunning();
130 ConditionThread myThread2(aCond, thread_type_set);
131 myThread2.create();
133 myThread1.join();
134 bRes1 = myThread1.isRunning();
135 bRes2 = aCond.check();
136 myThread2.join();
138 CPPUNIT_ASSERT_MESSAGE("#test comment#: use one thread to set the condition in order to release another thread.",
139 bRes);
140 CPPUNIT_ASSERT_MESSAGE("#test comment#: use one thread to set the condition in order to release another thread.",
141 !bRes1);
142 CPPUNIT_ASSERT_MESSAGE( "#test comment#: use one thread to set the condition in order to release another thread.",
143 bRes2);
146 CPPUNIT_TEST_SUITE(set);
147 CPPUNIT_TEST(set_createAndSet);
148 CPPUNIT_TEST(set_threadWaitRelease);
149 CPPUNIT_TEST_SUITE_END( );
152 /** testing the method:
153 void reset()
155 class reset : public CppUnit::TestFixture
157 public:
158 bool bRes, bRes1, bRes2;
160 void reset_resetWaitAndSet()
162 ::osl::Condition aCond;
163 aCond.reset();
165 ConditionThread myThread(aCond, thread_type_wait);
166 myThread.create();
167 bRes = myThread.isRunning();
168 bRes2 = aCond.check();
170 aCond.set();
171 myThread.join();
172 bRes1 = myThread.isRunning();
174 CPPUNIT_ASSERT_MESSAGE("#test comment#: wait will cause a reset thread block, use set to release it.",
175 bRes);
176 CPPUNIT_ASSERT_MESSAGE("#test comment#: wait will cause a reset thread block, use set to release it.",
177 !bRes1);
178 CPPUNIT_ASSERT_MESSAGE("#test comment#: wait will cause a reset thread block, use set to release it.",
179 !bRes2);
182 void reset_resetAndSet()
184 ::osl::Condition aCond;
185 aCond.reset();
186 bRes = aCond.check();
187 aCond.set();
188 bRes1 = aCond.check();
190 CPPUNIT_ASSERT_MESSAGE("#test comment#: create a condition and reset/set it.",
191 !bRes );
192 CPPUNIT_ASSERT_MESSAGE("#test comment#: create a condition and reset/set it.",
193 bRes1 );
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
207 public:
208 bool bRes, bRes1, bRes2;
209 std::unique_ptr<TimeValue> tv1;
211 void setUp() override
213 tv1.reset(new TimeValue);
214 tv1->Seconds = 1;
215 tv1->Nanosec = 0;
218 void tearDown() override
220 tv1.reset();
223 void wait_testAllCombos( )
225 ::osl::Condition cond1;
226 ::osl::Condition cond2;
227 ::osl::Condition cond3;
229 cond1.set();
230 cond2.set();
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;
249 aCond.reset();
250 bRes = aCond.check();
251 wRes = aCond.wait(tv1.get());
253 aCond.set();
254 wRes1 = aCond.wait(tv1.get());
255 bRes1 = aCond.check();
257 CPPUNIT_ASSERT_MESSAGE("#test comment#: wait a condition after set/reset.",
258 !bRes );
259 CPPUNIT_ASSERT_MESSAGE("#test comment#: wait a condition after set/reset.",
260 bRes1 );
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:
274 sal_Bool check()
276 class check : public CppUnit::TestFixture
278 public:
279 bool bRes, bRes1, bRes2;
281 void check_checkStates()
283 ::osl::Condition aCond;
285 aCond.reset();
286 bRes = aCond.check();
287 aCond.set();
288 bRes1 = aCond.check();
290 CPPUNIT_ASSERT_MESSAGE("#test comment#: check the condition states.",
291 !bRes );
292 CPPUNIT_ASSERT_MESSAGE("#test comment#: check the condition states.",
293 bRes1 );
296 void check_threadedCheckStates( )
298 ::osl::Condition aCond;
299 aCond.reset();
301 ConditionThread myThread(aCond, thread_type_set);
302 myThread.create();
303 myThread.join();
304 bRes = aCond.check();
306 ConditionThread myThread1(aCond, thread_type_reset);
307 myThread1.create();
308 myThread1.join();
309 bRes1 = aCond.check();
311 CPPUNIT_ASSERT_MESSAGE("#test comment#: use threads to set/reset Condition and check it in main routine.",
312 bRes );
313 CPPUNIT_ASSERT_MESSAGE("#test comment#: use threads to set/reset Condition and check it in main routine.",
314 !bRes1 );
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: */