FunctionPropertyTest.h 3.62 KB
Newer Older
1
2
3
4
5
6
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
//     NScD Oak Ridge National Laboratory, European Spallation Source
//     & Institut Laue - Langevin
// SPDX - License - Identifier: GPL - 3.0 +
7
8
9
10
11
12
#ifndef FUNCTIONPROPERTYTEST_H_
#define FUNCTIONPROPERTYTEST_H_

#include <cxxtest/TestSuite.h>

#include "MantidAPI/FunctionFactory.h"
LamarMoore's avatar
LamarMoore committed
13
#include "MantidAPI/FunctionProperty.h"
14
15
16
17
18
19
#include "MantidAPI/ParamFunction.h"
#include <boost/shared_ptr.hpp>

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

20
21
class FunctionPropertyTest_Function : public virtual ParamFunction,
                                      public virtual IFunction {
22
public:
23
24
25
  FunctionPropertyTest_Function() {
    this->declareParameter("A", 1.0);
    this->declareParameter("B", 2.0);
26
  }
27
28
  std::string name() const override { return "FunctionPropertyTest_Function"; }
  void function(const FunctionDomain &, FunctionValues &) const override {}
29
30
};

31
DECLARE_FUNCTION(FunctionPropertyTest_Function)
32

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

42
  void testConstructor() {
43
44
45
    TS_ASSERT_THROWS_NOTHING(FunctionProperty prop("fun"));
  }

46
  void testValue() {
47
    FunctionProperty prop("fun");
48
49
    TS_ASSERT_EQUALS("", prop.value());

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

63
  void testBadValue() {
64
65
    FunctionProperty prop("fun");
    std::string error;
66
67
    TS_ASSERT_THROWS_NOTHING(
        error = prop.setValue("name=FunctionDoesnotExist,A=3"));
68
69
70
71
72
    TS_ASSERT(!error.empty());
    TS_ASSERT_THROWS_NOTHING(error = prop.setValue("ghvjhgvjhgcjh"));
    TS_ASSERT(!error.empty());
  }

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

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

107
108
109
110
private:
};

#endif /*FUNCTIONPROPERTYTEST_H_*/