forked from npshub/mantid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PropertyNexusTest.h
93 lines (79 loc) · 3.92 KB
/
PropertyNexusTest.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
// 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 "MantidKernel/DateAndTime.h"
#include "MantidKernel/PropertyNexus.h"
#include "MantidKernel/PropertyWithValue.h"
#include "MantidKernel/System.h"
#include "MantidKernel/TimeSeriesProperty.h"
#include "MantidKernel/Timer.h"
#include "MantidTestHelpers/NexusTestHelper.h"
#include <cxxtest/TestSuite.h>
using namespace Mantid;
using namespace Mantid::API;
using namespace Mantid::Kernel;
using Mantid::Types::Core::DateAndTime;
class PropertyNexusTest : 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 PropertyNexusTest *createSuite() { return new PropertyNexusTest(); }
static void destroySuite(PropertyNexusTest *suite) { delete suite; }
/** Compare a property */
template <typename T> void check_prop(Property *prop, T *other) {
T *p = dynamic_cast<T *>(prop);
TSM_ASSERT("Loaded property was not of the expected type", p);
if (!p)
return;
TS_ASSERT_EQUALS(p->value(), other->value());
}
void test_saving_then_loading() {
NexusTestHelper th(true);
th.createFile("PropertyNexusTest.nxs");
PropertyWithValue<int> pi("int_val", 123);
PropertyWithValue<uint32_t> pu("uint_val", 123);
PropertyWithValue<double> pd("double_val", 456.78);
PropertyWithValue<float> pf("float_val", float(987.56));
PropertyWithValue<std::string> ps("string_val", "supercallifragalistic");
PropertyWithValue<std::vector<double>> pvd("vector_double_val", std::vector<double>(2, 1.4));
pi.saveProperty(th.file.get());
pu.saveProperty(th.file.get());
pd.saveProperty(th.file.get());
pf.saveProperty(th.file.get());
ps.saveProperty(th.file.get());
pvd.saveProperty(th.file.get());
TimeSeriesProperty<int> tspi("int_series");
tspi.addValue(DateAndTime("2011-01-01T00:00:01"), 1234);
tspi.addValue(DateAndTime("2011-01-01T00:01:02"), 4567);
TimeSeriesProperty<double> tspd("double_series");
tspd.addValue(DateAndTime("2011-01-01T00:00:01"), 1234.5);
tspd.addValue(DateAndTime("2011-01-01T00:01:02"), 4567.8);
TimeSeriesProperty<bool> tspb("bool_series");
tspb.addValue(DateAndTime("2011-01-01T00:00:01"), true);
tspb.addValue(DateAndTime("2011-01-01T00:01:02"), false);
TimeSeriesProperty<std::string> tsps("string_series");
tsps.addValue(DateAndTime("2011-01-01T00:00:01"), "help me i");
tsps.addValue(DateAndTime("2011-01-01T00:01:02"), "am stuck in a NXS file");
tspi.saveProperty(th.file.get());
tspd.saveProperty(th.file.get());
tspb.saveProperty(th.file.get());
tsps.saveProperty(th.file.get());
// ---- Now re-load and compare to the original ones
// ----------------------------
th.reopenFile();
check_prop(PropertyNexus::loadProperty(th.file.get(), "int_val").get(), &pi);
check_prop(PropertyNexus::loadProperty(th.file.get(), "uint_val").get(), &pu);
check_prop(PropertyNexus::loadProperty(th.file.get(), "double_val").get(), &pd);
check_prop(PropertyNexus::loadProperty(th.file.get(), "float_val").get(), &pf);
check_prop(PropertyNexus::loadProperty(th.file.get(), "string_val").get(), &ps);
check_prop(PropertyNexus::loadProperty(th.file.get(), "vector_double_val").get(), &pvd);
check_prop(PropertyNexus::loadProperty(th.file.get(), "int_series").get(), &tspi);
check_prop(PropertyNexus::loadProperty(th.file.get(), "double_series").get(), &tspd);
check_prop(PropertyNexus::loadProperty(th.file.get(), "bool_series").get(), &tspb);
check_prop(PropertyNexus::loadProperty(th.file.get(), "string_series").get(), &tsps);
}
};