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 WPROPERTYVARIABLE_TEST_H
26
#define WPROPERTYVARIABLE_TEST_H
30
#include <cxxtest/TestSuite.h>
32
#include "../WPropertyVariable.h"
33
#include "../constraints/WPropertyConstraintMin.h"
34
#include "../constraints/WPropertyConstraintMax.h"
36
#include "../exceptions/WPropertyNotUnique.h"
37
#include "../exceptions/WPropertyUnknown.h"
38
#include "../exceptions/WPropertyNameMalformed.h"
41
* Test WPropertyVariable
43
class WPropertyVariableTest : public CxxTest::TestSuite
48
* A temporary holder for some value.
50
bool m_testTemporary1;
53
* A temporary holder for some value.
55
bool m_testTemporary2;
58
* Helper function which simply sets the value above to true. It is used to test some conditions here.
62
m_testTemporary1 = true;
66
* Helper function which simply sets the value above to true. It is used to test some conditions here.
70
m_testTemporary2 = true;
74
* Test instantiation, also test name and description and type (from WPropertyBase)
76
void testInstantiation( void )
78
boost::shared_ptr< WPropertyVariable< bool > > p;
79
TS_ASSERT_THROWS_NOTHING( p = boost::shared_ptr< WPropertyVariable< bool > >( new WPropertyVariable< bool >( "hey", "you", false ) ) );
82
TS_ASSERT( p->getName() == "hey" );
83
TS_ASSERT( p->getDescription() == "you" );
84
TS_ASSERT( p->getType() == PV_BOOL );
86
TS_ASSERT_THROWS_NOTHING( p.reset() );
90
* Tests that only properties with proper names can be created
92
void testNameConvention( void )
94
WException::disableBacktrace(); // in tests, turn of backtrace globally
96
boost::shared_ptr< WPropertyVariable< bool > > p;
97
TS_ASSERT_THROWS( p = boost::shared_ptr< WPropertyVariable< bool > >( new WPropertyVariable< bool >( "hey/you", "you", false ) ),
98
WPropertyNameMalformed );
102
* Tests the cloning functionality.
106
WException::disableBacktrace(); // in tests, turn of backtrace globally
108
/////////////////////////
109
// Create an original
111
// create an int property
112
boost::shared_ptr< WPropertyVariable< int > > p =
113
boost::shared_ptr< WPropertyVariable< int > >( new WPropertyVariable< int >( "hey", "you", false ) );
114
// add a min/max prop
115
WPropertyVariable< int >::PropertyConstraintMin cmin = p->setMin( 0 );
116
WPropertyVariable< int >::PropertyConstraintMax cmax = p->setMax( 9 );
119
/////////////////////////
122
boost::shared_ptr< WPropertyVariable< int > > clone = p->clone()->toPropInt();
124
// some rudimentary tests (from WPropertyBase)
126
TS_ASSERT( clone->getType() == PV_INT );
127
TS_ASSERT( clone->getName() == p->getName() );
128
TS_ASSERT( clone->getDescription() == p->getDescription() );
129
TS_ASSERT( clone->getPurpose() == p->getPurpose() );
132
TS_ASSERT( p->get() == clone->get() );
134
// different conditions?
135
TS_ASSERT( p->getContraintsChangedCondition() != clone->getContraintsChangedCondition() );
136
TS_ASSERT( p->getUpdateCondition() != clone->getUpdateCondition() ); // this is from WPropertyBase
138
// cloned constraints?
139
TS_ASSERT( p->getMin() != clone->getMin() ); // the constraints of course need to be cloned too
140
TS_ASSERT( p->getMax() != clone->getMax() ); // the constraints of course need to be cloned too
142
TS_ASSERT( p->getMin()->getMin() == clone->getMin()->getMin() ); // but their values need to be the same. This somehow tests the clone
143
// feature of WPropertyConstrainMin and Max
144
TS_ASSERT( p->getMax()->getMax() == clone->getMax()->getMax() ); // but their values need to be the same. This somehow tests the clone
145
// feature of WPropertyConstrainMin and Max
147
// check independence of both update conditions
148
p->getUpdateCondition()->subscribeSignal( boost::bind( &WPropertyVariableTest::setTemporary1, this ) );
149
clone->getUpdateCondition()->subscribeSignal( boost::bind( &WPropertyVariableTest::setTemporary2, this ) );
151
// set the value of the clone -> fire condition of clone but not of original
152
m_testTemporary1 = false;
153
m_testTemporary2 = false;
155
TS_ASSERT( !m_testTemporary1 );
156
TS_ASSERT( m_testTemporary2 );
158
// and test vice versa
159
m_testTemporary1 = false;
160
m_testTemporary2 = false;
162
TS_ASSERT( m_testTemporary1 );
163
TS_ASSERT( !m_testTemporary2 );
165
// do the same for constraints
166
m_testTemporary1 = false;
167
m_testTemporary2 = false;
168
clone->removeConstraint( PC_MIN );
169
TS_ASSERT( !m_testTemporary1 );
170
TS_ASSERT( m_testTemporary2 );
173
m_testTemporary1 = false;
174
m_testTemporary2 = false;
175
p->removeConstraint( PC_MIN );
176
TS_ASSERT( m_testTemporary1 );
177
TS_ASSERT( !m_testTemporary2 );
181
* Test min/max functionality, including tests for set(), accept() and ensureValidity.
183
void testMinMaxWithSetAndAccept()
185
WException::disableBacktrace(); // in tests, turn of backtrace globally
187
// create an int property
188
boost::shared_ptr< WPropertyVariable< int > > p =
189
boost::shared_ptr< WPropertyVariable< int > >( new WPropertyVariable< int >( "hey", "you", false ) );
191
// by default there should be no min/max property set. Only IF the property was created using a WProperties::addProperty.
192
WPropertyVariable< int >::PropertyConstraintMin cmin = p->getMin();
193
WPropertyVariable< int >::PropertyConstraintMax cmax = p->getMax();
197
// does set() and accept work if no constraints are there?
198
TS_ASSERT( p->set( 123 ) );
199
TS_ASSERT( p->get() == 123 );
200
TS_ASSERT( p->accept( 12345 ) );
203
cmin = p->setMin( 10 );
204
cmax = p->setMax( 15 );
208
// compare that getMin/max returns the correct ones
209
TS_ASSERT( cmin == p->getMin() );
210
TS_ASSERT( cmax == p->getMax() );
212
// try to set a valid value
213
TS_ASSERT( p->set( 10 ) );
214
TS_ASSERT( p->get() == 10 );
216
// try to set an invalid value
217
TS_ASSERT( !p->set( 9 ) );
218
TS_ASSERT( p->get() == 10 );
219
TS_ASSERT( !p->set( 16 ) );
220
TS_ASSERT( p->get() == 10 );
222
// add another min value. Is the first one removed?
225
p->m_constraints->getReadTicket()->get().size();
227
// try to set a valid value, which was invalid previously
228
TS_ASSERT( p->set( 9 ) );
229
TS_ASSERT( p->get() == 9 );
230
TS_ASSERT( p->set( 16 ) );
231
TS_ASSERT( p->get() == 16 );
233
// finally, test ensureValidity
234
// this function helps to restore a property to a valid state after a constraint change
236
// currently, the state of p is valid. So ensureValidity should do nothing
237
TS_ASSERT( p->ensureValidity( 10 ) );
238
TS_ASSERT( p->get() == 16 );
240
// change the min constraint so that 16 gets invalid
241
TS_ASSERT( p->isValid() );
243
TS_ASSERT( !p->isValid() );
244
TS_ASSERT( p->get() == 16 ); // setting a new constraint should NOT modify the current value
246
// use ensureValidity
247
TS_ASSERT( p->ensureValidity( 18 ) );
248
TS_ASSERT( p->get() == 18 );
249
TS_ASSERT( p->isValid() );
251
// what happens if the ensureValidity parameter itself is invalid? It should return false
253
TS_ASSERT( !p->ensureValidity( 16 ) ); // 16 is invalid since minimum is 19
254
TS_ASSERT( !p->isValid() ); // the value should stay invalid
255
TS_ASSERT( p->get() == 18 );
259
* Tests constraint management. Especially add,replace,remove,count,getFirst.
261
void testConstraintManagement( void )
263
WException::disableBacktrace(); // in tests, turn of backtrace globally
265
// create an int property
266
boost::shared_ptr< WPropertyVariable< int > > p =
267
boost::shared_ptr< WPropertyVariable< int > >( new WPropertyVariable< int >( "hey", "you", false ) );
269
// register a condition callback
270
p->getUpdateCondition()->subscribeSignal( boost::bind( &WPropertyVariableTest::setTemporary1, this ) );
272
////////////////////////////////////
276
m_testTemporary1 = false;
277
WPropertyVariable< int >::PropertyConstraintMin cmin =
278
boost::shared_ptr< WPropertyConstraintMin< int > >( new WPropertyConstraintMin< int >( 10 ) );
279
p->addConstraint( cmin );
280
TS_ASSERT( p->m_constraints->getReadTicket()->get().size() == 1 );
281
TS_ASSERT( m_testTemporary1 ); // the update condition has to be fired on constraint updates
283
////////////////////////////////////
287
m_testTemporary1 = false;
288
TS_ASSERT( p->countConstraint( PC_MIN ) == 1 );
289
TS_ASSERT( p->countConstraint( PC_MAX ) == 0 );
291
// get first constraint should return the first constraint of a specified type
292
TS_ASSERT( cmin == p->getFirstConstraint( PC_MIN ) );
293
TS_ASSERT( !p->getFirstConstraint( PC_MAX ) ); // there is no max constraint
294
TS_ASSERT( !m_testTemporary1 ); // these operations should not fire the condition
296
////////////////////////////////////
299
// replace a constraint
300
m_testTemporary1 = false;
301
WPropertyVariable< int >::PropertyConstraintMax cmax =
302
boost::shared_ptr< WPropertyConstraintMax< int > >( new WPropertyConstraintMax< int >( 15 ) );
304
// replace non existent type
305
TS_ASSERT_THROWS_NOTHING( p->replaceConstraint( cmax, PC_MAX ) ); // since there is no max constraint, replace acts like addConstraint
306
TS_ASSERT( m_testTemporary1 );
308
// replace existent type ( note: there is now a min and a max constraint )
309
m_testTemporary1 = false;
310
WPropertyVariable< int >::PropertyConstraintMax cmax2 =
311
boost::shared_ptr< WPropertyConstraintMax< int > >( new WPropertyConstraintMax< int >( 20 ) );
312
p->replaceConstraint( cmax2, PC_MAX );
313
TS_ASSERT( m_testTemporary1 );
314
TS_ASSERT( cmax2 == p->getFirstConstraint( PC_MAX ) );
316
////////////////////////////////////
319
// removeConstraints should not fire the condition if nothing is removed
320
m_testTemporary1 = false;
321
p->removeConstraint( PC_NOTEMPTY );
322
TS_ASSERT( !m_testTemporary1 );
324
// remove max constraint
325
m_testTemporary1 = false;
326
TS_ASSERT( p->countConstraint( PC_MAX ) == 1 );
327
p->removeConstraint( PC_MAX );
328
TS_ASSERT( p->countConstraint( PC_MAX ) == 0 );
329
TS_ASSERT( m_testTemporary1 ); // should have fired
331
// remove min constraint with pointer
332
m_testTemporary1 = false;
333
TS_ASSERT( p->countConstraint( PC_MIN ) == 1 );
334
p->removeConstraint( cmin );
335
TS_ASSERT( p->countConstraint( PC_MIN ) == 0 );
336
TS_ASSERT( m_testTemporary1 ); // should have fired
340
#endif // WPROPERTYVARIABLE_TEST_H