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.

129 lines
4.0 KiB

3 months ago
  1. import pytest
  2. def test_unscoped_enum():
  3. from pybind11_tests import UnscopedEnum, EOne
  4. assert str(UnscopedEnum.EOne) == "UnscopedEnum.EOne"
  5. assert str(UnscopedEnum.ETwo) == "UnscopedEnum.ETwo"
  6. assert str(EOne) == "UnscopedEnum.EOne"
  7. # __members__ property
  8. assert UnscopedEnum.__members__ == {"EOne": UnscopedEnum.EOne, "ETwo": UnscopedEnum.ETwo}
  9. # __members__ readonly
  10. with pytest.raises(AttributeError):
  11. UnscopedEnum.__members__ = {}
  12. # __members__ returns a copy
  13. foo = UnscopedEnum.__members__
  14. foo["bar"] = "baz"
  15. assert UnscopedEnum.__members__ == {"EOne": UnscopedEnum.EOne, "ETwo": UnscopedEnum.ETwo}
  16. # no TypeError exception for unscoped enum ==/!= int comparisons
  17. y = UnscopedEnum.ETwo
  18. assert y == 2
  19. assert y != 3
  20. assert int(UnscopedEnum.ETwo) == 2
  21. assert str(UnscopedEnum(2)) == "UnscopedEnum.ETwo"
  22. # order
  23. assert UnscopedEnum.EOne < UnscopedEnum.ETwo
  24. assert UnscopedEnum.EOne < 2
  25. assert UnscopedEnum.ETwo > UnscopedEnum.EOne
  26. assert UnscopedEnum.ETwo > 1
  27. assert UnscopedEnum.ETwo <= 2
  28. assert UnscopedEnum.ETwo >= 2
  29. assert UnscopedEnum.EOne <= UnscopedEnum.ETwo
  30. assert UnscopedEnum.EOne <= 2
  31. assert UnscopedEnum.ETwo >= UnscopedEnum.EOne
  32. assert UnscopedEnum.ETwo >= 1
  33. assert not (UnscopedEnum.ETwo < UnscopedEnum.EOne)
  34. assert not (2 < UnscopedEnum.EOne)
  35. def test_scoped_enum():
  36. from pybind11_tests import ScopedEnum, test_scoped_enum
  37. assert test_scoped_enum(ScopedEnum.Three) == "ScopedEnum::Three"
  38. z = ScopedEnum.Two
  39. assert test_scoped_enum(z) == "ScopedEnum::Two"
  40. # expected TypeError exceptions for scoped enum ==/!= int comparisons
  41. with pytest.raises(TypeError):
  42. assert z == 2
  43. with pytest.raises(TypeError):
  44. assert z != 3
  45. # order
  46. assert ScopedEnum.Two < ScopedEnum.Three
  47. assert ScopedEnum.Three > ScopedEnum.Two
  48. assert ScopedEnum.Two <= ScopedEnum.Three
  49. assert ScopedEnum.Two <= ScopedEnum.Two
  50. assert ScopedEnum.Two >= ScopedEnum.Two
  51. assert ScopedEnum.Three >= ScopedEnum.Two
  52. def test_implicit_conversion():
  53. from pybind11_tests import ClassWithUnscopedEnum
  54. assert str(ClassWithUnscopedEnum.EMode.EFirstMode) == "EMode.EFirstMode"
  55. assert str(ClassWithUnscopedEnum.EFirstMode) == "EMode.EFirstMode"
  56. f = ClassWithUnscopedEnum.test_function
  57. first = ClassWithUnscopedEnum.EFirstMode
  58. second = ClassWithUnscopedEnum.ESecondMode
  59. assert f(first) == 1
  60. assert f(first) == f(first)
  61. assert not f(first) != f(first)
  62. assert f(first) != f(second)
  63. assert not f(first) == f(second)
  64. assert f(first) == int(f(first))
  65. assert not f(first) != int(f(first))
  66. assert f(first) != int(f(second))
  67. assert not f(first) == int(f(second))
  68. # noinspection PyDictCreation
  69. x = {f(first): 1, f(second): 2}
  70. x[f(first)] = 3
  71. x[f(second)] = 4
  72. # Hashing test
  73. assert str(x) == "{EMode.EFirstMode: 3, EMode.ESecondMode: 4}"
  74. def test_binary_operators():
  75. from pybind11_tests import Flags
  76. assert int(Flags.Read) == 4
  77. assert int(Flags.Write) == 2
  78. assert int(Flags.Execute) == 1
  79. assert int(Flags.Read | Flags.Write | Flags.Execute) == 7
  80. assert int(Flags.Read | Flags.Write) == 6
  81. assert int(Flags.Read | Flags.Execute) == 5
  82. assert int(Flags.Write | Flags.Execute) == 3
  83. assert int(Flags.Write | 1) == 3
  84. state = Flags.Read | Flags.Write
  85. assert (state & Flags.Read) != 0
  86. assert (state & Flags.Write) != 0
  87. assert (state & Flags.Execute) == 0
  88. assert (state & 1) == 0
  89. state2 = ~state
  90. assert state2 == -7
  91. assert int(state ^ state2) == -1
  92. def test_enum_to_int():
  93. from pybind11_tests import Flags, ClassWithUnscopedEnum
  94. from pybind11_tests import test_enum_to_int, test_enum_to_uint, test_enum_to_long_long
  95. test_enum_to_int(Flags.Read)
  96. test_enum_to_int(ClassWithUnscopedEnum.EMode.EFirstMode)
  97. test_enum_to_uint(Flags.Read)
  98. test_enum_to_uint(ClassWithUnscopedEnum.EMode.EFirstMode)
  99. test_enum_to_long_long(Flags.Read)
  100. test_enum_to_long_long(ClassWithUnscopedEnum.EMode.EFirstMode)