OpenWalnut  1.2.5
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
WProgressCombiner_test.h
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #ifndef WPROGRESSCOMBINER_TEST_H
26 #define WPROGRESSCOMBINER_TEST_H
27 
28 #include <iostream>
29 
30 #include <boost/shared_ptr.hpp>
31 
32 #include <cxxtest/TestSuite.h>
33 
34 #include "../WProgress.h"
35 #include "../WProgressCombiner.h"
36 
37 /**
38  * Class testing the functionality of progress combiners.
39  */
40 class WProgressCombinerTest : public CxxTest::TestSuite
41 {
42 public:
43 
44  /**
45  * Test whether WProgress is instantiatable.
46  */
48  {
49  TS_ASSERT_THROWS_NOTHING( WProgressCombiner p( "Test" ) );
50  }
51 
52  /**
53  * Test whether the combiner ignores manual increments.
54  */
56  {
57  WProgressCombiner p( "Test" );
58 
59  // try increment
60  ++++++p;
61  TS_ASSERT_THROWS_NOTHING( p.update() );
62  TS_ASSERT( p.getProgress() == 0.0 );
63 
64  // should ignore finish()
65  p.finish();
66  TS_ASSERT_THROWS_NOTHING( p.update() );
67  TS_ASSERT( !p.isPending() );
68  }
69 
70  /**
71  * Test the combiner when some childs got added to it.
72  */
74  {
75  WProgressCombiner p( "Test" );
76 
77  // create some children
78  boost::shared_ptr< WProgress> p1 = boost::shared_ptr< WProgress>( new WProgress( "TestP1", 11 ) );
79  boost::shared_ptr< WProgress> p2 = boost::shared_ptr< WProgress>( new WProgress( "TestP2", 11 ) );
80  boost::shared_ptr< WProgress> p3 = boost::shared_ptr< WProgress>( new WProgress( "TestP3" ) );
81 
82  // as the first and only child is determined (has a known end point) -> the combiner is determined
83  TS_ASSERT_THROWS_NOTHING( p.addSubProgress( p1 ) );
84  p.update();
85  TS_ASSERT( p.isDetermined() );
86 
87  // increment a bit
88  ++++++++++( *p1 );
89  p.update(); // updating is needed in every case, as this is used to propagate changes.
90  // p1 is now at 50% -> the combiner should also be at 50%
91  TS_ASSERT( p1->getProgress() == 50.0 );
92  TS_ASSERT( p.getProgress() == 50.0 );
93 
94  // add another determined progress
95  TS_ASSERT_THROWS_NOTHING( p.addSubProgress( p2 ) );
96  p.update();
97  TS_ASSERT( p.isDetermined() );
98  TS_ASSERT( p.getProgress() == 25.0 ); // as p2 is at 0% currently
99 
100  // now add an indetermined progress
101  TS_ASSERT_THROWS_NOTHING( p.addSubProgress( p3 ) );
102  p.update();
103  TS_ASSERT( !p3->isDetermined() );
104  TS_ASSERT( !p.isDetermined() );
105 
106  // now finish the progress and test whether to combiner reacts on it.
107 
108  // when finishing the indetermined progress the combiner is determined again.
109  p3->finish();
110  p.update();
111  TS_ASSERT( p.isDetermined() );
112  TS_ASSERT( p.isPending() );
113 
114  // finish the other progress
115  p1->finish();
116  p2->finish();
117  p.update();
118  TS_ASSERT( !p.isPending() );
119 
120  // finish the combiner
121  p.finish();
122  TS_ASSERT( p.m_children.empty() );
123  }
124 };
125 
126 #endif // WPROGRESSCOMBINER_TEST_H
127