merge the formfield patch from ooo-build
[ooovba.git] / sal / qa / rtl / doublelock / rtl_doublelocking.cxx
blob1c5ba8506bedebcc4787c7540058bde398ea0668
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: rtl_doublelocking.cxx,v $
10 * $Revision: 1.5 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sal.hxx"
33 //------------------------------------------------------------------------
34 // include files
35 //------------------------------------------------------------------------
36 #include <sal/types.h>
38 #ifndef _RTL_USTRING_HXX_
39 #include <rtl/string.hxx>
40 #endif
42 #ifndef _OSL_THREAD_HXX
43 #include <osl/thread.hxx>
44 #endif
45 #include <osl/time.h>
47 #include <rtl/instance.hxx>
49 #include <cppunit/simpleheader.hxx>
51 // -----------------------------------------------------------------------------
52 #define CONST_TEST_STRING "gregorian"
54 namespace {
55 struct Gregorian : public rtl::StaticWithInit<const ::rtl::OUString, Gregorian> {
56 const ::rtl::OUString operator () () {
57 return ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( CONST_TEST_STRING ));
62 inline void printOUString( ::rtl::OUString const & _suStr )
64 rtl::OString aString;
66 t_print( "OUString: " );
67 aString = ::rtl::OUStringToOString( _suStr, RTL_TEXTENCODING_ASCII_US );
68 t_print( "'%s'\n", aString.getStr( ) );
71 // -----------------------------------------------------------------------------
72 namespace ThreadHelper
74 // typedef enum {
75 // QUIET=1,
76 // VERBOSE
77 // } eSleepVerboseMode;
79 void thread_sleep_tenth_sec(sal_Int32 _nTenthSec/*, eSleepVerboseMode nVerbose = VERBOSE*/)
81 // if (nVerbose == VERBOSE)
82 // {
83 // t_print("wait %d tenth seconds. ", _nTenthSec );
84 // fflush(stdout);
85 // }
86 #ifdef WNT //Windows
87 Sleep(_nTenthSec * 100 );
88 #endif
89 #if ( defined UNX ) || ( defined OS2 ) //Unix
90 TimeValue nTV;
91 nTV.Seconds = static_cast<sal_uInt32>( _nTenthSec/10 );
92 nTV.Nanosec = ( (_nTenthSec%10 ) * 100000000 );
93 osl_waitThread(&nTV);
94 #endif
95 // if (nVerbose == VERBOSE)
96 // {
97 // t_print("done\n");
98 // }
102 // -----------------------------------------------------------------------------
104 /** Simple thread for testing Thread-create.
105 * Just add 1 of value 0, and after running, result is 1.
107 class OGetThread : public osl::Thread
109 sal_Int32 m_nOK;
110 sal_Int32 m_nFails;
112 rtl::OUString m_sConstStr;
113 public:
114 OGetThread()
115 :m_nOK(0),
116 m_nFails(0)
118 m_sConstStr = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( CONST_TEST_STRING ));
121 sal_Int32 getOK() { return m_nOK; }
122 sal_Int32 getFails() {return m_nFails;}
124 protected:
126 /** guarded value which initialized 0
128 @see ThreadSafeValue
130 void SAL_CALL run()
132 while(schedule())
134 rtl::OUString aStr = Gregorian::get();
135 // printOUString(aStr);
136 // printOUString(m_sConstStr);
137 if (aStr.equals(m_sConstStr))
139 m_nOK++;
141 else
143 m_nFails++;
145 ThreadHelper::thread_sleep_tenth_sec(1);
149 public:
151 virtual void SAL_CALL suspend()
153 ::osl::Thread::suspend();
156 ~OGetThread()
158 if (isRunning())
160 t_print("error: not terminated.\n");
165 // -----------------------------------------------------------------------------
166 namespace rtl_DoubleLocking
169 /** Test of the osl::Thread::create method
172 class getValue : public CppUnit::TestFixture
174 public:
176 // initialise your test code values here.
177 void setUp()
181 void tearDown()
186 void getValue_001()
188 rtl::OUString aStr = Gregorian::get();
189 printOUString(aStr);
191 CPPUNIT_ASSERT_MESSAGE(
192 "Gregorian::get() failed, wrong value expected.",
193 aStr.getLength() != 0
197 /** check 2 threads.
199 ALGORITHM:
200 Here the function should show, that 2 different threads,
201 which only increase a value, should run at the same time with same prio.
202 The test fails, if the difference between the two values is more than 5%
203 but IMHO this isn't a failure, it's only a feature of the OS.
206 void getValue_002()
208 // initial 5 threads with different priorities
209 OGetThread* pThread = new OGetThread();
210 OGetThread* p2Thread = new OGetThread();
212 //Create them and start running at the same time
213 pThread->create();
214 p2Thread->create();
216 ThreadHelper::thread_sleep_tenth_sec(50);
218 pThread->terminate();
219 p2Thread->terminate();
221 sal_Int32 nValueOK = 0;
222 nValueOK = pThread->getOK();
224 sal_Int32 nValueOK2 = 0;
225 nValueOK2 = p2Thread->getOK();
227 t_print("Value in Thread #1 is %d\n", nValueOK);
228 t_print("Value in Thread #2 is %d\n", nValueOK2);
230 sal_Int32 nValueFails = 0;
231 nValueFails = pThread->getFails();
233 sal_Int32 nValueFails2 = 0;
234 nValueFails2 = p2Thread->getFails();
236 t_print("Fails in Thread #1 is %d\n", nValueFails);
237 t_print("Fails in Thread #2 is %d\n", nValueFails2);
239 // ThreadHelper::thread_sleep_tenth_sec(1);
240 pThread->join();
241 p2Thread->join();
243 delete pThread;
244 delete p2Thread;
246 CPPUNIT_ASSERT_MESSAGE(
247 "getValue() failed, wrong value expected.",
248 nValueOK != 0 && nValueFails == 0 && nValueFails2 == 0
252 CPPUNIT_TEST_SUITE(getValue);
253 CPPUNIT_TEST(getValue_001);
254 CPPUNIT_TEST(getValue_002);
255 CPPUNIT_TEST_SUITE_END();
256 }; // class create
257 // -----------------------------------------------------------------------------
258 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_DoubleLocking::getValue, "rtl_DoubleLocking");
259 } // namespace rtl_DoubleLocking
261 // this macro creates an empty function, which will called by the RegisterAllFunctions()
262 // to let the user the possibility to also register some functions by hand.
263 NOADDITIONAL;