Update git submodules
[LibreOffice.git] / sal / qa / rtl / doublelock / rtl_doublelocking.cxx
blob60a5161c6661992f151ab42513313ff9343aca76
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/config.h>
22 #include <iostream>
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>
34 #ifdef _WIN32
35 #if !defined WIN32_LEAN_AND_MEAN
36 # define WIN32_LEAN_AND_MEAN
37 #endif
38 #include <windows.h>
39 #endif
41 #define CONST_TEST_STRING "gregorian"
43 namespace {
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
55 osl::Mutex m_mutex;
56 sal_Int32 m_nOK;
57 sal_Int32 m_nFails;
59 OUString m_sConstStr;
60 public:
61 OGetThread()
62 :m_nOK(0),
63 m_nFails(0),
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;}
71 protected:
73 /** guarded value which initialized 0
75 @see ThreadSafeValue
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);
85 m_nOK++;
87 else
89 osl::MutexGuard g(m_mutex);
90 m_nFails++;
95 public:
97 virtual ~OGetThread() override
99 if (isRunning())
101 printf("error: not terminated.\n");
108 namespace rtl_DoubleLocking
111 /** Test of the osl::Thread::create method
114 class getValue : public CppUnit::TestFixture
116 public:
118 void getValue_001()
120 OUString aStr = Gregorian::get();
122 CPPUNIT_ASSERT_MESSAGE(
123 "Gregorian::get() failed, wrong value expected.",
124 !aStr.isEmpty()
128 /** check 2 threads.
130 ALGORITHM:
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.
137 void getValue_002()
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
144 pThread->create();
145 p2Thread->create();
147 pThread->join();
148 p2Thread->join();
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();
160 delete pThread;
161 delete p2Thread;
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();
173 }; // class create
175 CPPUNIT_TEST_SUITE_REGISTRATION(rtl_DoubleLocking::getValue);
176 } // namespace rtl_DoubleLocking
178 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */