test_sequences_and_iterators.py
Go to the documentation of this file.
1 # -*- coding: utf-8 -*-
2 import pytest
3 from pybind11_tests import sequences_and_iterators as m
4 from pybind11_tests import ConstructorStats
5 
6 
7 def isclose(a, b, rel_tol=1e-05, abs_tol=0.0):
8  """Like math.isclose() from Python 3.5"""
9  return abs(a - b) <= max(rel_tol * max(abs(a), abs(b)), abs_tol)
10 
11 
12 def allclose(a_list, b_list, rel_tol=1e-05, abs_tol=0.0):
13  return all(isclose(a, b, rel_tol=rel_tol, abs_tol=abs_tol) for a, b in zip(a_list, b_list))
14 
15 
17  assert list(m.IntPairs([(1, 2), (3, 4), (0, 5)]).nonzero()) == [(1, 2), (3, 4)]
18  assert list(m.IntPairs([(1, 2), (2, 0), (0, 3), (4, 5)]).nonzero()) == [(1, 2)]
19  assert list(m.IntPairs([(0, 3), (1, 2), (3, 4)]).nonzero()) == []
20 
21  assert list(m.IntPairs([(1, 2), (3, 4), (0, 5)]).nonzero_keys()) == [1, 3]
22  assert list(m.IntPairs([(1, 2), (2, 0), (0, 3), (4, 5)]).nonzero_keys()) == [1]
23  assert list(m.IntPairs([(0, 3), (1, 2), (3, 4)]).nonzero_keys()) == []
24 
25  # __next__ must continue to raise StopIteration
26  it = m.IntPairs([(0, 0)]).nonzero()
27  for _ in range(3):
28  with pytest.raises(StopIteration):
29  next(it)
30 
31  it = m.IntPairs([(0, 0)]).nonzero_keys()
32  for _ in range(3):
33  with pytest.raises(StopIteration):
34  next(it)
35 
36 
38  sliceable = m.Sliceable(100)
39  assert sliceable[::] == (0, 100, 1)
40  assert sliceable[10::] == (10, 100, 1)
41  assert sliceable[:10:] == (0, 10, 1)
42  assert sliceable[::10] == (0, 100, 10)
43  assert sliceable[-10::] == (90, 100, 1)
44  assert sliceable[:-10:] == (0, 90, 1)
45  assert sliceable[::-10] == (99, -1, -10)
46  assert sliceable[50:60:1] == (50, 60, 1)
47  assert sliceable[50:60:-1] == (50, 60, -1)
48 
49 
51  cstats = ConstructorStats.get(m.Sequence)
52 
53  s = m.Sequence(5)
54  assert cstats.values() == ['of size', '5']
55 
56  assert "Sequence" in repr(s)
57  assert len(s) == 5
58  assert s[0] == 0 and s[3] == 0
59  assert 12.34 not in s
60  s[0], s[3] = 12.34, 56.78
61  assert 12.34 in s
62  assert isclose(s[0], 12.34) and isclose(s[3], 56.78)
63 
64  rev = reversed(s)
65  assert cstats.values() == ['of size', '5']
66 
67  rev2 = s[::-1]
68  assert cstats.values() == ['of size', '5']
69 
70  it = iter(m.Sequence(0))
71  for _ in range(3): # __next__ must continue to raise StopIteration
72  with pytest.raises(StopIteration):
73  next(it)
74  assert cstats.values() == ['of size', '0']
75 
76  expected = [0, 56.78, 0, 0, 12.34]
77  assert allclose(rev, expected)
78  assert allclose(rev2, expected)
79  assert rev == rev2
80 
81  rev[0::2] = m.Sequence([2.0, 2.0, 2.0])
82  assert cstats.values() == ['of size', '3', 'from std::vector']
83 
84  assert allclose(rev, [2, 56.78, 2, 0, 2])
85 
86  assert cstats.alive() == 4
87  del it
88  assert cstats.alive() == 3
89  del s
90  assert cstats.alive() == 2
91  del rev
92  assert cstats.alive() == 1
93  del rev2
94  assert cstats.alive() == 0
95 
96  assert cstats.values() == []
97  assert cstats.default_constructions == 0
98  assert cstats.copy_constructions == 0
99  assert cstats.move_constructions >= 1
100  assert cstats.copy_assignments == 0
101  assert cstats.move_assignments == 0
102 
103 
105  """#2076: Exception raised by len(arg) should be propagated """
106  class BadLen(RuntimeError):
107  pass
108 
109  class SequenceLike():
110  def __getitem__(self, i):
111  return None
112 
113  def __len__(self):
114  raise BadLen()
115 
116  with pytest.raises(BadLen):
117  m.sequence_length(SequenceLike())
118 
119  assert m.sequence_length([1, 2, 3]) == 3
120  assert m.sequence_length("hello") == 5
121 
122 
124  sm = m.StringMap({'hi': 'bye', 'black': 'white'})
125  assert sm['hi'] == 'bye'
126  assert len(sm) == 2
127  assert sm['black'] == 'white'
128 
129  with pytest.raises(KeyError):
130  assert sm['orange']
131  sm['orange'] = 'banana'
132  assert sm['orange'] == 'banana'
133 
134  expected = {'hi': 'bye', 'black': 'white', 'orange': 'banana'}
135  for k in sm:
136  assert sm[k] == expected[k]
137  for k, v in sm.items():
138  assert v == expected[k]
139 
140  it = iter(m.StringMap({}))
141  for _ in range(3): # __next__ must continue to raise StopIteration
142  with pytest.raises(StopIteration):
143  next(it)
144 
145 
147  t = (1, 2, 3)
148  assert m.object_to_list(t) == [1, 2, 3]
149  assert m.object_to_list(iter(t)) == [1, 2, 3]
150  assert m.iterator_to_list(iter(t)) == [1, 2, 3]
151 
152  with pytest.raises(TypeError) as excinfo:
153  m.object_to_list(1)
154  assert "object is not iterable" in str(excinfo.value)
155 
156  with pytest.raises(TypeError) as excinfo:
157  m.iterator_to_list(1)
158  assert "incompatible function arguments" in str(excinfo.value)
159 
160  def bad_next_call():
161  raise RuntimeError("py::iterator::advance() should propagate errors")
162 
163  with pytest.raises(RuntimeError) as excinfo:
164  m.iterator_to_list(iter(bad_next_call, None))
165  assert str(excinfo.value) == "py::iterator::advance() should propagate errors"
166 
167  lst = [1, None, 0, None]
168  assert m.count_none(lst) == 2
169  assert m.find_none(lst) is True
170  assert m.count_nonzeros({"a": 0, "b": 1, "c": 2}) == 2
171 
172  r = range(5)
173  assert all(m.tuple_iterator(tuple(r)))
174  assert all(m.list_iterator(list(r)))
175  assert all(m.sequence_iterator(r))
176 
177 
179  """#181: iterator passthrough did not compile"""
180  from pybind11_tests.sequences_and_iterators import iterator_passthrough
181 
182  assert list(iterator_passthrough(iter([3, 5, 7, 9, 11, 13, 15]))) == [3, 5, 7, 9, 11, 13, 15]
183 
184 
186  """#388: Can't make iterators via make_iterator() with different r/v policies """
187  import pybind11_tests.sequences_and_iterators as m
188 
189  assert list(m.make_iterator_1()) == [1, 2, 3]
190  assert list(m.make_iterator_2()) == [1, 2, 3]
191  assert not isinstance(m.make_iterator_1(), type(m.make_iterator_2()))
#define max(a, b)
Definition: datatypes.h:20
def isclose(a, b, rel_tol=1e-05, abs_tol=0.0)
iterator iter(handle obj)
Definition: pytypes.h:1547
bool isinstance(handle obj)
Definition: pytypes.h:384
static ConstructorStats & get(std::type_index type)
Definition: pytypes.h:928
Definition: pytypes.h:1301
str repr(handle h)
Definition: pytypes.h:1536
size_t len(handle h)
Definition: pytypes.h:1514
def allclose(a_list, b_list, rel_tol=1e-05, abs_tol=0.0)
#define abs(x)
Definition: datatypes.h:17
Definition: pytypes.h:897


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:46:04