forked from npshub/mantid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AddableTest.h
94 lines (82 loc) · 3.21 KB
/
AddableTest.h
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// 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 & CSNS, Institute of High Energy Physics, CAS
// SPDX - License - Identifier: GPL - 3.0 +
#pragma once
#include <cxxtest/TestSuite.h>
#include "MantidHistogramData/Addable.h"
#include "MantidHistogramData/FixedLengthVector.h"
#include "MantidHistogramData/HistogramX.h"
#include "MantidHistogramData/Iterable.h"
#include "MantidHistogramData/VectorOf.h"
using Mantid::HistogramData::HistogramX;
using Mantid::HistogramData::detail::Addable;
using Mantid::HistogramData::detail::FixedLengthVector;
using Mantid::HistogramData::detail::Iterable;
using Mantid::HistogramData::detail::VectorOf;
class AddableTester : public VectorOf<AddableTester, HistogramX>,
public Iterable<AddableTester>,
public Addable<AddableTester> {
using VectorOf<AddableTester, HistogramX>::VectorOf;
};
// Does Addable also work with FixedLengthVector instead of VectorOf?
struct AddableTester2 : public FixedLengthVector<AddableTester2>, public Addable<AddableTester2> {
using FixedLengthVector<AddableTester2>::FixedLengthVector;
};
class AddableTest : 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 AddableTest *createSuite() { return new AddableTest(); }
static void destroySuite(AddableTest *suite) { delete suite; }
void test_plus_equals() {
AddableTester lhs{0.1, 0.2};
const AddableTester rhs{0.01, 0.02};
lhs += rhs;
TS_ASSERT_EQUALS(lhs.size(), 2);
TS_ASSERT_DELTA(lhs[0], 0.11, 1e-14);
TS_ASSERT_DELTA(lhs[1], 0.22, 1e-14);
}
void test_minus_equals() {
AddableTester lhs{0.1, 0.2};
const AddableTester rhs{0.01, 0.02};
lhs -= rhs;
TS_ASSERT_EQUALS(lhs.size(), 2);
TS_ASSERT_DELTA(lhs[0], 0.09, 1e-14);
TS_ASSERT_DELTA(lhs[1], 0.18, 1e-14);
}
void test_plus() {
const AddableTester rhs1{0.1, 0.2};
const AddableTester rhs2{0.01, 0.02};
const AddableTester lhs(rhs1 + rhs2);
TS_ASSERT_EQUALS(lhs.size(), 2);
TS_ASSERT_DELTA(lhs[0], 0.11, 1e-14);
TS_ASSERT_DELTA(lhs[1], 0.22, 1e-14);
}
void test_minus() {
const AddableTester rhs1{0.1, 0.2};
const AddableTester rhs2{0.01, 0.02};
const AddableTester lhs(rhs1 - rhs2);
TS_ASSERT_EQUALS(lhs.size(), 2);
TS_ASSERT_DELTA(lhs[0], 0.09, 1e-14);
TS_ASSERT_DELTA(lhs[1], 0.18, 1e-14);
}
void test_length_mismatch() {
AddableTester rhs1{1, 2};
const AddableTester rhs2{1, 2, 3};
TS_ASSERT_THROWS(rhs1 + rhs2, const std::runtime_error &);
TS_ASSERT_THROWS(rhs1 - rhs2, const std::runtime_error &);
TS_ASSERT_THROWS(rhs1 += rhs2, const std::runtime_error &);
TS_ASSERT_THROWS(rhs1 -= rhs2, const std::runtime_error &);
}
void test_with_FixedLengthVector() {
AddableTester2 lhs{0.1, 0.2};
const AddableTester2 rhs{0.01, 0.02};
lhs += rhs;
TS_ASSERT_EQUALS(lhs.size(), 2);
TS_ASSERT_DELTA(lhs[0], 0.11, 1e-14);
TS_ASSERT_DELTA(lhs[1], 0.22, 1e-14);
}
};