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>
28 #include <rtl/instance.hxx>
29 #include <rtl/ustring.hxx>
31 #include <cppunit/TestFixture.h>
32 #include <cppunit/extensions/HelperMacros.h>
35 #if !defined WIN32_LEAN_AND_MEAN
36 # define WIN32_LEAN_AND_MEAN
41 #define CONST_TEST_STRING "gregorian"
44 struct Gregorian
: public rtl::StaticWithInit
<OUString
, Gregorian
> {
45 OUString
operator () () {
46 return u
"" CONST_TEST_STRING
""_ustr
;
50 /** Simple thread for testing Thread-create.
51 * Just add 1 of value 0, and after running, result is 1.
53 class OGetThread
: public osl::Thread
64 m_sConstStr(u
"" CONST_TEST_STRING
""_ustr
)
68 sal_Int32
getOK() { osl::MutexGuard
g(m_mutex
); return m_nOK
; }
69 sal_Int32
getFails() {osl::MutexGuard
g(m_mutex
); return m_nFails
;}
73 /** guarded value which initialized 0
77 void SAL_CALL
run() override
79 for (int i
= 0; i
!= 5; ++i
)
81 OUString aStr
= Gregorian::get();
82 if (aStr
== m_sConstStr
)
84 osl::MutexGuard
g(m_mutex
);
89 osl::MutexGuard
g(m_mutex
);
97 virtual ~OGetThread() override
101 printf("error: not terminated.\n");
108 namespace rtl_DoubleLocking
111 /** Test of the osl::Thread::create method
114 class getValue
: public CppUnit::TestFixture
120 OUString aStr
= Gregorian::get();
122 CPPUNIT_ASSERT_MESSAGE(
123 "Gregorian::get() failed, wrong value expected.",
131 Here the function should show, that 2 different threads,
132 which only increase a value, should run at the same time with same prio.
133 The test fails, if the difference between the two values is more than 5%
134 but IMHO this isn't a failure, it's only a feature of the OS.
139 // initial 5 threads with different priorities
140 OGetThread
* pThread
= new OGetThread();
141 OGetThread
* p2Thread
= new OGetThread();
143 //Create them and start running at the same time
150 sal_Int32 nValueOK
= pThread
->getOK();
152 sal_Int32 nValueOK2
= p2Thread
->getOK();
154 std::cout
<< "Value in Thread #1 is " << nValueOK
<< "\n";
155 std::cout
<< "Value in Thread #2 is " << nValueOK2
<< "\n";
156 sal_Int32 nValueFails
= pThread
->getFails();
158 sal_Int32 nValueFails2
= p2Thread
->getFails();
163 CPPUNIT_ASSERT_EQUAL(sal_Int32(5), nValueOK
);
164 CPPUNIT_ASSERT_EQUAL(sal_Int32(5), nValueOK2
);
165 CPPUNIT_ASSERT_EQUAL(sal_Int32(0), nValueFails
);
166 CPPUNIT_ASSERT_EQUAL(sal_Int32(0), nValueFails2
);
169 CPPUNIT_TEST_SUITE(getValue
);
170 CPPUNIT_TEST(getValue_001
);
171 CPPUNIT_TEST(getValue_002
);
172 CPPUNIT_TEST_SUITE_END();
175 CPPUNIT_TEST_SUITE_REGISTRATION(rtl_DoubleLocking::getValue
);
176 } // namespace rtl_DoubleLocking
178 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */