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.

125 lines
3.8 KiB

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. def test_sequence():
  16. from pybind11_tests import ConstructorStats
  17. from pybind11_tests.sequences_and_iterators import Sequence
  18. cstats = ConstructorStats.get(Sequence)
  19. s = Sequence(5)
  20. assert cstats.values() == ['of size', '5']
  21. assert "Sequence" in repr(s)
  22. assert len(s) == 5
  23. assert s[0] == 0 and s[3] == 0
  24. assert 12.34 not in s
  25. s[0], s[3] = 12.34, 56.78
  26. assert 12.34 in s
  27. assert isclose(s[0], 12.34) and isclose(s[3], 56.78)
  28. rev = reversed(s)
  29. assert cstats.values() == ['of size', '5']
  30. rev2 = s[::-1]
  31. assert cstats.values() == ['of size', '5']
  32. expected = [0, 56.78, 0, 0, 12.34]
  33. assert allclose(rev, expected)
  34. assert allclose(rev2, expected)
  35. assert rev == rev2
  36. rev[0::2] = Sequence([2.0, 2.0, 2.0])
  37. assert cstats.values() == ['of size', '3', 'from std::vector']
  38. assert allclose(rev, [2, 56.78, 2, 0, 2])
  39. assert cstats.alive() == 3
  40. del s
  41. assert cstats.alive() == 2
  42. del rev
  43. assert cstats.alive() == 1
  44. del rev2
  45. assert cstats.alive() == 0
  46. assert cstats.values() == []
  47. assert cstats.default_constructions == 0
  48. assert cstats.copy_constructions == 0
  49. assert cstats.move_constructions >= 1
  50. assert cstats.copy_assignments == 0
  51. assert cstats.move_assignments == 0
  52. def test_map_iterator():
  53. from pybind11_tests.sequences_and_iterators import StringMap
  54. m = StringMap({'hi': 'bye', 'black': 'white'})
  55. assert m['hi'] == 'bye'
  56. assert len(m) == 2
  57. assert m['black'] == 'white'
  58. with pytest.raises(KeyError):
  59. assert m['orange']
  60. m['orange'] = 'banana'
  61. assert m['orange'] == 'banana'
  62. expected = {'hi': 'bye', 'black': 'white', 'orange': 'banana'}
  63. for k in m:
  64. assert m[k] == expected[k]
  65. for k, v in m.items():
  66. assert v == expected[k]
  67. def test_python_iterator_in_cpp():
  68. import pybind11_tests.sequences_and_iterators as m
  69. t = (1, 2, 3)
  70. assert m.object_to_list(t) == [1, 2, 3]
  71. assert m.object_to_list(iter(t)) == [1, 2, 3]
  72. assert m.iterator_to_list(iter(t)) == [1, 2, 3]
  73. with pytest.raises(TypeError) as excinfo:
  74. m.object_to_list(1)
  75. assert "object is not iterable" in str(excinfo.value)
  76. with pytest.raises(TypeError) as excinfo:
  77. m.iterator_to_list(1)
  78. assert "incompatible function arguments" in str(excinfo.value)
  79. def bad_next_call():
  80. raise RuntimeError("py::iterator::advance() should propagate errors")
  81. with pytest.raises(RuntimeError) as excinfo:
  82. m.iterator_to_list(iter(bad_next_call, None))
  83. assert str(excinfo.value) == "py::iterator::advance() should propagate errors"
  84. l = [1, None, 0, None]
  85. assert m.count_none(l) == 2
  86. assert m.find_none(l) is True
  87. assert m.count_nonzeros({"a": 0, "b": 1, "c": 2}) == 2
  88. r = range(5)
  89. assert all(m.tuple_iterator(tuple(r)))
  90. assert all(m.list_iterator(list(r)))
  91. assert all(m.sequence_iterator(r))