bidict/tests/test_bidict.bidict.txt

199 lines
4.2 KiB
Plaintext
Raw Normal View History

Test script for bidict.bidict::
>>> from bidict import bidict
>>> keys = (1, 2, 3)
>>> vals = ('one', 'two', 'three')
>>> bi = bidict(zip(keys, vals))
>>> bi == bidict({1: 'one', 2: 'two', 3: 'three'})
True
Standard getitem syntax gets and sets forward mappings::
>>> bi[2]
'two'
>>> bi[2] = 'twain'
>>> bi[2]
'twain'
>>> bi[4]
Traceback (most recent call last):
...
KeyError: 4
As does slice with only a ``start``::
>>> bi[2:]
'twain'
>>> bi[0:] = 'naught'
>>> bi[0:]
'naught'
Slice with only a ``stop`` gets and sets inverse mappings::
>>> bi[:'one']
1
>>> bi[:'aught'] = 1
>>> bi[:'aught']
1
>>> bi[1]
'aught'
>>> bi[:'one']
Traceback (most recent call last):
...
KeyError: 'one'
Likewise, deleting items::
>>> del bi[0]
>>> del bi[2:]
>>> del bi[:'three']
>>> bi
bidict({1: 'aught'})
``put`` can also be used to update mappings::
>>> bi.put(0, 'zero')
>>> bi[0]
'zero'
>>> del bi[0]
>>> bi.put(1, 'one')
>>> bi
bidict({1: 'one'})
>>> bi.put(1, 'aught')
bidicts maintain references to their inverses via the ``inv`` property,
which can also be used to access or modify them::
>>> bi.inv
bidict({'aught': 1})
>>> bi.inv['aught']
1
>>> bi.inv[:1]
'aught'
>>> bi.inv[:1] = 'one'
>>> bi.inv
bidict({'one': 1})
>>> bi
bidict({1: 'one'})
>>> bi.inv.inv is bi
True
>>> bi.inv.inv.inv is bi.inv
True
A bidict's inverse can also be accessed via ~ operator::
>>> ~bi
bidict({'one': 1})
>>> ~bi is bi.inv
True
bidicts work with ``inverted`` as expected::
>>> from bidict import inverted
>>> biinv = bidict(inverted(bi))
>>> biinv
bidict({'one': 1})
This created a new object (equivalent but not identical)::
>>> biinv == bi.inv
True
>>> biinv is bi.inv
False
2015-11-25 04:29:21 +00:00
To replace a bidict with its inverse (and in constant time), simply do::
>>> bi = bi.inv
>>> bi
bidict({'one': 1})
Inverting the inverse should round-trip::
>>> bi == bidict(inverted(inverted(bi)))
True
The rest of the ``MutableMapping`` interface is supported::
>>> bi.get('one')
1
>>> bi.get('zero')
>>> bi.get('zero', 0)
0
>>> list(bi.keys())
['one']
>>> list(bi.values())
[1]
>>> list(bi.items())
[('one', 1)]
>>> bi.setdefault('one', 2)
1
>>> bi.setdefault('two', 2)
2
>>> bi.pop('one')
1
>>> bi.popitem()
('two', 2)
>>> bi.popitem()
Traceback (most recent call last):
...
KeyError: 'popitem(): bidict is empty'
>>> bi.inv.setdefault(3, 'three')
'three'
>>> bi
bidict({'three': 3})
>>> len(bi) # calls __len__
1
>>> [key for key in bi] # calls __iter__, returns keys like dict
['three']
>>> 'three' in bi # calls __contains__
True
>>> list(bi.keys())
['three']
>>> list(bi.values())
[3]
>>> bi.update([('four', 4)])
>>> bi.update({'five': 5}, six=6, seven=7)
>>> sorted(bi.items(), key=lambda x: x[1])
[('three', 3), ('four', 4), ('five', 5), ('six', 6), ('seven', 7)]
>>> bi.clear()
>>> bi
bidict({})
2015-11-25 06:36:41 +00:00
Initializing with collapsing mappings fails::
>>> bidict([(1, 1), (2, 2), (1, 2)]) # doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
...
CollapseException: ((1, 1), (2, 2))
Collapsing updates fail::
2015-11-25 06:36:41 +00:00
>>> b = bidict({1: 1, 2: 2})
>>> b[1] = 2 # doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
...
CollapseException: ((1, 1), (2, 2))
>>> b[:2] = 1 # doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
...
2015-11-25 06:36:41 +00:00
CollapseException: ((1, 1), (2, 2))
>>> b.update({1: 2}) # doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
...
2015-11-25 06:36:41 +00:00
CollapseException: ((1, 1), (2, 2))
``forceput`` can be used instead::
2015-11-25 06:36:41 +00:00
>>> b.forceput(1, 2)
>>> b
2015-11-25 06:36:41 +00:00
bidict({1: 2})
2015-11-25 06:36:41 +00:00
Trying to insert an existing mapping does not raise, and is a no-op::
>>> b = bidict({1: 'one'})
>>> b[1] = 'one'
>>> b[1]
'one'
>>> b[:'one'] = 1
>>> b[:'one']
1