LCOV - code coverage report
Current view: top level - libreoffice/sal/qa/osl/condition - osl_Condition.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 151 152 99.3 %
Date: 2012-12-27 Functions: 48 50 96.0 %
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             : //------------------------------------------------------------------------
      22             : // include files
      23             : //------------------------------------------------------------------------
      24             : #include <osl_Condition_Const.h>
      25             : #include <stdlib.h>
      26             : 
      27             : using namespace osl;
      28             : using namespace rtl;
      29             : 
      30             : 
      31             : //------------------------------------------------------------------------
      32             : // helper functions and classes
      33             : //------------------------------------------------------------------------
      34             : 
      35             : /** print Boolean value.
      36             : */
      37             : inline void printBool( sal_Bool bOk )
      38             : {
      39             :     printf("#printBool# " );
      40             :     ( sal_True == bOk ) ? printf("TRUE!\n" ): printf("FALSE!\n" );
      41             : }
      42             : 
      43             : /** print a UNI_CODE String.
      44             : */
      45             : inline void printUString( const ::rtl::OUString & str )
      46             : {
      47             :     rtl::OString aString;
      48             : 
      49             :     printf("#printUString_u# " );
      50             :     aString = ::rtl::OUStringToOString( str, RTL_TEXTENCODING_ASCII_US );
      51             :     printf("%s\n", aString.getStr( ) );
      52             : }
      53             : 
      54             : enum ConditionType
      55             : {
      56             :     thread_type_set,
      57             :     thread_type_reset,
      58             :     thread_type_wait,
      59             :     thread_type_check
      60             : };
      61             : 
      62             : /** thread for testing Condition.
      63             :  */
      64             : class ConditionThread : public Thread
      65             : {
      66             : public:
      67             :     //get the Condition to operate
      68           5 :     ConditionThread( ::osl::Condition& Con, ConditionType tType): m_MyCon( Con ), m_MyType( tType ) { }
      69             : 
      70           5 :     ~ConditionThread( )
      71           5 :     {
      72             :         // LLA: do not throw in DTors!
      73             :         // LLA: CPPUNIT_ASSERT_MESSAGE( "#ConditionThread does not shutdown properly.\n", sal_False == this -> isRunning( ) );
      74           5 :     }
      75             : protected:
      76             :     ::osl::Condition& m_MyCon;
      77             :     ConditionType m_MyType;
      78             : 
      79           5 :     void SAL_CALL run()
      80             :     {
      81           5 :         switch ( m_MyType )
      82             :         {
      83             :             case thread_type_wait:
      84           2 :                 m_MyCon.wait(); break;
      85             :             case thread_type_set:
      86           2 :                 m_MyCon.set(); break;
      87             :             case thread_type_reset:
      88           1 :                 m_MyCon.reset(); break;
      89             :             default:
      90           0 :                 break;
      91             :         }
      92           5 :     }
      93             : };
      94             : 
      95             : 
      96             : //------------------------------------------------------------------------
      97             : // test code start here
      98             : //------------------------------------------------------------------------
      99             : 
     100             : namespace osl_Condition
     101             : {
     102             : 
     103             :     /** testing the method:
     104             :         Condition()
     105             :     */
     106           6 :     class ctors : public CppUnit::TestFixture
     107             :     {
     108             :     public:
     109             :         sal_Bool bRes, bRes1;
     110             : 
     111           1 :         void ctors_001( )
     112             :         {
     113           1 :             ::osl::Condition aCond;
     114           1 :             bRes = aCond.check( );
     115             : 
     116           2 :             CPPUNIT_ASSERT_MESSAGE( "#test comment#: create a condition its initial check state should be sal_False.",
     117           2 :                                     sal_False == bRes );
     118           1 :         }
     119             : 
     120           1 :         void ctors_002( )
     121             :         {
     122           1 :             ::osl::Condition aCond;
     123           1 :             aCond.set( );
     124           1 :             bRes = aCond.check( );
     125             : 
     126           2 :             CPPUNIT_ASSERT_MESSAGE( "#test comment#: create a condition and set it.",
     127           2 :                                     sal_True == bRes );
     128           1 :         }
     129             : 
     130           2 :         CPPUNIT_TEST_SUITE( ctors );
     131           1 :         CPPUNIT_TEST( ctors_001 );
     132           1 :         CPPUNIT_TEST( ctors_002 );
     133           2 :         CPPUNIT_TEST_SUITE_END( );
     134             :     }; // class ctors
     135             : 
     136             : 
     137             :     /** testing the method:
     138             :         void set()
     139             :     */
     140           6 :     class set : public CppUnit::TestFixture
     141             :     {
     142             :     public:
     143             :         sal_Bool bRes, bRes1, bRes2;
     144             : 
     145           1 :         void set_001( )
     146             :         {
     147           1 :             ::osl::Condition aCond;
     148           1 :             aCond.set( );
     149           1 :             bRes = aCond.check( );
     150             : 
     151           2 :             CPPUNIT_ASSERT_MESSAGE( "#test comment#: check state should be sal_True after set.",
     152           2 :                                     sal_True == bRes );
     153           1 :         }
     154             : 
     155           1 :         void set_002( )
     156             :         {
     157           1 :             ::osl::Condition aCond;
     158           1 :             ConditionThread myThread1( aCond, thread_type_wait );
     159           1 :             myThread1.create();
     160           1 :             bRes = myThread1.isRunning( );
     161             : 
     162           1 :             ConditionThread myThread2( aCond, thread_type_set );
     163           1 :             myThread2.create();
     164             : 
     165           1 :             myThread1.join( );
     166           1 :             bRes1 = myThread1.isRunning( );
     167           1 :             bRes2 = aCond.check( );
     168           1 :             myThread2.join( );
     169             : 
     170           2 :             CPPUNIT_ASSERT_MESSAGE( "#test comment#: use one thread to set the condition in order to release another thread.",
     171           2 :                                     sal_True == bRes && sal_False == bRes1 && sal_True == bRes2 );
     172           1 :         }
     173             : 
     174             : 
     175           2 :         CPPUNIT_TEST_SUITE( set );
     176           1 :         CPPUNIT_TEST( set_001 );
     177           1 :         CPPUNIT_TEST( set_002 );
     178           2 :         CPPUNIT_TEST_SUITE_END( );
     179             :     }; // class set
     180             : 
     181             : 
     182             :     /** testing the method:
     183             :         void reset()
     184             :     */
     185           6 :     class reset : public CppUnit::TestFixture
     186             :     {
     187             :     public:
     188             :         sal_Bool bRes, bRes1, bRes2;
     189             : 
     190           1 :         void reset_001( )
     191             :         {
     192           1 :             ::osl::Condition aCond;
     193           1 :             aCond.reset( );
     194             : 
     195           1 :             ConditionThread myThread( aCond, thread_type_wait );
     196           1 :             myThread.create();
     197           1 :             bRes = myThread.isRunning( );
     198           1 :             bRes2 = aCond.check( );
     199             : 
     200           1 :             aCond.set( );
     201           1 :             myThread.join( );
     202           1 :             bRes1 = myThread.isRunning( );
     203             : 
     204           2 :             CPPUNIT_ASSERT_MESSAGE( "#test comment#: wait will cause a reset thread block, use set to release it.",
     205           2 :                                     sal_True == bRes && sal_False == bRes1 && sal_False == bRes2 );
     206           1 :         }
     207             : 
     208           1 :         void reset_002( )
     209             :         {
     210           1 :             ::osl::Condition aCond;
     211           1 :             aCond.reset( );
     212           1 :             bRes = aCond.check( );
     213           1 :             aCond.set( );
     214           1 :             bRes1 = aCond.check( );
     215             : 
     216           2 :             CPPUNIT_ASSERT_MESSAGE( "#test comment#: create a condition and reset/set it.",
     217           2 :                                     ( sal_False == bRes && sal_True == bRes1 ) );
     218           1 :         }
     219             : 
     220           2 :         CPPUNIT_TEST_SUITE( reset );
     221           1 :         CPPUNIT_TEST( reset_001 );
     222           1 :         CPPUNIT_TEST( reset_002 );
     223           2 :         CPPUNIT_TEST_SUITE_END( );
     224             :     }; // class reset
     225             : 
     226             : 
     227             :     /** testing the method:
     228             :         Result wait(const TimeValue *pTimeout = 0)
     229             :     */
     230           6 :     class wait : public CppUnit::TestFixture
     231             :     {
     232             :     public:
     233             :         sal_Bool bRes, bRes1, bRes2;
     234             :         TimeValue *tv1;
     235             : 
     236           2 :         void setUp( )
     237             :         {
     238           2 :             tv1 = new TimeValue;
     239           2 :             tv1->Seconds = 1;
     240           2 :             tv1->Nanosec = 0;
     241             : 
     242           2 :         }
     243             : 
     244           2 :         void tearDown( )
     245             :         {
     246           2 :             delete tv1;
     247           2 :         }
     248             : 
     249             : 
     250           1 :         void wait_001( )
     251             :         {
     252           1 :             ::osl::Condition cond1;
     253           1 :             ::osl::Condition cond2;
     254           1 :             ::osl::Condition cond3;
     255             : 
     256           1 :             cond1.set();
     257           1 :             cond2.set();
     258             : 
     259           1 :             osl::Condition::Result r1=cond1.wait(tv1);
     260           1 :             osl::Condition::Result r2=cond2.wait();
     261           1 :             osl::Condition::Result r3=cond3.wait(tv1);
     262             : 
     263           2 :             CPPUNIT_ASSERT_MESSAGE( "#test comment#: test three types of wait.",
     264             :                                     (r1 == ::osl::Condition::result_ok) &&
     265             :                                     (r2 == ::osl::Condition::result_ok) &&
     266           2 :                                     (r3 == ::osl::Condition::result_timeout) );
     267           1 :         }
     268             : 
     269           1 :         void wait_002( )
     270             :         {
     271           1 :             ::osl::Condition aCond;
     272             :             ::osl::Condition::Result wRes, wRes1;
     273             : 
     274           1 :             aCond.reset( );
     275           1 :             bRes = aCond.check( );
     276           1 :             wRes = aCond.wait( tv1 );
     277             : 
     278           1 :             aCond.set( );
     279           1 :             wRes1 = aCond.wait( tv1 );
     280           1 :             bRes1 = aCond.check( );
     281             : 
     282           2 :             CPPUNIT_ASSERT_MESSAGE( "#test comment#: wait a condition after set/reset.",
     283             :                                     ( sal_False == bRes ) && ( sal_True == bRes1 ) &&
     284             :                                     ( ::osl::Condition::result_timeout == wRes ) &&
     285           2 :                                     ( ::osl::Condition::result_ok == wRes1 ) );
     286           1 :         }
     287             : 
     288           2 :         CPPUNIT_TEST_SUITE( wait );
     289           1 :         CPPUNIT_TEST( wait_001 );
     290           1 :         CPPUNIT_TEST( wait_002 );
     291           2 :         CPPUNIT_TEST_SUITE_END( );
     292             :     }; // class wait
     293             : 
     294             : 
     295             :     /** testing the method:
     296             :         sal_Bool check()
     297             :     */
     298           6 :     class check : public CppUnit::TestFixture
     299             :     {
     300             :     public:
     301             :         sal_Bool bRes, bRes1, bRes2;
     302             : 
     303           1 :         void check_001( )
     304             :         {
     305           1 :             ::osl::Condition aCond;
     306             : 
     307           1 :             aCond.reset( );
     308           1 :             bRes = aCond.check( );
     309           1 :             aCond.set( );
     310           1 :             bRes1 = aCond.check( );
     311             : 
     312           2 :             CPPUNIT_ASSERT_MESSAGE( "#test comment#: check the condition states.",
     313           2 :                                     ( sal_False == bRes && sal_True == bRes1 ) );
     314           1 :         }
     315             : 
     316           1 :         void check_002( )
     317             :         {
     318           1 :             ::osl::Condition aCond;
     319           1 :             aCond.reset( );
     320             : 
     321           1 :             ConditionThread myThread( aCond, thread_type_set );
     322           1 :             myThread.create( );
     323           1 :             myThread.join( );
     324           1 :             bRes = aCond.check( );
     325             : 
     326           1 :             ConditionThread myThread1( aCond, thread_type_reset );
     327           1 :             myThread1.create( );
     328           1 :             myThread1.join( );
     329           1 :             bRes1 = aCond.check( );
     330             : 
     331           2 :             CPPUNIT_ASSERT_MESSAGE( "#test comment#: use threads to set/reset Condition and check it in main routine.",
     332           2 :                                     ( sal_True == bRes && sal_False == bRes1 ) );
     333           1 :         }
     334             : 
     335           2 :         CPPUNIT_TEST_SUITE( check );
     336           1 :         CPPUNIT_TEST( check_001 );
     337           1 :         CPPUNIT_TEST( check_002 );
     338           2 :         CPPUNIT_TEST_SUITE_END( );
     339             :     }; // class check
     340             : 
     341             : 
     342             : // -----------------------------------------------------------------------------
     343           1 : CPPUNIT_TEST_SUITE_REGISTRATION(osl_Condition::ctors);
     344           1 : CPPUNIT_TEST_SUITE_REGISTRATION(osl_Condition::set);
     345           1 : CPPUNIT_TEST_SUITE_REGISTRATION(osl_Condition::reset);
     346           1 : CPPUNIT_TEST_SUITE_REGISTRATION(osl_Condition::wait);
     347           1 : CPPUNIT_TEST_SUITE_REGISTRATION(osl_Condition::check);
     348             : // -----------------------------------------------------------------------------
     349             : 
     350             : } // namespace osl_Condition
     351             : 
     352             : 
     353             : // -----------------------------------------------------------------------------
     354             : 
     355           4 : CPPUNIT_PLUGIN_IMPLEMENT();
     356             : 
     357             : 
     358             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10