FunctionPropertyTest.h 3.17 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
#ifndef FUNCTIONPROPERTYTEST_H_
#define FUNCTIONPROPERTYTEST_H_

#include <cxxtest/TestSuite.h>

#include "MantidAPI/FunctionProperty.h"
#include "MantidAPI/FunctionFactory.h"
#include "MantidAPI/ParamFunction.h"
#include <boost/shared_ptr.hpp>

using namespace Mantid::Kernel;
using namespace Mantid::API;

14
class FunctionPropertyTest_Function: public virtual ParamFunction, public virtual IFunction
15
16
17
18
19
20
21
22
{
public:
  FunctionPropertyTest_Function()
  {
    this->declareParameter("A",1.0);
    this->declareParameter("B",2.0);
  }
  virtual std::string name()const {return "FunctionPropertyTest_Function";}
23
  virtual void function(const FunctionDomain&,FunctionValues&)const {}
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
};

DECLARE_FUNCTION(FunctionPropertyTest_Function);

class FunctionPropertyTest : public CxxTest::TestSuite
{
public:
  // This pair of boilerplate methods prevent the suite being created statically
  // This means the constructor isn't called when running other tests
  static FunctionPropertyTest *createSuite() { return new FunctionPropertyTest (); }
  static void destroySuite( FunctionPropertyTest *suite ) { delete suite; }

  void testConstructor()
  {
    TS_ASSERT_THROWS_NOTHING(FunctionProperty prop("fun"));
  }

  void testValue()
  {
    FunctionProperty prop("fun");
44
45
    TS_ASSERT_EQUALS("", prop.value());

46
47
48
    std::string error;
    TS_ASSERT_THROWS_NOTHING(error = prop.setValue("name=FunctionPropertyTest_Function,A=3"));
    TS_ASSERT(error.empty());
49
    boost::shared_ptr<IFunction> fun_p = prop;
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
    TS_ASSERT_EQUALS(fun_p->asString(),"name=FunctionPropertyTest_Function,A=3,B=2");
    TS_ASSERT_EQUALS(fun_p->getParameter("A"), 3.0);
    TS_ASSERT_EQUALS(fun_p->getParameter("B"), 2.0);
    TS_ASSERT_EQUALS(prop.value(),"name=FunctionPropertyTest_Function,A=3,B=2");
  }

  void testBadValue()
  {  
    FunctionProperty prop("fun");
    std::string error;
    TS_ASSERT_THROWS_NOTHING(error = prop.setValue("name=FunctionDoesnotExist,A=3"));
    TS_ASSERT(!error.empty());
    TS_ASSERT_THROWS_NOTHING(error = prop.setValue("ghvjhgvjhgcjh"));
    TS_ASSERT(!error.empty());
  }

  void testSetValue()
  {
    FunctionProperty prop("fun");
    std::string error;
70
    boost::shared_ptr<IFunction> fun_p(FunctionFactory::Instance().createInitialized("name=FunctionPropertyTest_Function,A=3"));
71
72
    TS_ASSERT(fun_p);
    prop = fun_p;
73
    boost::shared_ptr<IFunction> fun1_p = prop;
74
75
76
77
78
79
    TS_ASSERT(fun1_p);
    TS_ASSERT_EQUALS(fun_p,fun1_p);
    TS_ASSERT_EQUALS(fun1_p->asString(),"name=FunctionPropertyTest_Function,A=3,B=2");
    TS_ASSERT_EQUALS(fun1_p->getParameter("A"), 3.0);
    TS_ASSERT_EQUALS(fun1_p->getParameter("B"), 2.0);
  }
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96

  void testSharedPointer()
  {
    FunctionProperty prop("fun");
    std::string error;
    boost::shared_ptr<FunctionPropertyTest_Function> fun_p(new FunctionPropertyTest_Function);
    TS_ASSERT(fun_p);
    fun_p->setParameter("A",3);
    prop = fun_p;
    boost::shared_ptr<IFunction> fun1_p = prop;
    TS_ASSERT(fun1_p);
    TS_ASSERT_EQUALS(fun_p,fun1_p);
    TS_ASSERT_EQUALS(fun1_p->asString(),"name=FunctionPropertyTest_Function,A=3,B=2");
    TS_ASSERT_EQUALS(fun1_p->getParameter("A"), 3.0);
    TS_ASSERT_EQUALS(fun1_p->getParameter("B"), 2.0);
  }

97
98
99
100
private:
};

#endif /*FUNCTIONPROPERTYTEST_H_*/