forked from AmbaPant/mantid
-
Notifications
You must be signed in to change notification settings - Fork 1
/
PySequenceToVectorTest.h
64 lines (55 loc) · 2.39 KB
/
PySequenceToVectorTest.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
// 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 "MantidPythonInterface/core/Converters/PySequenceToVector.h"
#include <boost/python/dict.hpp>
#include <boost/python/list.hpp>
#include <boost/python/ssize_t.hpp>
#include <cxxtest/TestSuite.h>
using namespace Mantid::PythonInterface::Converters;
class PySequenceToVectorTest : public CxxTest::TestSuite {
public:
static PySequenceToVectorTest *createSuite() { return new PySequenceToVectorTest(); }
static void destroySuite(PySequenceToVectorTest *suite) { delete suite; }
private:
using PySequenceToVectorDouble = PySequenceToVector<double>;
public:
void tearDown() override { PyErr_Clear(); }
void test_construction_succeeds_with_a_valid_sequence_type() {
boost::python::list testList;
TS_ASSERT_THROWS_NOTHING(PySequenceToVectorDouble converter(testList));
}
void test_that_a_python_list_of_all_matching_types_is_converted_correctly() {
boost::python::list testlist = createHomogeneousPythonList();
const size_t ntestvals = boost::python::len(testlist);
std::vector<double> cvector;
TS_ASSERT_THROWS_NOTHING(cvector = PySequenceToVectorDouble(testlist)());
// Test values
TS_ASSERT_EQUALS(cvector.size(), ntestvals);
// Check values
for (size_t i = 0; i < ntestvals; ++i) {
double testval = boost::python::extract<double>(testlist[i])();
TS_ASSERT_EQUALS(cvector[i], testval);
}
}
void test_that_trying_to_convert_a_list_of_incompatible_types_throws_error_already_set() {
// Double->int is not generally safe so should not be allowed
boost::python::list testlist = createHomogeneousPythonList();
using PySequenceToVectorInt = PySequenceToVector<int>;
std::vector<int> cvector;
TS_ASSERT_THROWS(cvector = PySequenceToVectorInt(testlist)(), const boost::python::error_already_set &);
}
/// Creates a python list where all of the types are the same
static boost::python::list createHomogeneousPythonList() {
boost::python::list testlist;
const size_t ntestvals(20);
for (size_t i = 0; i < ntestvals; ++i) {
testlist.append((double)i + 10.0);
}
return testlist;
}
};