diff --git a/src/utils/helpers.py b/src/utils/helpers.py index 0e7080d..89a8468 100644 --- a/src/utils/helpers.py +++ b/src/utils/helpers.py @@ -35,17 +35,19 @@ def time_conversion( Returns: float: The time in the target unit. """ + # Same unit, no conversion needed if unit == target_unit: return time units = ["s", "ms", "us", "ns", "ps"] - # target_unit must be a SI unit - assert target_unit in units + assert target_unit in units, "Target unit must be a SI unit" # Convert dt (device-dependent time) to SI unit if unit == "dt": - assert dt is not None + assert ( + dt is not None + ), "If the target unit is dt, the time of 1dt must be provided" time *= dt unit = "s" diff --git a/tests/utils/test_helpers.py b/tests/utils/test_helpers.py new file mode 100644 index 0000000..295f756 --- /dev/null +++ b/tests/utils/test_helpers.py @@ -0,0 +1,25 @@ +from pytest import approx, raises +from src.utils.helpers import time_conversion + + +def test_time_conversion(): + assert time_conversion(1, "s", "s") == 1 + assert time_conversion(5, "s", "ms") == 5000 + assert time_conversion(2, "s", "us") == 2000000 + assert time_conversion(3, "s", "ns") == 3000000000 + assert time_conversion(2.4, "ms", "s") == approx(0.0024) + assert time_conversion(9.1, "ms", "us") == 9100 + assert time_conversion(7.8, "ms", "ns") == 7800000 + assert time_conversion(3, "dt", "s", dt=1.5) == approx(4.5) + assert time_conversion(4, "dt", "ms", dt=2) == 8000 + assert time_conversion(3, "dt", "dt", dt=1) == 3 + + +def test_time_conversion_errors(): + with raises(Exception) as e_info: + time_conversion(1, "s", "dt", dt=5) + assert e_info.match("Target unit must be a SI unit") + + with raises(Exception) as e_info: + time_conversion(1, "dt", "s") + assert e_info.match("dt must be provided")