Newer
Older
#ifndef ARRAYPROPERTYTEST_H_
#define ARRAYPROPERTYTEST_H_
#include <cxxtest/TestSuite.h>
#include "MantidKernel/ArrayProperty.h"
using namespace Mantid::Kernel;
class ArrayPropertyTest : public CxxTest::TestSuite
{
public:
void setUp()
{
iProp = new ArrayProperty<int>("intProp");
dProp = new ArrayProperty<double>("doubleProp");
sProp = new ArrayProperty<std::string>("stringProp");
}
void tearDown()
{
delete iProp;
delete dProp;
delete sProp;
}
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
void testConstructor()
{
TS_ASSERT( ! iProp->name().compare("intProp") )
TS_ASSERT( ! iProp->documentation().compare("") )
TS_ASSERT( typeid( std::vector<int> ) == *iProp->type_info() )
TS_ASSERT( iProp->isDefault() )
TS_ASSERT( iProp->operator()().empty() )
TS_ASSERT( ! dProp->name().compare("doubleProp") )
TS_ASSERT( ! dProp->documentation().compare("") )
TS_ASSERT( typeid( std::vector<double> ) == *dProp->type_info() )
TS_ASSERT( dProp->isDefault() )
TS_ASSERT( dProp->operator()().empty() )
TS_ASSERT( ! sProp->name().compare("stringProp") )
TS_ASSERT( ! sProp->documentation().compare("") )
TS_ASSERT( typeid( std::vector<std::string> ) == *sProp->type_info() )
TS_ASSERT( sProp->isDefault() )
TS_ASSERT( sProp->operator()().empty() )
std::vector<int> i(5,2);
ArrayProperty<int> ip("ip",i);
TS_ASSERT_EQUALS( ip.operator()().size(), 5 )
TS_ASSERT_EQUALS( ip.operator()()[3], 2 )
std::vector<double> d(4,6.66);
ArrayProperty<double> dp("dp",d);
TS_ASSERT_EQUALS( dp.operator()().size(), 4 )
TS_ASSERT_EQUALS( dp.operator()()[1], 6.66 )
std::vector<std::string> s(3,"yyy");
ArrayProperty<std::string> sp("sp",s);
TS_ASSERT_EQUALS( sp.operator()().size(), 3 )
TS_ASSERT( ! sp.operator()()[2].compare("yyy") )
}
void testSize()
{
TS_ASSERT_EQUALS(0, iProp->size());
TS_ASSERT_EQUALS(0, dProp->size());
TS_ASSERT_EQUALS(0, sProp->size());
// Make something bigger and test that.
Property* a = new ArrayProperty<int>("int_property", "1, 2, 3");
TS_ASSERT_EQUALS(3, a->size());
delete a;
// Test vector of vector.
std::vector<std::vector<int> > input;
input.push_back(std::vector<int>(10)); // Make it 10 elements long, but should only be the size of the parent vector that is counted.
Property* b = new ArrayProperty<std::vector<int> >("vec_property", input);
TS_ASSERT_EQUALS(1, b->size());
delete b;
}
void testConstructorByString()
{
ArrayProperty<int> i("i","1,2,3");
TS_ASSERT_EQUALS( i.operator()()[0], 1 )
TS_ASSERT_EQUALS( i.operator()()[1], 2 )
TS_ASSERT_EQUALS( i.operator()()[2], 3 )
Peterson, Peter
committed
ArrayProperty<int> i2("i", "-1-1");
TS_ASSERT_EQUALS( i2.operator()()[0], -1);
TS_ASSERT_EQUALS( i2.operator()()[1], 0);
TS_ASSERT_EQUALS( i2.operator()()[2], 1);
ArrayProperty<int> i4("i", "-1:1");
TS_ASSERT_EQUALS( i4.operator()()[0], -1);
TS_ASSERT_EQUALS( i4.operator()()[1], 0);
TS_ASSERT_EQUALS( i4.operator()()[2], 1);
ArrayProperty<int> i5("i", "-3--1");
TS_ASSERT_EQUALS( i5.operator()()[0], -3);
TS_ASSERT_EQUALS( i5.operator()()[1], -2);
TS_ASSERT_EQUALS( i5.operator()()[2], -1);
ArrayProperty<int> i7("i", "-3:-1");
TS_ASSERT_EQUALS( i7.operator()()[0], -3);
TS_ASSERT_EQUALS( i7.operator()()[1], -2);
TS_ASSERT_EQUALS( i7.operator()()[2], -1);
ArrayProperty<unsigned int> i3("i", "0:2,5");
TS_ASSERT_EQUALS( i3.operator()()[0], 0);
TS_ASSERT_EQUALS( i3.operator()()[1], 1);
TS_ASSERT_EQUALS( i3.operator()()[2], 2);
Peterson, Peter
committed
TS_ASSERT_EQUALS( i3.operator()()[3], 5);
ArrayProperty<unsigned int> i6("i", "5,0-2,5");
TS_ASSERT_EQUALS( i6.operator()()[0], 5);
TS_ASSERT_EQUALS( i6.operator()()[1], 0);
TS_ASSERT_EQUALS( i6.operator()()[2], 1);
TS_ASSERT_EQUALS( i6.operator()()[3], 2);
TS_ASSERT_EQUALS( i6.operator()()[4], 5);
ArrayProperty<double> d("d","7.77,8.88,9.99");
TS_ASSERT_EQUALS( d.operator()()[0], 7.77 )
TS_ASSERT_EQUALS( d.operator()()[1], 8.88 )
TS_ASSERT_EQUALS( d.operator()()[2], 9.99 )
ArrayProperty<double> d2("d","-0.15,0.0,0.15");
TS_ASSERT_EQUALS( d2.operator()()[0], -0.15 )
TS_ASSERT_EQUALS( d2.operator()()[1], 0.0 )
TS_ASSERT_EQUALS( d2.operator()()[2], 0.15 )
//Now we change the values that are set in the indices of d2
//by using a string
TS_ASSERT_EQUALS( d2.operator()()[0], 0.3 )
TS_ASSERT_EQUALS( d2.operator()()[1], 0.1)
TS_ASSERT_EQUALS( d2.operator()()[2], -0.2 )
ArrayProperty<std::string> s("d","a,b,c");
TS_ASSERT( ! s.operator()()[0].compare("a") )
TS_ASSERT( ! s.operator()()[1].compare("b") )
TS_ASSERT( ! s.operator()()[2].compare("c") )
Peterson, Peter
committed
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
TS_ASSERT_THROWS( ArrayProperty<int> ii("ii","aa,bb"), std::invalid_argument )
TS_ASSERT_THROWS( ArrayProperty<int> ii("ii","5.5,6.6"), std::invalid_argument )
TS_ASSERT_THROWS( ArrayProperty<double> dd("dd","aa,bb"), std::invalid_argument )
}
void testCopyConstructor()
{
ArrayProperty<int> i = *iProp;
TS_ASSERT( ! i.name().compare("intProp") )
TS_ASSERT( ! i.documentation().compare("") )
TS_ASSERT( typeid( std::vector<int> ) == *i.type_info() )
TS_ASSERT( i.isDefault() )
TS_ASSERT( i.operator()().empty() )
ArrayProperty<double> d = *dProp;
TS_ASSERT( ! d.name().compare("doubleProp") )
TS_ASSERT( ! d.documentation().compare("") )
TS_ASSERT( typeid( std::vector<double> ) == *d.type_info() )
TS_ASSERT( d.isDefault() )
TS_ASSERT( d.operator()().empty() )
ArrayProperty<std::string> s = *sProp;
TS_ASSERT( ! s.name().compare("stringProp") )
TS_ASSERT( ! s.documentation().compare("") )
TS_ASSERT( typeid( std::vector<std::string> ) == *s.type_info() )
TS_ASSERT( s.isDefault() )
TS_ASSERT( s.operator()().empty() )
}
void testValue()
{
std::vector<int> i(3,3);
ArrayProperty<int> ip("ip",i);
TS_ASSERT( ! ip.value().compare("3,3,3") )
std::vector<double> d(4,1.23);
ArrayProperty<double> dp("dp",d);
TS_ASSERT( ! dp.value().compare("1.23,1.23,1.23,1.23") )
std::vector<std::string> s(2,"yyy");
ArrayProperty<std::string> sp("sp",s);
TS_ASSERT( ! sp.value().compare("yyy,yyy") )
}
Steve Williams
committed
void testSetValueAndIsDefault()
Steve Williams
committed
std::string couldnt = "Could not set property ", cant = ". Can not convert \"";
TS_ASSERT_EQUALS( iProp->setValue("1.1,2,2"),
couldnt + iProp->name() + cant + "1.1,2,2\" to " + iProp->type() )
TS_ASSERT( iProp->operator()().empty() )
Steve Williams
committed
TS_ASSERT( iProp->isDefault() )
TS_ASSERT_EQUALS( iProp->setValue("aaa,bbb"),
couldnt + iProp->name() + cant + "aaa,bbb\" to " + iProp->type() )
TS_ASSERT( iProp->operator()().empty() )
Steve Williams
committed
TS_ASSERT( iProp->isDefault() )
TS_ASSERT_EQUALS( iProp->setValue("1,2,3,4"), "" )
TS_ASSERT_EQUALS( iProp->operator()().size(), 4 )
Peterson, Peter
committed
for ( std::size_t i=0; i < 4; ++i )
{
TS_ASSERT_EQUALS( iProp->operator()()[i], i+1 )
}
Steve Williams
committed
TS_ASSERT( !iProp->isDefault() )
TS_ASSERT_EQUALS( iProp->setValue(""), "" )
TS_ASSERT( iProp->operator()().empty() )
Steve Williams
committed
TS_ASSERT( iProp->isDefault() )
Steve Williams
committed
TS_ASSERT_EQUALS( dProp->setValue("aaa,bbb"),
couldnt + dProp->name() + cant + "aaa,bbb\" to " + dProp->type() )
TS_ASSERT( dProp->operator()().empty() )
Steve Williams
committed
TS_ASSERT( dProp->isDefault() )
TS_ASSERT_EQUALS( dProp->setValue("1,2"), "" )
TS_ASSERT_EQUALS( dProp->operator()()[1], 2 )
Steve Williams
committed
TS_ASSERT( !dProp->isDefault() )
TS_ASSERT_EQUALS( dProp->setValue("1.11,2.22,3.33,4.44"), "" )
TS_ASSERT_EQUALS( dProp->operator()()[0], 1.11 )
Steve Williams
committed
TS_ASSERT( !dProp->isDefault() )
TS_ASSERT_EQUALS( dProp->setValue(""), "" )
TS_ASSERT( dProp->operator()().empty() )
Steve Williams
committed
TS_ASSERT( dProp->isDefault() )
Steve Williams
committed
TS_ASSERT_EQUALS( sProp->setValue("This,is,a,test"), "" )
TS_ASSERT_EQUALS( sProp->operator()()[2], "a" )
Steve Williams
committed
TS_ASSERT( !sProp->isDefault() )
TS_ASSERT_EQUALS( sProp->setValue(""), "" )
TS_ASSERT( sProp->operator()().empty() )
Steve Williams
committed
TS_ASSERT( sProp->isDefault() )
}
void testAssignmentOperator()
{
ArrayProperty<int> i("i");
Steve Williams
committed
TS_ASSERT( i.isDefault() )
std::vector<int> ii(3,4);
TS_ASSERT_EQUALS( i = ii, ii )
TS_ASSERT_EQUALS( i.operator()()[1], 4 )
Steve Williams
committed
TS_ASSERT( !i.isDefault() )
Steve Williams
committed
TS_ASSERT( d.isDefault() )
std::vector<double> dd(5,9.99);
TS_ASSERT_EQUALS( d = dd, dd )
TS_ASSERT_EQUALS( d.operator()()[3], 9.99 )
Steve Williams
committed
TS_ASSERT( !d.isDefault() )
ArrayProperty<std::string> s("s");
Steve Williams
committed
TS_ASSERT( s.isDefault() )
std::vector<std::string> ss(2,"zzz");
TS_ASSERT_EQUALS( s = ss, ss )
Steve Williams
committed
TS_ASSERT_EQUALS( s.operator()()[0], "zzz" )
TS_ASSERT( !s.isDefault() )
}
void testOperatorBrackets()
{
TS_ASSERT( iProp->operator()().empty() )
TS_ASSERT( dProp->operator()().empty() )
TS_ASSERT( sProp->operator()().empty() )
}
void testOperatorNothing()
{
std::vector<int> i = *iProp;
TS_ASSERT( i.empty() )
std::vector<double> d(3,8.8);
*dProp = d;
std::vector<double> dd = *dProp;
Peterson, Peter
committed
for(std::size_t i = 0; i<3; ++i)
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
{
TS_ASSERT_EQUALS( dProp->operator()()[i], 8.8 )
}
std::vector<std::string> s = *sProp;
TS_ASSERT( s.empty() )
}
void testCasting()
{
TS_ASSERT_DIFFERS( dynamic_cast<PropertyWithValue<std::vector<int> >*>(iProp),
static_cast<PropertyWithValue<std::vector<int> >*>(0) )
TS_ASSERT_DIFFERS( dynamic_cast<PropertyWithValue<std::vector<double> >*>(dProp),
static_cast<PropertyWithValue<std::vector<double> >*>(0) )
TS_ASSERT_DIFFERS( dynamic_cast<PropertyWithValue<std::vector<std::string> >*>(sProp),
static_cast<PropertyWithValue<std::vector<std::string> >*>(0) )
TS_ASSERT_DIFFERS( dynamic_cast<Property*>(iProp), static_cast<Property*>(0) )
TS_ASSERT_DIFFERS( dynamic_cast<Property*>(dProp), static_cast<Property*>(0) )
TS_ASSERT_DIFFERS( dynamic_cast<Property*>(sProp), static_cast<Property*>(0) )
}
private:
ArrayProperty<int> *iProp;
ArrayProperty<double> *dProp;
ArrayProperty<std::string> *sProp;
};
#endif /*ARRAYPROPERTYTEST_H_*/