1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
22 #include <sal/types.h>
24 #include <osl/thread.hxx>
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>
41 #define CONST_TEST_STRING "gregorian"
44 struct Gregorian
: public rtl::StaticWithInit
<rtl::OUString
, Gregorian
> {
45 const rtl::OUString
operator () () {
46 return rtl::OUString( CONST_TEST_STRING
);
51 namespace ThreadHelper
56 // } eSleepVerboseMode;
58 void thread_sleep_tenth_sec(sal_Int32 _nTenthSec
/*, eSleepVerboseMode nVerbose = VERBOSE*/)
60 // if (nVerbose == VERBOSE)
62 // printf("wait %d tenth seconds. ", _nTenthSec );
66 Sleep(_nTenthSec
* 100 );
70 nTV
.Seconds
= static_cast<sal_uInt32
>( _nTenthSec
/10 );
71 nTV
.Nanosec
= ( (_nTenthSec
%10 ) * 100000000 );
74 // if (nVerbose == VERBOSE)
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
89 rtl::OUString m_sConstStr
;
95 m_sConstStr
= CONST_TEST_STRING
;
98 sal_Int32
getOK() { return m_nOK
; }
99 sal_Int32
getFails() {return m_nFails
;}
103 /** guarded value which initialized 0
107 void SAL_CALL
run() SAL_OVERRIDE
111 rtl::OUString aStr
= Gregorian::get();
112 if (aStr
.equals(m_sConstStr
))
120 ThreadHelper::thread_sleep_tenth_sec(1);
126 virtual void SAL_CALL
suspend() SAL_OVERRIDE
128 ::osl::Thread::suspend();
131 virtual ~OGetThread()
135 printf("error: not terminated.\n");
140 namespace rtl_DoubleLocking
143 /** Test of the osl::Thread::create method
146 class getValue
: public CppUnit::TestFixture
150 // initialise your test code values here.
151 void setUp() SAL_OVERRIDE
155 void tearDown() SAL_OVERRIDE
161 rtl::OUString aStr
= Gregorian::get();
163 CPPUNIT_ASSERT_MESSAGE(
164 "Gregorian::get() failed, wrong value expected.",
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.
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
188 ThreadHelper::thread_sleep_tenth_sec(5);
190 pThread
->terminate();
191 p2Thread
->terminate();
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
);
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
);
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();
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: */