RulesTest.h 4.78 KB
Newer Older
1
2
3
4
5
#ifndef MANTID_RULESTEST__
#define MANTID_RULESTEST__
#include <cxxtest/TestSuite.h>
#include <cmath>
#include <vector>
6
#include "MantidKernel/Logger.h"
7
8
#include "MantidKernel/System.h"
#include <cfloat>
9
#include "MantidKernel/V3D.h"
10
#include "MantidKernel/make_unique.h"
Nick Draper's avatar
re #843    
Nick Draper committed
11
#include "MantidGeometry/Surfaces/Quadratic.h"
12
#include "MantidGeometry/Objects/CSGObject.h"
13
14
15
16
17
18
#include "MantidGeometry/Objects/Rules.h"
#include "MantidGeometry/Surfaces/Plane.h"
#include "MantidGeometry/Surfaces/Sphere.h"
#include "MantidGeometry/Surfaces/Cylinder.h"
#include "MantidGeometry/Surfaces/Cone.h"

19
20
#include "boost/make_shared.hpp"

21
22
23
using namespace Mantid;
using namespace Geometry;

24
class RulesTest : public CxxTest::TestSuite {
25
public:
26
27
28
29
30
  void testMakeCNFCopy() {}

  void testMakeFullDNF() {}

  void testMakeCNF() {
31
    auto tree = createAMixedTree();
32
33
34
35
36
37
    TS_ASSERT_EQUALS(tree->display(), "(10 11) : (12 10)");
    // TS_ASSERT_EQUALS(Rule::makeCNFcopy(tree),1);
    // TS_ASSERT_EQUALS(tree->display(),"(10 11) : (12 10)");
  }

  void testRemoveComplementary() {
38
    auto tree = createAUnionTree();
39
    TS_ASSERT_EQUALS(tree->display(), "10 : 10 : 12 : 11");
40
    TS_ASSERT_EQUALS(tree->removeComplementary(tree), 1);
41
42
43
44
45
    //		TS_ASSERT_EQUALS(tree->display(),"10 : 12 : 11"); //Problem: The
    // comments don't match to the functionaility it is supposed to do
  }

  void testRemoveItem() { // Problem: in removing the item of the tree that has
46
47
48
49
50
51
    // more than one instances,possibly double deletion
    auto tree = createAUnionTree();
    TS_ASSERT_EQUALS(tree->removeItem(tree, 11), 1);
    //		TS_ASSERT_EQUALS(tree->removeItem(tree,10),2);
    TS_ASSERT_EQUALS(tree->removeItem(tree, 11), 0);
    TS_ASSERT_EQUALS(tree->removeItem(tree, 12), 1);
52
53
54
  }

  void testCommonType() {
55
    auto uTree = createAUnionTree();
56
    TS_ASSERT_EQUALS(uTree->commonType(), -1);
57
    auto iTree = createAIntersectionTree();
58
    TS_ASSERT_EQUALS(iTree->commonType(), 1);
59
    auto mTree = createAMixedTree();
60
61
62
63
    TS_ASSERT_EQUALS(mTree->commonType(), 0);
  }

  void testSubstituteSurf() {
64
    auto uTree = createAUnionTree();
65
66
    TS_ASSERT_EQUALS(uTree->substituteSurf(11, 13, boost::make_shared<Cone>()),
                     1);
67
    TS_ASSERT_EQUALS(uTree->display(), "10 : 10 : 12 : 13");
68
69
70
    TS_ASSERT_EQUALS(
        uTree->substituteSurf(10, 14, boost::make_shared<Sphere>()),
        2); // its suppose to return 2
71
72
73
74
75
    TS_ASSERT_EQUALS(uTree->display(), "14 : 14 : 12 : 13");
  }

  void testEliminate() {}

76
private:
77
78
79
80
  std::unique_ptr<Rule> createAUnionTree() { // A:B:C:A
                                             // A Node
    // SurfPoint *A, *B, *C;
    auto A = Mantid::Kernel::make_unique<SurfPoint>();
81
    A->setKey(boost::make_shared<Plane>());
82
    A->setKeyN(10);
83
    auto B = Mantid::Kernel::make_unique<SurfPoint>();
84
    B->setKey(boost::make_shared<Sphere>());
85
    B->setKeyN(11);
86
    auto C = Mantid::Kernel::make_unique<SurfPoint>();
87
    C->setKey(boost::make_shared<Cylinder>());
88
89
    C->setKeyN(12);

90
91
92
93
    auto Left = Mantid::Kernel::make_unique<Union>(A->clone(), A->clone());
    auto Right = Mantid::Kernel::make_unique<Union>(std::move(C), std::move(B));
    return Mantid::Kernel::make_unique<Union>(std::move(Left),
                                              std::move(Right));
94
95
  }

96
  std::unique_ptr<Rule> createAIntersectionTree() { // A B C A
97
    // A Node
98
    auto A = Mantid::Kernel::make_unique<SurfPoint>();
99
    A->setKey(boost::make_shared<Plane>());
100
    A->setKeyN(10);
101
    auto B = Mantid::Kernel::make_unique<SurfPoint>();
102
    B->setKey(boost::make_shared<Sphere>());
103
    B->setKeyN(11);
104
    auto C = Mantid::Kernel::make_unique<SurfPoint>();
105
    C->setKey(boost::make_shared<Cylinder>());
106
107
    C->setKeyN(12);

108
    auto Left = Mantid::Kernel::make_unique<Intersection>();
109

110
    Left->setLeaves(A->clone(), std::move(B));
111

112
113
114
115
116
    auto Right = Mantid::Kernel::make_unique<Intersection>();
    Right->setLeaves(std::move(C), std::move(A));

    return Mantid::Kernel::make_unique<Intersection>(std::move(Left),
                                                     std::move(Right));
117
118
  }

119
120
  std::unique_ptr<Rule> createAMixedTree() { // A B : C A
    auto A = Mantid::Kernel::make_unique<SurfPoint>();
121
    A->setKey(boost::make_shared<Plane>());
122
    A->setKeyN(10);
123
    auto B = Mantid::Kernel::make_unique<SurfPoint>();
124
    B->setKey(boost::make_shared<Sphere>());
125
    B->setKeyN(11);
126
    auto C = Mantid::Kernel::make_unique<SurfPoint>();
127
    C->setKey(boost::make_shared<Cylinder>());
128
129
    C->setKeyN(12);

130
131
132
133
    auto Root = Mantid::Kernel::make_unique<Union>();

    auto Left = Mantid::Kernel::make_unique<Intersection>();
    Left->setLeaves(A->clone(), std::move(B));
134

135
136
    auto Right = Mantid::Kernel::make_unique<Intersection>();
    Right->setLeaves(std::move(C), std::move(A));
137

138
139
    Root->setLeaves(std::move(Left), std::move(Right));
    return std::move(Root);
140
  }
141
142
143
};

#endif