OpenWalnut  1.4.0
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  * Test whether WProgress is instantiatable.
45  */
47  {
48  TS_ASSERT_THROWS_NOTHING( WProgressCombiner p( "Test" ) );
49  }
50 
51  /**
52  * Test whether the combiner ignores manual increments.
53  */
55  {
56  WProgressCombiner p( "Test" );
57 
58  // try increment
59  ++++++p;
60  TS_ASSERT_THROWS_NOTHING( p.update() );
61  TS_ASSERT( p.getProgress() == 0.0 );
62 
63  // should ignore finish()
64  p.finish();
65  TS_ASSERT_THROWS_NOTHING( p.update() );
66  TS_ASSERT( !p.isPending() );
67  }
68 
69  /**
70  * Test the combiner when some childs got added to it.
71  */
73  {
74  WProgressCombiner p( "Test" );
75 
76  // create some children
77  boost::shared_ptr< WProgress> p1( new WProgress( "TestP1", 11 ) );
78  boost::shared_ptr< WProgress> p2( new WProgress( "TestP2", 11 ) );
79  boost::shared_ptr< WProgress> p3( new WProgress( "TestP3" ) );
80 
81  // as the first and only child is determined (has a known end point) -> the combiner is determined
82  TS_ASSERT_THROWS_NOTHING( p.addSubProgress( p1 ) );
83  p.update();
84  TS_ASSERT( p.isDetermined() );
85 
86  // increment a bit
87  ++++++++++( *p1 );
88  p.update(); // updating is needed in every case, as this is used to propagate changes.
89  // p1 is now at 50% -> the combiner should also be at 50%
90  TS_ASSERT( p1->getProgress() == 50.0 );
91  TS_ASSERT( p.getProgress() == 50.0 );
92 
93  // add another determined progress
94  TS_ASSERT_THROWS_NOTHING( p.addSubProgress( p2 ) );
95  p.update();
96  TS_ASSERT( p.isDetermined() );
97  TS_ASSERT( p.getProgress() == 25.0 ); // as p2 is at 0% currently
98 
99  // now add an indetermined progress
100  TS_ASSERT_THROWS_NOTHING( p.addSubProgress( p3 ) );
101  p.update();
102  TS_ASSERT( !p3->isDetermined() );
103  TS_ASSERT( !p.isDetermined() );
104 
105  // now finish the progress and test whether to combiner reacts on it.
106 
107  // when finishing the indetermined progress the combiner is determined again.
108  p3->finish();
109  p.update();
110  TS_ASSERT( p.isDetermined() );
111  TS_ASSERT( p.isPending() );
112 
113  // finish the other progress
114  p1->finish();
115  p2->finish();
116  p.update();
117  TS_ASSERT( !p.isPending() );
118 
119  // finish the combiner
120  p.finish();
121  TS_ASSERT( p.m_children.empty() );
122  }
123 };
124 
125 #endif // WPROGRESSCOMBINER_TEST_H
126