Version 6.1.0.2, tag libreoffice-6.1.0.2
[LibreOffice.git] / sal / qa / rtl / doublelock / rtl_doublelocking.cxx
blobf882b104c9bc66c7f974d0f62a7704f2b8150756
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>
27 #include <osl/time.h>
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>
36 #ifdef _WIN32
37 #if !defined WIN32_LEAN_AND_MEAN
38 # define WIN32_LEAN_AND_MEAN
39 #endif
40 #include <windows.h>
41 #else
42 #include <unistd.h>
43 #include <time.h>
44 #endif
46 #define CONST_TEST_STRING "gregorian"
48 namespace {
49 struct Gregorian : public rtl::StaticWithInit<rtl::OUString, Gregorian> {
50 const rtl::OUString operator () () {
51 return rtl::OUString( CONST_TEST_STRING );
56 namespace ThreadHelper
58 // typedef enum {
59 // QUIET=1,
60 // VERBOSE
61 // } eSleepVerboseMode;
63 void thread_sleep_tenth_sec(sal_Int32 _nTenthSec/*, eSleepVerboseMode nVerbose = VERBOSE*/)
65 // if (nVerbose == VERBOSE)
66 // {
67 // printf("wait %d tenth seconds. ", _nTenthSec );
68 // fflush(stdout);
69 // }
70 #ifdef _WIN32 //Windows
71 Sleep(_nTenthSec * 100 );
72 #endif
73 #if ( defined UNX )
74 TimeValue nTV;
75 nTV.Seconds = static_cast<sal_uInt32>( _nTenthSec/10 );
76 nTV.Nanosec = ( (_nTenthSec%10 ) * 100000000 );
77 osl_waitThread(&nTV);
78 #endif
79 // if (nVerbose == VERBOSE)
80 // {
81 // printf("done\n");
82 // }
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
91 sal_Int32 m_nOK;
92 sal_Int32 m_nFails;
94 rtl::OUString m_sConstStr;
95 public:
96 OGetThread()
97 :m_nOK(0),
98 m_nFails(0)
100 m_sConstStr = CONST_TEST_STRING;
103 sal_Int32 getOK() { return m_nOK; }
104 sal_Int32 getFails() {return m_nFails;}
106 protected:
108 /** guarded value which initialized 0
110 @see ThreadSafeValue
112 void SAL_CALL run() override
114 while(schedule())
116 rtl::OUString aStr = Gregorian::get();
117 if (aStr == m_sConstStr)
119 m_nOK++;
121 else
123 m_nFails++;
125 ThreadHelper::thread_sleep_tenth_sec(1);
129 public:
131 virtual ~OGetThread() override
133 if (isRunning())
135 printf("error: not terminated.\n");
140 namespace rtl_DoubleLocking
143 /** Test of the osl::Thread::create method
146 class getValue : public CppUnit::TestFixture
148 public:
150 void getValue_001()
152 rtl::OUString aStr = Gregorian::get();
154 CPPUNIT_ASSERT_MESSAGE(
155 "Gregorian::get() failed, wrong value expected.",
156 !aStr.isEmpty()
160 /** check 2 threads.
162 ALGORITHM:
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.
169 void getValue_002()
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
176 pThread->create();
177 p2Thread->create();
179 ThreadHelper::thread_sleep_tenth_sec(5);
181 pThread->terminate();
182 p2Thread->terminate();
184 pThread->join();
185 p2Thread->join();
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();
201 delete pThread;
202 delete p2Thread;
204 CPPUNIT_ASSERT_MESSAGE(
205 "getValue() failed, wrong value expected.",
206 nValueOK != 0
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();
216 }; // class create
218 CPPUNIT_TEST_SUITE_REGISTRATION(rtl_DoubleLocking::getValue);
219 } // namespace rtl_DoubleLocking
221 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */