tdf#154546 skip dispatch when presenter controller is not set
[LibreOffice.git] / sal / qa / rtl / doublelock / rtl_doublelocking.cxx
blobef639423d774eb9324a52ff6e321e720f4507858
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>
33 #include <cppunit/plugin/TestPlugIn.h>
35 #ifdef _WIN32
36 #if !defined WIN32_LEAN_AND_MEAN
37 # define WIN32_LEAN_AND_MEAN
38 #endif
39 #include <windows.h>
40 #else
41 #include <unistd.h>
42 #include <time.h>
43 #endif
45 #define CONST_TEST_STRING "gregorian"
47 namespace {
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
59 osl::Mutex m_mutex;
60 sal_Int32 m_nOK;
61 sal_Int32 m_nFails;
63 OUString m_sConstStr;
64 public:
65 OGetThread()
66 :m_nOK(0),
67 m_nFails(0),
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;}
75 protected:
77 /** guarded value which initialized 0
79 @see ThreadSafeValue
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);
89 m_nOK++;
91 else
93 osl::MutexGuard g(m_mutex);
94 m_nFails++;
99 public:
101 virtual ~OGetThread() override
103 if (isRunning())
105 printf("error: not terminated.\n");
112 namespace rtl_DoubleLocking
115 /** Test of the osl::Thread::create method
118 class getValue : public CppUnit::TestFixture
120 public:
122 void getValue_001()
124 OUString aStr = Gregorian::get();
126 CPPUNIT_ASSERT_MESSAGE(
127 "Gregorian::get() failed, wrong value expected.",
128 !aStr.isEmpty()
132 /** check 2 threads.
134 ALGORITHM:
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.
141 void getValue_002()
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
148 pThread->create();
149 p2Thread->create();
151 pThread->join();
152 p2Thread->join();
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();
164 delete pThread;
165 delete p2Thread;
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();
177 }; // class create
179 CPPUNIT_TEST_SUITE_REGISTRATION(rtl_DoubleLocking::getValue);
180 } // namespace rtl_DoubleLocking
182 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */