vector_indexing_suite.py 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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. '''
  5. #####################################################################
  6. # Check an object that we will use as container element
  7. #####################################################################
  8. >>> from vector_indexing_suite_ext import *
  9. >>> x = X('hi')
  10. >>> x
  11. hi
  12. >>> x.reset() # a member function that modifies X
  13. >>> x
  14. reset
  15. >>> x.foo() # another member function that modifies X
  16. >>> x
  17. foo
  18. # test that a string is implicitly convertible
  19. # to an X
  20. >>> x_value('bochi bochi')
  21. 'gotya bochi bochi'
  22. #####################################################################
  23. # Iteration
  24. #####################################################################
  25. >>> def print_xvec(xvec):
  26. ... s = '[ '
  27. ... for x in xvec:
  28. ... s += repr(x)
  29. ... s += ' '
  30. ... s += ']'
  31. ... print(s)
  32. #####################################################################
  33. # Replace all the contents using slice syntax
  34. #####################################################################
  35. >>> v = XVec()
  36. >>> v[:] = [X('a'),X('b'),X('c'),X('d'),X('e')]
  37. >>> print_xvec(v)
  38. [ a b c d e ]
  39. #####################################################################
  40. # Indexing
  41. #####################################################################
  42. >>> len(v)
  43. 5
  44. >>> v[0]
  45. a
  46. >>> v[1]
  47. b
  48. >>> v[2]
  49. c
  50. >>> v[3]
  51. d
  52. >>> v[4]
  53. e
  54. >>> v[-1]
  55. e
  56. >>> v[-2]
  57. d
  58. >>> v[-3]
  59. c
  60. >>> v[-4]
  61. b
  62. >>> v[-5]
  63. a
  64. #####################################################################
  65. # Deleting an element
  66. #####################################################################
  67. >>> del v[0]
  68. >>> v[0] = 'yaba' # must do implicit conversion
  69. >>> print_xvec(v)
  70. [ yaba c d e ]
  71. #####################################################################
  72. # Calling a mutating function of a container element
  73. #####################################################################
  74. >>> v[3].reset()
  75. >>> v[3]
  76. reset
  77. #####################################################################
  78. # Copying a container element
  79. #####################################################################
  80. >>> x = X(v[3])
  81. >>> x
  82. reset
  83. >>> x.foo()
  84. >>> x
  85. foo
  86. >>> v[3] # should not be changed to 'foo'
  87. reset
  88. #####################################################################
  89. # Referencing a container element
  90. #####################################################################
  91. >>> x = v[3]
  92. >>> x
  93. reset
  94. >>> x.foo()
  95. >>> x
  96. foo
  97. >>> v[3] # should be changed to 'foo'
  98. foo
  99. #####################################################################
  100. # Slice
  101. #####################################################################
  102. >>> sl = v[0:2]
  103. >>> print_xvec(sl)
  104. [ yaba c ]
  105. >>> sl[0].reset()
  106. >>> sl[0]
  107. reset
  108. #####################################################################
  109. # Reset the container again
  110. #####################################################################
  111. >>> v[:] = ['a','b','c','d','e'] # perform implicit conversion to X
  112. >>> print_xvec(v)
  113. [ a b c d e ]
  114. #####################################################################
  115. # Slice: replace [1:3] with an element
  116. #####################################################################
  117. >>> v[1:3] = X('z')
  118. >>> print_xvec(v)
  119. [ a z d e ]
  120. #####################################################################
  121. # Slice: replace [0:2] with a list
  122. #####################################################################
  123. >>> v[0:2] = ['1','2','3','4'] # perform implicit conversion to X
  124. >>> print_xvec(v)
  125. [ 1 2 3 4 d e ]
  126. #####################################################################
  127. # Slice: delete [3:4]
  128. #####################################################################
  129. >>> del v[3:4]
  130. >>> print_xvec(v)
  131. [ 1 2 3 d e ]
  132. #####################################################################
  133. # Slice: set [3:] to a list
  134. #####################################################################
  135. >>> v[3:] = [X('trailing'), X('stuff')] # a list
  136. >>> print_xvec(v)
  137. [ 1 2 3 trailing stuff ]
  138. #####################################################################
  139. # Slice: delete [:3]
  140. #####################################################################
  141. >>> del v[:3]
  142. >>> print_xvec(v)
  143. [ trailing stuff ]
  144. #####################################################################
  145. # Slice: insert a tuple to [0:0]
  146. #####################################################################
  147. >>> v[0:0] = ('leading','stuff') # can also be a tuple
  148. >>> print_xvec(v)
  149. [ leading stuff trailing stuff ]
  150. #####################################################################
  151. # Reset the container again
  152. #####################################################################
  153. >>> v[:] = ['a','b','c','d','e']
  154. #####################################################################
  155. # Some references to the container elements
  156. #####################################################################
  157. >>> z0 = v[0]
  158. >>> z1 = v[1]
  159. >>> z2 = v[2]
  160. >>> z3 = v[3]
  161. >>> z4 = v[4]
  162. >>> z0 # proxy
  163. a
  164. >>> z1 # proxy
  165. b
  166. >>> z2 # proxy
  167. c
  168. >>> z3 # proxy
  169. d
  170. >>> z4 # proxy
  171. e
  172. #####################################################################
  173. # Delete a container element
  174. #####################################################################
  175. >>> del v[2]
  176. >>> print_xvec(v)
  177. [ a b d e ]
  178. #####################################################################
  179. # Show that the references are still valid
  180. #####################################################################
  181. >>> z0 # proxy
  182. a
  183. >>> z1 # proxy
  184. b
  185. >>> z2 # proxy detached
  186. c
  187. >>> z3 # proxy index adjusted
  188. d
  189. >>> z4 # proxy index adjusted
  190. e
  191. #####################################################################
  192. # Delete all container elements
  193. #####################################################################
  194. >>> del v[:]
  195. >>> print_xvec(v)
  196. [ ]
  197. #####################################################################
  198. # Show that the references are still valid
  199. #####################################################################
  200. >>> z0 # proxy detached
  201. a
  202. >>> z1 # proxy detached
  203. b
  204. >>> z2 # proxy detached
  205. c
  206. >>> z3 # proxy detached
  207. d
  208. >>> z4 # proxy detached
  209. e
  210. #####################################################################
  211. # Reset the container again
  212. #####################################################################
  213. >>> v[:] = ['a','b','c','d','e']
  214. #####################################################################
  215. # renew the references to the container elements
  216. #####################################################################
  217. >>> z0 = v[0]
  218. >>> z1 = v[1]
  219. >>> z2 = v[2]
  220. >>> z3 = v[3]
  221. >>> z4 = v[4]
  222. >>> z0 # proxy
  223. a
  224. >>> z1 # proxy
  225. b
  226. >>> z2 # proxy
  227. c
  228. >>> z3 # proxy
  229. d
  230. >>> z4 # proxy
  231. e
  232. #####################################################################
  233. # Set [2:4] to a list such that there will be more elements
  234. #####################################################################
  235. >>> v[2:4] = ['x','y','v']
  236. >>> print_xvec(v)
  237. [ a b x y v e ]
  238. #####################################################################
  239. # Show that the references are still valid
  240. #####################################################################
  241. >>> z0 # proxy
  242. a
  243. >>> z1 # proxy
  244. b
  245. >>> z2 # proxy detached
  246. c
  247. >>> z3 # proxy detached
  248. d
  249. >>> z4 # proxy index adjusted
  250. e
  251. #####################################################################
  252. # Contains
  253. #####################################################################
  254. >>> v[:] = ['a','b','c','d','e'] # reset again
  255. >>> assert 'a' in v
  256. >>> assert 'b' in v
  257. >>> assert 'c' in v
  258. >>> assert 'd' in v
  259. >>> assert 'e' in v
  260. >>> assert not 'X' in v
  261. >>> assert not 12345 in v
  262. #####################################################################
  263. # Show that iteration allows mutable access to the elements
  264. #####################################################################
  265. >>> v[:] = ['a','b','c','d','e'] # reset again
  266. >>> for x in v:
  267. ... x.reset()
  268. >>> print_xvec(v)
  269. [ reset reset reset reset reset ]
  270. #####################################################################
  271. # append
  272. #####################################################################
  273. >>> v[:] = ['a','b','c','d','e'] # reset again
  274. >>> v.append('f')
  275. >>> print_xvec(v)
  276. [ a b c d e f ]
  277. #####################################################################
  278. # extend
  279. #####################################################################
  280. >>> v[:] = ['a','b','c','d','e'] # reset again
  281. >>> v.extend(['f','g','h','i','j'])
  282. >>> print_xvec(v)
  283. [ a b c d e f g h i j ]
  284. #####################################################################
  285. # extend using a generator expression
  286. #####################################################################
  287. >>> v[:] = ['a','b','c','d','e'] # reset again
  288. >>> def generator():
  289. ... addlist = ['f','g','h','i','j']
  290. ... for i in addlist:
  291. ... if i != 'g':
  292. ... yield i
  293. >>> v.extend(generator())
  294. >>> print_xvec(v)
  295. [ a b c d e f h i j ]
  296. #####################################################################
  297. # vector of strings
  298. #####################################################################
  299. >>> sv = StringVec()
  300. >>> sv.append('a')
  301. >>> print(sv[0])
  302. a
  303. #####################################################################
  304. # END....
  305. #####################################################################
  306. '''
  307. def run(args = None):
  308. import sys
  309. import doctest
  310. if args is not None:
  311. sys.argv = args
  312. return doctest.testmod(sys.modules.get(__name__))
  313. if __name__ == '__main__':
  314. print('running...')
  315. import sys
  316. status = run()[0]
  317. if (status == 0): print("Done.")
  318. sys.exit(status)