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 //------------------------------------------------------------------------
32 //------------------------------------------------------------------------
33 #include <sal/types.h>
35 #ifndef _RTL_USTRING_HXX_
36 #include <rtl/string.hxx>
39 #ifndef _OSL_THREAD_HXX
40 #include <osl/thread.hxx>
44 #include <rtl/instance.hxx>
46 #include <testshl/simpleheader.hxx>
48 // -----------------------------------------------------------------------------
49 #define CONST_TEST_STRING "gregorian"
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
)
63 t_print( "OUString: " );
64 aString
= ::rtl::OUStringToOString( _suStr
, RTL_TEXTENCODING_ASCII_US
);
65 t_print( "'%s'\n", aString
.getStr( ) );
68 // -----------------------------------------------------------------------------
69 namespace ThreadHelper
74 // } eSleepVerboseMode;
76 void thread_sleep_tenth_sec(sal_Int32 _nTenthSec
/*, eSleepVerboseMode nVerbose = VERBOSE*/)
78 // if (nVerbose == VERBOSE)
80 // t_print("wait %d tenth seconds. ", _nTenthSec );
84 Sleep(_nTenthSec
* 100 );
86 #if ( defined UNX ) || ( defined OS2 ) //Unix
88 nTV
.Seconds
= static_cast<sal_uInt32
>( _nTenthSec
/10 );
89 nTV
.Nanosec
= ( (_nTenthSec
%10 ) * 100000000 );
92 // if (nVerbose == VERBOSE)
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
109 rtl::OUString m_sConstStr
;
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
;}
123 /** guarded value which initialized 0
131 rtl::OUString aStr
= Gregorian::get();
132 // printOUString(aStr);
133 // printOUString(m_sConstStr);
134 if (aStr
.equals(m_sConstStr
))
142 ThreadHelper::thread_sleep_tenth_sec(1);
148 virtual void SAL_CALL
suspend()
150 ::osl::Thread::suspend();
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
173 // initialise your test code values here.
185 rtl::OUString aStr
= Gregorian::get();
188 CPPUNIT_ASSERT_MESSAGE(
189 "Gregorian::get() failed, wrong value expected.",
190 aStr
.getLength() != 0
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.
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
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);
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();
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.