ut_indexed_dict.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #!/usr/bin/env python3
  2. """
  3. test/unit_tests_d/ut_indexed_dict: IndexedDict class unit test for the MMGen suite
  4. """
  5. from mmgen.util import msg,msg_r,die
  6. from ..include.common import vmsg
  7. class unit_test:
  8. def run_test(self,name,ut):
  9. bad_msg = (
  10. 'initializing values via constructor',
  11. 'reassignment to existing key',
  12. 'item deletion',
  13. 'item moving',
  14. 'clearing',
  15. 'updating')
  16. def bad0(): IndexedDict(arg)
  17. def bad1(): d['a'] = 2
  18. def bad2(): del d['a']
  19. def bad3(): d.move_to_end('a')
  20. def bad4(): d.clear()
  21. def bad5(): d.update(d)
  22. def odie(n): die(4,f'\nillegal action {bad_msg[n]!r} failed to raise exception')
  23. def omsg(e): vmsg(' - ' + e.args[0])
  24. msg_r('Testing class IndexedDict...')
  25. from mmgen.obj import IndexedDict
  26. d = IndexedDict()
  27. d['a'] = 1
  28. d['b'] = 2
  29. vmsg('\nChecking error handling:')
  30. arg = [('a',1),('b',2)]
  31. dict(arg)
  32. for i in range(6):
  33. try:
  34. locals()['bad'+str(i)]()
  35. except NotImplementedError as e:
  36. omsg(e)
  37. else:
  38. odie(i)
  39. try:
  40. d.key(2)
  41. except Exception as e:
  42. omsg(e)
  43. else:
  44. odie('list index out of range')
  45. d['c'] = 3
  46. d_chk = {'a':1,'b':2,'c':3}
  47. assert d == d_chk, d
  48. d_keys_chk = ['a','b','c']
  49. assert d.keys == d_keys_chk, d.keys
  50. A = d.key(0)
  51. assert A == 'a', A
  52. A = d.key(2)
  53. assert A == 'c', A
  54. msg('OK')
  55. return True