1
//---------------------------------------------------------------------------
3
// Project: OpenWalnut ( http://www.openwalnut.org )
5
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6
// For more information see http://www.openwalnut.org/copying
8
// This file is part of OpenWalnut.
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.
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.
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/>.
23
//---------------------------------------------------------------------------
25
#ifndef WCONDITIONSET_TEST_H
26
#define WCONDITIONSET_TEST_H
30
#include <boost/thread.hpp>
31
#include <cxxtest/TestSuite.h>
33
#include "../WConditionSet.h"
34
#include "../WCondition.h"
43
* Flag set to true when thread starts
48
* The condition to be used for signalling.
59
// let the test's thread reach its "wait" call first
60
boost::this_thread::sleep( boost::posix_time::seconds( 1 ) );
68
class WConditionSetTest : public CxxTest::TestSuite
72
* An instantiation should never throw an exception, as well as tear down.
74
void testInstantiation( void )
76
WConditionSet* c = NULL;
78
TS_ASSERT_THROWS_NOTHING( c = new WConditionSet() );
79
TS_ASSERT_THROWS_NOTHING( delete c );
83
* Tests add and remove methods. Also check double remove/add.
85
void testAddRemove( void )
87
WConditionSet* cs = new WConditionSet();
89
// create some conditions
90
boost::shared_ptr< WCondition > c1( new WCondition() );
91
boost::shared_ptr< WCondition > c2( new WCondition() );
92
boost::shared_ptr< WCondition > c3( new WCondition() );
95
TS_ASSERT_THROWS_NOTHING( cs->add( c1 ) );
96
// adding it a second time should not cause any exception
97
TS_ASSERT_THROWS_NOTHING( cs->add( c1 ) );
98
TS_ASSERT_THROWS_NOTHING( cs->add( c2 ) );
100
// the size should now be 2
101
TS_ASSERT( cs->m_conditionSet.size() == 2 );
103
// try to remove a condition which is NOT inside the condition set
104
TS_ASSERT_THROWS_NOTHING( cs->remove( c3 ) );
105
TS_ASSERT( cs->m_conditionSet.size() == 2 );
107
// remove a condition inside the set
108
TS_ASSERT_THROWS_NOTHING( cs->remove( c1 ) );
109
TS_ASSERT( cs->m_conditionSet.size() == 1 );
111
// remove a condition inside the set
112
TS_ASSERT_THROWS_NOTHING( cs->remove( c2 ) );
113
TS_ASSERT( cs->m_conditionSet.size() == 0 );
119
* Tests whether the condition set really reacts on fired conditions.
121
void testWait( void )
123
WConditionSet* cs = new WConditionSet();
125
// create some conditions
126
boost::shared_ptr< WCondition > c1( new WCondition() );
127
boost::shared_ptr< WCondition > c2( new WCondition() );
129
// disable resetable feature
130
cs->setResetable( false, false );
134
// create a thread which fires a condition in the set for us
140
boost::thread thread = boost::thread( boost::bind( &Callable::threadMain, &t ) );
142
// wait for condition
145
// ugly but this is the only way to test it.
150
TS_ASSERT( !cs->m_fired );
152
// ensure that a condition which has been removed does NOT fire the condition set
155
TS_ASSERT( !cs->m_fired );
157
// the other one should fire the set
159
TS_ASSERT( cs->m_fired );
165
* Tests the resetable feature.
167
void testWaitResetable( void )
169
WConditionSet* cs = new WConditionSet();
171
// create some conditions
172
boost::shared_ptr< WCondition > c1( new WCondition() );
173
boost::shared_ptr< WCondition > c2( new WCondition() );
175
// disable resetable feature
176
cs->setResetable( true, true );
180
// notify a condition
183
// after the notification, m_fired should be true
184
TS_ASSERT( cs->m_fired );
186
// wait should return instantly
187
cs->wait(); // how to test it? it just freezes the test if it does not work ;-) also some kind of error notification :-)
189
// as the condition set has auto-reset enabled: m_fired should be false again
190
TS_ASSERT( !cs->m_fired );
192
// if resetable without auto-reset:
193
cs->setResetable( true, false );
198
// m_fired should be true
199
TS_ASSERT( cs->m_fired );
201
// wait should return instantly
202
cs->wait(); // how to test it? it just freezes the test if it does not work ;-) also some kind of error notification :-)
204
// m_fired should stay true
205
TS_ASSERT( cs->m_fired );
211
* Ensures reset() never throws something.
213
void testResetOnNotResetable( void )
215
WConditionSet* cs = new WConditionSet();
217
cs->setResetable( false, false );
219
// just ensure it does not throw something
220
TS_ASSERT_THROWS_NOTHING( cs->reset() );
226
#endif // WCONDITIONSET_TEST_H