Skip to content

Commit

Permalink
Add test for time conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
Ectras committed Apr 19, 2024
1 parent 7df1d43 commit 233ef5e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/utils/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
25 changes: 25 additions & 0 deletions tests/utils/test_helpers.py
Original file line number Diff line number Diff line change
@@ -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")

0 comments on commit 233ef5e

Please sign in to comment.