Coverage for tests/common/monero_utils/test_amounts.py: 100%
57 statements
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-22 19:49 +0000
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-22 19:49 +0000
1from __future__ import annotations
3import logging
5import pytest
6from monero import MoneroUtils
8from utils import BaseTestClass
10logger: logging.Logger = logging.getLogger("TestMoneroUtilsAmounts")
13@pytest.mark.unit
14class TestMoneroUtilsAmounts(BaseTestClass):
15 """Monero utilities amounts unit tests."""
17 # Can convert between XMR and atomic units
18 def test_atomic_unit_conversion(self) -> None:
19 assert 1000000000000 == MoneroUtils.xmr_to_atomic_units(1)
20 assert 1 == MoneroUtils.atomic_units_to_xmr(1000000000000)
21 assert 1000000000 == MoneroUtils.xmr_to_atomic_units(0.001)
22 assert 0.001 == MoneroUtils.atomic_units_to_xmr(1000000000)
23 assert 250000000000 == MoneroUtils.xmr_to_atomic_units(0.25)
24 assert 0.25 == MoneroUtils.atomic_units_to_xmr(250000000000)
25 assert 1250000000000 == MoneroUtils.xmr_to_atomic_units(1.25)
26 assert 1.25 == MoneroUtils.atomic_units_to_xmr(1250000000000)
27 assert 2796726190000 == MoneroUtils.xmr_to_atomic_units(2.79672619)
28 assert 2.79672619 == MoneroUtils.atomic_units_to_xmr(2796726190000)
29 assert 2796726190001 == MoneroUtils.xmr_to_atomic_units(2.796726190001)
30 assert 2.796726190001 == MoneroUtils.atomic_units_to_xmr(2796726190001)
31 assert 2796726189999 == MoneroUtils.xmr_to_atomic_units(2.796726189999)
32 assert 2.796726189999 == MoneroUtils.atomic_units_to_xmr(2796726189999)
33 assert 2796726180000 == MoneroUtils.xmr_to_atomic_units(2.79672618)
34 assert 2.79672618 == MoneroUtils.atomic_units_to_xmr(2796726180000)
36 # xmr_to_atomic_units(0.0) and negative zero are valid, not errors
37 def test_xmr_to_atomic_units_zero(self) -> None:
38 assert 0 == MoneroUtils.xmr_to_atomic_units(0.0)
39 # -0.0 < 0 is False in IEEE 754, so it must not be rejected by the
40 # "amount must be non-negative" check
41 assert 0 == MoneroUtils.xmr_to_atomic_units(-0.0)
43 # Amounts smaller than half an atomic unit (1e-12 XMR) round down to 0
44 def test_xmr_to_atomic_units_rounds_down_to_zero(self) -> None:
45 assert 0 == MoneroUtils.xmr_to_atomic_units(1e-13)
46 assert 0 == MoneroUtils.xmr_to_atomic_units(4e-13)
48 # xmr_to_atomic_units() rounds the underlying long double * 1e12 product to
49 # the nearest atomic unit (away from zero on an exact .5). Decimal literals
50 # ending in .5e-12 are not necessarily exact halves once represented as a
51 # double, so which way they round depends on whether the closest double is
52 # a hair above or below the nominal decimal value, and that itself can
53 # depend on the platform's "long double" precision (80-bit extended on
54 # x86_64 Linux/glibc, but identical to a plain 64-bit double on Apple
55 # Silicon/macOS), so the same literal can round differently on different
56 # platforms. This is inherent to IEEE 754, not an inconsistency in
57 # xmr_to_atomic_units() itself, assert the result lands on one of the
58 # two atomic units the value sits between, not a specific platform's tie-break.
59 def test_xmr_to_atomic_units_half_atomic_unit_rounding(self) -> None:
60 cases: list[tuple[float, int, int]] = [
61 (0.5e-12, 0, 1),
62 (1.5e-12, 1, 2),
63 (2.5e-12, 2, 3),
64 (3.5e-12, 3, 4),
65 ]
66 for amount_xmr, floor_atomic, ceil_atomic in cases:
67 actual: int = MoneroUtils.xmr_to_atomic_units(amount_xmr)
68 logger.debug(f"xmr_to_atomic_units({amount_xmr!r}) = {actual} (expected {floor_atomic} or {ceil_atomic})")
69 assert actual in (floor_atomic, ceil_atomic), f"xmr_to_atomic_units({amount_xmr!r}) == {actual}, expected {floor_atomic} or {ceil_atomic}"
71 # amount_xmr must be finite and non-negative
72 @pytest.mark.parametrize("amount_xmr", [-1.0, -0.0000000001, float("nan"), float("inf"), float("-inf")])
73 def test_xmr_to_atomic_units_invalid_amount(self, amount_xmr: float) -> None:
74 with pytest.raises(RuntimeError, match="amount must be a finite, non-negative number"):
75 MoneroUtils.xmr_to_atomic_units(amount_xmr)
77 # amounts whose rounded atomic-unit value would overflow uint64_t are rejected
78 # rather than silently wrapping or invoking undefined behavior on the cast
79 def test_xmr_to_atomic_units_overflow(self) -> None:
80 # UINT64_MAX atomic units is ~18446744.0737... XMR; comfortably over that
81 # (with margin for float imprecision) must raise
82 with pytest.raises(RuntimeError, match="amount exceeds maximum representable atomic units"):
83 MoneroUtils.xmr_to_atomic_units(18446745.0)
84 with pytest.raises(RuntimeError, match="amount exceeds maximum representable atomic units"):
85 MoneroUtils.xmr_to_atomic_units(2e22)
87 # values comfortably below the uint64_t boundary succeed
88 def test_xmr_to_atomic_units_near_uint64_max_boundary(self) -> None:
89 uint64_max: int = 2 ** 64 - 1
90 boundary_xmr: float = uint64_max / 1e12 # ~18446744.073709551615 XMR
91 margin_xmr: float = 1.0
93 safely_below: float = boundary_xmr - margin_xmr
94 below: int = MoneroUtils.xmr_to_atomic_units(safely_below)
95 logger.debug(f"xmr_to_atomic_units({safely_below!r}) = {below} (uint64_max = {uint64_max})")
96 assert below <= uint64_max
98 safely_above: float = boundary_xmr + margin_xmr
99 with pytest.raises(RuntimeError, match="amount exceeds maximum representable atomic units"):
100 MoneroUtils.xmr_to_atomic_units(safely_above)