CanTest.h 4.57 KB
Newer Older
1
2
3
4
5
6
#ifndef MANTID_GEOMETRY_CANTEST_H_
#define MANTID_GEOMETRY_CANTEST_H_

#include <cxxtest/TestSuite.h>

#include "MantidGeometry/Instrument/Can.h"
7
8
9
10
#include "MantidGeometry/Objects/Rules.h"
#include "MantidGeometry/Surfaces/Sphere.h"

#include <boost/make_shared.hpp>
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

using Mantid::Geometry::Can;

class CanTest : 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 CanTest *createSuite() { return new CanTest(); }
  static void destroySuite(CanTest *suite) { delete suite; }

  // ---------------------------------------------------------------------------
  // Success tests
  // ---------------------------------------------------------------------------
  void test_Default_Constructor_Has_No_Sample_Shape() {
    Can can;
26
27
28
    TS_ASSERT_EQUALS(false, can.hasSampleShape());
    TS_ASSERT_THROWS(can.createSampleShape(Can::ShapeArgs()),
                     std::runtime_error);
29
30
  }

31
  void test_Construction_With_XML_Assumes_XML_For_Can_Itself() {
32
33
    std::string xml = "<cylinder>"
                      "<centre-of-bottom-base x=\"0.0\" y=\"0.0\" z=\"0.0\" />"
34
35
36
                      "<axis x=\"0.0\" y=\"1.0\" z=\"0\" />"
                      "<radius val=\"0.0030\" />"
                      "<height val=\"0.05\" />"
37
38
                      "</cylinder>";
    Can can(xml);
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
    TS_ASSERT_EQUALS(false, can.hasSampleShape());
    TS_ASSERT_EQUALS(xml, can.getShapeXML());
  }

  void test_SetSampleShape_Allows_Creating_Sample_Shape_Object() {
    using Mantid::Geometry::Object_sptr;
    auto can = createTestCan();
    can->setSampleShape("<samplegeometry><sphere id=\"shape\"> "
                        "<radius val=\"1.0\" /> "
                        "</sphere></samplegeometry>");
    Object_sptr sampleShape;
    TS_ASSERT_THROWS_NOTHING(sampleShape =
                                 can->createSampleShape(Can::ShapeArgs()));
    TS_ASSERT(sampleShape->hasValidShape());
    TS_ASSERT_DELTA(1.0, getSphereRadius(*sampleShape), 1e-10);
  }

  void test_CreateSampleShape_Args_Override_Defaults() {
    using Mantid::Geometry::Object_sptr;
    auto can = createTestCan();
    can->setSampleShape("<samplegeometry><sphere id=\"shape\"> "
                        "<radius val=\"1.0\" /> "
                        "</sphere></samplegeometry>");
    Object_sptr sampleShape;
    Can::ShapeArgs args = {{"radius", 0.5}};
    TS_ASSERT_THROWS_NOTHING(sampleShape = can->createSampleShape(args));
    TS_ASSERT(sampleShape->hasValidShape());
    TS_ASSERT_DELTA(0.5, getSphereRadius(*sampleShape), 1e-10);
  }

  void test_CreateSampleShape_Args_Not_Matching_Do_Nothing() {
    using Mantid::Geometry::Object_sptr;
    auto can = createTestCan();
    can->setSampleShape("<samplegeometry><sphere id=\"shape\"> "
                        "<radius val=\"1.0\" /> "
                        "</sphere></samplegeometry>");
    Object_sptr sampleShape;
    Can::ShapeArgs args = {{"height", 0.5}};
    TS_ASSERT_THROWS_NOTHING(sampleShape = can->createSampleShape(args));
    TS_ASSERT(sampleShape->hasValidShape());
    TS_ASSERT_DELTA(1.0, getSphereRadius(*sampleShape), 1e-10);
  }

  // ---------------------------------------------------------------------------
  // Failure tests
  // ---------------------------------------------------------------------------
  void test_SetSampleShape_Throws_If_Top_Tag_Not_SampleGeometry() {
    auto can = createTestCan();
    TS_ASSERT_THROWS(can->setSampleShape("<sphere id=\"shape\"> "
                                         "<radius val=\"1.0\" /> "
                                         "</sphere>"),
                     std::invalid_argument);
  }

private:
  Mantid::Geometry::Can_sptr createTestCan() {
    return boost::make_shared<Can>(
        "<type name=\"usertype\"><cylinder>"
        "<centre-of-bottom-base x=\"0.0\" y=\"0.0\" z=\"0.0\" />"
        "<axis x=\"0.0\" y=\"1.0\" z=\"0\" />"
        "<radius val =\"0.0030\" />"
        "<height val =\"0.05\" />"
        "</cylinder></type>");
  }
  double getSphereRadius(const Mantid::Geometry::Object &shape) {
    using Mantid::Geometry::SurfPoint;
    using Mantid::Geometry::Sphere;
    auto topRule = shape.topRule();
    if (auto surfpoint = dynamic_cast<const SurfPoint *>(topRule)) {
      if (auto sphere = dynamic_cast<Sphere *>(surfpoint->getKey())) {
        return sphere->getRadius();
      } else {
        throw std::runtime_error("Expected Sphere as SurfPoint key.");
      }
    } else {
      throw std::runtime_error("Expected SurfPoint as top rule");
    }
116
117
118
119
  }
};

#endif /* MANTID_GEOMETRY_CANTEST_H_ */