2017-11-25 22:59:32 +00:00
|
|
|
from decimal import Decimal
|
2019-04-04 11:06:04 +00:00
|
|
|
import sys
|
2017-11-25 22:59:32 +00:00
|
|
|
import unittest
|
|
|
|
|
2018-01-30 08:43:08 +00:00
|
|
|
from monero.numbers import to_atomic, from_atomic, as_monero, PaymentID
|
2017-11-25 22:59:32 +00:00
|
|
|
|
|
|
|
class NumbersTestCase(unittest.TestCase):
|
|
|
|
def test_simple_numbers(self):
|
|
|
|
self.assertEqual(to_atomic(Decimal('0')), 0)
|
|
|
|
self.assertEqual(from_atomic(0), Decimal('0'))
|
|
|
|
self.assertEqual(to_atomic(Decimal('1')), 1000000000000)
|
|
|
|
self.assertEqual(from_atomic(1000000000000), Decimal('1'))
|
|
|
|
self.assertEqual(to_atomic(Decimal('0.000000000001')), 1)
|
|
|
|
self.assertEqual(from_atomic(1), Decimal('0.000000000001'))
|
|
|
|
|
2019-04-04 11:06:04 +00:00
|
|
|
def test_numeric_types(self):
|
|
|
|
"Only check if conversion of given type succeeds or fails."
|
|
|
|
self.assertTrue(to_atomic(1))
|
|
|
|
self.assertTrue(to_atomic(1.0))
|
|
|
|
if hasattr(sys, 'maxint'): # Python 2.x
|
|
|
|
self.assertTrue(to_atomic(sys.maxint + 1))
|
|
|
|
self.assertRaises(ValueError, to_atomic, '1')
|
|
|
|
|
2017-11-25 22:59:32 +00:00
|
|
|
def test_rounding(self):
|
|
|
|
self.assertEqual(to_atomic(Decimal('1.0000000000004')), 1000000000000)
|
2018-01-30 08:43:08 +00:00
|
|
|
self.assertEqual(as_monero(Decimal('1.0000000000014')), Decimal('1.000000000001'))
|
2017-12-27 00:49:59 +00:00
|
|
|
|
|
|
|
def test_payment_id(self):
|
2018-01-06 22:12:42 +00:00
|
|
|
pid = PaymentID('0')
|
|
|
|
self.assertTrue(pid.is_short())
|
|
|
|
self.assertEqual(pid, 0)
|
|
|
|
self.assertEqual(pid, '0000000000000000')
|
|
|
|
self.assertEqual(PaymentID(pid), pid)
|
2018-01-30 08:43:08 +00:00
|
|
|
self.assertNotEqual(pid, None)
|
2018-01-06 22:12:42 +00:00
|
|
|
pid = PaymentID('abcdef')
|
|
|
|
self.assertTrue(pid.is_short())
|
|
|
|
self.assertEqual(pid, 0xabcdef)
|
|
|
|
self.assertEqual(pid, '0000000000abcdef')
|
|
|
|
self.assertEqual(PaymentID(pid), pid)
|
|
|
|
pid = PaymentID('1234567812345678')
|
|
|
|
self.assertTrue(pid.is_short())
|
|
|
|
self.assertEqual(pid, 0x1234567812345678)
|
|
|
|
self.assertEqual(pid, '1234567812345678')
|
|
|
|
self.assertEqual(PaymentID(pid), pid)
|
|
|
|
pid = PaymentID('a1234567812345678')
|
|
|
|
self.assertFalse(pid.is_short())
|
|
|
|
self.assertEqual(pid, 0xa1234567812345678)
|
|
|
|
self.assertEqual(pid, '00000000000000000000000000000000000000000000000a1234567812345678')
|
|
|
|
self.assertEqual(PaymentID(pid), pid)
|
2018-01-07 00:26:30 +00:00
|
|
|
self.assertRaises(ValueError, PaymentID, 2**256+1)
|