From e42c404d9d5a09ab22da9776d7ba0c8f9295166f Mon Sep 17 00:00:00 2001 From: The MMGen Project Date: Fri, 18 Oct 2024 10:32:04 +0000 Subject: [PATCH] unit_tests.py obj: refactor --- test/unit_tests.py | 3 + test/unit_tests_d/ut_obj.py | 127 +++++++++++++++++++----------------- 2 files changed, 71 insertions(+), 59 deletions(-) diff --git a/test/unit_tests.py b/test/unit_tests.py index 7f2211c6..ae9aea8e 100755 --- a/test/unit_tests.py +++ b/test/unit_tests.py @@ -66,6 +66,9 @@ If no test is specified, all available tests are run } } +if os.path.islink(Config.test_datadir): + os.unlink(Config.test_datadir) + sys.argv.insert(1,'--skip-cfg-file') cfg = Config(opts_data=opts_data) diff --git a/test/unit_tests_d/ut_obj.py b/test/unit_tests_d/ut_obj.py index 987c0c03..9ee5138a 100755 --- a/test/unit_tests_d/ut_obj.py +++ b/test/unit_tests_d/ut_obj.py @@ -8,67 +8,76 @@ from decimal import Decimal from ..include.common import vmsg +def coinamt_test(cls, aa, bb, ut): + + def do(desc, res, chk): + vmsg(f'{desc:10} = {res:<{cls.max_prec+10}} [{type(res).__name__}]') + if chk is not None: + assert res == chk, f'{res} != {chk}' + assert type(res) is cls, f'{type(res).__name__} != {cls.__name__}' + + vmsg(f'\nTesting {cls.__name__} arithmetic operations...') + + A, B = (Decimal(aa), Decimal(bb)) + a, b = (cls(aa), cls(bb)) + + do('A', A, None) + do('B', B, None) + do('a', a, A) + do('b', b, B) + do('b + a', b + a, B + A) + do('sum([b,a])', sum([b,a]), B + A) + do('b - a', b - a, B - A) + do('b * a', b * a, B * A) + do('b * A', b * A, B * A) + do('B * a', B * a, B * A) + do('b / a', b / a, cls(B / A, from_decimal=True)) + do('b / A', b / A, cls(B / A, from_decimal=True)) + do('a / b', a / b, cls(A / B, from_decimal=True)) + + do('a * a / a', a * a / a, A * A / A) + do('a * b / a', a * b / a, A * B / A) + do('a * b / b', a * b / b, A * B / B) + + vmsg(f'\nChecking {cls.__name__} error handling...') + + bad_data = ( + ('negation', 'NotImplementedError', 'not implemented', lambda: -a), + ('modulus', 'NotImplementedError', 'not implemented', lambda: b % a), + ('floor division', 'NotImplementedError', 'not implemented', lambda: b // a), + ('negative result', 'ObjectInitError', 'cannot be negative', lambda: a - b), + ('operand type', 'ValueError', 'incorrect type', lambda: a + B), + ('operand type', 'ValueError', 'incorrect type', lambda: b - A), + ) + + if cls.max_amt is not None: + bad_data += ( + ('result', 'ObjectInitError', 'too large', lambda: b + b), + ('result', 'ObjectInitError', 'too large', lambda: b * b), + ) + + ut.process_bad_data(bad_data) + + vmsg('OK') + class unit_tests: - def coinamt(self, name, ut, desc='BTCAmt, LTCAmt, XMRAmt and ETHAmt classes'): + altcoin_deps = ('coinamt_alt', 'coinamt_alt2') - from mmgen.amt import BTCAmt,LTCAmt,XMRAmt,ETHAmt - - for cls,aa,bb in ( - ( BTCAmt, '1.2345', '11234567.897' ), - ( LTCAmt, '1.2345', '44938271.588' ), - ( XMRAmt, '1.2345', '11234567.98765432' ), - ( ETHAmt, '1.2345', '11234567.98765432123456' ), + def coinamt(self, name, ut, desc='BTCAmt class'): + from mmgen.amt import BTCAmt + for cls, aa, bb in ( + (BTCAmt, '1.2345', '11234567.897'), ): - - def do(desc,res,chk): - vmsg(f'{desc:10} = {res:<{cls.max_prec+10}} [{type(res).__name__}]') - if chk is not None: - assert res == chk, f'{res} != {chk}' - assert type(res) is cls, f'{type(res).__name__} != {cls.__name__}' - - vmsg(f'\nTesting {cls.__name__} arithmetic operations...') - - A,B = ( Decimal(aa), Decimal(bb) ) - a,b = ( cls(aa), cls(bb) ) - - do('A', A, None) - do('B', B, None) - do('a', a, A) - do('b', b, B) - do('b + a', b + a, B + A) - do('sum([b,a])', sum([b,a]), B + A) - do('b - a', b - a, B - A) - do('b * a', b * a, B * A) - do('b * A', b * A, B * A) - do('B * a', B * a, B * A) - do('b / a', b / a, cls( B / A, from_decimal=True )) - do('b / A', b / A, cls( B / A, from_decimal=True )) - do('a / b', a / b, cls( A / B, from_decimal=True )) - - do('a * a / a', a * a / a, A * A / A) - do('a * b / a', a * b / a, A * B / A) - do('a * b / b', a * b / b, A * B / B) - - vmsg(f'\nChecking {cls.__name__} error handling...') - - bad_data = ( - ('negation', 'NotImplementedError', 'not implemented', lambda: -a ), - ('modulus', 'NotImplementedError', 'not implemented', lambda: b % a ), - ('floor division', 'NotImplementedError', 'not implemented', lambda: b // a ), - ('negative result', 'ObjectInitError', 'cannot be negative', lambda: a - b ), - ('operand type', 'ValueError', 'incorrect type', lambda: a + B ), - ('operand type', 'ValueError', 'incorrect type', lambda: b - A ), - ) - - if cls.max_amt is not None: - bad_data += ( - ('result', 'ObjectInitError', 'too large', lambda: b + b ), - ('result', 'ObjectInitError', 'too large', lambda: b * b ), - ) - - ut.process_bad_data(bad_data) - - vmsg('OK') - + coinamt_test(cls, aa, bb, ut) + return True + + def coinamt_alt(self, name, ut, desc='LTCAmt, XMRAmt and ETHAmt classes'): + from mmgen.amt import LTCAmt, XMRAmt, ETHAmt + for cls, aa, bb in ( + (LTCAmt, '1.2345', '44938271.588'), + (XMRAmt, '1.2345', '11234567.98765432'), + (ETHAmt, '1.2345', '11234567.98765432123456'), + ): + coinamt_test(cls, aa, bb, ut) return True