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

Generated by: LCOV version 1.10