ut_obj.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #!/usr/bin/env python3
  2. """
  3. test.unit_tests_d.ut_obj: data object unit tests for the MMGen suite
  4. """
  5. from decimal import Decimal
  6. from mmgen.common import *
  7. from ..include.common import qmsg,qmsg_r,vmsg
  8. class unit_tests:
  9. def coinamt(self,name,ut):
  10. from mmgen.amt import BTCAmt,LTCAmt,XMRAmt,ETHAmt
  11. for cls,aa,bb in (
  12. ( BTCAmt, '1.2345', '11234567.897' ),
  13. ( LTCAmt, '1.2345', '44938271.588' ),
  14. ( XMRAmt, '1.2345', '11234567.98765432' ),
  15. ( ETHAmt, '1.2345', '11234567.98765432123456' ),
  16. ):
  17. def do(desc,res,chk):
  18. vmsg(f'{desc:10} = {res:<{cls.max_prec+10}} [{type(res).__name__}]')
  19. if chk is not None:
  20. assert res == chk, f'{res} != {chk}'
  21. assert type(res) == cls, f'{type(res).__name__} != {cls.__name__}'
  22. qmsg_r(f'Testing {cls.__name__} arithmetic operations...')
  23. vmsg('')
  24. A,B = ( Decimal(aa), Decimal(bb) )
  25. a,b = ( cls(aa), cls(bb) )
  26. do('A', A, None)
  27. do('B', B, None)
  28. do('a', a, A)
  29. do('b', b, B)
  30. do('b + a', b + a, B + A)
  31. do('sum([b,a])', sum([b,a]), B + A)
  32. do('b - a', b - a, B - A)
  33. do('b * a', b * a, B * A)
  34. do('b * A', b * A, B * A)
  35. do('B * a', B * a, B * A)
  36. do('b / a', b / a, cls( B / A, from_decimal=True ))
  37. do('b / A', b / A, cls( B / A, from_decimal=True ))
  38. do('a / b', a / b, cls( A / B, from_decimal=True ))
  39. do('a * a / a', a * a / a, A * A / A)
  40. do('a * b / a', a * b / a, A * B / A)
  41. do('a * b / b', a * b / b, A * B / B)
  42. qmsg('OK')
  43. qmsg_r(f'Checking {cls.__name__} error handling...')
  44. vmsg('')
  45. bad_data = (
  46. ('negation', 'NotImplementedError', 'not implemented', lambda: -a ),
  47. ('modulus', 'NotImplementedError', 'not implemented', lambda: b % a ),
  48. ('floor division', 'NotImplementedError', 'not implemented', lambda: b // a ),
  49. ('negative result', 'ObjectInitError', 'cannot be negative', lambda: a - b ),
  50. ('operand type', 'ValueError', 'incorrect type', lambda: a + B ),
  51. ('operand type', 'ValueError', 'incorrect type', lambda: b - A ),
  52. )
  53. if cls.max_amt is not None:
  54. bad_data += (
  55. ('result', 'ObjectInitError', 'too large', lambda: b + b ),
  56. ('result', 'ObjectInitError', 'too large', lambda: b * b ),
  57. )
  58. ut.process_bad_data(bad_data)
  59. qmsg('OK')
  60. return True