FunctionPropertyTest.h 3.34 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
15
class FunctionPropertyTest_Function : public virtual ParamFunction,
                                      public virtual IFunction {
16
public:
17
18
19
  FunctionPropertyTest_Function() {
    this->declareParameter("A", 1.0);
    this->declareParameter("B", 2.0);
20
  }
21
22
  virtual std::string name() const { return "FunctionPropertyTest_Function"; }
  virtual void function(const FunctionDomain &, FunctionValues &) const {}
23
24
};

25
DECLARE_FUNCTION(FunctionPropertyTest_Function)
26

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

36
  void testConstructor() {
37
38
39
    TS_ASSERT_THROWS_NOTHING(FunctionProperty prop("fun"));
  }

40
  void testValue() {
41
    FunctionProperty prop("fun");
42
43
    TS_ASSERT_EQUALS("", prop.value());

44
    std::string error;
45
46
    TS_ASSERT_THROWS_NOTHING(
        error = prop.setValue("name=FunctionPropertyTest_Function,A=3"));
47
    TS_ASSERT(error.empty());
48
    boost::shared_ptr<IFunction> fun_p = prop;
49
50
    TS_ASSERT_EQUALS(fun_p->asString(),
                     "name=FunctionPropertyTest_Function,A=3,B=2");
51
52
    TS_ASSERT_EQUALS(fun_p->getParameter("A"), 3.0);
    TS_ASSERT_EQUALS(fun_p->getParameter("B"), 2.0);
53
54
    TS_ASSERT_EQUALS(prop.value(),
                     "name=FunctionPropertyTest_Function,A=3,B=2");
55
56
  }

57
  void testBadValue() {
58
59
    FunctionProperty prop("fun");
    std::string error;
60
61
    TS_ASSERT_THROWS_NOTHING(
        error = prop.setValue("name=FunctionDoesnotExist,A=3"));
62
63
64
65
66
    TS_ASSERT(!error.empty());
    TS_ASSERT_THROWS_NOTHING(error = prop.setValue("ghvjhgvjhgcjh"));
    TS_ASSERT(!error.empty());
  }

67
  void testSetValue() {
68
69
    FunctionProperty prop("fun");
    std::string error;
70
71
72
    boost::shared_ptr<IFunction> fun_p(
        FunctionFactory::Instance().createInitialized(
            "name=FunctionPropertyTest_Function,A=3"));
73
74
    TS_ASSERT(fun_p);
    prop = fun_p;
75
    boost::shared_ptr<IFunction> fun1_p = prop;
76
    TS_ASSERT(fun1_p);
77
78
79
    TS_ASSERT_EQUALS(fun_p, fun1_p);
    TS_ASSERT_EQUALS(fun1_p->asString(),
                     "name=FunctionPropertyTest_Function,A=3,B=2");
80
81
82
    TS_ASSERT_EQUALS(fun1_p->getParameter("A"), 3.0);
    TS_ASSERT_EQUALS(fun1_p->getParameter("B"), 2.0);
  }
83

84
  void testSharedPointer() {
85
86
    FunctionProperty prop("fun");
    std::string error;
87
88
    boost::shared_ptr<FunctionPropertyTest_Function> fun_p(
        new FunctionPropertyTest_Function);
89
    TS_ASSERT(fun_p);
90
    fun_p->setParameter("A", 3);
91
92
93
    prop = fun_p;
    boost::shared_ptr<IFunction> fun1_p = prop;
    TS_ASSERT(fun1_p);
94
95
96
    TS_ASSERT_EQUALS(fun_p, fun1_p);
    TS_ASSERT_EQUALS(fun1_p->asString(),
                     "name=FunctionPropertyTest_Function,A=3,B=2");
97
98
99
100
    TS_ASSERT_EQUALS(fun1_p->getParameter("A"), 3.0);
    TS_ASSERT_EQUALS(fun1_p->getParameter("B"), 2.0);
  }

101
102
103
104
private:
};

#endif /*FUNCTIONPROPERTYTEST_H_*/