test_pointer_adoption.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. # Copyright David Abrahams 2004. Distributed under the Boost
  2. # Software License, Version 1.0. (See accompanying
  3. # file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  4. """
  5. >>> from test_pointer_adoption_ext import *
  6. >>> num_a_instances()
  7. 0
  8. >>> a = create('dynamically allocated')
  9. >>> num_a_instances()
  10. 1
  11. >>> a.content()
  12. 'dynamically allocated'
  13. >>> innards = a.get_inner()
  14. >>> innards.change('with an exposed reference')
  15. >>> a.content()
  16. 'with an exposed reference'
  17. # The a instance should be kept alive...
  18. >>> a = None
  19. >>> num_a_instances()
  20. 1
  21. # ...until we're done with its innards
  22. >>> innards = None
  23. >>> num_a_instances()
  24. 0
  25. >>> b = B()
  26. >>> a = create('another')
  27. >>> b.a_content()
  28. 'empty'
  29. >>> innards = b.adopt(a);
  30. >>> b.a_content()
  31. 'another'
  32. >>> num_a_instances()
  33. 1
  34. >>> del a # innards and b are both holding a reference
  35. >>> num_a_instances()
  36. 1
  37. >>> innards.change('yet another')
  38. >>> b.a_content()
  39. 'yet another'
  40. >>> del innards
  41. >>> num_a_instances() # b still owns a reference to a
  42. 1
  43. >>> del b
  44. >>> num_a_instances()
  45. 0
  46. Test call policies for constructors here
  47. >>> a = create('second a')
  48. >>> num_a_instances()
  49. 1
  50. >>> b = B(a)
  51. >>> num_a_instances()
  52. 1
  53. >>> a.content()
  54. 'second a'
  55. >>> del a
  56. >>> num_a_instances()
  57. 1
  58. >>> b.a_content()
  59. 'second a'
  60. >>> del b
  61. >>> num_a_instances()
  62. 0
  63. >>> assert as_A(create('dynalloc')) is not None
  64. >>> base = Base()
  65. >>> assert as_A(base) is None
  66. """
  67. def run(args = None):
  68. import sys
  69. import doctest
  70. if args is not None:
  71. sys.argv = args
  72. return doctest.testmod(sys.modules.get(__name__))
  73. if __name__ == '__main__':
  74. print("running...")
  75. import sys
  76. status = run()[0]
  77. if (status == 0): print("Done.")
  78. sys.exit(status)