You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

165 lines
5.1 KiB

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