test_sequences_and_iterators.py
Go to the documentation of this file.
1 from __future__ import annotations
2 
3 import pytest
4 from pytest import approx # noqa: PT013
5 
6 from pybind11_tests import ConstructorStats
7 from pybind11_tests import sequences_and_iterators as m
8 
9 
11  assert m.make_forward_slice_size_t() == slice(0, -1, 1)
12  assert m.make_reversed_slice_object() == slice(None, None, -1)
13 
14 
15 @pytest.mark.skipif(not m.has_optional, reason="no <optional>")
17  assert m.make_reversed_slice_size_t_optional() == slice(None, None, -1)
18  assert m.make_reversed_slice_size_t_optional_verbose() == slice(None, None, -1)
19 
20 
22  assert list(m.IntPairs([(1, 2), (3, 4), (0, 5)]).nonzero()) == [(1, 2), (3, 4)]
23  assert list(m.IntPairs([(1, 2), (2, 0), (0, 3), (4, 5)]).nonzero()) == [(1, 2)]
24  assert list(m.IntPairs([(0, 3), (1, 2), (3, 4)]).nonzero()) == []
25 
26  assert list(m.IntPairs([(1, 2), (3, 4), (0, 5)]).nonzero_keys()) == [1, 3]
27  assert list(m.IntPairs([(1, 2), (2, 0), (0, 3), (4, 5)]).nonzero_keys()) == [1]
28  assert list(m.IntPairs([(0, 3), (1, 2), (3, 4)]).nonzero_keys()) == []
29 
30  assert list(m.IntPairs([(1, 2), (3, 4), (0, 5)]).nonzero_values()) == [2, 4]
31  assert list(m.IntPairs([(1, 2), (2, 0), (0, 3), (4, 5)]).nonzero_values()) == [2]
32  assert list(m.IntPairs([(0, 3), (1, 2), (3, 4)]).nonzero_values()) == []
33 
34  # __next__ must continue to raise StopIteration
35  it = m.IntPairs([(0, 0)]).nonzero()
36  for _ in range(3):
37  with pytest.raises(StopIteration):
38  next(it)
39 
40  it = m.IntPairs([(0, 0)]).nonzero_keys()
41  for _ in range(3):
42  with pytest.raises(StopIteration):
43  next(it)
44 
45 
47  pairs = m.IntPairs([(1, 2), (3, 4), (0, 5)])
48  assert list(pairs.nonref()) == [(1, 2), (3, 4), (0, 5)]
49  assert list(pairs.nonref_keys()) == [1, 3, 0]
50  assert list(pairs.nonref_values()) == [2, 4, 5]
51 
52 
54  assert list(m.IntPairs([(1, 2), (3, 4), (0, 5)]).simple_iterator()) == [
55  (1, 2),
56  (3, 4),
57  (0, 5),
58  ]
59  assert list(m.IntPairs([(1, 2), (3, 4), (0, 5)]).simple_keys()) == [1, 3, 0]
60  assert list(m.IntPairs([(1, 2), (3, 4), (0, 5)]).simple_values()) == [2, 4, 5]
61 
62 
64  assert m.IntPairs.nonref.__doc__.endswith("-> Iterator[tuple[int, int]]\n")
65  assert m.IntPairs.nonref_keys.__doc__.endswith("-> Iterator[int]\n")
66  assert m.IntPairs.nonref_values.__doc__.endswith("-> Iterator[int]\n")
67  assert m.IntPairs.simple_iterator.__doc__.endswith("-> Iterator[tuple[int, int]]\n")
68  assert m.IntPairs.simple_keys.__doc__.endswith("-> Iterator[int]\n")
69  assert m.IntPairs.simple_values.__doc__.endswith("-> Iterator[int]\n")
70 
71 
73  """Test that iterators reference rather than copy their referents."""
74  vec = m.VectorNonCopyableInt()
75  vec.append(3)
76  vec.append(5)
77  assert [int(x) for x in vec] == [3, 5]
78  # Increment everything to make sure the referents can be mutated
79  for x in vec:
80  x.set(int(x) + 1)
81  assert [int(x) for x in vec] == [4, 6]
82 
83  vec = m.VectorNonCopyableIntPair()
84  vec.append([3, 4])
85  vec.append([5, 7])
86  assert [int(x) for x in vec.keys()] == [3, 5]
87  assert [int(x) for x in vec.values()] == [4, 7]
88  for x in vec.keys():
89  x.set(int(x) + 1)
90  for x in vec.values():
91  x.set(int(x) + 10)
92  assert [int(x) for x in vec.keys()] == [4, 6]
93  assert [int(x) for x in vec.values()] == [14, 17]
94 
95 
97  sliceable = m.Sliceable(100)
98  assert sliceable[::] == (0, 100, 1)
99  assert sliceable[10::] == (10, 100, 1)
100  assert sliceable[:10:] == (0, 10, 1)
101  assert sliceable[::10] == (0, 100, 10)
102  assert sliceable[-10::] == (90, 100, 1)
103  assert sliceable[:-10:] == (0, 90, 1)
104  assert sliceable[::-10] == (99, -1, -10)
105  assert sliceable[50:60:1] == (50, 60, 1)
106  assert sliceable[50:60:-1] == (50, 60, -1)
107 
108 
110  cstats = ConstructorStats.get(m.Sequence)
111 
112  s = m.Sequence(5)
113  assert cstats.values() == ["of size", "5"]
114 
115  assert "Sequence" in repr(s)
116  assert len(s) == 5
117  assert s[0] == 0
118  assert s[3] == 0
119  assert 12.34 not in s
120  s[0], s[3] = 12.34, 56.78
121  assert 12.34 in s
122  assert s[0] == approx(12.34, rel=1e-05)
123  assert s[3] == approx(56.78, rel=1e-05)
124 
125  rev = reversed(s)
126  assert cstats.values() == ["of size", "5"]
127 
128  rev2 = s[::-1]
129  assert cstats.values() == ["of size", "5"]
130 
131  it = iter(m.Sequence(0))
132  for _ in range(3): # __next__ must continue to raise StopIteration
133  with pytest.raises(StopIteration):
134  next(it)
135  assert cstats.values() == ["of size", "0"]
136 
137  expected = [0, 56.78, 0, 0, 12.34]
138  assert rev == approx(expected, rel=1e-05)
139  assert rev2 == approx(expected, rel=1e-05)
140  assert rev == rev2
141 
142  rev[0::2] = m.Sequence([2.0, 2.0, 2.0])
143  assert cstats.values() == ["of size", "3", "from std::vector"]
144 
145  assert rev == approx([2, 56.78, 2, 0, 2], rel=1e-05)
146 
147  assert cstats.alive() == 4
148  del it
149  assert cstats.alive() == 3
150  del s
151  assert cstats.alive() == 2
152  del rev
153  assert cstats.alive() == 1
154  del rev2
155  assert cstats.alive() == 0
156 
157  assert cstats.values() == []
158  assert cstats.default_constructions == 0
159  assert cstats.copy_constructions == 0
160  assert cstats.move_constructions >= 1
161  assert cstats.copy_assignments == 0
162  assert cstats.move_assignments == 0
163 
164 
166  """#2076: Exception raised by len(arg) should be propagated"""
167 
168  class BadLen(RuntimeError):
169  pass
170 
171  class SequenceLike:
172  def __getitem__(self, i):
173  return None
174 
175  def __len__(self):
176  raise BadLen()
177 
178  with pytest.raises(BadLen):
179  m.sequence_length(SequenceLike())
180 
181  assert m.sequence_length([1, 2, 3]) == 3
182  assert m.sequence_length("hello") == 5
183 
184 
186  assert m.sequence_length.__doc__.strip() == "sequence_length(arg0: Sequence) -> int"
187 
188 
190  sm = m.StringMap({"hi": "bye", "black": "white"})
191  assert sm["hi"] == "bye"
192  assert len(sm) == 2
193  assert sm["black"] == "white"
194 
195  with pytest.raises(KeyError):
196  assert sm["orange"]
197  sm["orange"] = "banana"
198  assert sm["orange"] == "banana"
199 
200  expected = {"hi": "bye", "black": "white", "orange": "banana"}
201  for k in sm:
202  assert sm[k] == expected[k]
203  for k, v in sm.items():
204  assert v == expected[k]
205  assert list(sm.values()) == [expected[k] for k in sm]
206 
207  it = iter(m.StringMap({}))
208  for _ in range(3): # __next__ must continue to raise StopIteration
209  with pytest.raises(StopIteration):
210  next(it)
211 
212 
214  t = (1, 2, 3)
215  assert m.object_to_list(t) == [1, 2, 3]
216  assert m.object_to_list(iter(t)) == [1, 2, 3]
217  assert m.iterator_to_list(iter(t)) == [1, 2, 3]
218 
219  with pytest.raises(TypeError) as excinfo:
220  m.object_to_list(1)
221  assert "object is not iterable" in str(excinfo.value)
222 
223  with pytest.raises(TypeError) as excinfo:
224  m.iterator_to_list(1)
225  assert "incompatible function arguments" in str(excinfo.value)
226 
227  def bad_next_call():
228  raise RuntimeError("py::iterator::advance() should propagate errors")
229 
230  with pytest.raises(RuntimeError) as excinfo:
231  m.iterator_to_list(iter(bad_next_call, None))
232  assert str(excinfo.value) == "py::iterator::advance() should propagate errors"
233 
234  lst = [1, None, 0, None]
235  assert m.count_none(lst) == 2
236  assert m.find_none(lst) is True
237  assert m.count_nonzeros({"a": 0, "b": 1, "c": 2}) == 2
238 
239  r = range(5)
240  assert all(m.tuple_iterator(tuple(r)))
241  assert all(m.list_iterator(list(r)))
242  assert all(m.sequence_iterator(r))
243 
244 
246  """#181: iterator passthrough did not compile"""
247  from pybind11_tests.sequences_and_iterators import iterator_passthrough
248 
249  values = [3, 5, 7, 9, 11, 13, 15]
250  assert list(iterator_passthrough(iter(values))) == values
251 
252 
254  """#388: Can't make iterators via make_iterator() with different r/v policies"""
255  import pybind11_tests.sequences_and_iterators as m
256 
257  assert list(m.make_iterator_1()) == [1, 2, 3]
258  assert list(m.make_iterator_2()) == [1, 2, 3]
259  assert not isinstance(m.make_iterator_1(), type(m.make_iterator_2()))
260 
261 
263  """#4100: Check for proper iterator overload with C-Arrays"""
264  args_gt = [float(i) for i in range(3)]
265  arr_h = m.CArrayHolder(*args_gt)
266  args = list(arr_h)
267  assert args_gt == args
gtsam.examples.DogLegOptimizerExample.int
int
Definition: DogLegOptimizerExample.py:111
test_sequences_and_iterators.test_iterator_referencing
def test_iterator_referencing()
Definition: test_sequences_and_iterators.py:72
test_sequences_and_iterators.test_slice_constructors_explicit_optional
def test_slice_constructors_explicit_optional()
Definition: test_sequences_and_iterators.py:16
list
Definition: pytypes.h:2166
test_sequences_and_iterators.test_python_iterator_in_cpp
def test_python_iterator_in_cpp()
Definition: test_sequences_and_iterators.py:213
test_sequences_and_iterators.test_sequence_length
def test_sequence_length()
Definition: test_sequences_and_iterators.py:165
test_sequences_and_iterators.test_generalized_iterators_simple
def test_generalized_iterators_simple()
Definition: test_sequences_and_iterators.py:53
type
Definition: pytypes.h:1525
test_sequences_and_iterators.test_sliceable
def test_sliceable()
Definition: test_sequences_and_iterators.py:96
gtsam::range
Double_ range(const Point2_ &p, const Point2_ &q)
Definition: slam/expressions.h:30
test_sequences_and_iterators.test_nonref_iterators
def test_nonref_iterators()
Definition: test_sequences_and_iterators.py:46
slice
Definition: pytypes.h:1909
test_sequences_and_iterators.test_sequence
def test_sequence()
Definition: test_sequences_and_iterators.py:109
isinstance
bool isinstance(handle obj)
Definition: pytypes.h:842
Eigen::all
static const Eigen::internal::all_t all
Definition: IndexedViewHelper.h:171
test_sequences_and_iterators.test_sequence_doc
def test_sequence_doc()
Definition: test_sequences_and_iterators.py:185
test_sequences_and_iterators.test_slice_constructors
def test_slice_constructors()
Definition: test_sequences_and_iterators.py:10
test_sequences_and_iterators.test_iterator_doc_annotations
def test_iterator_doc_annotations()
Definition: test_sequences_and_iterators.py:63
test_sequences_and_iterators.test_map_iterator
def test_map_iterator()
Definition: test_sequences_and_iterators.py:189
test_sequences_and_iterators.test_carray_iterator
def test_carray_iterator()
Definition: test_sequences_and_iterators.py:262
str
Definition: pytypes.h:1558
test_sequences_and_iterators.test_iterator_passthrough
def test_iterator_passthrough()
Definition: test_sequences_and_iterators.py:245
iter
iterator iter(handle obj)
Definition: pytypes.h:2475
ConstructorStats::get
static ConstructorStats & get(std::type_index type)
Definition: constructor_stats.h:163
tuple
Definition: pytypes.h:2077
gtsam.examples.DogLegOptimizerExample.float
float
Definition: DogLegOptimizerExample.py:113
len
size_t len(handle h)
Get the length of a Python object.
Definition: pytypes.h:2446
test_sequences_and_iterators.test_generalized_iterators
def test_generalized_iterators()
Definition: test_sequences_and_iterators.py:21
test_sequences_and_iterators.test_iterator_rvp
def test_iterator_rvp()
Definition: test_sequences_and_iterators.py:253
repr
str repr(handle h)
Definition: pytypes.h:2467


gtsam
Author(s):
autogenerated on Sat Nov 16 2024 04:07:08