Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
gaoqiong
pybind11
Commits
7f913aec
Commit
7f913aec
authored
Jun 19, 2016
by
Ivan Smirnov
Browse files
Add tests for nested recarrays
parent
80a3785a
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
41 additions
and
14 deletions
+41
-14
example/example20.cpp
example/example20.cpp
+23
-9
example/example20.py
example/example20.py
+18
-5
No files found.
example/example20.cpp
View file @
7f913aec
...
@@ -30,21 +30,34 @@ struct PackedStruct {
...
@@ -30,21 +30,34 @@ struct PackedStruct {
struct
NestedStruct
{
struct
NestedStruct
{
Struct
a
;
Struct
a
;
PackedStruct
b
;
PackedStruct
b
;
};
}
__attribute__
((
packed
));
template
<
typename
T
>
py
::
array
mkarray_via_buffer
(
size_t
n
)
{
return
py
::
array
(
py
::
buffer_info
(
nullptr
,
sizeof
(
T
),
py
::
format_descriptor
<
T
>::
value
(),
1
,
{
n
},
{
sizeof
(
T
)
}));
}
template
<
typename
S
>
template
<
typename
S
>
py
::
array_t
<
S
>
create_recarray
(
size_t
n
)
{
py
::
array_t
<
S
>
create_recarray
(
size_t
n
)
{
auto
arr
=
py
::
array
(
py
::
buffer_info
(
nullptr
,
sizeof
(
S
),
auto
arr
=
mkarray_via_buffer
<
S
>
(
n
);
py
::
format_descriptor
<
S
>::
value
(),
auto
ptr
=
static_cast
<
S
*>
(
arr
.
request
().
ptr
);
1
,
{
n
},
{
sizeof
(
S
)
}));
for
(
size_t
i
=
0
;
i
<
n
;
i
++
)
{
auto
buf
=
arr
.
request
();
ptr
[
i
].
x
=
i
%
2
;
ptr
[
i
].
y
=
(
uint32_t
)
i
;
ptr
[
i
].
z
=
(
float
)
i
*
1.5
f
;
auto
ptr
=
static_cast
<
S
*>
(
buf
.
ptr
);
}
return
arr
;
}
py
::
array_t
<
NestedStruct
>
create_nested
(
size_t
n
)
{
auto
arr
=
mkarray_via_buffer
<
NestedStruct
>
(
n
);
auto
ptr
=
static_cast
<
NestedStruct
*>
(
arr
.
request
().
ptr
);
for
(
size_t
i
=
0
;
i
<
n
;
i
++
)
{
for
(
size_t
i
=
0
;
i
<
n
;
i
++
)
{
ptr
[
i
].
x
=
i
%
2
;
ptr
[
i
].
a
.
x
=
i
%
2
;
ptr
[
i
].
a
.
y
=
(
uint32_t
)
i
;
ptr
[
i
].
a
.
z
=
(
float
)
i
*
1.5
f
;
ptr
[
i
].
y
=
i
;
ptr
[
i
].
b
.
x
=
(
i
+
1
)
%
2
;
ptr
[
i
].
b
.
y
=
(
uint32_t
)
(
i
+
1
);
ptr
[
i
].
b
.
z
=
(
float
)
(
i
+
1
)
*
1.5
f
;
ptr
[
i
].
z
=
i
*
1.5
;
}
}
return
arr
;
return
arr
;
}
}
void
init_ex20
(
py
::
module
&
m
)
{
void
init_ex20
(
py
::
module
&
m
)
{
...
@@ -54,4 +67,5 @@ void init_ex20(py::module &m) {
...
@@ -54,4 +67,5 @@ void init_ex20(py::module &m) {
m
.
def
(
"create_rec_simple"
,
&
create_recarray
<
Struct
>
);
m
.
def
(
"create_rec_simple"
,
&
create_recarray
<
Struct
>
);
m
.
def
(
"create_rec_packed"
,
&
create_recarray
<
PackedStruct
>
);
m
.
def
(
"create_rec_packed"
,
&
create_recarray
<
PackedStruct
>
);
m
.
def
(
"create_rec_nested"
,
&
create_nested
);
}
}
example/example20.py
View file @
7f913aec
...
@@ -2,7 +2,7 @@
...
@@ -2,7 +2,7 @@
from
__future__
import
print_function
from
__future__
import
print_function
import
numpy
as
np
import
numpy
as
np
from
example
import
create_rec_simple
,
create_rec_packed
from
example
import
create_rec_simple
,
create_rec_packed
,
create_rec_nested
def
check_eq
(
arr
,
data
,
dtype
):
def
check_eq
(
arr
,
data
,
dtype
):
...
@@ -14,12 +14,25 @@ simple_dtype = np.dtype({'names': ['x', 'y', 'z'],
...
@@ -14,12 +14,25 @@ simple_dtype = np.dtype({'names': ['x', 'y', 'z'],
packed_dtype
=
np
.
dtype
([(
'x'
,
'?'
),
(
'y'
,
'u4'
),
(
'z'
,
'f4'
)])
packed_dtype
=
np
.
dtype
([(
'x'
,
'?'
),
(
'y'
,
'u4'
),
(
'z'
,
'f4'
)])
for
func
,
dtype
in
[(
create_rec_simple
,
simple_dtype
),
(
create_rec_packed
,
packed_dtype
)]:
for
func
,
dtype
in
[(
create_rec_simple
,
simple_dtype
),
(
create_rec_packed
,
packed_dtype
)]:
arr
=
func
(
0
)
assert
arr
.
dtype
==
dtype
check_eq
(
arr
,
[],
simple_dtype
)
check_eq
(
arr
,
[],
packed_dtype
)
arr
=
func
(
3
)
arr
=
func
(
3
)
assert
arr
.
dtype
==
dtype
assert
arr
.
dtype
==
dtype
check_eq
(
arr
,
[(
False
,
0
,
0.0
),
(
True
,
1
,
1.5
),
(
False
,
2
,
3.0
)],
simple_dtype
)
check_eq
(
arr
,
[(
False
,
0
,
0.0
),
(
True
,
1
,
1.5
),
(
False
,
2
,
3.0
)],
simple_dtype
)
check_eq
(
arr
,
[(
False
,
0
,
0.0
),
(
True
,
1
,
1.5
),
(
False
,
2
,
3.0
)],
packed_dtype
)
check_eq
(
arr
,
[(
False
,
0
,
0.0
),
(
True
,
1
,
1.5
),
(
False
,
2
,
3.0
)],
packed_dtype
)
arr
=
func
(
0
)
assert
arr
.
dtype
==
dtype
nested_dtype
=
np
.
dtype
([(
'a'
,
simple_dtype
),
(
'b'
,
packed_dtype
)])
check_eq
(
arr
,
[],
simple_dtype
)
check_eq
(
arr
,
[],
packed_dtype
)
arr
=
create_rec_nested
(
0
)
assert
arr
.
dtype
==
nested_dtype
check_eq
(
arr
,
[],
nested_dtype
)
arr
=
create_rec_nested
(
3
)
assert
arr
.
dtype
==
nested_dtype
check_eq
(
arr
,
[((
False
,
0
,
0.0
),
(
True
,
1
,
1.5
)),
((
True
,
1
,
1.5
),
(
False
,
2
,
3.0
)),
((
False
,
2
,
3.0
),
(
True
,
3
,
4.5
))],
nested_dtype
)
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