1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- #!/usr/bin/env python3
- """
- test/unit_tests_d/ut_indexed_dict: IndexedDict class unit test for the MMGen suite
- """
- from mmgen.util import msg,msg_r,die
- from ..include.common import vmsg
- class unit_test:
- def run_test(self,name,ut):
- bad_msg = (
- 'initializing values via constructor',
- 'reassignment to existing key',
- 'item deletion',
- 'item moving',
- 'clearing',
- 'updating')
- def bad0(): IndexedDict(arg)
- def bad1(): d['a'] = 2
- def bad2(): del d['a']
- def bad3(): d.move_to_end('a')
- def bad4(): d.clear()
- def bad5(): d.update(d)
- def odie(n): die(4,f'\nillegal action {bad_msg[n]!r} failed to raise exception')
- def omsg(e): vmsg(' - ' + e.args[0])
- msg_r('Testing class IndexedDict...')
- from mmgen.obj import IndexedDict
- d = IndexedDict()
- d['a'] = 1
- d['b'] = 2
- vmsg('\nChecking error handling:')
- arg = [('a',1),('b',2)]
- dict(arg)
- for i in range(6):
- try:
- locals()['bad'+str(i)]()
- except NotImplementedError as e:
- omsg(e)
- else:
- odie(i)
- try:
- d.key(2)
- except Exception as e:
- omsg(e)
- else:
- odie('list index out of range')
- d['c'] = 3
- d_chk = {'a':1,'b':2,'c':3}
- assert d == d_chk, d
- d_keys_chk = ['a','b','c']
- assert d.keys == d_keys_chk, d.keys
- A = d.key(0)
- assert A == 'a', A
- A = d.key(2)
- assert A == 'c', A
- msg('OK')
- return True
|