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