| 1 |
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 |
/* |
| 3 |
* This file is part of the LibreOffice project. |
| 4 |
* |
| 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/. |
| 8 |
* |
| 9 |
* This file incorporates work covered by the following license notice: |
| 10 |
* |
| 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 . |
| 18 |
*/ |
| 19 |
|
| 20 |
#include <sal/types.h> |
| 21 |
#include <osl/thread.hxx> |
| 22 |
#include <osl/conditn.hxx> |
| 23 |
#include <osl/time.h> |
| 24 |
|
| 25 |
#include <cppunit/TestFixture.h> |
| 26 |
#include <cppunit/extensions/HelperMacros.h> |
| 27 |
#include <cppunit/plugin/TestPlugIn.h> |
| 28 |
|
| 29 |
using namespace osl; |
| 30 |
|
| 31 |
namespace { |
| 32 |
|
| 33 |
enum ConditionType |
| 34 |
{ |
| 35 |
thread_type_set, |
| 36 |
thread_type_reset, |
| 37 |
thread_type_wait |
| 38 |
}; |
| 39 |
|
| 40 |
/** thread for testing Condition. |
| 41 |
*/ |
| 42 |
class ConditionThread : public Thread |
| 43 |
{ |
| 44 |
public: |
| 45 |
//get the Condition to operate |
| 46 |
ConditionThread( ::osl::Condition& Con, ConditionType tType): m_MyCon( Con ), m_MyType( tType ) { } |
| 47 |
|
| 48 |
protected: |
| 49 |
::osl::Condition& m_MyCon; |
| 50 |
ConditionType m_MyType; |
| 51 |
|
| 52 |
void SAL_CALL run() override |
| 53 |
{ |
| 54 |
switch ( m_MyType ) |
| 55 |
{ |
| 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; |
| 64 |
} |
| 65 |
} |
| 66 |
}; |
| 67 |
|
| 68 |
} |
| 69 |
|
| 70 |
namespace osl_Condition |
| 71 |
{ |
| 72 |
/** testing the method: |
| 73 |
Condition() |
| 74 |
*/ |
| 75 |
class ctors : public CppUnit::TestFixture |
| 76 |
{ |
| 77 |
public: |
| 78 |
bool bRes, bRes1; |
| 79 |
|
| 80 |
void ctors_create() |
| 81 |
{ |
| 82 |
::osl::Condition aCond; |
| 83 |
bRes = aCond.check(); |
| 84 |
|
| 85 |
CPPUNIT_ASSERT_MESSAGE("#test comment#: create a condition its initial check state should be sal_False.", |
| 86 |
!bRes ); |
| 87 |
} |
| 88 |
|
| 89 |
void ctors_createAndSet() |
| 90 |
{ |
| 91 |
::osl::Condition aCond; |
| 92 |
aCond.set(); |
| 93 |
bRes = aCond.check(); |
| 94 |
|
| 95 |
CPPUNIT_ASSERT_MESSAGE("#test comment#: create a condition and set it.", |
| 96 |
bRes ); |
| 97 |
} |
| 98 |
|
| 99 |
CPPUNIT_TEST_SUITE(ctors); |
| 100 |
CPPUNIT_TEST(ctors_create); |
| 101 |
CPPUNIT_TEST(ctors_createAndSet); |
| 102 |
CPPUNIT_TEST_SUITE_END(); |
| 103 |
}; |
| 104 |
|
| 105 |
/** testing the method: |
| 106 |
void set() |
| 107 |
*/ |
| 108 |
class set : public CppUnit::TestFixture |
| 109 |
{ |
| 110 |
public: |
| 111 |
bool bRes, bRes1, bRes2; |
| 112 |
|
| 113 |
void set_createAndSet() |
| 114 |
{ |
| 115 |
::osl::Condition aCond; |
| 116 |
aCond.set(); |
| 117 |
bRes = aCond.check(); |
| 118 |
|
| 119 |
CPPUNIT_ASSERT_MESSAGE("#test comment#: check state should be sal_True after set.", |
| 120 |
bRes ); |
| 121 |
} |
| 122 |
|
| 123 |
void set_threadWaitRelease() |
| 124 |
{ |
| 125 |
::osl::Condition aCond; |
| 126 |
ConditionThread myThread1(aCond, thread_type_wait); |
| 127 |
myThread1.create(); |
| 128 |
bRes = myThread1.isRunning(); |
| 129 |
|
| 130 |
ConditionThread myThread2(aCond, thread_type_set); |
| 131 |
myThread2.create(); |
| 132 |
|
| 133 |
myThread1.join(); |
| 134 |
bRes1 = myThread1.isRunning(); |
| 135 |
bRes2 = aCond.check(); |
| 136 |
myThread2.join(); |
| 137 |
|
| 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); |
| 144 |
} |
| 145 |
|
| 146 |
CPPUNIT_TEST_SUITE(set); |
| 147 |
CPPUNIT_TEST(set_createAndSet); |
| 148 |
CPPUNIT_TEST(set_threadWaitRelease); |
| 149 |
CPPUNIT_TEST_SUITE_END( ); |
| 150 |
}; |
| 151 |
|
| 152 |
/** testing the method: |
| 153 |
void reset() |
| 154 |
*/ |
| 155 |
class reset : public CppUnit::TestFixture |
| 156 |
{ |
| 157 |
public: |
| 158 |
bool bRes, bRes1, bRes2; |
| 159 |
|
| 160 |
void reset_resetWaitAndSet() |
| 161 |
{ |
| 162 |
::osl::Condition aCond; |
| 163 |
aCond.reset(); |
| 164 |
|
| 165 |
ConditionThread myThread(aCond, thread_type_wait); |
| 166 |
myThread.create(); |
| 167 |
bRes = myThread.isRunning(); |
| 168 |
bRes2 = aCond.check(); |
| 169 |
|
| 170 |
aCond.set(); |
| 171 |
myThread.join(); |
| 172 |
bRes1 = myThread.isRunning(); |
| 173 |
|
| 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); |
| 180 |
} |
| 181 |
|
| 182 |
void reset_resetAndSet() |
| 183 |
{ |
| 184 |
::osl::Condition aCond; |
| 185 |
aCond.reset(); |
| 186 |
bRes = aCond.check(); |
| 187 |
aCond.set(); |
| 188 |
bRes1 = aCond.check(); |
| 189 |
|
| 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 ); |
| 194 |
} |
| 195 |
|
| 196 |
CPPUNIT_TEST_SUITE(reset); |
| 197 |
CPPUNIT_TEST(reset_resetWaitAndSet); |
| 198 |
CPPUNIT_TEST(reset_resetAndSet); |
| 199 |
CPPUNIT_TEST_SUITE_END(); |
| 200 |
}; |
| 201 |
|
| 202 |
/** testing the method: |
| 203 |
Result wait(const TimeValue *pTimeout = 0) |
| 204 |
*/ |
| 205 |
class wait : public CppUnit::TestFixture |
| 206 |
{ |
| 207 |
public: |
| 208 |
bool bRes, bRes1, bRes2; |
| 209 |
std::unique_ptr<TimeValue> tv1; |
| 210 |
|
| 211 |
void setUp() override |
| 212 |
{ |
| 213 |
tv1.reset(new TimeValue); |
| 214 |
tv1->Seconds = 1; |
| 215 |
tv1->Nanosec = 0; |
| 216 |
} |
| 217 |
|
| 218 |
void tearDown() override |
| 219 |
{ |
| 220 |
tv1.reset(); |
| 221 |
} |
| 222 |
|
| 223 |
void wait_testAllCombos( ) |
| 224 |
{ |
| 225 |
::osl::Condition cond1; |
| 226 |
::osl::Condition cond2; |
| 227 |
::osl::Condition cond3; |
| 228 |
|
| 229 |
cond1.set(); |
| 230 |
cond2.set(); |
| 231 |
|
| 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()); |
| 235 |
|
| 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 ); |
| 242 |
} |
| 243 |
|
| 244 |
void wait_timeoutWaits() |
| 245 |
{ |
| 246 |
::osl::Condition aCond; |
| 247 |
::osl::Condition::Result wRes, wRes1; |
| 248 |
|
| 249 |
aCond.reset(); |
| 250 |
bRes = aCond.check(); |
| 251 |
wRes = aCond.wait(tv1.get()); |
| 252 |
|
| 253 |
aCond.set(); |
| 254 |
wRes1 = aCond.wait(tv1.get()); |
| 255 |
bRes1 = aCond.check(); |
| 256 |
|
| 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 ); |
| 265 |
} |
| 266 |
|
| 267 |
CPPUNIT_TEST_SUITE(wait); |
| 268 |
CPPUNIT_TEST(wait_testAllCombos); |
| 269 |
CPPUNIT_TEST(wait_timeoutWaits); |
| 270 |
CPPUNIT_TEST_SUITE_END(); |
| 271 |
}; |
| 272 |
|
| 273 |
/** testing the method: |
| 274 |
sal_Bool check() |
| 275 |
*/ |
| 276 |
class check : public CppUnit::TestFixture |
| 277 |
{ |
| 278 |
public: |
| 279 |
bool bRes, bRes1, bRes2; |
| 280 |
|
| 281 |
void check_checkStates() |
| 282 |
{ |
| 283 |
::osl::Condition aCond; |
| 284 |
|
| 285 |
aCond.reset(); |
| 286 |
bRes = aCond.check(); |
| 287 |
aCond.set(); |
| 288 |
bRes1 = aCond.check(); |
| 289 |
|
| 290 |
CPPUNIT_ASSERT_MESSAGE("#test comment#: check the condition states.", |
| 291 |
!bRes ); |
| 292 |
CPPUNIT_ASSERT_MESSAGE("#test comment#: check the condition states.", |
| 293 |
bRes1 ); |
| 294 |
} |
| 295 |
|
| 296 |
void check_threadedCheckStates( ) |
| 297 |
{ |
| 298 |
::osl::Condition aCond; |
| 299 |
aCond.reset(); |
| 300 |
|
| 301 |
ConditionThread myThread(aCond, thread_type_set); |
| 302 |
myThread.create(); |
| 303 |
myThread.join(); |
| 304 |
bRes = aCond.check(); |
| 305 |
|
| 306 |
ConditionThread myThread1(aCond, thread_type_reset); |
| 307 |
myThread1.create(); |
| 308 |
myThread1.join(); |
| 309 |
bRes1 = aCond.check(); |
| 310 |
|
| 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 ); |
| 315 |
} |
| 316 |
|
| 317 |
CPPUNIT_TEST_SUITE(check); |
| 318 |
CPPUNIT_TEST(check_checkStates); |
| 319 |
CPPUNIT_TEST(check_threadedCheckStates); |
| 320 |
CPPUNIT_TEST_SUITE_END(); |
| 321 |
}; |
| 322 |
|
| 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); |
| 328 |
|
| 329 |
} // namespace osl_Condition |
| 330 |
|
| 331 |
CPPUNIT_PLUGIN_IMPLEMENT(); |
| 332 |
|
| 333 |
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |