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.

140 lines
3.1 KiB

  1. def test_vector_int():
  2. from pybind11_tests import VectorInt
  3. v_int = VectorInt([0, 0])
  4. assert len(v_int) == 2
  5. assert bool(v_int) is True
  6. v_int2 = VectorInt([0, 0])
  7. assert v_int == v_int2
  8. v_int2[1] = 1
  9. assert v_int != v_int2
  10. v_int2.append(2)
  11. v_int2.append(3)
  12. v_int2.insert(0, 1)
  13. v_int2.insert(0, 2)
  14. v_int2.insert(0, 3)
  15. assert str(v_int2) == "VectorInt[3, 2, 1, 0, 1, 2, 3]"
  16. v_int.append(99)
  17. v_int2[2:-2] = v_int
  18. assert v_int2 == VectorInt([3, 2, 0, 0, 99, 2, 3])
  19. del v_int2[1:3]
  20. assert v_int2 == VectorInt([3, 0, 99, 2, 3])
  21. del v_int2[0]
  22. assert v_int2 == VectorInt([0, 99, 2, 3])
  23. def test_vector_custom():
  24. from pybind11_tests import El, VectorEl, VectorVectorEl
  25. v_a = VectorEl()
  26. v_a.append(El(1))
  27. v_a.append(El(2))
  28. assert str(v_a) == "VectorEl[El{1}, El{2}]"
  29. vv_a = VectorVectorEl()
  30. vv_a.append(v_a)
  31. vv_b = vv_a[0]
  32. assert str(vv_b) == "VectorEl[El{1}, El{2}]"
  33. def test_vector_bool():
  34. from pybind11_tests import VectorBool
  35. vv_c = VectorBool()
  36. for i in range(10):
  37. vv_c.append(i % 2 == 0)
  38. for i in range(10):
  39. assert vv_c[i] == (i % 2 == 0)
  40. assert str(vv_c) == "VectorBool[1, 0, 1, 0, 1, 0, 1, 0, 1, 0]"
  41. def test_map_string_double():
  42. from pybind11_tests import MapStringDouble, UnorderedMapStringDouble
  43. m = MapStringDouble()
  44. m['a'] = 1
  45. m['b'] = 2.5
  46. assert list(m) == ['a', 'b']
  47. assert list(m.items()) == [('a', 1), ('b', 2.5)]
  48. assert str(m) == "MapStringDouble{a: 1, b: 2.5}"
  49. um = UnorderedMapStringDouble()
  50. um['ua'] = 1.1
  51. um['ub'] = 2.6
  52. assert sorted(list(um)) == ['ua', 'ub']
  53. assert sorted(list(um.items())) == [('ua', 1.1), ('ub', 2.6)]
  54. assert "UnorderedMapStringDouble" in str(um)
  55. def test_map_string_double_const():
  56. from pybind11_tests import MapStringDoubleConst, UnorderedMapStringDoubleConst
  57. mc = MapStringDoubleConst()
  58. mc['a'] = 10
  59. mc['b'] = 20.5
  60. assert str(mc) == "MapStringDoubleConst{a: 10, b: 20.5}"
  61. umc = UnorderedMapStringDoubleConst()
  62. umc['a'] = 11
  63. umc['b'] = 21.5
  64. str(umc)
  65. def test_noncopyable_vector():
  66. from pybind11_tests import get_vnc
  67. vnc = get_vnc(5)
  68. for i in range(0, 5):
  69. assert vnc[i].value == i + 1
  70. for i, j in enumerate(vnc, start=1):
  71. assert j.value == i
  72. def test_noncopyable_deque():
  73. from pybind11_tests import get_dnc
  74. dnc = get_dnc(5)
  75. for i in range(0, 5):
  76. assert dnc[i].value == i + 1
  77. i = 1
  78. for j in dnc:
  79. assert(j.value == i)
  80. i += 1
  81. def test_noncopyable_map():
  82. from pybind11_tests import get_mnc
  83. mnc = get_mnc(5)
  84. for i in range(1, 6):
  85. assert mnc[i].value == 10 * i
  86. vsum = 0
  87. for k, v in mnc.items():
  88. assert v.value == 10 * k
  89. vsum += v.value
  90. assert vsum == 150
  91. def test_noncopyable_unordered_map():
  92. from pybind11_tests import get_umnc
  93. mnc = get_umnc(5)
  94. for i in range(1, 6):
  95. assert mnc[i].value == 10 * i
  96. vsum = 0
  97. for k, v in mnc.items():
  98. assert v.value == 10 * k
  99. vsum += v.value
  100. assert vsum == 150