ut_obj.py 2.2 KB

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