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.

254 lines
8.0 KiB

  1. import pytest
  2. import pybind11_tests
  3. from pybind11_tests import ConstructorStats
  4. def test_override(capture, msg):
  5. from pybind11_tests import (ExampleVirt, runExampleVirt, runExampleVirtVirtual,
  6. runExampleVirtBool)
  7. class ExtendedExampleVirt(ExampleVirt):
  8. def __init__(self, state):
  9. super(ExtendedExampleVirt, self).__init__(state + 1)
  10. self.data = "Hello world"
  11. def run(self, value):
  12. print('ExtendedExampleVirt::run(%i), calling parent..' % value)
  13. return super(ExtendedExampleVirt, self).run(value + 1)
  14. def run_bool(self):
  15. print('ExtendedExampleVirt::run_bool()')
  16. return False
  17. def get_string1(self):
  18. return "override1"
  19. def pure_virtual(self):
  20. print('ExtendedExampleVirt::pure_virtual(): %s' % self.data)
  21. class ExtendedExampleVirt2(ExtendedExampleVirt):
  22. def __init__(self, state):
  23. super(ExtendedExampleVirt2, self).__init__(state + 1)
  24. def get_string2(self):
  25. return "override2"
  26. ex12 = ExampleVirt(10)
  27. with capture:
  28. assert runExampleVirt(ex12, 20) == 30
  29. assert capture == "Original implementation of ExampleVirt::run(state=10, value=20, str1=default1, str2=default2)"
  30. with pytest.raises(RuntimeError) as excinfo:
  31. runExampleVirtVirtual(ex12)
  32. assert msg(excinfo.value) == 'Tried to call pure virtual function "ExampleVirt::pure_virtual"'
  33. ex12p = ExtendedExampleVirt(10)
  34. with capture:
  35. assert runExampleVirt(ex12p, 20) == 32
  36. assert capture == """
  37. ExtendedExampleVirt::run(20), calling parent..
  38. Original implementation of ExampleVirt::run(state=11, value=21, str1=override1, str2=default2)
  39. """
  40. with capture:
  41. assert runExampleVirtBool(ex12p) is False
  42. assert capture == "ExtendedExampleVirt::run_bool()"
  43. with capture:
  44. runExampleVirtVirtual(ex12p)
  45. assert capture == "ExtendedExampleVirt::pure_virtual(): Hello world"
  46. ex12p2 = ExtendedExampleVirt2(15)
  47. with capture:
  48. assert runExampleVirt(ex12p2, 50) == 68
  49. assert capture == """
  50. ExtendedExampleVirt::run(50), calling parent..
  51. Original implementation of ExampleVirt::run(state=17, value=51, str1=override1, str2=override2)
  52. """
  53. cstats = ConstructorStats.get(ExampleVirt)
  54. assert cstats.alive() == 3
  55. del ex12, ex12p, ex12p2
  56. assert cstats.alive() == 0
  57. assert cstats.values() == ['10', '11', '17']
  58. assert cstats.copy_constructions == 0
  59. assert cstats.move_constructions >= 0
  60. def test_inheriting_repeat():
  61. from pybind11_tests import A_Repeat, B_Repeat, C_Repeat, D_Repeat, A_Tpl, B_Tpl, C_Tpl, D_Tpl
  62. class AR(A_Repeat):
  63. def unlucky_number(self):
  64. return 99
  65. class AT(A_Tpl):
  66. def unlucky_number(self):
  67. return 999
  68. obj = AR()
  69. assert obj.say_something(3) == "hihihi"
  70. assert obj.unlucky_number() == 99
  71. assert obj.say_everything() == "hi 99"
  72. obj = AT()
  73. assert obj.say_something(3) == "hihihi"
  74. assert obj.unlucky_number() == 999
  75. assert obj.say_everything() == "hi 999"
  76. for obj in [B_Repeat(), B_Tpl()]:
  77. assert obj.say_something(3) == "B says hi 3 times"
  78. assert obj.unlucky_number() == 13
  79. assert obj.lucky_number() == 7.0
  80. assert obj.say_everything() == "B says hi 1 times 13"
  81. for obj in [C_Repeat(), C_Tpl()]:
  82. assert obj.say_something(3) == "B says hi 3 times"
  83. assert obj.unlucky_number() == 4444
  84. assert obj.lucky_number() == 888.0
  85. assert obj.say_everything() == "B says hi 1 times 4444"
  86. class CR(C_Repeat):
  87. def lucky_number(self):
  88. return C_Repeat.lucky_number(self) + 1.25
  89. obj = CR()
  90. assert obj.say_something(3) == "B says hi 3 times"
  91. assert obj.unlucky_number() == 4444
  92. assert obj.lucky_number() == 889.25
  93. assert obj.say_everything() == "B says hi 1 times 4444"
  94. class CT(C_Tpl):
  95. pass
  96. obj = CT()
  97. assert obj.say_something(3) == "B says hi 3 times"
  98. assert obj.unlucky_number() == 4444
  99. assert obj.lucky_number() == 888.0
  100. assert obj.say_everything() == "B says hi 1 times 4444"
  101. class CCR(CR):
  102. def lucky_number(self):
  103. return CR.lucky_number(self) * 10
  104. obj = CCR()
  105. assert obj.say_something(3) == "B says hi 3 times"
  106. assert obj.unlucky_number() == 4444
  107. assert obj.lucky_number() == 8892.5
  108. assert obj.say_everything() == "B says hi 1 times 4444"
  109. class CCT(CT):
  110. def lucky_number(self):
  111. return CT.lucky_number(self) * 1000
  112. obj = CCT()
  113. assert obj.say_something(3) == "B says hi 3 times"
  114. assert obj.unlucky_number() == 4444
  115. assert obj.lucky_number() == 888000.0
  116. assert obj.say_everything() == "B says hi 1 times 4444"
  117. class DR(D_Repeat):
  118. def unlucky_number(self):
  119. return 123
  120. def lucky_number(self):
  121. return 42.0
  122. for obj in [D_Repeat(), D_Tpl()]:
  123. assert obj.say_something(3) == "B says hi 3 times"
  124. assert obj.unlucky_number() == 4444
  125. assert obj.lucky_number() == 888.0
  126. assert obj.say_everything() == "B says hi 1 times 4444"
  127. obj = DR()
  128. assert obj.say_something(3) == "B says hi 3 times"
  129. assert obj.unlucky_number() == 123
  130. assert obj.lucky_number() == 42.0
  131. assert obj.say_everything() == "B says hi 1 times 123"
  132. class DT(D_Tpl):
  133. def say_something(self, times):
  134. return "DT says:" + (' quack' * times)
  135. def unlucky_number(self):
  136. return 1234
  137. def lucky_number(self):
  138. return -4.25
  139. obj = DT()
  140. assert obj.say_something(3) == "DT says: quack quack quack"
  141. assert obj.unlucky_number() == 1234
  142. assert obj.lucky_number() == -4.25
  143. assert obj.say_everything() == "DT says: quack 1234"
  144. class DT2(DT):
  145. def say_something(self, times):
  146. return "DT2: " + ('QUACK' * times)
  147. def unlucky_number(self):
  148. return -3
  149. class BT(B_Tpl):
  150. def say_something(self, times):
  151. return "BT" * times
  152. def unlucky_number(self):
  153. return -7
  154. def lucky_number(self):
  155. return -1.375
  156. obj = BT()
  157. assert obj.say_something(3) == "BTBTBT"
  158. assert obj.unlucky_number() == -7
  159. assert obj.lucky_number() == -1.375
  160. assert obj.say_everything() == "BT -7"
  161. @pytest.mark.skipif(not hasattr(pybind11_tests, 'NCVirt'),
  162. reason="NCVirt test broken on ICPC")
  163. def test_move_support():
  164. from pybind11_tests import NCVirt, NonCopyable, Movable
  165. class NCVirtExt(NCVirt):
  166. def get_noncopyable(self, a, b):
  167. # Constructs and returns a new instance:
  168. nc = NonCopyable(a * a, b * b)
  169. return nc
  170. def get_movable(self, a, b):
  171. # Return a referenced copy
  172. self.movable = Movable(a, b)
  173. return self.movable
  174. class NCVirtExt2(NCVirt):
  175. def get_noncopyable(self, a, b):
  176. # Keep a reference: this is going to throw an exception
  177. self.nc = NonCopyable(a, b)
  178. return self.nc
  179. def get_movable(self, a, b):
  180. # Return a new instance without storing it
  181. return Movable(a, b)
  182. ncv1 = NCVirtExt()
  183. assert ncv1.print_nc(2, 3) == "36"
  184. assert ncv1.print_movable(4, 5) == "9"
  185. ncv2 = NCVirtExt2()
  186. assert ncv2.print_movable(7, 7) == "14"
  187. # Don't check the exception message here because it differs under debug/non-debug mode
  188. with pytest.raises(RuntimeError):
  189. ncv2.print_nc(9, 9)
  190. nc_stats = ConstructorStats.get(NonCopyable)
  191. mv_stats = ConstructorStats.get(Movable)
  192. assert nc_stats.alive() == 1
  193. assert mv_stats.alive() == 1
  194. del ncv1, ncv2
  195. assert nc_stats.alive() == 0
  196. assert mv_stats.alive() == 0
  197. assert nc_stats.values() == ['4', '9', '9', '9']
  198. assert mv_stats.values() == ['4', '5', '7', '7']
  199. assert nc_stats.copy_constructions == 0
  200. assert mv_stats.copy_constructions == 1
  201. assert nc_stats.move_constructions >= 0
  202. assert mv_stats.move_constructions >= 0