ut_obj.py 2.2 KB

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