| 1 |
"""Tests for DisplayCAL.util_decimal module.""" |
| 2 |
|
| 3 |
# Standard library imports |
| 4 |
import decimal |
| 5 |
|
| 6 |
|
| 7 |
# Third-party Imports |
| 8 |
import pytest |
| 9 |
|
| 10 |
# Local Imports |
| 11 |
from DisplayCAL.util_decimal import float2dec, stripzeros |
| 12 |
|
| 13 |
|
| 14 |
@pytest.mark.parametrize( |
| 15 |
"input_value,expected", |
| 16 |
[ |
| 17 |
(1.5, decimal.Decimal("1.5")), |
| 18 |
(1.9999999999, decimal.Decimal("2")), |
| 19 |
(1.0000000000, decimal.Decimal("1")), |
| 20 |
(5.0, decimal.Decimal("5")), |
| 21 |
(-3.5, decimal.Decimal("-3.5")), |
| 22 |
(-1.9999999999, decimal.Decimal("-1")), |
| 23 |
(1.123456789, decimal.Decimal("1.123456789")), |
| 24 |
] |
| 25 |
) |
| 26 |
def test_float2dec(input_value, expected): |
| 27 |
"""Test conversion of a simple float.""" |
| 28 |
assert float2dec(input_value) == expected |
| 29 |
|
| 30 |
|
| 31 |
@pytest.mark.parametrize( |
| 32 |
"input_value,digits,expected", |
| 33 |
[ |
| 34 |
(1.999, 3, decimal.Decimal("2")), |
| 35 |
(1.000, 3, decimal.Decimal("1")), |
| 36 |
(1.99, 2, decimal.Decimal("2")), |
| 37 |
] |
| 38 |
) |
| 39 |
def test_float2dec_with_custom_digits(input_value, digits, expected): |
| 40 |
"""Test with custom digit precision.""" |
| 41 |
assert float2dec(input_value, digits=digits) == expected |
| 42 |
|
| 43 |
|
| 44 |
@pytest.mark.parametrize( |
| 45 |
"input_value,expected", [ |
| 46 |
(1.0, decimal.Decimal("1")), |
| 47 |
(1.234567890, decimal.Decimal("1.23456789")), |
| 48 |
("1.2000", decimal.Decimal("1.2")), |
| 49 |
("1.234", decimal.Decimal("1.234")), |
| 50 |
(decimal.Decimal("1.5000"), decimal.Decimal("1.5")), |
| 51 |
(5.0, decimal.Decimal("5")), |
| 52 |
("123", decimal.Decimal("123")), |
| 53 |
(-3.5000, decimal.Decimal("-3.5")), |
| 54 |
(0.0001, decimal.Decimal("0.0001")), |
| 55 |
(0.0, decimal.Decimal("0")), |
| 56 |
(42, decimal.Decimal("42")), |
| 57 |
("1.23e-4", decimal.Decimal("0.000123")), |
| 58 |
|
| 59 |
] |
| 60 |
) |
| 61 |
def test_stripzeros(input_value, expected): |
| 62 |
"""Test stripping trailing zeros from float.""" |
| 63 |
assert stripzeros(input_value) == expected |
| 64 |
|
| 65 |
|
| 66 |
def test_stripzeros_invalid_string(): |
| 67 |
"""Test with invalid string that cannot be converted.""" |
| 68 |
result = stripzeros("invalid") |
| 69 |
assert result == "invalid" |