merged tag ooo/DEV300_m102
[LibreOffice.git] / sal / qa / rtl / doublelock / rtl_doublelocking.cxx
blob716a37df028129c2806b1f9a8a77d7f4f5d40aa0
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_sal.hxx"
30 //------------------------------------------------------------------------
31 // include files
32 //------------------------------------------------------------------------
33 #include <sal/types.h>
35 #ifndef _RTL_USTRING_HXX_
36 #include <rtl/string.hxx>
37 #endif
39 #ifndef _OSL_THREAD_HXX
40 #include <osl/thread.hxx>
41 #endif
42 #include <osl/time.h>
44 #include <rtl/instance.hxx>
46 #include <testshl/simpleheader.hxx>
48 // -----------------------------------------------------------------------------
49 #define CONST_TEST_STRING "gregorian"
51 namespace {
52 struct Gregorian : public rtl::StaticWithInit<const ::rtl::OUString, Gregorian> {
53 const ::rtl::OUString operator () () {
54 return ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( CONST_TEST_STRING ));
59 inline void printOUString( ::rtl::OUString const & _suStr )
61 rtl::OString aString;
63 t_print( "OUString: " );
64 aString = ::rtl::OUStringToOString( _suStr, RTL_TEXTENCODING_ASCII_US );
65 t_print( "'%s'\n", aString.getStr( ) );
68 // -----------------------------------------------------------------------------
69 namespace ThreadHelper
71 // typedef enum {
72 // QUIET=1,
73 // VERBOSE
74 // } eSleepVerboseMode;
76 void thread_sleep_tenth_sec(sal_Int32 _nTenthSec/*, eSleepVerboseMode nVerbose = VERBOSE*/)
78 // if (nVerbose == VERBOSE)
79 // {
80 // t_print("wait %d tenth seconds. ", _nTenthSec );
81 // fflush(stdout);
82 // }
83 #ifdef WNT //Windows
84 Sleep(_nTenthSec * 100 );
85 #endif
86 #if ( defined UNX ) || ( defined OS2 ) //Unix
87 TimeValue nTV;
88 nTV.Seconds = static_cast<sal_uInt32>( _nTenthSec/10 );
89 nTV.Nanosec = ( (_nTenthSec%10 ) * 100000000 );
90 osl_waitThread(&nTV);
91 #endif
92 // if (nVerbose == VERBOSE)
93 // {
94 // t_print("done\n");
95 // }
99 // -----------------------------------------------------------------------------
101 /** Simple thread for testing Thread-create.
102 * Just add 1 of value 0, and after running, result is 1.
104 class OGetThread : public osl::Thread
106 sal_Int32 m_nOK;
107 sal_Int32 m_nFails;
109 rtl::OUString m_sConstStr;
110 public:
111 OGetThread()
112 :m_nOK(0),
113 m_nFails(0)
115 m_sConstStr = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( CONST_TEST_STRING ));
118 sal_Int32 getOK() { return m_nOK; }
119 sal_Int32 getFails() {return m_nFails;}
121 protected:
123 /** guarded value which initialized 0
125 @see ThreadSafeValue
127 void SAL_CALL run()
129 while(schedule())
131 rtl::OUString aStr = Gregorian::get();
132 // printOUString(aStr);
133 // printOUString(m_sConstStr);
134 if (aStr.equals(m_sConstStr))
136 m_nOK++;
138 else
140 m_nFails++;
142 ThreadHelper::thread_sleep_tenth_sec(1);
146 public:
148 virtual void SAL_CALL suspend()
150 ::osl::Thread::suspend();
153 ~OGetThread()
155 if (isRunning())
157 t_print("error: not terminated.\n");
162 // -----------------------------------------------------------------------------
163 namespace rtl_DoubleLocking
166 /** Test of the osl::Thread::create method
169 class getValue : public CppUnit::TestFixture
171 public:
173 // initialise your test code values here.
174 void setUp()
178 void tearDown()
183 void getValue_001()
185 rtl::OUString aStr = Gregorian::get();
186 printOUString(aStr);
188 CPPUNIT_ASSERT_MESSAGE(
189 "Gregorian::get() failed, wrong value expected.",
190 aStr.getLength() != 0
194 /** check 2 threads.
196 ALGORITHM:
197 Here the function should show, that 2 different threads,
198 which only increase a value, should run at the same time with same prio.
199 The test fails, if the difference between the two values is more than 5%
200 but IMHO this isn't a failure, it's only a feature of the OS.
203 void getValue_002()
205 // initial 5 threads with different priorities
206 OGetThread* pThread = new OGetThread();
207 OGetThread* p2Thread = new OGetThread();
209 //Create them and start running at the same time
210 pThread->create();
211 p2Thread->create();
213 ThreadHelper::thread_sleep_tenth_sec(50);
215 pThread->terminate();
216 p2Thread->terminate();
218 sal_Int32 nValueOK = 0;
219 nValueOK = pThread->getOK();
221 sal_Int32 nValueOK2 = 0;
222 nValueOK2 = p2Thread->getOK();
224 t_print("Value in Thread #1 is %d\n", nValueOK);
225 t_print("Value in Thread #2 is %d\n", nValueOK2);
227 sal_Int32 nValueFails = 0;
228 nValueFails = pThread->getFails();
230 sal_Int32 nValueFails2 = 0;
231 nValueFails2 = p2Thread->getFails();
233 t_print("Fails in Thread #1 is %d\n", nValueFails);
234 t_print("Fails in Thread #2 is %d\n", nValueFails2);
236 // ThreadHelper::thread_sleep_tenth_sec(1);
237 pThread->join();
238 p2Thread->join();
240 delete pThread;
241 delete p2Thread;
243 CPPUNIT_ASSERT_MESSAGE(
244 "getValue() failed, wrong value expected.",
245 nValueOK != 0 && nValueFails == 0 && nValueFails2 == 0
249 CPPUNIT_TEST_SUITE(getValue);
250 CPPUNIT_TEST(getValue_001);
251 CPPUNIT_TEST(getValue_002);
252 CPPUNIT_TEST_SUITE_END();
253 }; // class create
254 // -----------------------------------------------------------------------------
255 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_DoubleLocking::getValue, "rtl_DoubleLocking");
256 } // namespace rtl_DoubleLocking
258 // this macro creates an empty function, which will called by the RegisterAllFunctions()
259 // to let the user the possibility to also register some functions by hand.
260 NOADDITIONAL;