LCOV - code coverage report
Current view: top level - sal/qa/rtl/doublelock - rtl_doublelocking.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 64 69 92.8 %
Date: 2014-11-03 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             : // include files
      21             : 
      22             : #include <sal/types.h>
      23             : 
      24             : #include <osl/thread.hxx>
      25             : #include <osl/time.h>
      26             : 
      27             : #include <rtl/instance.hxx>
      28             : #include <rtl/ustring.hxx>
      29             : 
      30             : #include <cppunit/TestFixture.h>
      31             : #include <cppunit/extensions/HelperMacros.h>
      32             : #include <cppunit/plugin/TestPlugIn.h>
      33             : 
      34             : #ifdef WNT
      35             : #include <windows.h>
      36             : #else
      37             : #include <unistd.h>
      38             : #include <time.h>
      39             : #endif
      40             : 
      41             : #define CONST_TEST_STRING "gregorian"
      42             : 
      43             : namespace {
      44             : struct Gregorian : public rtl::StaticWithInit<rtl::OUString, Gregorian> {
      45           2 :     const rtl::OUString operator () () {
      46           2 :         return rtl::OUString( CONST_TEST_STRING );
      47             :     }
      48             : };
      49             : }
      50             : 
      51             : namespace ThreadHelper
      52             : {
      53             :     // typedef enum {
      54             :     //     QUIET=1,
      55             :     //     VERBOSE
      56             :     // } eSleepVerboseMode;
      57             : 
      58          22 :     void thread_sleep_tenth_sec(sal_Int32 _nTenthSec/*, eSleepVerboseMode nVerbose = VERBOSE*/)
      59             :     {
      60             :         // if (nVerbose == VERBOSE)
      61             :         // {
      62             :         //     printf("wait %d tenth seconds. ", _nTenthSec );
      63             :         //     fflush(stdout);
      64             :         // }
      65             : #ifdef WNT      //Windows
      66             :         Sleep(_nTenthSec * 100 );
      67             : #endif
      68             : #if ( defined UNX )
      69             :         TimeValue nTV;
      70          22 :         nTV.Seconds = static_cast<sal_uInt32>( _nTenthSec/10 );
      71          22 :         nTV.Nanosec = ( (_nTenthSec%10 ) * 100000000 );
      72          22 :         osl_waitThread(&nTV);
      73             : #endif
      74             :         // if (nVerbose == VERBOSE)
      75             :         // {
      76             :         //     printf("done\n");
      77             :         // }
      78          22 :     }
      79             : }
      80             : 
      81             : /** Simple thread for testing Thread-create.
      82             :  * Just add 1 of value 0, and after running, result is 1.
      83             :  */
      84             : class OGetThread : public osl::Thread
      85             : {
      86             :     sal_Int32 m_nOK;
      87             :     sal_Int32 m_nFails;
      88             : 
      89             :     rtl::OUString m_sConstStr;
      90             : public:
      91           4 :     OGetThread()
      92             :             :m_nOK(0),
      93           4 :              m_nFails(0)
      94             :         {
      95           4 :             m_sConstStr = rtl::OUString( CONST_TEST_STRING );
      96           4 :         }
      97             : 
      98           4 :     sal_Int32 getOK() { return m_nOK; }
      99           4 :     sal_Int32 getFails() {return m_nFails;}
     100             : 
     101             : protected:
     102             : 
     103             :     /** guarded value which initialized 0
     104             : 
     105             :         @see ThreadSafeValue
     106             :     */
     107           4 :     void SAL_CALL run() SAL_OVERRIDE
     108             :         {
     109          28 :             while(schedule())
     110             :             {
     111          20 :                 rtl::OUString aStr = Gregorian::get();
     112          20 :                 if (aStr.equals(m_sConstStr))
     113             :                 {
     114          20 :                     m_nOK++;
     115             :                 }
     116             :                 else
     117             :                 {
     118           0 :                     m_nFails++;
     119             :                 }
     120          20 :                 ThreadHelper::thread_sleep_tenth_sec(1);
     121          20 :             }
     122           4 :         }
     123             : 
     124             : public:
     125             : 
     126           0 :     virtual void SAL_CALL suspend() SAL_OVERRIDE
     127             :         {
     128           0 :             ::osl::Thread::suspend();
     129           0 :         }
     130             : 
     131           8 :     virtual ~OGetThread()
     132           8 :         {
     133           4 :             if (isRunning())
     134             :             {
     135           0 :                 printf("error: not terminated.\n");
     136             :             }
     137           8 :         }
     138             : };
     139             : 
     140             : namespace rtl_DoubleLocking
     141             : {
     142             : 
     143             : /** Test of the osl::Thread::create method
     144             :  */
     145             : 
     146          12 :     class getValue : public CppUnit::TestFixture
     147             :     {
     148             :     public:
     149             : 
     150             :         // initialise your test code values here.
     151           4 :         void setUp() SAL_OVERRIDE
     152             :             {
     153           4 :             }
     154             : 
     155           4 :         void tearDown() SAL_OVERRIDE
     156             :             {
     157           4 :             }
     158             : 
     159           2 :         void getValue_001()
     160             :             {
     161           2 :                 rtl::OUString aStr = Gregorian::get();
     162             : 
     163           4 :                 CPPUNIT_ASSERT_MESSAGE(
     164             :                     "Gregorian::get() failed, wrong value expected.",
     165             :                     !aStr.isEmpty()
     166           4 :                     );
     167           2 :             }
     168             : 
     169             :         /** check 2 threads.
     170             : 
     171             :             ALGORITHM:
     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.
     176             :         */
     177             : 
     178           2 :         void getValue_002()
     179             :             {
     180             :                 // initial 5 threads with different priorities
     181           2 :                 OGetThread* pThread = new OGetThread();
     182           2 :                 OGetThread* p2Thread = new OGetThread();
     183             : 
     184             :                 //Create them and start running at the same time
     185           2 :                 pThread->create();
     186           2 :                 p2Thread->create();
     187             : 
     188           2 :                 ThreadHelper::thread_sleep_tenth_sec(5);
     189             : 
     190           2 :                 pThread->terminate();
     191           2 :                 p2Thread->terminate();
     192             : 
     193           2 :                 pThread->join();
     194           2 :                 p2Thread->join();
     195             : 
     196           2 :                 sal_Int32 nValueOK = 0;
     197           2 :                 nValueOK = pThread->getOK();
     198             : 
     199           2 :                 sal_Int32 nValueOK2 = 0;
     200           2 :                 nValueOK2 = p2Thread->getOK();
     201             : 
     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);
     205             : #else
     206             :                 (void)nValueOK2;
     207             : #endif
     208             : 
     209           2 :                 sal_Int32 nValueFails = 0;
     210           2 :                 nValueFails = pThread->getFails();
     211             : 
     212           2 :                 sal_Int32 nValueFails2 = 0;
     213           2 :                 nValueFails2 = p2Thread->getFails();
     214             : 
     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);
     218             : #endif
     219             : 
     220           2 :                 delete pThread;
     221           2 :                 delete p2Thread;
     222             : 
     223           4 :                 CPPUNIT_ASSERT_MESSAGE(
     224             :                     "getValue() failed, wrong value expected.",
     225             :                     nValueOK != 0 && nValueFails == 0 && nValueFails2 == 0
     226           2 :                     );
     227           2 :             }
     228             : 
     229           4 :         CPPUNIT_TEST_SUITE(getValue);
     230           2 :         CPPUNIT_TEST(getValue_001);
     231           2 :         CPPUNIT_TEST(getValue_002);
     232           4 :         CPPUNIT_TEST_SUITE_END();
     233             :     }; // class create
     234             : 
     235           2 :     CPPUNIT_TEST_SUITE_REGISTRATION(rtl_DoubleLocking::getValue);
     236             : } // namespace rtl_DoubleLocking
     237             : 
     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           8 : CPPUNIT_PLUGIN_IMPLEMENT();
     241             : 
     242             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10