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