123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243 |
- # Copyright Joel de Guzman 2004. Distributed under the Boost
- # Software License, Version 1.0. (See accompanying
- # file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
- from __future__ import print_function
- '''
- #####################################################################
- # Check an object that we will use as container element
- #####################################################################
- >>> from map_indexing_suite_ext import *
- >>> assert "map_indexing_suite_IntMap_entry" in dir()
- >>> assert "map_indexing_suite_TestMap_entry" in dir()
- >>> assert "map_indexing_suite_XMap_entry" in dir()
- >>> assert "map_indexing_suite_AMap_entry" in dir()
- >>> x = X('hi')
- >>> x
- hi
- >>> x.reset() # a member function that modifies X
- >>> x
- reset
- >>> x.foo() # another member function that modifies X
- >>> x
- foo
- # test that a string is implicitly convertible
- # to an X
- >>> x_value('bochi bochi')
- 'gotya bochi bochi'
- #####################################################################
- # Iteration
- #####################################################################
- >>> def print_xmap(xmap):
- ... s = '[ '
- ... for x in xmap:
- ... s += repr(x)
- ... s += ' '
- ... s += ']'
- ... print(s)
- #####################################################################
- # Setting (adding entries)
- #####################################################################
- >>> xm = XMap()
- >>> xm['joel'] = 'apple'
- >>> xm['tenji'] = 'orange'
- >>> xm['mariel'] = 'grape'
- >>> xm['tutit'] = 'banana'
- >>> xm['kim'] = 'kiwi'
- >>> print_xmap(xm)
- [ (joel, apple) (kim, kiwi) (mariel, grape) (tenji, orange) (tutit, banana) ]
- #####################################################################
- # Changing an entry
- #####################################################################
- >>> xm['joel'] = 'pineapple'
- >>> print_xmap(xm)
- [ (joel, pineapple) (kim, kiwi) (mariel, grape) (tenji, orange) (tutit, banana) ]
- #####################################################################
- # Deleting an entry
- #####################################################################
- >>> del xm['joel']
- >>> print_xmap(xm)
- [ (kim, kiwi) (mariel, grape) (tenji, orange) (tutit, banana) ]
- #####################################################################
- # adding an entry
- #####################################################################
- >>> xm['joel'] = 'apple'
- >>> print_xmap(xm)
- [ (joel, apple) (kim, kiwi) (mariel, grape) (tenji, orange) (tutit, banana) ]
- #####################################################################
- # Indexing
- #####################################################################
- >>> len(xm)
- 5
- >>> xm['joel']
- apple
- >>> xm['tenji']
- orange
- >>> xm['mariel']
- grape
- >>> xm['tutit']
- banana
- >>> xm['kim']
- kiwi
- #####################################################################
- # Calling a mutating function of a container element
- #####################################################################
- >>> xm['joel'].reset()
- >>> xm['joel']
- reset
- #####################################################################
- # Copying a container element
- #####################################################################
- >>> x = X(xm['mariel'])
- >>> x
- grape
- >>> x.foo()
- >>> x
- foo
- >>> xm['mariel'] # should not be changed to 'foo'
- grape
- #####################################################################
- # Referencing a container element
- #####################################################################
- >>> x = xm['mariel']
- >>> x
- grape
- >>> x.foo()
- >>> x
- foo
- >>> xm['mariel'] # should be changed to 'foo'
- foo
- >>> xm['mariel'] = 'grape' # take it back
- >>> xm['joel'] = 'apple' # take it back
- #####################################################################
- # Contains
- #####################################################################
- >>> assert 'joel' in xm
- >>> assert 'mariel' in xm
- >>> assert 'tenji' in xm
- >>> assert 'tutit' in xm
- >>> assert 'kim' in xm
- >>> assert not 'X' in xm
- >>> assert not 12345 in xm
- #####################################################################
- # Some references to the container elements
- #####################################################################
- >>> z0 = xm['joel']
- >>> z1 = xm['mariel']
- >>> z2 = xm['tenji']
- >>> z3 = xm['tutit']
- >>> z4 = xm['kim']
- >>> z0 # proxy
- apple
- >>> z1 # proxy
- grape
- >>> z2 # proxy
- orange
- >>> z3 # proxy
- banana
- >>> z4 # proxy
- kiwi
- #####################################################################
- # Delete some container element
- #####################################################################
- >>> del xm['tenji']
- >>> print_xmap(xm)
- [ (joel, apple) (kim, kiwi) (mariel, grape) (tutit, banana) ]
- >>> del xm['tutit']
- >>> print_xmap(xm)
- [ (joel, apple) (kim, kiwi) (mariel, grape) ]
- #####################################################################
- # Show that the references are still valid
- #####################################################################
- >>> z0 # proxy
- apple
- >>> z1 # proxy
- grape
- >>> z2 # proxy detached
- orange
- >>> z3 # proxy detached
- banana
- >>> z4 # proxy
- kiwi
- #####################################################################
- # Show that iteration allows mutable access to the elements
- #####################################################################
- >>> for x in xm:
- ... x.data().reset()
- >>> print_xmap(xm)
- [ (joel, reset) (kim, reset) (mariel, reset) ]
- #####################################################################
- # Some more...
- #####################################################################
- >>> tm = TestMap()
- >>> tm["joel"] = X("aaa")
- >>> tm["kimpo"] = X("bbb")
- >>> print_xmap(tm)
- [ (joel, aaa) (kimpo, bbb) ]
- >>> for el in tm: #doctest: +NORMALIZE_WHITESPACE
- ... print(el.key(), end='')
- ... dom = el.data()
- joel kimpo
- #####################################################################
- # Test custom converter...
- #####################################################################
- >>> am = AMap()
- >>> am[3] = 4
- >>> am[3]
- 4
- >>> for i in am:
- ... i.data()
- 4
- #####################################################################
- # END....
- #####################################################################
- '''
- def run(args = None):
- import sys
- import doctest
- if args is not None:
- sys.argxm = args
- return doctest.testmod(sys.modules.get(__name__))
- if __name__ == '__main__':
- print('running...')
- import sys
- status = run()[0]
- if (status == 0): print("Done.")
- sys.exit(status)
|