test_stl_binders.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. import pytest
  2. import sys
  3. from pybind11_tests import stl_binders as m
  4. with pytest.suppress(ImportError):
  5. import numpy as np
  6. def test_vector_int():
  7. v_int = m.VectorInt([0, 0])
  8. assert len(v_int) == 2
  9. assert bool(v_int) is True
  10. v_int2 = m.VectorInt([0, 0])
  11. assert v_int == v_int2
  12. v_int2[1] = 1
  13. assert v_int != v_int2
  14. v_int2.append(2)
  15. v_int2.insert(0, 1)
  16. v_int2.insert(0, 2)
  17. v_int2.insert(0, 3)
  18. v_int2.insert(6, 3)
  19. assert str(v_int2) == "VectorInt[3, 2, 1, 0, 1, 2, 3]"
  20. with pytest.raises(IndexError):
  21. v_int2.insert(8, 4)
  22. v_int.append(99)
  23. v_int2[2:-2] = v_int
  24. assert v_int2 == m.VectorInt([3, 2, 0, 0, 99, 2, 3])
  25. del v_int2[1:3]
  26. assert v_int2 == m.VectorInt([3, 0, 99, 2, 3])
  27. del v_int2[0]
  28. assert v_int2 == m.VectorInt([0, 99, 2, 3])
  29. # related to the PyPy's buffer protocol.
  30. @pytest.unsupported_on_pypy
  31. def test_vector_buffer():
  32. b = bytearray([1, 2, 3, 4])
  33. v = m.VectorUChar(b)
  34. assert v[1] == 2
  35. v[2] = 5
  36. mv = memoryview(v) # We expose the buffer interface
  37. if sys.version_info.major > 2:
  38. assert mv[2] == 5
  39. mv[2] = 6
  40. else:
  41. assert mv[2] == '\x05'
  42. mv[2] = '\x06'
  43. assert v[2] == 6
  44. with pytest.raises(RuntimeError) as excinfo:
  45. m.create_undeclstruct() # Undeclared struct contents, no buffer interface
  46. assert "NumPy type info missing for " in str(excinfo.value)
  47. @pytest.unsupported_on_pypy
  48. @pytest.requires_numpy
  49. def test_vector_buffer_numpy():
  50. a = np.array([1, 2, 3, 4], dtype=np.int32)
  51. with pytest.raises(TypeError):
  52. m.VectorInt(a)
  53. a = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]], dtype=np.uintc)
  54. v = m.VectorInt(a[0, :])
  55. assert len(v) == 4
  56. assert v[2] == 3
  57. ma = np.asarray(v)
  58. ma[2] = 5
  59. assert v[2] == 5
  60. v = m.VectorInt(a[:, 1])
  61. assert len(v) == 3
  62. assert v[2] == 10
  63. v = m.get_vectorstruct()
  64. assert v[0].x == 5
  65. ma = np.asarray(v)
  66. ma[1]['x'] = 99
  67. assert v[1].x == 99
  68. v = m.VectorStruct(np.zeros(3, dtype=np.dtype([('w', 'bool'), ('x', 'I'),
  69. ('y', 'float64'), ('z', 'bool')], align=True)))
  70. assert len(v) == 3
  71. def test_vector_bool():
  72. import pybind11_cross_module_tests as cm
  73. vv_c = cm.VectorBool()
  74. for i in range(10):
  75. vv_c.append(i % 2 == 0)
  76. for i in range(10):
  77. assert vv_c[i] == (i % 2 == 0)
  78. assert str(vv_c) == "VectorBool[1, 0, 1, 0, 1, 0, 1, 0, 1, 0]"
  79. def test_vector_custom():
  80. v_a = m.VectorEl()
  81. v_a.append(m.El(1))
  82. v_a.append(m.El(2))
  83. assert str(v_a) == "VectorEl[El{1}, El{2}]"
  84. vv_a = m.VectorVectorEl()
  85. vv_a.append(v_a)
  86. vv_b = vv_a[0]
  87. assert str(vv_b) == "VectorEl[El{1}, El{2}]"
  88. def test_map_string_double():
  89. mm = m.MapStringDouble()
  90. mm['a'] = 1
  91. mm['b'] = 2.5
  92. assert list(mm) == ['a', 'b']
  93. assert list(mm.items()) == [('a', 1), ('b', 2.5)]
  94. assert str(mm) == "MapStringDouble{a: 1, b: 2.5}"
  95. um = m.UnorderedMapStringDouble()
  96. um['ua'] = 1.1
  97. um['ub'] = 2.6
  98. assert sorted(list(um)) == ['ua', 'ub']
  99. assert sorted(list(um.items())) == [('ua', 1.1), ('ub', 2.6)]
  100. assert "UnorderedMapStringDouble" in str(um)
  101. def test_map_string_double_const():
  102. mc = m.MapStringDoubleConst()
  103. mc['a'] = 10
  104. mc['b'] = 20.5
  105. assert str(mc) == "MapStringDoubleConst{a: 10, b: 20.5}"
  106. umc = m.UnorderedMapStringDoubleConst()
  107. umc['a'] = 11
  108. umc['b'] = 21.5
  109. str(umc)
  110. def test_noncopyable_containers():
  111. # std::vector
  112. vnc = m.get_vnc(5)
  113. for i in range(0, 5):
  114. assert vnc[i].value == i + 1
  115. for i, j in enumerate(vnc, start=1):
  116. assert j.value == i
  117. # std::deque
  118. dnc = m.get_dnc(5)
  119. for i in range(0, 5):
  120. assert dnc[i].value == i + 1
  121. i = 1
  122. for j in dnc:
  123. assert(j.value == i)
  124. i += 1
  125. # std::map
  126. mnc = m.get_mnc(5)
  127. for i in range(1, 6):
  128. assert mnc[i].value == 10 * i
  129. vsum = 0
  130. for k, v in mnc.items():
  131. assert v.value == 10 * k
  132. vsum += v.value
  133. assert vsum == 150
  134. # std::unordered_map
  135. mnc = m.get_umnc(5)
  136. for i in range(1, 6):
  137. assert mnc[i].value == 10 * i
  138. vsum = 0
  139. for k, v in mnc.items():
  140. assert v.value == 10 * k
  141. vsum += v.value
  142. assert vsum == 150
  143. def test_map_delitem():
  144. mm = m.MapStringDouble()
  145. mm['a'] = 1
  146. mm['b'] = 2.5
  147. assert list(mm) == ['a', 'b']
  148. assert list(mm.items()) == [('a', 1), ('b', 2.5)]
  149. del mm['a']
  150. assert list(mm) == ['b']
  151. assert list(mm.items()) == [('b', 2.5)]
  152. um = m.UnorderedMapStringDouble()
  153. um['ua'] = 1.1
  154. um['ub'] = 2.6
  155. assert sorted(list(um)) == ['ua', 'ub']
  156. assert sorted(list(um.items())) == [('ua', 1.1), ('ub', 2.6)]
  157. del um['ua']
  158. assert sorted(list(um)) == ['ub']
  159. assert sorted(list(um.items())) == [('ub', 2.6)]