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.

203 lines
4.7 KiB

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