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
611e6146
Commit
611e6146
authored
Jul 24, 2016
by
Ivan Smirnov
Browse files
Add tests for py::dtype ctors
parent
e19980cc
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
31 additions
and
1 deletion
+31
-1
example/example-numpy-dtypes.cpp
example/example-numpy-dtypes.cpp
+24
-0
example/example-numpy-dtypes.py
example/example-numpy-dtypes.py
+7
-1
No files found.
example/example-numpy-dtypes.cpp
View file @
611e6146
...
...
@@ -200,6 +200,28 @@ py::array_t<int32_t, 0> test_array_ctors(int i) {
return
arr_t
();
}
py
::
list
test_dtype_ctors
()
{
py
::
list
list
;
list
.
append
(
py
::
dtype
(
"int32"
));
list
.
append
(
py
::
dtype
(
std
::
string
(
"float64"
)));
list
.
append
(
py
::
dtype
::
from_args
(
py
::
str
(
"bool"
)));
py
::
list
names
,
offsets
,
formats
;
py
::
dict
dict
;
names
.
append
(
py
::
str
(
"a"
));
names
.
append
(
py
::
str
(
"b"
));
dict
[
"names"
]
=
names
;
offsets
.
append
(
py
::
int_
(
1
));
offsets
.
append
(
py
::
int_
(
10
));
dict
[
"offsets"
]
=
offsets
;
formats
.
append
(
py
::
dtype
(
"int32"
));
formats
.
append
(
py
::
dtype
(
"float64"
));
dict
[
"formats"
]
=
formats
;
dict
[
"itemsize"
]
=
py
::
int_
(
20
);
list
.
append
(
py
::
dtype
::
from_args
(
dict
));
list
.
append
(
py
::
dtype
(
names
,
formats
,
offsets
,
20
));
list
.
append
(
py
::
dtype
(
py
::
buffer_info
((
void
*
)
0
,
1
,
"I"
,
1
)));
list
.
append
(
py
::
dtype
(
py
::
buffer_info
((
void
*
)
0
,
1
,
"T{i:a:f:b:}"
,
1
)));
return
list
;
}
void
test_dtype_methods
()
{
}
void
init_ex_numpy_dtypes
(
py
::
module
&
m
)
{
PYBIND11_NUMPY_DTYPE
(
SimpleStruct
,
x
,
y
,
z
);
PYBIND11_NUMPY_DTYPE
(
PackedStruct
,
x
,
y
,
z
);
...
...
@@ -222,6 +244,8 @@ void init_ex_numpy_dtypes(py::module &m) {
m
.
def
(
"create_string_array"
,
&
create_string_array
);
m
.
def
(
"print_string_array"
,
&
print_recarray
<
StringStruct
>
);
m
.
def
(
"test_array_ctors"
,
&
test_array_ctors
);
m
.
def
(
"test_dtype_ctors"
,
&
test_dtype_ctors
);
m
.
def
(
"test_dtype_methods"
,
&
test_dtype_methods
);
}
#undef PYBIND11_PACKED
example/example-numpy-dtypes.py
View file @
611e6146
...
...
@@ -7,7 +7,7 @@ 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
,
test_array_ctors
test_array_ctors
,
test_dtype_ctors
)
...
...
@@ -86,3 +86,9 @@ 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
)
d1
=
np
.
dtype
({
'names'
:
[
'a'
,
'b'
],
'formats'
:
[
'int32'
,
'float64'
],
'offsets'
:
[
1
,
10
],
'itemsize'
:
20
})
d2
=
np
.
dtype
([(
'a'
,
'i4'
),
(
'b'
,
'f4'
)])
assert
test_dtype_ctors
()
==
[
np
.
dtype
(
'int32'
),
np
.
dtype
(
'float64'
),
np
.
dtype
(
'bool'
),
d1
,
d1
,
np
.
dtype
(
'uint32'
),
d2
]
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