bump product version to 5.0.4.1
[LibreOffice.git] / sal / qa / rtl / doublelock / rtl_doublelocking.cxx
blob79451cc6fa49e7565f3718ce746580efb53dd197
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 files
22 #include <sal/types.h>
24 #include <osl/thread.hxx>
25 #include <osl/time.h>
27 #include <rtl/instance.hxx>
28 #include <rtl/ustring.hxx>
30 #include <cppunit/TestFixture.h>
31 #include <cppunit/extensions/HelperMacros.h>
32 #include <cppunit/plugin/TestPlugIn.h>
34 #ifdef WNT
35 #include <windows.h>
36 #else
37 #include <unistd.h>
38 #include <time.h>
39 #endif
41 #define CONST_TEST_STRING "gregorian"
43 namespace {
44 struct Gregorian : public rtl::StaticWithInit<rtl::OUString, Gregorian> {
45 const rtl::OUString operator () () {
46 return rtl::OUString( CONST_TEST_STRING );
51 namespace ThreadHelper
53 // typedef enum {
54 // QUIET=1,
55 // VERBOSE
56 // } eSleepVerboseMode;
58 void thread_sleep_tenth_sec(sal_Int32 _nTenthSec/*, eSleepVerboseMode nVerbose = VERBOSE*/)
60 // if (nVerbose == VERBOSE)
61 // {
62 // printf("wait %d tenth seconds. ", _nTenthSec );
63 // fflush(stdout);
64 // }
65 #ifdef WNT //Windows
66 Sleep(_nTenthSec * 100 );
67 #endif
68 #if ( defined UNX )
69 TimeValue nTV;
70 nTV.Seconds = static_cast<sal_uInt32>( _nTenthSec/10 );
71 nTV.Nanosec = ( (_nTenthSec%10 ) * 100000000 );
72 osl_waitThread(&nTV);
73 #endif
74 // if (nVerbose == VERBOSE)
75 // {
76 // printf("done\n");
77 // }
81 /** Simple thread for testing Thread-create.
82 * Just add 1 of value 0, and after running, result is 1.
84 class OGetThread : public osl::Thread
86 sal_Int32 m_nOK;
87 sal_Int32 m_nFails;
89 rtl::OUString m_sConstStr;
90 public:
91 OGetThread()
92 :m_nOK(0),
93 m_nFails(0)
95 m_sConstStr = CONST_TEST_STRING;
98 sal_Int32 getOK() { return m_nOK; }
99 sal_Int32 getFails() {return m_nFails;}
101 protected:
103 /** guarded value which initialized 0
105 @see ThreadSafeValue
107 void SAL_CALL run() SAL_OVERRIDE
109 while(schedule())
111 rtl::OUString aStr = Gregorian::get();
112 if (aStr.equals(m_sConstStr))
114 m_nOK++;
116 else
118 m_nFails++;
120 ThreadHelper::thread_sleep_tenth_sec(1);
124 public:
126 virtual void SAL_CALL suspend() SAL_OVERRIDE
128 ::osl::Thread::suspend();
131 virtual ~OGetThread()
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 // initialise your test code values here.
151 void setUp() SAL_OVERRIDE
155 void tearDown() SAL_OVERRIDE
159 void getValue_001()
161 rtl::OUString aStr = Gregorian::get();
163 CPPUNIT_ASSERT_MESSAGE(
164 "Gregorian::get() failed, wrong value expected.",
165 !aStr.isEmpty()
169 /** check 2 threads.
171 ALGORITHM:
172 Here the function should show, that 2 different threads,
173 which only increase a value, should run at the same time with same prio.
174 The test fails, if the difference between the two values is more than 5%
175 but IMHO this isn't a failure, it's only a feature of the OS.
178 void getValue_002()
180 // initial 5 threads with different priorities
181 OGetThread* pThread = new OGetThread();
182 OGetThread* p2Thread = new OGetThread();
184 //Create them and start running at the same time
185 pThread->create();
186 p2Thread->create();
188 ThreadHelper::thread_sleep_tenth_sec(5);
190 pThread->terminate();
191 p2Thread->terminate();
193 pThread->join();
194 p2Thread->join();
196 sal_Int32 nValueOK = 0;
197 nValueOK = pThread->getOK();
199 sal_Int32 nValueOK2 = 0;
200 nValueOK2 = p2Thread->getOK();
202 #if OSL_DEBUG_LEVEL > 2
203 printf("Value in Thread #1 is %" SAL_PRIdINT32 "\n", nValueOK);
204 printf("Value in Thread #2 is %" SAL_PRIdINT32 "\n", nValueOK2);
205 #else
206 (void)nValueOK2;
207 #endif
209 sal_Int32 nValueFails = 0;
210 nValueFails = pThread->getFails();
212 sal_Int32 nValueFails2 = 0;
213 nValueFails2 = p2Thread->getFails();
215 #if OSL_DEBUG_LEVEL > 2
216 printf("Fails in Thread #1 is %" SAL_PRIdINT32 "\n", nValueFails);
217 printf("Fails in Thread #2 is %" SAL_PRIdINT32 "\n", nValueFails2);
218 #endif
220 delete pThread;
221 delete p2Thread;
223 CPPUNIT_ASSERT_MESSAGE(
224 "getValue() failed, wrong value expected.",
225 nValueOK != 0 && nValueFails == 0 && nValueFails2 == 0
229 CPPUNIT_TEST_SUITE(getValue);
230 CPPUNIT_TEST(getValue_001);
231 CPPUNIT_TEST(getValue_002);
232 CPPUNIT_TEST_SUITE_END();
233 }; // class create
235 CPPUNIT_TEST_SUITE_REGISTRATION(rtl_DoubleLocking::getValue);
236 } // namespace rtl_DoubleLocking
238 // this macro creates an empty function, which will called by the RegisterAllFunctions()
239 // to let the user the possibility to also register some functions by hand.
240 CPPUNIT_PLUGIN_IMPLEMENT();
242 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */