OpenWalnut  1.5.0dev
WCondition_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 WCONDITION_TEST_H
26 #define WCONDITION_TEST_H
27 
28 #include <iostream>
29 
30 #include <boost/thread.hpp>
31 #include <cxxtest/TestSuite.h>
32 
33 #include "../WCondition.h"
34 
35 /**
36  * Helper class.
37  */
39 {
40 public:
41  /**
42  * Flag set to true when thread starts
43  */
44  bool flag;
45 
46  /**
47  * The condition to be used for signalling.
48  */
50 
51  /**
52  * Thread main method.
53  */
54  void threadMain()
55  {
56  flag = true;
57 
58  // let the test's thread reach its "wait" call first
59  boost::this_thread::sleep( boost::posix_time::seconds( 1 ) );
60  c->notify();
61  };
62 };
63 
64 /**
65  * Test WCondition
66  */
67 class WConditionTest : public CxxTest::TestSuite
68 {
69 public:
70  /**
71  * An instantiation should never throw an exception, as well as tear down.
72  */
73  void testInstantiation( void )
74  {
75  WCondition* c = 0;
76 
77  TS_ASSERT_THROWS_NOTHING( c = new WCondition() );
78  TS_ASSERT_THROWS_NOTHING( delete c );
79  }
80 
81  /**
82  * Test whether notification is working.
83  */
85  {
86  WCondition c;
88  t.flag = false;
89  t.c = &c;
90 
91  // Start a thread
92  boost::thread thread = boost::thread( boost::bind( &CallableHelperCl::threadMain, &t ) );
93 
94  // Wait for condition
95  c.wait();
96 
97  TS_ASSERT( t.flag );
98  }
99 };
100 
101 #endif // WCONDITION_TEST_H
102 
Helper class.
bool flag
Flag set to true when thread starts.
WCondition * c
The condition to be used for signalling.
void threadMain()
Thread main method.
Test WCondition.
void testInstantiation(void)
An instantiation should never throw an exception, as well as tear down.
void testWaitNotify()
Test whether notification is working.
Class to encapsulate boost::condition_variable_any.
Definition: WCondition.h:42
virtual void notify()
Notifies all waiting threads.
Definition: WCondition.cpp:44
virtual void wait() const
Wait for the condition.
Definition: WCondition.cpp:37