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