ut_misc.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #!/usr/bin/env python3
  2. """
  3. test.modtest_d.ut_misc: miscellaneous unit tests for the MMGen suite
  4. """
  5. import re, time
  6. from collections import namedtuple
  7. from mmgen.color import yellow, blue, brown
  8. from ..include.common import vmsg
  9. class unit_tests:
  10. def format_elapsed_hr(self, name, ut, desc='function util.format_elapsed_hr()'):
  11. from mmgen.util2 import format_elapsed_hr
  12. vectors = (
  13. # t now rel_now show_secs out
  14. (1, 1, False, False, '0 minutes'),
  15. (1, 1, True, False, 'just now'),
  16. (0, -2, True, False, 'just now'),
  17. (0, -62, True, False, '1 minute in the future'),
  18. (0, -62, False, False, '1 minute (negative elapsed)'),
  19. (0, 7, False, False, '0 minutes'),
  20. (0, 7, True, False, 'just now'),
  21. (0, 60, True, False, '1 minute ago'),
  22. (0, 67, True, False, '1 minute ago'),
  23. (0, 7200, True, False, '2 hours ago'),
  24. (0, 3600 + 180 + 1, True, False, '1 hour 3 minutes ago'),
  25. (0, 3600*27 + 180 + 7, True, False, '1 day 3 hours 3 minutes ago'),
  26. (0, 3600*24*367 + 180 + 7, True, False, '367 days 3 minutes ago'),
  27. (1, 1, False, True, '0 seconds'),
  28. (1, 1, True, True, 'just now'),
  29. (0, -2, True, True, '2 seconds in the future'),
  30. (0, -62, True, True, '1 minute 2 seconds in the future'),
  31. (0, -62, False, True, '1 minute 2 seconds (negative elapsed)'),
  32. (0, 7, False, True, '7 seconds'),
  33. (0, 7, True, True, '7 seconds ago'),
  34. (0, 60, False, True, '1 minute'),
  35. (0, 67, False, True, '1 minute 7 seconds'),
  36. (0, 7200, False, True, '2 hours'),
  37. (0, 3600 + 180 + 1, False, True, '1 hour 3 minutes 1 second'),
  38. (0, 3600*27 + 180 + 7, False, True, '1 day 3 hours 3 minutes 7 seconds'),
  39. (0, 3600*24*367 + 180 + 7, True, True, '367 days 3 minutes 7 seconds ago'),
  40. )
  41. fs = ' {:7} {:9} {:<8} {}'
  42. vmsg(brown(' vectors:'))
  43. vmsg(fs.format('REL_NOW', 'SHOW_SECS', 'ELAPSED', 'OUTPUT'))
  44. for (t, now, rel_now, show_secs, out_chk) in vectors:
  45. out = format_elapsed_hr(t, now, rel_now=rel_now, show_secs=show_secs)
  46. assert out == out_chk, f'{out} != {out_chk}'
  47. vmsg(fs.format(repr(rel_now), repr(show_secs), now-t, out))
  48. vmsg(brown(' real time:'))
  49. start = time.time() - 3600 - 127
  50. ret = format_elapsed_hr(start) # test old default behavior
  51. vmsg(f' {3600 - 127:<8} {ret}')
  52. ret = format_elapsed_hr(start, show_secs=True)
  53. vmsg(f' {3600 - 127:<8} {ret}')
  54. return True
  55. def xmrwallet_uarg_info(self, name, ut, desc='dict xmrwallet.xmrwallet_uarg_info'): # WIP
  56. from mmgen.xmrwallet import uarg_info
  57. vs = namedtuple('vector_data', ['text', 'groups'])
  58. fs = '{:16} {}'
  59. vmsg(blue(' ' + fs.format('ID', 'ANNOT')))
  60. for k, v in uarg_info.items():
  61. vmsg(' ' + fs.format(k, v[0]))
  62. vectors = {
  63. 'sweep_spec': (
  64. vs('1:2', "('1', '2', None, None)"),
  65. vs('1:2,3', "('1', '2', '3', None)"),
  66. vs('1:2,3:4', "('1', '2', '3', '4')"),
  67. ),
  68. }
  69. vmsg('')
  70. for k, v in uarg_info.items():
  71. vmsg(f' {k}')
  72. if k in vectors:
  73. vmsg(f' pat: {v.pat}')
  74. vmsg( ' vectors:')
  75. for vec in vectors[k]:
  76. m = re.match(v.pat, vec.text)
  77. vmsg(f' {vec.text:10} ==> {m.groups()}')
  78. assert str(m.groups()) == vec.groups
  79. else:
  80. vmsg(yellow(' TBD'))
  81. return True
  82. def pyversion(self, name, ut, desc='class pyversion.PythonVersion'):
  83. from mmgen.pyversion import PythonVersion, python_version
  84. ver = {}
  85. fs = '{:<7} {:<9} {:<5} {}'
  86. vmsg('\n' + fs.format('Version', 'PyVersion', 'Major', 'Minor'))
  87. for k in ('current', '3.3', '3.12', '4.3', '7.0'):
  88. obj = python_version if k == 'current' else PythonVersion(k)
  89. major, minor = [int(s) for s in obj.split('.')]
  90. assert obj.major == major and obj.minor == minor
  91. vmsg(fs.format(k.upper(), obj, major, minor))
  92. ver[k] = obj
  93. vmsg('\nPerforming comparison tests:')
  94. assert ver['3.3'] == '3.3'
  95. assert ver['current'] < ver['7.0']
  96. assert ver['3.3'] < ver['4.3']
  97. assert ver['3.12'] < ver['7.0']
  98. assert ver['3.3'] < ver['3.12']
  99. assert ver['current'] < '7.0'
  100. assert ver['3.3'] < '4.3'
  101. assert ver['3.12'] < '7.0'
  102. assert ver['3.3'] < '3.12' # ensure we’re comparing numerically, not stringwise
  103. assert ver['current'] <= ver['current']
  104. assert ver['3.3'] <= '4.3'
  105. assert ver['3.12'] <= '7.0'
  106. assert ver['3.3'] <= '3.12'
  107. assert ver['current'] == ver['current']
  108. assert ver['3.3'] == ver['3.3']
  109. assert ver['3.3'] != ver['3.12']
  110. assert ver['3.3'] != ver['4.3']
  111. assert ver['3.3'] == '3.3'
  112. assert ver['3.3'] != '3.12'
  113. assert ver['3.3'] != '4.3'
  114. assert ver['current'] > '3.6'
  115. assert ver['7.0'] > ver['current']
  116. assert ver['4.3'] > '3.3'
  117. assert ver['3.12'] > '3.3'
  118. assert ver['current'] >= ver['current']
  119. assert ver['7.0'] >= ver['current']
  120. assert ver['4.3'] >= '3.3'
  121. assert ver['3.12'] >= '3.12'
  122. assert ver['3.12'] >= '3.3'
  123. assert '3.0' < ver['3.12'] < '3.13'
  124. assert '3.9' < ver['3.12'] # ensure we’re reverse comparing numerically, not stringwise
  125. assert '3.3' < ver['4.3'] <= '4.3'
  126. assert '4.3' <= ver['4.3'] <= '4.3'
  127. assert '4.3' == ver['4.3'] == '4.3'
  128. return True