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