Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
gaoqiong
pybind11
Commits
e19980cc
Commit
e19980cc
authored
Jul 24, 2016
by
Ivan Smirnov
Browse files
Add tests for new array/array_t ctors
parent
98ba98c0
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
42 additions
and
1 deletion
+42
-1
example/example-numpy-dtypes.cpp
example/example-numpy-dtypes.cpp
+35
-0
example/example-numpy-dtypes.py
example/example-numpy-dtypes.py
+7
-1
No files found.
example/example-numpy-dtypes.cpp
View file @
e19980cc
...
...
@@ -166,6 +166,40 @@ void print_dtypes() {
std
::
cout
<<
(
std
::
string
)
py
::
dtype
::
of
<
StringStruct
>
().
str
()
<<
std
::
endl
;
}
py
::
array_t
<
int32_t
,
0
>
test_array_ctors
(
int
i
)
{
using
arr_t
=
py
::
array_t
<
int32_t
,
0
>
;
std
::
vector
<
int32_t
>
data
{
1
,
2
,
3
,
4
,
5
,
6
};
std
::
vector
<
size_t
>
shape
{
3
,
2
};
std
::
vector
<
size_t
>
strides
{
8
,
4
};
auto
ptr
=
data
.
data
();
auto
vptr
=
(
void
*
)
ptr
;
auto
dtype
=
py
::
dtype
(
"int32"
);
py
::
buffer_info
buf_ndim1
(
vptr
,
4
,
"i"
,
6
);
py
::
buffer_info
buf_ndim2
(
vptr
,
4
,
"i"
,
2
,
shape
,
strides
);
switch
(
i
)
{
// shape: (3, 2)
case
0
:
return
arr_t
(
shape
,
ptr
,
strides
);
case
1
:
return
py
::
array
(
shape
,
ptr
,
strides
);
case
2
:
return
py
::
array
(
dtype
,
shape
,
vptr
,
strides
);
case
3
:
return
arr_t
(
shape
,
ptr
);
case
4
:
return
py
::
array
(
shape
,
ptr
);
case
5
:
return
py
::
array
(
dtype
,
shape
,
vptr
);
case
6
:
return
arr_t
(
buf_ndim2
);
case
7
:
return
py
::
array
(
buf_ndim2
);
// shape: (6, )
case
8
:
return
arr_t
(
6
,
ptr
);
case
9
:
return
py
::
array
(
6
,
ptr
);
case
10
:
return
py
::
array
(
dtype
,
6
,
vptr
);
case
11
:
return
arr_t
(
buf_ndim1
);
case
12
:
return
py
::
array
(
buf_ndim1
);
}
return
arr_t
();
}
void
init_ex_numpy_dtypes
(
py
::
module
&
m
)
{
PYBIND11_NUMPY_DTYPE
(
SimpleStruct
,
x
,
y
,
z
);
PYBIND11_NUMPY_DTYPE
(
PackedStruct
,
x
,
y
,
z
);
...
...
@@ -187,6 +221,7 @@ void init_ex_numpy_dtypes(py::module &m) {
m
.
def
(
"get_format_unbound"
,
&
get_format_unbound
);
m
.
def
(
"create_string_array"
,
&
create_string_array
);
m
.
def
(
"print_string_array"
,
&
print_recarray
<
StringStruct
>
);
m
.
def
(
"test_array_ctors"
,
&
test_array_ctors
);
}
#undef PYBIND11_PACKED
example/example-numpy-dtypes.py
View file @
e19980cc
...
...
@@ -6,7 +6,8 @@ import numpy as np
from
example
import
(
create_rec_simple
,
create_rec_packed
,
create_rec_nested
,
print_format_descriptors
,
print_rec_simple
,
print_rec_packed
,
print_rec_nested
,
print_dtypes
,
get_format_unbound
,
create_rec_partial
,
create_rec_partial_nested
,
create_string_array
,
print_string_array
create_rec_partial
,
create_rec_partial_nested
,
create_string_array
,
print_string_array
,
test_array_ctors
)
...
...
@@ -80,3 +81,8 @@ assert arr['a'].tolist() == [b'', b'a', b'ab', b'abc']
assert
arr
[
'b'
].
tolist
()
==
[
b
''
,
b
'a'
,
b
'ab'
,
b
'abc'
]
arr
=
create_string_array
(
False
)
assert
dtype
==
arr
.
dtype
data
=
np
.
arange
(
1
,
7
,
dtype
=
'int32'
)
for
i
in
range
(
13
):
expected
=
data
if
i
>=
8
else
data
.
reshape
((
3
,
2
))
np
.
testing
.
assert_array_equal
(
test_array_ctors
(
i
),
expected
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment