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
4c5e21b0
Commit
4c5e21b0
authored
Aug 24, 2016
by
Ivan Smirnov
Browse files
Add tests for generalized iterators
parent
2b308e01
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
46 additions
and
0 deletions
+46
-0
tests/test_sequences_and_iterators.cpp
tests/test_sequences_and_iterators.cpp
+34
-0
tests/test_sequences_and_iterators.py
tests/test_sequences_and_iterators.py
+12
-0
No files found.
tests/test_sequences_and_iterators.cpp
View file @
4c5e21b0
...
@@ -116,6 +116,15 @@ private:
...
@@ -116,6 +116,15 @@ private:
float
*
m_data
;
float
*
m_data
;
};
};
class
IntPairs
{
public:
IntPairs
(
std
::
vector
<
std
::
pair
<
int
,
int
>>
data
)
:
data_
(
std
::
move
(
data
))
{}
const
std
::
pair
<
int
,
int
>*
begin
()
const
{
return
data_
.
data
();
}
private:
std
::
vector
<
std
::
pair
<
int
,
int
>>
data_
;
};
// Interface of a map-like object that isn't (directly) an unordered_map, but provides some basic
// Interface of a map-like object that isn't (directly) an unordered_map, but provides some basic
// map-like functionality.
// map-like functionality.
class
StringMap
{
class
StringMap
{
...
@@ -143,8 +152,24 @@ public:
...
@@ -143,8 +152,24 @@ public:
decltype
(
map
.
cend
())
end
()
const
{
return
map
.
cend
();
}
decltype
(
map
.
cend
())
end
()
const
{
return
map
.
cend
();
}
};
};
template
<
typename
T
>
class
NonZeroIterator
{
const
T
*
ptr_
;
public:
NonZeroIterator
(
const
T
*
ptr
)
:
ptr_
(
ptr
)
{}
const
T
&
operator
*
()
const
{
return
*
ptr_
;
}
NonZeroIterator
&
operator
++
()
{
++
ptr_
;
return
*
this
;
}
};
class
NonZeroSentinel
{};
template
<
typename
A
,
typename
B
>
bool
operator
==
(
const
NonZeroIterator
<
std
::
pair
<
A
,
B
>>&
it
,
const
NonZeroSentinel
&
)
{
return
!
(
*
it
).
first
||
!
(
*
it
).
second
;
}
void
init_ex_sequences_and_iterators
(
py
::
module
&
m
)
{
void
init_ex_sequences_and_iterators
(
py
::
module
&
m
)
{
py
::
class_
<
Sequence
>
seq
(
m
,
"Sequence"
);
py
::
class_
<
Sequence
>
seq
(
m
,
"Sequence"
);
seq
.
def
(
py
::
init
<
size_t
>
())
seq
.
def
(
py
::
init
<
size_t
>
())
...
@@ -210,6 +235,15 @@ void init_ex_sequences_and_iterators(py::module &m) {
...
@@ -210,6 +235,15 @@ void init_ex_sequences_and_iterators(py::module &m) {
py
::
keep_alive
<
0
,
1
>
())
py
::
keep_alive
<
0
,
1
>
())
;
;
py
::
class_
<
IntPairs
>
(
m
,
"IntPairs"
)
.
def
(
py
::
init
<
std
::
vector
<
std
::
pair
<
int
,
int
>>>
())
.
def
(
"nonzero"
,
[](
const
IntPairs
&
s
)
{
return
py
::
make_iterator
(
NonZeroIterator
<
std
::
pair
<
int
,
int
>>
(
s
.
begin
()),
NonZeroSentinel
());
},
py
::
keep_alive
<
0
,
1
>
())
.
def
(
"nonzero_keys"
,
[](
const
IntPairs
&
s
)
{
return
py
::
make_key_iterator
(
NonZeroIterator
<
std
::
pair
<
int
,
int
>>
(
s
.
begin
()),
NonZeroSentinel
());
},
py
::
keep_alive
<
0
,
1
>
());
#if 0
#if 0
// Obsolete: special data structure for exposing custom iterator types to python
// Obsolete: special data structure for exposing custom iterator types to python
...
...
tests/test_sequences_and_iterators.py
View file @
4c5e21b0
...
@@ -10,6 +10,18 @@ def allclose(a_list, b_list, rel_tol=1e-05, abs_tol=0.0):
...
@@ -10,6 +10,18 @@ def allclose(a_list, b_list, rel_tol=1e-05, abs_tol=0.0):
return
all
(
isclose
(
a
,
b
,
rel_tol
=
rel_tol
,
abs_tol
=
abs_tol
)
for
a
,
b
in
zip
(
a_list
,
b_list
))
return
all
(
isclose
(
a
,
b
,
rel_tol
=
rel_tol
,
abs_tol
=
abs_tol
)
for
a
,
b
in
zip
(
a_list
,
b_list
))
def
test_generalized_iterators
():
from
pybind11_tests
import
IntPairs
assert
list
(
IntPairs
([(
1
,
2
),
(
3
,
4
),
(
0
,
5
)]).
nonzero
())
==
[(
1
,
2
),
(
3
,
4
)]
assert
list
(
IntPairs
([(
1
,
2
),
(
2
,
0
),
(
0
,
3
),
(
4
,
5
)]).
nonzero
())
==
[(
1
,
2
)]
assert
list
(
IntPairs
([(
0
,
3
),
(
1
,
2
),
(
3
,
4
)]).
nonzero
())
==
[]
assert
list
(
IntPairs
([(
1
,
2
),
(
3
,
4
),
(
0
,
5
)]).
nonzero_keys
())
==
[
1
,
3
]
assert
list
(
IntPairs
([(
1
,
2
),
(
2
,
0
),
(
0
,
3
),
(
4
,
5
)]).
nonzero_keys
())
==
[
1
]
assert
list
(
IntPairs
([(
0
,
3
),
(
1
,
2
),
(
3
,
4
)]).
nonzero_keys
())
==
[]
def
test_sequence
():
def
test_sequence
():
from
pybind11_tests
import
Sequence
,
ConstructorStats
from
pybind11_tests
import
Sequence
,
ConstructorStats
...
...
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