LCOV - code coverage report
Current view: top level - sal/qa/rtl/doublelock - rtl_doublelocking.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 64 69 92.8 %
Date: 2012-08-25 Functions: 21 23 91.3 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 67 138 48.6 %

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

Generated by: LCOV version 1.10