LCOV - code coverage report
Current view: top level - libreoffice/sal/qa/rtl/doublelock - rtl_doublelocking.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 64 69 92.8 %
Date: 2012-12-17 Functions: 21 23 91.3 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       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/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      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 .
      18             :  */
      19             : 
      20             : //------------------------------------------------------------------------
      21             : // include files
      22             : //------------------------------------------------------------------------
      23             : #include <sal/types.h>
      24             : 
      25             : #include <osl/thread.hxx>
      26             : #include <osl/time.h>
      27             : 
      28             : #include <rtl/instance.hxx>
      29             : #include <rtl/ustring.hxx>
      30             : 
      31             : #include <cppunit/TestFixture.h>
      32             : #include <cppunit/extensions/HelperMacros.h>
      33             : #include <cppunit/plugin/TestPlugIn.h>
      34             : 
      35             : #ifdef WNT
      36             : #include <windows.h>
      37             : #else
      38             : #include <unistd.h>
      39             : #include <time.h>
      40             : #endif
      41             : 
      42             : // -----------------------------------------------------------------------------
      43             : #define CONST_TEST_STRING "gregorian"
      44             : 
      45             : namespace {
      46             : struct Gregorian : public rtl::StaticWithInit<rtl::OUString, Gregorian> {
      47           2 :     const rtl::OUString operator () () {
      48           2 :         return rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( CONST_TEST_STRING ));
      49             :     }
      50             : };
      51             : }
      52             : 
      53             : // -----------------------------------------------------------------------------
      54             : namespace ThreadHelper
      55             : {
      56             :     // typedef enum {
      57             :     //     QUIET=1,
      58             :     //     VERBOSE
      59             :     // } eSleepVerboseMode;
      60             : 
      61          22 :     void thread_sleep_tenth_sec(sal_Int32 _nTenthSec/*, eSleepVerboseMode nVerbose = VERBOSE*/)
      62             :     {
      63             :         // if (nVerbose == VERBOSE)
      64             :         // {
      65             :         //     printf("wait %d tenth seconds. ", _nTenthSec );
      66             :         //     fflush(stdout);
      67             :         // }
      68             : #ifdef WNT      //Windows
      69             :         Sleep(_nTenthSec * 100 );
      70             : #endif
      71             : #if ( defined UNX )
      72             :         TimeValue nTV;
      73          22 :         nTV.Seconds = static_cast<sal_uInt32>( _nTenthSec/10 );
      74          22 :         nTV.Nanosec = ( (_nTenthSec%10 ) * 100000000 );
      75          22 :         osl_waitThread(&nTV);
      76             : #endif
      77             :         // if (nVerbose == VERBOSE)
      78             :         // {
      79             :         //     printf("done\n");
      80             :         // }
      81          22 :     }
      82             : }
      83             : 
      84             : // -----------------------------------------------------------------------------
      85             : 
      86             : /** Simple thread for testing Thread-create.
      87             :  * Just add 1 of value 0, and after running, result is 1.
      88             :  */
      89             : class OGetThread : public osl::Thread
      90             : {
      91             :     sal_Int32 m_nOK;
      92             :     sal_Int32 m_nFails;
      93             : 
      94             :     rtl::OUString m_sConstStr;
      95             : public:
      96           4 :     OGetThread()
      97             :             :m_nOK(0),
      98           4 :              m_nFails(0)
      99             :         {
     100           4 :             m_sConstStr = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( CONST_TEST_STRING ));
     101           4 :         }
     102             : 
     103           4 :     sal_Int32 getOK() { return m_nOK; }
     104           4 :     sal_Int32 getFails() {return m_nFails;}
     105             : 
     106             : protected:
     107             : 
     108             :     /** guarded value which initialized 0
     109             : 
     110             :         @see ThreadSafeValue
     111             :     */
     112           4 :     void SAL_CALL run()
     113             :         {
     114          28 :             while(schedule())
     115             :             {
     116          20 :                 rtl::OUString aStr = Gregorian::get();
     117          20 :                 if (aStr.equals(m_sConstStr))
     118             :                 {
     119          20 :                     m_nOK++;
     120             :                 }
     121             :                 else
     122             :                 {
     123           0 :                     m_nFails++;
     124             :                 }
     125          20 :                 ThreadHelper::thread_sleep_tenth_sec(1);
     126          20 :             }
     127           4 :         }
     128             : 
     129             : public:
     130             : 
     131           0 :     virtual void SAL_CALL suspend()
     132             :         {
     133           0 :             ::osl::Thread::suspend();
     134           0 :         }
     135             : 
     136           8 :     ~OGetThread()
     137           8 :         {
     138           4 :             if (isRunning())
     139             :             {
     140           0 :                 printf("error: not terminated.\n");
     141             :             }
     142           8 :         }
     143             : };
     144             : 
     145             : // -----------------------------------------------------------------------------
     146             : namespace rtl_DoubleLocking
     147             : {
     148             : 
     149             : /** Test of the osl::Thread::create method
     150             :  */
     151             : 
     152          12 :     class getValue : public CppUnit::TestFixture
     153             :     {
     154             :     public:
     155             : 
     156             :         // initialise your test code values here.
     157           4 :         void setUp()
     158             :             {
     159           4 :             }
     160             : 
     161           4 :         void tearDown()
     162             :             {
     163           4 :             }
     164             : 
     165             : 
     166           2 :         void getValue_001()
     167             :             {
     168           2 :                 rtl::OUString aStr = Gregorian::get();
     169             : 
     170           4 :                 CPPUNIT_ASSERT_MESSAGE(
     171             :                     "Gregorian::get() failed, wrong value expected.",
     172             :                     !aStr.isEmpty()
     173           4 :                     );
     174           2 :             }
     175             : 
     176             :         /** check 2 threads.
     177             : 
     178             :             ALGORITHM:
     179             :             Here the function should show, that 2 different threads,
     180             :             which only increase a value, should run at the same time with same prio.
     181             :             The test fails, if the difference between the two values is more than 5%
     182             :             but IMHO this isn't a failure, it's only a feature of the OS.
     183             :         */
     184             : 
     185           2 :         void getValue_002()
     186             :             {
     187             :                 // initial 5 threads with different priorities
     188           2 :                 OGetThread* pThread = new OGetThread();
     189           2 :                 OGetThread* p2Thread = new OGetThread();
     190             : 
     191             :                 //Create them and start running at the same time
     192           2 :                 pThread->create();
     193           2 :                 p2Thread->create();
     194             : 
     195           2 :                 ThreadHelper::thread_sleep_tenth_sec(5);
     196             : 
     197           2 :                 pThread->terminate();
     198           2 :                 p2Thread->terminate();
     199             : 
     200           2 :                 pThread->join();
     201           2 :                 p2Thread->join();
     202             : 
     203           2 :                 sal_Int32 nValueOK = 0;
     204           2 :                 nValueOK = pThread->getOK();
     205             : 
     206           2 :                 sal_Int32 nValueOK2 = 0;
     207           2 :                 nValueOK2 = p2Thread->getOK();
     208             : 
     209             : #if OSL_DEBUG_LEVEL > 2
     210             :                 printf("Value in Thread #1 is %d\n", nValueOK);
     211             :                 printf("Value in Thread #2 is %d\n", nValueOK2);
     212             : #else
     213             :                 (void)nValueOK2;
     214             : #endif
     215             : 
     216           2 :                 sal_Int32 nValueFails = 0;
     217           2 :                 nValueFails = pThread->getFails();
     218             : 
     219           2 :                 sal_Int32 nValueFails2 = 0;
     220           2 :                 nValueFails2 = p2Thread->getFails();
     221             : 
     222             : #if OSL_DEBUG_LEVEL > 2
     223             :                 printf("Fails in Thread #1 is %d\n", nValueFails);
     224             :                 printf("Fails in Thread #2 is %d\n", nValueFails2);
     225             : #endif
     226             : 
     227           2 :                 delete pThread;
     228           2 :                 delete p2Thread;
     229             : 
     230           4 :                 CPPUNIT_ASSERT_MESSAGE(
     231             :                     "getValue() failed, wrong value expected.",
     232             :                     nValueOK != 0 && nValueFails == 0 && nValueFails2 == 0
     233           2 :                     );
     234           2 :             }
     235             : 
     236           4 :         CPPUNIT_TEST_SUITE(getValue);
     237           2 :         CPPUNIT_TEST(getValue_001);
     238           2 :         CPPUNIT_TEST(getValue_002);
     239           4 :         CPPUNIT_TEST_SUITE_END();
     240             :     }; // class create
     241             : // -----------------------------------------------------------------------------
     242           2 :     CPPUNIT_TEST_SUITE_REGISTRATION(rtl_DoubleLocking::getValue);
     243             : } // namespace rtl_DoubleLocking
     244             : 
     245             : // this macro creates an empty function, which will called by the RegisterAllFunctions()
     246             : // to let the user the possibility to also register some functions by hand.
     247           8 : CPPUNIT_PLUGIN_IMPLEMENT();
     248             : 
     249             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10