Skip to content

Commit 5ec050f

Browse files
committed
tests for ndarray input and exceptions
1 parent a68803a commit 5ec050f

File tree

6 files changed

+115
-102
lines changed

6 files changed

+115
-102
lines changed

.vscode/settings.json

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,18 @@
88
"python.unitTest.pyTestEnabled": true,
99
"python.formatting.provider": "black",
1010
"python.linting.pylamaEnabled": true,
11-
"python.linting.pylintEnabled": false
11+
"python.linting.pylintEnabled": false,
12+
"files.associations": {
13+
".flaskenv": "dotenv",
14+
"array": "cpp",
15+
"deque": "cpp",
16+
"forward_list": "cpp",
17+
"list": "cpp",
18+
"unordered_map": "cpp",
19+
"unordered_set": "cpp",
20+
"vector": "cpp",
21+
"initializer_list": "cpp",
22+
"string_view": "cpp",
23+
"valarray": "cpp"
24+
}
1225
}

Pipfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pybind11 = "*"
1313
[dev-packages]
1414
pylama = "*"
1515
pytest = "*"
16-
pylint = "*"
16+
numpy = "*"
1717

1818
[requires]
1919
python_version = "3.6"

Pipfile.lock

Lines changed: 35 additions & 94 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

setup.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,6 @@ class BuildExt(build_ext):
8080
'-DWINDOWS',
8181
],
8282
'unix': [
83-
'-O3',
84-
'-DNDEBUG',
8583
'-DUNIX',
8684
],
8785
}

src/main.cpp

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,43 @@
1+
#include <exception>
2+
#include <numeric>
3+
#include <vector>
4+
#include <pybind11/numpy.h>
15
#include <pybind11/pybind11.h>
6+
#include <pybind11/stl.h>
27

3-
int add(int i, int j) {
8+
namespace py = pybind11;
9+
10+
void runtime_error()
11+
{
12+
throw std::runtime_error("C++ failure");
13+
}
14+
15+
void range_error()
16+
{
17+
throw std::range_error("C++ range error");
18+
}
19+
20+
int add(int i, int j)
21+
{
422
return i + j;
523
}
624

7-
namespace py = pybind11;
25+
double ndarray_sum(py::array_t<double> ndarray)
26+
{
27+
// double ndarray_sum(std::vector<double> &ndarray) {
28+
// return std::accumulate(ndarray.begin(), ndarray.end(), 0.0);
29+
auto buf1 = ndarray.request();
30+
double sum = 0;
31+
int n = buf1.size;
32+
for (int i = 0; i < n; i++)
33+
{
34+
sum += ((double *)buf1.ptr)[i];
35+
}
36+
return sum;
37+
}
838

9-
PYBIND11_MODULE(python_example, m) {
39+
PYBIND11_MODULE(python_example, m)
40+
{
1041
m.doc() = R"pbdoc(
1142
Pybind11 example plugin
1243
-----------------------
@@ -32,6 +63,16 @@ PYBIND11_MODULE(python_example, m) {
3263
Some other explanation about the subtract function.
3364
)pbdoc");
3465

66+
m.def("ndarray_sum", &ndarray_sum, R"pbdoc(
67+
Return the sum of a numpy ndarray.
68+
69+
Tests functions that accept a numpy array and
70+
autoconvert to std::vector.
71+
)pbdoc");
72+
73+
m.def("runtime_error", &runtime_error);
74+
m.def("range_error", &range_error);
75+
3576
#ifdef VERSION_INFO
3677
m.attr("__version__") = VERSION_INFO;
3778
#else

tests/test_python_example.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,30 @@
1+
import numpy as np
12
import python_example as m
3+
import pytest
4+
25

36
def test_version():
4-
assert m.__version__ == '0.0.1'
7+
assert m.__version__ == "0.0.1"
8+
59

610
def test_add_1_2():
711
assert m.add(1, 2) == 3
812

13+
914
def test_subtract_1_2():
1015
assert m.subtract(1, 2) == -1
16+
17+
18+
def test_ndarray_args():
19+
data = np.array([range(10)])
20+
assert m.ndarray_sum(data) == sum(range(10))
21+
22+
23+
def test_runtime_error():
24+
with pytest.raises(RuntimeError):
25+
m.runtime_error()
26+
27+
28+
def test_range_error():
29+
with pytest.raises(ValueError):
30+
m.range_error()

0 commit comments

Comments
 (0)