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

Generated by: LCOV version 1.11