ut_amt.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #!/usr/bin/env python3
  2. """
  3. test.modtest_d.ut_amt: CoinAmt unit tests for the MMGen suite
  4. """
  5. from decimal import Decimal
  6. from mmgen.protocol import init_proto
  7. from mmgen.cfg import Config
  8. from ..include.common import cfg, vmsg
  9. def get_protos(data):
  10. return {coin: init_proto(cfg, coin, need_amt=True) for coin in set(d[0] for d in data)}
  11. def test_to_unit(data):
  12. protos = get_protos(data)
  13. for proto, amt, unit, chk in data:
  14. amt = protos[proto].coin_amt(amt)
  15. res = amt.to_unit(unit)
  16. vmsg(f' {proto.upper()} {amt.fmt(8)} => {res:<14} {unit}')
  17. if '.' in chk:
  18. assert res == Decimal(chk), f'{res} != {Decimal(chk)}'
  19. else:
  20. assert res == int(chk), f'{res} != {int(chk)}'
  21. return True
  22. class unit_tests:
  23. altcoin_deps = ('to_unit_alt',)
  24. def to_unit(self, name, ut, desc='CoinAmt.to_unit() (BTC)'):
  25. return test_to_unit((
  26. ('btc', '0.00000001', 'satoshi', '1'),
  27. ('btc', '1.23456789', 'satoshi', '123456789')))
  28. def to_unit_alt(self, name, ut, desc='CoinAmt.to_unit() (LTC, BCH, ETH, XMR)'):
  29. return test_to_unit((
  30. ('ltc', '0.00000001', 'satoshi', '1'),
  31. ('ltc', '1.23456789', 'satoshi', '123456789'),
  32. ('bch', '0.00000001', 'satoshi', '1'),
  33. ('bch', '1.23456789', 'satoshi', '123456789'),
  34. ('eth', '1.234567890123456789', 'wei', '1234567890123456789'),
  35. ('eth', '1.234567890123456789', 'Kwei', '1234567890123456.789'),
  36. ('eth', '1.234567890123456789', 'Mwei', '1234567890123.456789'),
  37. ('eth', '1.234567890123456789', 'finney', '1234.567890123456789'),
  38. ('eth', '0.000000012345678901', 'Mwei', '12345.678901'),
  39. ('eth', '0.000000000000000001', 'Kwei', '0.001'),
  40. ('eth', '0.000000000000000001', 'Gwei', '0.000000001'),
  41. ('eth', '0.00000001', 'Gwei', '10'),
  42. ('eth', '1', 'Gwei', '1000000000'),
  43. ('eth', '1', 'finney', '1000'),
  44. ('xmr', '1', 'atomic', '1000000000000'),
  45. ('xmr', '0.000000000001', 'atomic', '1'),
  46. ('xmr', '1.234567890123', 'atomic', '1234567890123')))