ut_obj.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 vmsg
  7. class unit_tests:
  8. def coinamt(self, name, ut, desc='BTCAmt, LTCAmt, XMRAmt and ETHAmt classes'):
  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. vmsg(f'\nTesting {cls.__name__} arithmetic operations...')
  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. vmsg(f'\nChecking {cls.__name__} error handling...')
  41. bad_data = (
  42. ('negation', 'NotImplementedError', 'not implemented', lambda: -a ),
  43. ('modulus', 'NotImplementedError', 'not implemented', lambda: b % a ),
  44. ('floor division', 'NotImplementedError', 'not implemented', lambda: b // a ),
  45. ('negative result', 'ObjectInitError', 'cannot be negative', lambda: a - b ),
  46. ('operand type', 'ValueError', 'incorrect type', lambda: a + B ),
  47. ('operand type', 'ValueError', 'incorrect type', lambda: b - A ),
  48. )
  49. if cls.max_amt is not None:
  50. bad_data += (
  51. ('result', 'ObjectInitError', 'too large', lambda: b + b ),
  52. ('result', 'ObjectInitError', 'too large', lambda: b * b ),
  53. )
  54. ut.process_bad_data(bad_data)
  55. vmsg('OK')
  56. return True