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>
33 #include <cppunit/plugin/TestPlugIn.h>
36 #if !defined WIN32_LEAN_AND_MEAN
37 # define WIN32_LEAN_AND_MEAN
45 #define CONST_TEST_STRING "gregorian"
48 struct Gregorian
: public rtl::StaticWithInit
<OUString
, Gregorian
> {
49 OUString
operator () () {
50 return CONST_TEST_STRING
;
54 /** Simple thread for testing Thread-create.
55 * Just add 1 of value 0, and after running, result is 1.
57 class OGetThread
: public osl::Thread
68 m_sConstStr(CONST_TEST_STRING
)
72 sal_Int32
getOK() { osl::MutexGuard
g(m_mutex
); return m_nOK
; }
73 sal_Int32
getFails() {osl::MutexGuard
g(m_mutex
); return m_nFails
;}
77 /** guarded value which initialized 0
81 void SAL_CALL
run() override
83 for (int i
= 0; i
!= 5; ++i
)
85 OUString aStr
= Gregorian::get();
86 if (aStr
== m_sConstStr
)
88 osl::MutexGuard
g(m_mutex
);
93 osl::MutexGuard
g(m_mutex
);
101 virtual ~OGetThread() override
105 printf("error: not terminated.\n");
112 namespace rtl_DoubleLocking
115 /** Test of the osl::Thread::create method
118 class getValue
: public CppUnit::TestFixture
124 OUString aStr
= Gregorian::get();
126 CPPUNIT_ASSERT_MESSAGE(
127 "Gregorian::get() failed, wrong value expected.",
135 Here the function should show, that 2 different threads,
136 which only increase a value, should run at the same time with same prio.
137 The test fails, if the difference between the two values is more than 5%
138 but IMHO this isn't a failure, it's only a feature of the OS.
143 // initial 5 threads with different priorities
144 OGetThread
* pThread
= new OGetThread();
145 OGetThread
* p2Thread
= new OGetThread();
147 //Create them and start running at the same time
154 sal_Int32 nValueOK
= pThread
->getOK();
156 sal_Int32 nValueOK2
= p2Thread
->getOK();
158 std::cout
<< "Value in Thread #1 is " << nValueOK
<< "\n";
159 std::cout
<< "Value in Thread #2 is " << nValueOK2
<< "\n";
160 sal_Int32 nValueFails
= pThread
->getFails();
162 sal_Int32 nValueFails2
= p2Thread
->getFails();
167 CPPUNIT_ASSERT_EQUAL(sal_Int32(5), nValueOK
);
168 CPPUNIT_ASSERT_EQUAL(sal_Int32(5), nValueOK2
);
169 CPPUNIT_ASSERT_EQUAL(sal_Int32(0), nValueFails
);
170 CPPUNIT_ASSERT_EQUAL(sal_Int32(0), nValueFails2
);
173 CPPUNIT_TEST_SUITE(getValue
);
174 CPPUNIT_TEST(getValue_001
);
175 CPPUNIT_TEST(getValue_002
);
176 CPPUNIT_TEST_SUITE_END();
179 CPPUNIT_TEST_SUITE_REGISTRATION(rtl_DoubleLocking::getValue
);
180 } // namespace rtl_DoubleLocking
182 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */