ut_BlocksInfo.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #!/usr/bin/env python3
  2. """
  3. test.unit_tests_d.nt_BlocksInfo:
  4. BlocksInfo unit test for the MMGen Node Tools suite
  5. """
  6. from mmgen.common import *
  7. from mmgen.exception import *
  8. from mmgen_node_tools.BlocksInfo import BlocksInfo
  9. tip = 50000
  10. range_vecs = (
  11. # First Last FromTip nBlocks Step First Last BlockList
  12. ( (), (), (tip, tip, None) ),
  13. ( (199,2,37), (), (None, None, [199,2,37]) ),
  14. ( '0', (0, 0, None, None, None), (0, 0, None) ),
  15. ( '0-0', (0, 0, None, None, None), (0, 0, None) ),
  16. (f'-{tip}', (0, 0, tip, None, None), (0, 0, None) ),
  17. ( '0-10', (0, 10, None, None, None), (0, 10, None) ),
  18. ( '0+10', (0, 9, None, 10, None), (0, 9, None) ),
  19. ( '0+10+2', (0, 9, None, 10, 2 ), (0, 9, [0,2,4,6,8]) ),
  20. ( '1', (1, 1, None, None, None), (1, 1, None) ),
  21. ( '1-1', (1, 1, None, None, None), (1, 1, None) ),
  22. ( '1-10', (1, 10, None, None, None), (1, 10, None) ),
  23. ( '1+10', (1, 10, None, 10, None), (1, 10, None) ),
  24. ( '1+10+2', (1, 10, None, 10, 2 ), (1, 10, [1,3,5,7,9]) ),
  25. ( '+1', (tip, tip, None, 1, None), (tip, tip, None) ),
  26. ( '+10', (tip-9, tip, None, 10, None), (tip-9, tip, None) ),
  27. ( '-1', (tip-1, tip-1, 1, None, None), (tip-1, tip-1, None) ),
  28. ( '-1+1', (tip-1, tip-1, 1, 1, None), (tip-1, tip-1, None) ),
  29. ( '-1+2', (tip-1, tip, 1, 2, None), (tip-1, tip, None) ),
  30. ( '-10', (tip-10, tip-10, 10, None, None), (tip-10, tip-10, None) ),
  31. ( '-10+11', (tip-10, tip, 10, 11, None), (tip-10, tip, None) ),
  32. ( '-10+11+2', (tip-10, tip, 10, 11, 2 ), (tip-10, tip, list(range(tip-10,tip+1,2))) ),
  33. ( 'cUr', (tip, tip, None, None, None), (tip, tip, None) ),
  34. ( 'cur-cUR', (tip, tip, None, None, None), (tip, tip, None) ),
  35. ( '0-cur', (0, tip, None, None, None), (0, tip, None) ),
  36. (f'{tip-1}-cur', (tip-1, tip, None, None, None), (tip-1, tip, None) ),
  37. ( '0-cur+3000', (0, tip, None, None, 3000 ), (0, tip, list(range(0,tip+1,3000))) ),
  38. ( '+1440+144', (tip-1439, tip, None, 1440, 144 ), (tip-1439, tip, list(range(tip-1439,tip+1,144))) ),
  39. ( '+144*10+12*12', (tip-1439, tip, None, 1440, 144 ), (tip-1439, tip, list(range(tip-1439,tip+1,144))) ),
  40. )
  41. full_set = ['aa','bbb','ccc_P2','ddddd','eeeeee','ffffffff','gg']
  42. dfl_set = ['aa','ddddd','ffffffff']
  43. fields_vecs = (
  44. ( 'Ccc_P2', ['ccc_P2'] ),
  45. ( '+CCC_P2', ['aa','ccc_P2','ddddd','ffffffff'] ),
  46. ( '+Aa', dfl_set ),
  47. ( '+ddDDD,FffffffF', dfl_set ),
  48. ( '+bBb', ['aa','bbb','ddddd','ffffffff'] ),
  49. ( '-ddddd', ['aa','ffffffff'] ),
  50. ( '-DDDDD,fFffffff', ['aa'] ),
  51. ( '-ffffffff,AA,DdDdD', [] ),
  52. ( '+aa,gG,ccC_P2', ['aa','ccc_P2','ddddd','ffffffff','gg'] ),
  53. ( '+BBB,gg-dDddD,fFffffff', ['aa','bbb','gg'] ),
  54. ( '-dDddD,fFffffff+BBB,gg', ['aa','bbb','gg'] ),
  55. ( 'aLL-Ccc_P2', [e for e in full_set if e != 'ccc_P2'] ),
  56. ( 'All-dDddd,aa', [e for e in full_set if e not in ('aa','ddddd')] ),
  57. )
  58. class dummyRPC:
  59. blockcount = tip
  60. class dummyOpt:
  61. fields = None
  62. stats = None
  63. miner_info = None
  64. header_info = None
  65. full_stats = None
  66. class unit_tests:
  67. def parse_field(self,name,ut):
  68. vmsg('{:28} => {}'.format('FULL SET:',full_set))
  69. vmsg('{:28} => {}'.format('DFL SET: ',dfl_set))
  70. b = BlocksInfo
  71. for opt,chk in fields_vecs:
  72. ret = b.parse_cslist(opt,full_set,dfl_set,'field')
  73. vmsg(f'{opt:28} => {ret}')
  74. assert ret == chk, f'{ret} != {chk}'
  75. return True
  76. def parse_rangespec(self,name,ut):
  77. b = BlocksInfo(0,dummyOpt(),dummyRPC())
  78. def test(spec,chk,foo):
  79. ret = b.parse_rangespec(spec)
  80. vmsg(f'{spec:13} => {BlocksInfo.range_data(*chk)}')
  81. assert ret == chk, f'{ret} != {chk}'
  82. for vec in range_vecs:
  83. if vec[1]:
  84. test(*vec)
  85. return True
  86. def parse_cmd_args(self,name,ut):
  87. def test(spec,foo,chk):
  88. b = BlocksInfo(spec if type(spec) == tuple else [spec],dummyOpt(),dummyRPC())
  89. ret = (b.first,b.last,b.block_list)
  90. vmsg('{:13} => {}'.format(
  91. (repr(spec) if type(spec) == tuple else spec),
  92. chk ))
  93. assert ret == chk, f'{ret} != {chk}'
  94. for vec in range_vecs:
  95. test(*vec)
  96. return True