OpenWalnut  1.4.0
WProgressCombiner_test.h
00001 //---------------------------------------------------------------------------
00002 //
00003 // Project: OpenWalnut ( http://www.openwalnut.org )
00004 //
00005 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
00006 // For more information see http://www.openwalnut.org/copying
00007 //
00008 // This file is part of OpenWalnut.
00009 //
00010 // OpenWalnut is free software: you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as published by
00012 // the Free Software Foundation, either version 3 of the License, or
00013 // (at your option) any later version.
00014 //
00015 // OpenWalnut is distributed in the hope that it will be useful,
00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 // GNU Lesser General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU Lesser General Public License
00021 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
00022 //
00023 //---------------------------------------------------------------------------
00024 
00025 #ifndef WPROGRESSCOMBINER_TEST_H
00026 #define WPROGRESSCOMBINER_TEST_H
00027 
00028 #include <iostream>
00029 
00030 #include <boost/shared_ptr.hpp>
00031 
00032 #include <cxxtest/TestSuite.h>
00033 
00034 #include "../WProgress.h"
00035 #include "../WProgressCombiner.h"
00036 
00037 /**
00038  * Class testing the functionality of progress combiners.
00039  */
00040 class WProgressCombinerTest : public CxxTest::TestSuite
00041 {
00042 public:
00043    /**
00044     * Test whether WProgress is instantiatable.
00045     */
00046     void testInstantiation()
00047     {
00048         TS_ASSERT_THROWS_NOTHING( WProgressCombiner p( "Test" ) );
00049     }
00050 
00051    /**
00052     * Test whether the combiner ignores manual increments.
00053     */
00054     void testInternalStateIgnoresIncrementAndFinish()
00055     {
00056         WProgressCombiner p( "Test" );
00057 
00058         // try increment
00059         ++++++p;
00060         TS_ASSERT_THROWS_NOTHING( p.update() );
00061         TS_ASSERT( p.getProgress() == 0.0 );
00062 
00063         // should ignore finish()
00064         p.finish();
00065         TS_ASSERT_THROWS_NOTHING( p.update() );
00066         TS_ASSERT( !p.isPending() );
00067     }
00068 
00069     /**
00070      * Test the combiner when some childs got added to it.
00071      */
00072     void testWithChilds()
00073     {
00074         WProgressCombiner p( "Test" );
00075 
00076         // create some children
00077         boost::shared_ptr< WProgress> p1( new WProgress( "TestP1", 11 ) );
00078         boost::shared_ptr< WProgress> p2( new WProgress( "TestP2", 11 ) );
00079         boost::shared_ptr< WProgress> p3( new WProgress( "TestP3" ) );
00080 
00081         // as the first and only child is determined (has a known end point) -> the combiner is determined
00082         TS_ASSERT_THROWS_NOTHING( p.addSubProgress( p1 ) );
00083         p.update();
00084         TS_ASSERT( p.isDetermined() );
00085 
00086         // increment a bit
00087         ++++++++++( *p1 );
00088         p.update(); // updating is needed in every case, as this is used to propagate changes.
00089         // p1 is now at 50% -> the combiner should also be at 50%
00090         TS_ASSERT( p1->getProgress() == 50.0 );
00091         TS_ASSERT( p.getProgress() == 50.0 );
00092 
00093         // add another determined progress
00094         TS_ASSERT_THROWS_NOTHING( p.addSubProgress( p2 ) );
00095         p.update();
00096         TS_ASSERT( p.isDetermined() );
00097         TS_ASSERT( p.getProgress() == 25.0 );           // as p2 is at 0% currently
00098 
00099         // now add an indetermined progress
00100         TS_ASSERT_THROWS_NOTHING( p.addSubProgress( p3 ) );
00101         p.update();
00102         TS_ASSERT( !p3->isDetermined() );
00103         TS_ASSERT( !p.isDetermined() );
00104 
00105         // now finish the progress and test whether to combiner reacts on it.
00106 
00107         // when finishing the indetermined progress the combiner is determined again.
00108         p3->finish();
00109         p.update();
00110         TS_ASSERT( p.isDetermined() );
00111         TS_ASSERT( p.isPending() );
00112 
00113         // finish the other progress
00114         p1->finish();
00115         p2->finish();
00116         p.update();
00117         TS_ASSERT( !p.isPending() );
00118 
00119         // finish the combiner
00120         p.finish();
00121         TS_ASSERT( p.m_children.empty() );
00122     }
00123 };
00124 
00125 #endif  // WPROGRESSCOMBINER_TEST_H
00126