diff options
-rw-r--r-- | src/ee/__init__.py | 2 | ||||
-rw-r--r-- | test/test_EeVal.py | 45 | ||||
-rw-r--r-- | test/test_formatting.py | 43 |
3 files changed, 46 insertions, 44 deletions
diff --git a/src/ee/__init__.py b/src/ee/__init__.py index 7fa2243..2fad46d 100644 --- a/src/ee/__init__.py +++ b/src/ee/__init__.py @@ -46,7 +46,7 @@ class EeVal(object): exp = gs[1].strip() exp = EeVal.exponents.get(exp) if len(exp) > 0 else 0 unit = gs[2] - if value < 1: + if value != 0 and value < 1: e = math.ceil(math.log10(value)) exp = exp + e value = value * math.pow(10, -e) diff --git a/test/test_EeVal.py b/test/test_EeVal.py index b96e089..6e15ff0 100644 --- a/test/test_EeVal.py +++ b/test/test_EeVal.py @@ -1,5 +1,50 @@ +import pytest from ee import EeVal +@pytest.mark.parametrize("s, num_value, str_value, unit", [ + ("0", 0, "0", None), + ("0 k", 0, "0", None), + ("1", 1, "1", None), + ("10", 10, "10", None), + ("10k", 10 * 1000, "10 k", None), + ("10 k", 10 * 1000, "10 k", None), + ("10 k", 10 * 1000, "10 k", None), + ("1000 m", 1, "1", None), + ("100 m", 0.1, "100 m", None), + ("100 n", 0.0000001, "100 n", None), + ("1 m", 0.001, "1 m", None), + ("1 u", 0.000001, "1 u", None), + ("0.1 u", 0.0000001, "100 n", None), + (".1 u", 0.0000001, "100 n", None), + ("4.7 u", 0.0000047, "4.7 u", None), + ("1 µ", 0.000001, "1 u", None), + ("1 n", 0.000000001, "1 n", None), + ("10 n", 0.00000001, "10 n", None), + ("1p", 1e-12, "1 p", None), + ("0.8p", 8e-13, "0.8 p", None), + ("0.008p", 8e-15, "0.008 p", None), + ]) +def test_basic(s, num_value, str_value, unit): + num_value = float(num_value) + v = EeVal(s) + assert float(v) == v.value + epsilon = num_value * 0.01 + assert (num_value + epsilon) >= float(v) and (num_value - epsilon) <= float(v) + assert str_value == str(v) + assert unit == v.unit + +@pytest.mark.parametrize("s, str_value, unit", [ + ("10nF", "10 nF", "F"), + ("10n F", "10 nF", "F"), + ("10 n F", "10 nF", "F"), + ("4.7 n F", "4.7 nF", "F"), + ]) +def test_units(s, str_value, unit): + v = EeVal(s) + assert unit == v.unit + assert float(v) == v.value + assert str_value == str(v) + def test_ordering(): p100 = EeVal('100 p') n100 = EeVal('100 n') diff --git a/test/test_formatting.py b/test/test_formatting.py index bcb9300..4b5d1fe 100644 --- a/test/test_formatting.py +++ b/test/test_formatting.py @@ -61,46 +61,3 @@ def xx_test_eng_str2(input, expected): ]) def xx_test_eng_str3(input, expected): assert expected == eng_str(input) - - -@pytest.mark.parametrize("s, num_value, str_value, unit", [ - ("1", 1, "1", None), - ("10", 10, "10", None), - ("10k", 10 * 1000, "10 k", None), - ("10 k", 10 * 1000, "10 k", None), - ("10 k", 10 * 1000, "10 k", None), - ("1000 m", 1, "1", None), - ("100 m", 0.1, "100 m", None), - ("100 n", 0.0000001, "100 n", None), - ("1 m", 0.001, "1 m", None), - ("1 u", 0.000001, "1 u", None), - ("0.1 u", 0.0000001, "100 n", None), - (".1 u", 0.0000001, "100 n", None), - ("4.7 u", 0.0000047, "4.7 u", None), - ("1 µ", 0.000001, "1 u", None), - ("1 n", 0.000000001, "1 n", None), - ("10 n", 0.00000001, "10 n", None), - ("1p", 1e-12, "1 p", None), - ("0.8p", 8e-13, "0.8 p", None), - ("0.008p", 8e-15, "0.008 p", None), - ]) -def test_EeVal(s, num_value, str_value, unit): - num_value = float(num_value) - v = EeVal(s) - assert float(v) == v.value - epsilon = num_value * 0.01 - assert (num_value + epsilon) >= float(v) and (num_value - epsilon) <= float(v) - assert str_value == str(v) - assert unit == v.unit - -@pytest.mark.parametrize("s, str_value, unit", [ - ("10nF", "10 nF", "F"), - ("10n F", "10 nF", "F"), - ("10 n F", "10 nF", "F"), - ("4.7 n F", "4.7 nF", "F"), - ]) -def test_EeVal_with_units(s, str_value, unit): - v = EeVal(s) - assert unit == v.unit - assert float(v) == v.value - assert str_value == str(v) |