test_sequences_and_iterators.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. import pytest
  2. from pybind11_tests import sequences_and_iterators as m
  3. from pybind11_tests import ConstructorStats
  4. def isclose(a, b, rel_tol=1e-05, abs_tol=0.0):
  5. """Like math.isclose() from Python 3.5"""
  6. return abs(a - b) <= max(rel_tol * max(abs(a), abs(b)), abs_tol)
  7. def allclose(a_list, b_list, rel_tol=1e-05, abs_tol=0.0):
  8. return all(isclose(a, b, rel_tol=rel_tol, abs_tol=abs_tol) for a, b in zip(a_list, b_list))
  9. def test_generalized_iterators():
  10. assert list(m.IntPairs([(1, 2), (3, 4), (0, 5)]).nonzero()) == [(1, 2), (3, 4)]
  11. assert list(m.IntPairs([(1, 2), (2, 0), (0, 3), (4, 5)]).nonzero()) == [(1, 2)]
  12. assert list(m.IntPairs([(0, 3), (1, 2), (3, 4)]).nonzero()) == []
  13. assert list(m.IntPairs([(1, 2), (3, 4), (0, 5)]).nonzero_keys()) == [1, 3]
  14. assert list(m.IntPairs([(1, 2), (2, 0), (0, 3), (4, 5)]).nonzero_keys()) == [1]
  15. assert list(m.IntPairs([(0, 3), (1, 2), (3, 4)]).nonzero_keys()) == []
  16. # __next__ must continue to raise StopIteration
  17. it = m.IntPairs([(0, 0)]).nonzero()
  18. for _ in range(3):
  19. with pytest.raises(StopIteration):
  20. next(it)
  21. it = m.IntPairs([(0, 0)]).nonzero_keys()
  22. for _ in range(3):
  23. with pytest.raises(StopIteration):
  24. next(it)
  25. def test_sequence():
  26. cstats = ConstructorStats.get(m.Sequence)
  27. s = m.Sequence(5)
  28. assert cstats.values() == ['of size', '5']
  29. assert "Sequence" in repr(s)
  30. assert len(s) == 5
  31. assert s[0] == 0 and s[3] == 0
  32. assert 12.34 not in s
  33. s[0], s[3] = 12.34, 56.78
  34. assert 12.34 in s
  35. assert isclose(s[0], 12.34) and isclose(s[3], 56.78)
  36. rev = reversed(s)
  37. assert cstats.values() == ['of size', '5']
  38. rev2 = s[::-1]
  39. assert cstats.values() == ['of size', '5']
  40. it = iter(m.Sequence(0))
  41. for _ in range(3): # __next__ must continue to raise StopIteration
  42. with pytest.raises(StopIteration):
  43. next(it)
  44. assert cstats.values() == ['of size', '0']
  45. expected = [0, 56.78, 0, 0, 12.34]
  46. assert allclose(rev, expected)
  47. assert allclose(rev2, expected)
  48. assert rev == rev2
  49. rev[0::2] = m.Sequence([2.0, 2.0, 2.0])
  50. assert cstats.values() == ['of size', '3', 'from std::vector']
  51. assert allclose(rev, [2, 56.78, 2, 0, 2])
  52. assert cstats.alive() == 4
  53. del it
  54. assert cstats.alive() == 3
  55. del s
  56. assert cstats.alive() == 2
  57. del rev
  58. assert cstats.alive() == 1
  59. del rev2
  60. assert cstats.alive() == 0
  61. assert cstats.values() == []
  62. assert cstats.default_constructions == 0
  63. assert cstats.copy_constructions == 0
  64. assert cstats.move_constructions >= 1
  65. assert cstats.copy_assignments == 0
  66. assert cstats.move_assignments == 0
  67. def test_map_iterator():
  68. sm = m.StringMap({'hi': 'bye', 'black': 'white'})
  69. assert sm['hi'] == 'bye'
  70. assert len(sm) == 2
  71. assert sm['black'] == 'white'
  72. with pytest.raises(KeyError):
  73. assert sm['orange']
  74. sm['orange'] = 'banana'
  75. assert sm['orange'] == 'banana'
  76. expected = {'hi': 'bye', 'black': 'white', 'orange': 'banana'}
  77. for k in sm:
  78. assert sm[k] == expected[k]
  79. for k, v in sm.items():
  80. assert v == expected[k]
  81. it = iter(m.StringMap({}))
  82. for _ in range(3): # __next__ must continue to raise StopIteration
  83. with pytest.raises(StopIteration):
  84. next(it)
  85. def test_python_iterator_in_cpp():
  86. t = (1, 2, 3)
  87. assert m.object_to_list(t) == [1, 2, 3]
  88. assert m.object_to_list(iter(t)) == [1, 2, 3]
  89. assert m.iterator_to_list(iter(t)) == [1, 2, 3]
  90. with pytest.raises(TypeError) as excinfo:
  91. m.object_to_list(1)
  92. assert "object is not iterable" in str(excinfo.value)
  93. with pytest.raises(TypeError) as excinfo:
  94. m.iterator_to_list(1)
  95. assert "incompatible function arguments" in str(excinfo.value)
  96. def bad_next_call():
  97. raise RuntimeError("py::iterator::advance() should propagate errors")
  98. with pytest.raises(RuntimeError) as excinfo:
  99. m.iterator_to_list(iter(bad_next_call, None))
  100. assert str(excinfo.value) == "py::iterator::advance() should propagate errors"
  101. lst = [1, None, 0, None]
  102. assert m.count_none(lst) == 2
  103. assert m.find_none(lst) is True
  104. assert m.count_nonzeros({"a": 0, "b": 1, "c": 2}) == 2
  105. r = range(5)
  106. assert all(m.tuple_iterator(tuple(r)))
  107. assert all(m.list_iterator(list(r)))
  108. assert all(m.sequence_iterator(r))
  109. def test_iterator_passthrough():
  110. """#181: iterator passthrough did not compile"""
  111. from pybind11_tests.sequences_and_iterators import iterator_passthrough
  112. assert list(iterator_passthrough(iter([3, 5, 7, 9, 11, 13, 15]))) == [3, 5, 7, 9, 11, 13, 15]
  113. def test_iterator_rvp():
  114. """#388: Can't make iterators via make_iterator() with different r/v policies """
  115. import pybind11_tests.sequences_and_iterators as m
  116. assert list(m.make_iterator_1()) == [1, 2, 3]
  117. assert list(m.make_iterator_2()) == [1, 2, 3]
  118. assert not isinstance(m.make_iterator_1(), type(m.make_iterator_2()))