Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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
#ifndef MANTID_KERNEL_NEARESTNEIGHBOURSTEST_H_
#define MANTID_KERNEL_NEARESTNEIGHBOURSTEST_H_
#include <cxxtest/TestSuite.h>
#include "MantidKernel/NearestNeighbours.h"
using Mantid::Kernel::NearestNeighbours;
using namespace Eigen;
class NearestNeighboursTest : 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 NearestNeighboursTest *createSuite() {
return new NearestNeighboursTest();
}
static void destroySuite(NearestNeighboursTest *suite) { delete suite; }
NearestNeighboursTest() {}
void test_construct() {
std::vector<Array3d> pts1 = { Array3d(1, 1, 1), Array3d(2, 2, 2) };
TS_ASSERT_THROWS_NOTHING( NearestNeighbours<> nn(pts1) );
std::vector<Array2d> pts2 = { Array2d(1, 1), Array2d(2, 2) };
TS_ASSERT_THROWS_NOTHING( NearestNeighbours<2> nn(pts2) );
}
void test_find_nearest() {
std::vector<Eigen::Array3d> pts = { Array3d(1, 1, 1), Array3d(2, 2, 2) };
NearestNeighbours<> nn(pts);
auto results = nn.findNearest(Array3d(1, 1, 0.9));
TS_ASSERT_EQUALS(results.size(), 1)
Eigen::Array3d pos = std::get<0>(results[0]);
auto index = std::get<1>(results[0]);
auto dist = std::get<2>(results[0]);
TS_ASSERT_EQUALS(pos[0], 1)
TS_ASSERT_EQUALS(pos[1], 1)
TS_ASSERT_EQUALS(pos[2], 1)
TS_ASSERT_EQUALS(index, 0)
TS_ASSERT_DELTA(dist, 0, 0.01)
}
void test_find_nearest_2() {
std::vector<Eigen::Array2d> pts = { Array2d(1, 1), Array2d(2, 2), Array2d(2, 3) };
NearestNeighbours<2> nn(pts);
auto results = nn.findNearest(Array2d(1, 0.9), 2);
TS_ASSERT_EQUALS(results.size(), 2)
Eigen::Array2d pos = std::get<0>(results[0]);
auto index = std::get<1>(results[0]);
auto dist = std::get<2>(results[0]);
TS_ASSERT_EQUALS(pos[0], 1)
TS_ASSERT_EQUALS(pos[1], 1)
TS_ASSERT_EQUALS(index, 0)
TS_ASSERT_DELTA(dist, 0, 0.01)
pos = std::get<0>(results[1]);
index = std::get<1>(results[1]);
dist = std::get<2>(results[1]);
TS_ASSERT_EQUALS(pos[0], 2)
TS_ASSERT_EQUALS(pos[1], 2)
TS_ASSERT_EQUALS(index, 1)
TS_ASSERT_DELTA(dist, 2.21, 0.01)
}
};
#endif