ut_BlocksInfo.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. def info(self,arg):
  61. return True
  62. class proto:
  63. class coin_amt:
  64. satoshi = 0.00000001
  65. class dummyOpt:
  66. fields = None
  67. stats = None
  68. miner_info = None
  69. header_info = None
  70. full_stats = None
  71. class unit_tests:
  72. def parse_field(self,name,ut):
  73. vmsg('{:28} => {}'.format('FULL SET:',full_set))
  74. vmsg('{:28} => {}'.format('DFL SET: ',dfl_set))
  75. b = BlocksInfo
  76. for opt,chk in fields_vecs:
  77. ret = b.parse_cslist(opt,full_set,dfl_set,'field')
  78. vmsg(f'{opt:28} => {ret}')
  79. assert ret == chk, f'{ret} != {chk}'
  80. return True
  81. def parse_rangespec(self,name,ut):
  82. b = BlocksInfo(0,dummyOpt(),dummyRPC())
  83. def test(spec,chk,foo):
  84. ret = b.parse_rangespec(spec)
  85. vmsg(f'{spec:13} => {BlocksInfo.range_data(*chk)}')
  86. assert ret == chk, f'{ret} != {chk}'
  87. for vec in range_vecs:
  88. if vec[1]:
  89. test(*vec)
  90. return True
  91. def parse_cmd_args(self,name,ut):
  92. def test(spec,foo,chk):
  93. b = BlocksInfo(spec if type(spec) == tuple else [spec],dummyOpt(),dummyRPC())
  94. ret = (b.first,b.last,b.block_list)
  95. vmsg('{:13} => {}'.format(
  96. (repr(spec) if type(spec) == tuple else spec),
  97. chk ))
  98. assert ret == chk, f'{ret} != {chk}'
  99. for vec in range_vecs:
  100. test(*vec)
  101. return True