map_indexing_suite.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. # Copyright Joel de Guzman 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. from __future__ import print_function
  5. '''
  6. #####################################################################
  7. # Check an object that we will use as container element
  8. #####################################################################
  9. >>> from map_indexing_suite_ext import *
  10. >>> assert "map_indexing_suite_IntMap_entry" in dir()
  11. >>> assert "map_indexing_suite_TestMap_entry" in dir()
  12. >>> assert "map_indexing_suite_XMap_entry" in dir()
  13. >>> assert "map_indexing_suite_AMap_entry" in dir()
  14. >>> x = X('hi')
  15. >>> x
  16. hi
  17. >>> x.reset() # a member function that modifies X
  18. >>> x
  19. reset
  20. >>> x.foo() # another member function that modifies X
  21. >>> x
  22. foo
  23. # test that a string is implicitly convertible
  24. # to an X
  25. >>> x_value('bochi bochi')
  26. 'gotya bochi bochi'
  27. #####################################################################
  28. # Iteration
  29. #####################################################################
  30. >>> def print_xmap(xmap):
  31. ... s = '[ '
  32. ... for x in xmap:
  33. ... s += repr(x)
  34. ... s += ' '
  35. ... s += ']'
  36. ... print(s)
  37. #####################################################################
  38. # Setting (adding entries)
  39. #####################################################################
  40. >>> xm = XMap()
  41. >>> xm['joel'] = 'apple'
  42. >>> xm['tenji'] = 'orange'
  43. >>> xm['mariel'] = 'grape'
  44. >>> xm['tutit'] = 'banana'
  45. >>> xm['kim'] = 'kiwi'
  46. >>> print_xmap(xm)
  47. [ (joel, apple) (kim, kiwi) (mariel, grape) (tenji, orange) (tutit, banana) ]
  48. #####################################################################
  49. # Changing an entry
  50. #####################################################################
  51. >>> xm['joel'] = 'pineapple'
  52. >>> print_xmap(xm)
  53. [ (joel, pineapple) (kim, kiwi) (mariel, grape) (tenji, orange) (tutit, banana) ]
  54. #####################################################################
  55. # Deleting an entry
  56. #####################################################################
  57. >>> del xm['joel']
  58. >>> print_xmap(xm)
  59. [ (kim, kiwi) (mariel, grape) (tenji, orange) (tutit, banana) ]
  60. #####################################################################
  61. # adding an entry
  62. #####################################################################
  63. >>> xm['joel'] = 'apple'
  64. >>> print_xmap(xm)
  65. [ (joel, apple) (kim, kiwi) (mariel, grape) (tenji, orange) (tutit, banana) ]
  66. #####################################################################
  67. # Indexing
  68. #####################################################################
  69. >>> len(xm)
  70. 5
  71. >>> xm['joel']
  72. apple
  73. >>> xm['tenji']
  74. orange
  75. >>> xm['mariel']
  76. grape
  77. >>> xm['tutit']
  78. banana
  79. >>> xm['kim']
  80. kiwi
  81. #####################################################################
  82. # Calling a mutating function of a container element
  83. #####################################################################
  84. >>> xm['joel'].reset()
  85. >>> xm['joel']
  86. reset
  87. #####################################################################
  88. # Copying a container element
  89. #####################################################################
  90. >>> x = X(xm['mariel'])
  91. >>> x
  92. grape
  93. >>> x.foo()
  94. >>> x
  95. foo
  96. >>> xm['mariel'] # should not be changed to 'foo'
  97. grape
  98. #####################################################################
  99. # Referencing a container element
  100. #####################################################################
  101. >>> x = xm['mariel']
  102. >>> x
  103. grape
  104. >>> x.foo()
  105. >>> x
  106. foo
  107. >>> xm['mariel'] # should be changed to 'foo'
  108. foo
  109. >>> xm['mariel'] = 'grape' # take it back
  110. >>> xm['joel'] = 'apple' # take it back
  111. #####################################################################
  112. # Contains
  113. #####################################################################
  114. >>> assert 'joel' in xm
  115. >>> assert 'mariel' in xm
  116. >>> assert 'tenji' in xm
  117. >>> assert 'tutit' in xm
  118. >>> assert 'kim' in xm
  119. >>> assert not 'X' in xm
  120. >>> assert not 12345 in xm
  121. #####################################################################
  122. # Some references to the container elements
  123. #####################################################################
  124. >>> z0 = xm['joel']
  125. >>> z1 = xm['mariel']
  126. >>> z2 = xm['tenji']
  127. >>> z3 = xm['tutit']
  128. >>> z4 = xm['kim']
  129. >>> z0 # proxy
  130. apple
  131. >>> z1 # proxy
  132. grape
  133. >>> z2 # proxy
  134. orange
  135. >>> z3 # proxy
  136. banana
  137. >>> z4 # proxy
  138. kiwi
  139. #####################################################################
  140. # Delete some container element
  141. #####################################################################
  142. >>> del xm['tenji']
  143. >>> print_xmap(xm)
  144. [ (joel, apple) (kim, kiwi) (mariel, grape) (tutit, banana) ]
  145. >>> del xm['tutit']
  146. >>> print_xmap(xm)
  147. [ (joel, apple) (kim, kiwi) (mariel, grape) ]
  148. #####################################################################
  149. # Show that the references are still valid
  150. #####################################################################
  151. >>> z0 # proxy
  152. apple
  153. >>> z1 # proxy
  154. grape
  155. >>> z2 # proxy detached
  156. orange
  157. >>> z3 # proxy detached
  158. banana
  159. >>> z4 # proxy
  160. kiwi
  161. #####################################################################
  162. # Show that iteration allows mutable access to the elements
  163. #####################################################################
  164. >>> for x in xm:
  165. ... x.data().reset()
  166. >>> print_xmap(xm)
  167. [ (joel, reset) (kim, reset) (mariel, reset) ]
  168. #####################################################################
  169. # Some more...
  170. #####################################################################
  171. >>> tm = TestMap()
  172. >>> tm["joel"] = X("aaa")
  173. >>> tm["kimpo"] = X("bbb")
  174. >>> print_xmap(tm)
  175. [ (joel, aaa) (kimpo, bbb) ]
  176. >>> for el in tm: #doctest: +NORMALIZE_WHITESPACE
  177. ... print(el.key(), end='')
  178. ... dom = el.data()
  179. joel kimpo
  180. #####################################################################
  181. # Test custom converter...
  182. #####################################################################
  183. >>> am = AMap()
  184. >>> am[3] = 4
  185. >>> am[3]
  186. 4
  187. >>> for i in am:
  188. ... i.data()
  189. 4
  190. #####################################################################
  191. # END....
  192. #####################################################################
  193. '''
  194. def run(args = None):
  195. import sys
  196. import doctest
  197. if args is not None:
  198. sys.argxm = args
  199. return doctest.testmod(sys.modules.get(__name__))
  200. if __name__ == '__main__':
  201. print('running...')
  202. import sys
  203. status = run()[0]
  204. if (status == 0): print("Done.")
  205. sys.exit(status)