forked from npshub/mantid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MultipliableTest.h
94 lines (82 loc) · 3.34 KB
/
MultipliableTest.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/FixedLengthVector.h"
#include "MantidHistogramData/HistogramX.h"
#include "MantidHistogramData/Iterable.h"
#include "MantidHistogramData/Multipliable.h"
#include "MantidHistogramData/VectorOf.h"
using Mantid::HistogramData::HistogramX;
using Mantid::HistogramData::detail::FixedLengthVector;
using Mantid::HistogramData::detail::Iterable;
using Mantid::HistogramData::detail::Multipliable;
using Mantid::HistogramData::detail::VectorOf;
class MultipliableTester : public VectorOf<MultipliableTester, HistogramX>,
public Iterable<MultipliableTester>,
public Multipliable<MultipliableTester> {
using VectorOf<MultipliableTester, HistogramX>::VectorOf;
};
// Does Multipliable also work with FixedLengthVector instead of VectorOf?
struct MultipliableTester2 : public FixedLengthVector<MultipliableTester2>, public Multipliable<MultipliableTester2> {
using FixedLengthVector<MultipliableTester2>::FixedLengthVector;
};
class MultipliableTest : 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 MultipliableTest *createSuite() { return new MultipliableTest(); }
static void destroySuite(MultipliableTest *suite) { delete suite; }
void test_times_equals() {
MultipliableTester lhs{0.1, 0.2};
const MultipliableTester rhs{2, 4};
lhs *= rhs;
TS_ASSERT_EQUALS(lhs.size(), 2);
TS_ASSERT_DELTA(lhs[0], 0.2, 1e-14);
TS_ASSERT_DELTA(lhs[1], 0.8, 1e-14);
}
void test_divide_equals() {
MultipliableTester lhs{0.1, 0.2};
const MultipliableTester rhs{2, 4};
lhs /= rhs;
TS_ASSERT_EQUALS(lhs.size(), 2);
TS_ASSERT_DELTA(lhs[0], 0.05, 1e-14);
TS_ASSERT_DELTA(lhs[1], 0.05, 1e-14);
}
void test_times() {
const MultipliableTester rhs1{0.1, 0.2};
const MultipliableTester rhs2{2, 4};
const MultipliableTester lhs(rhs1 * rhs2);
TS_ASSERT_EQUALS(lhs.size(), 2);
TS_ASSERT_DELTA(lhs[0], 0.2, 1e-14);
TS_ASSERT_DELTA(lhs[1], 0.8, 1e-14);
}
void test_divide() {
const MultipliableTester rhs1{0.1, 0.2};
const MultipliableTester rhs2{2, 4};
const MultipliableTester lhs(rhs1 / rhs2);
TS_ASSERT_EQUALS(lhs.size(), 2);
TS_ASSERT_DELTA(lhs[0], 0.05, 1e-14);
TS_ASSERT_DELTA(lhs[1], 0.05, 1e-14);
}
void test_length_mismatch() {
MultipliableTester rhs1{1, 2};
const MultipliableTester 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() {
MultipliableTester2 lhs{0.1, 0.2};
const MultipliableTester2 rhs{2, 4};
lhs *= rhs;
TS_ASSERT_EQUALS(lhs.size(), 2);
TS_ASSERT_DELTA(lhs[0], 0.2, 1e-14);
TS_ASSERT_DELTA(lhs[1], 0.8, 1e-14);
}
};