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.

198 lines
4.5 KiB

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