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/config.h>
24 #include <sal/types.h>
26 #include <osl/thread.hxx>
29 #include <rtl/instance.hxx>
30 #include <rtl/ustring.hxx>
32 #include <cppunit/TestFixture.h>
33 #include <cppunit/extensions/HelperMacros.h>
34 #include <cppunit/plugin/TestPlugIn.h>
37 #if !defined WIN32_LEAN_AND_MEAN
38 # define WIN32_LEAN_AND_MEAN
46 #define CONST_TEST_STRING "gregorian"
49 struct Gregorian
: public rtl::StaticWithInit
<rtl::OUString
, Gregorian
> {
50 const rtl::OUString
operator () () {
51 return rtl::OUString( CONST_TEST_STRING
);
56 namespace ThreadHelper
61 // } eSleepVerboseMode;
63 void thread_sleep_tenth_sec(sal_Int32 _nTenthSec
/*, eSleepVerboseMode nVerbose = VERBOSE*/)
65 // if (nVerbose == VERBOSE)
67 // printf("wait %d tenth seconds. ", _nTenthSec );
70 #ifdef _WIN32 //Windows
71 Sleep(_nTenthSec
* 100 );
75 nTV
.Seconds
= static_cast<sal_uInt32
>( _nTenthSec
/10 );
76 nTV
.Nanosec
= ( (_nTenthSec
%10 ) * 100000000 );
79 // if (nVerbose == VERBOSE)
86 /** Simple thread for testing Thread-create.
87 * Just add 1 of value 0, and after running, result is 1.
89 class OGetThread
: public osl::Thread
94 rtl::OUString m_sConstStr
;
100 m_sConstStr
= CONST_TEST_STRING
;
103 sal_Int32
getOK() { return m_nOK
; }
104 sal_Int32
getFails() {return m_nFails
;}
108 /** guarded value which initialized 0
112 void SAL_CALL
run() override
116 rtl::OUString aStr
= Gregorian::get();
117 if (aStr
== m_sConstStr
)
125 ThreadHelper::thread_sleep_tenth_sec(1);
131 virtual ~OGetThread() override
135 printf("error: not terminated.\n");
140 namespace rtl_DoubleLocking
143 /** Test of the osl::Thread::create method
146 class getValue
: public CppUnit::TestFixture
152 rtl::OUString aStr
= Gregorian::get();
154 CPPUNIT_ASSERT_MESSAGE(
155 "Gregorian::get() failed, wrong value expected.",
163 Here the function should show, that 2 different threads,
164 which only increase a value, should run at the same time with same prio.
165 The test fails, if the difference between the two values is more than 5%
166 but IMHO this isn't a failure, it's only a feature of the OS.
171 // initial 5 threads with different priorities
172 OGetThread
* pThread
= new OGetThread();
173 OGetThread
* p2Thread
= new OGetThread();
175 //Create them and start running at the same time
179 ThreadHelper::thread_sleep_tenth_sec(5);
181 pThread
->terminate();
182 p2Thread
->terminate();
187 sal_Int32 nValueOK
= 0;
188 nValueOK
= pThread
->getOK();
190 sal_Int32 nValueOK2
= 0;
191 nValueOK2
= p2Thread
->getOK();
193 std::cout
<< "Value in Thread #1 is " << nValueOK
<< "\n";
194 std::cout
<< "Value in Thread #2 is " << nValueOK2
<< "\n";
195 sal_Int32 nValueFails
= 0;
196 nValueFails
= pThread
->getFails();
198 sal_Int32 nValueFails2
= 0;
199 nValueFails2
= p2Thread
->getFails();
204 CPPUNIT_ASSERT_MESSAGE(
205 "getValue() failed, wrong value expected.",
208 CPPUNIT_ASSERT_EQUAL(sal_Int32(0), nValueFails
);
209 CPPUNIT_ASSERT_EQUAL(sal_Int32(0), nValueFails2
);
212 CPPUNIT_TEST_SUITE(getValue
);
213 CPPUNIT_TEST(getValue_001
);
214 CPPUNIT_TEST(getValue_002
);
215 CPPUNIT_TEST_SUITE_END();
218 CPPUNIT_TEST_SUITE_REGISTRATION(rtl_DoubleLocking::getValue
);
219 } // namespace rtl_DoubleLocking
221 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */