1- from codeflash .code_utils .time_utils import humanize_runtime
1+ from codeflash .code_utils .time_utils import humanize_runtime , format_time
2+ import pytest
23
34
45def test_humanize_runtime ():
@@ -28,3 +29,147 @@ def test_humanize_runtime():
2829 assert humanize_runtime (12345678912345 ) == "3.43 hours"
2930 assert humanize_runtime (98765431298760 ) == "1.14 days"
3031 assert humanize_runtime (197530862597520 ) == "2.29 days"
32+
33+
34+ class TestFormatTime :
35+ """Test cases for the format_time function."""
36+
37+ def test_nanoseconds_range (self ):
38+ """Test formatting for nanoseconds (< 1,000 ns)."""
39+ assert format_time (0 ) == "0ns"
40+ assert format_time (1 ) == "1ns"
41+ assert format_time (500 ) == "500ns"
42+ assert format_time (999 ) == "999ns"
43+
44+ def test_microseconds_range (self ):
45+ """Test formatting for microseconds (1,000 ns to 999,999 ns)."""
46+ # Integer microseconds >= 100
47+ # assert format_time(100_000) == "100μs"
48+ # assert format_time(500_000) == "500μs"
49+ # assert format_time(999_000) == "999μs"
50+
51+ # Decimal microseconds with varying precision
52+ assert format_time (1_000 ) == "1.00μs" # 1.0 μs, 2 decimal places
53+ assert format_time (1_500 ) == "1.50μs" # 1.5 μs, 2 decimal places
54+ assert format_time (9_999 ) == "10.00μs" # 9.999 μs rounds to 10.00
55+ assert format_time (10_000 ) == "10.0μs" # 10.0 μs, 1 decimal place
56+ assert format_time (15_500 ) == "15.5μs" # 15.5 μs, 1 decimal place
57+ assert format_time (99_900 ) == "99.9μs" # 99.9 μs, 1 decimal place
58+
59+ def test_milliseconds_range (self ):
60+ """Test formatting for milliseconds (1,000,000 ns to 999,999,999 ns)."""
61+ # Integer milliseconds >= 100
62+ assert format_time (100_000_000 ) == "100ms"
63+ assert format_time (500_000_000 ) == "500ms"
64+ assert format_time (999_000_000 ) == "999ms"
65+
66+ # Decimal milliseconds with varying precision
67+ assert format_time (1_000_000 ) == "1.00ms" # 1.0 ms, 2 decimal places
68+ assert format_time (1_500_000 ) == "1.50ms" # 1.5 ms, 2 decimal places
69+ assert format_time (9_999_000 ) == "10.00ms" # 9.999 ms rounds to 10.00
70+ assert format_time (10_000_000 ) == "10.0ms" # 10.0 ms, 1 decimal place
71+ assert format_time (15_500_000 ) == "15.5ms" # 15.5 ms, 1 decimal place
72+ assert format_time (99_900_000 ) == "99.9ms" # 99.9 ms, 1 decimal place
73+
74+ def test_seconds_range (self ):
75+ """Test formatting for seconds (>= 1,000,000,000 ns)."""
76+ # Integer seconds >= 100
77+ assert format_time (100_000_000_000 ) == "100s"
78+ assert format_time (500_000_000_000 ) == "500s"
79+ assert format_time (999_000_000_000 ) == "999s"
80+
81+ # Decimal seconds with varying precision
82+ assert format_time (1_000_000_000 ) == "1.00s" # 1.0 s, 2 decimal places
83+ assert format_time (1_500_000_000 ) == "1.50s" # 1.5 s, 2 decimal places
84+ assert format_time (9_999_000_000 ) == "10.00s" # 9.999 s rounds to 10.00
85+ assert format_time (10_000_000_000 ) == "10.0s" # 10.0 s, 1 decimal place
86+ assert format_time (15_500_000_000 ) == "15.5s" # 15.5 s, 1 decimal place
87+ assert format_time (99_900_000_000 ) == "99.9s" # 99.9 s, 1 decimal place
88+
89+ def test_boundary_values (self ):
90+ """Test exact boundary values between units."""
91+ # Boundaries between nanoseconds and microseconds
92+ assert format_time (999 ) == "999ns"
93+ assert format_time (1_000 ) == "1.00μs"
94+
95+ # Boundaries between microseconds and milliseconds
96+ assert format_time (999_999 ) == "999μs" # This might round to 1000.00μs
97+ assert format_time (1_000_000 ) == "1.00ms"
98+
99+ # Boundaries between milliseconds and seconds
100+ assert format_time (999_999_999 ) == "999ms" # This might round to 1000.00ms
101+ assert format_time (1_000_000_000 ) == "1.00s"
102+
103+ def test_precision_boundaries (self ):
104+ """Test precision changes at significant digit boundaries."""
105+ # Microseconds precision changes
106+ assert format_time (9_950 ) == "9.95μs" # 2 decimal places
107+ assert format_time (10_000 ) == "10.0μs" # 1 decimal place
108+ assert format_time (99_900 ) == "99.9μs" # 1 decimal place
109+ assert format_time (100_000 ) == "100μs" # No decimal places
110+
111+ # Milliseconds precision changes
112+ assert format_time (9_950_000 ) == "9.95ms" # 2 decimal places
113+ assert format_time (10_000_000 ) == "10.0ms" # 1 decimal place
114+ assert format_time (99_900_000 ) == "99.9ms" # 1 decimal place
115+ assert format_time (100_000_000 ) == "100ms" # No decimal places
116+
117+ # Seconds precision changes
118+ assert format_time (9_950_000_000 ) == "9.95s" # 2 decimal places
119+ assert format_time (10_000_000_000 ) == "10.0s" # 1 decimal place
120+ assert format_time (99_900_000_000 ) == "99.9s" # 1 decimal place
121+ assert format_time (100_000_000_000 ) == "100s" # No decimal places
122+
123+ def test_rounding_behavior (self ):
124+ """Test rounding behavior for edge cases."""
125+ # Test rounding in microseconds
126+ assert format_time (1_234 ) == "1.23μs"
127+ assert format_time (1_235 ) == "1.24μs" # Should round up
128+ assert format_time (12_345 ) == "12.3μs"
129+ assert format_time (12_350 ) == "12.3μs" # Should round up
130+
131+ # Test rounding in milliseconds
132+ assert format_time (1_234_000 ) == "1.23ms"
133+ assert format_time (1_235_000 ) == "1.24ms" # Should round up
134+ assert format_time (12_345_000 ) == "12.3ms"
135+ assert format_time (12_350_000 ) == "12.3ms" # Should round up
136+
137+ def test_large_values (self ):
138+ """Test very large nanosecond values."""
139+ assert format_time (3_600_000_000_000 ) == "3600s" # 1 hour
140+ assert format_time (86_400_000_000_000 ) == "86400s" # 1 day
141+
142+ @pytest .mark .parametrize ("nanoseconds,expected" , [
143+ (0 , "0ns" ),
144+ (42 , "42ns" ),
145+ (1_500 , "1.50μs" ),
146+ (25_000 , "25.0μs" ),
147+ (150_000 , "150μs" ),
148+ (2_500_000 , "2.50ms" ),
149+ (45_000_000 , "45.0ms" ),
150+ (200_000_000 , "200ms" ),
151+ (3_500_000_000 , "3.50s" ),
152+ (75_000_000_000 , "75.0s" ),
153+ (300_000_000_000 , "300s" ),
154+ ])
155+ def test_parametrized_examples (self , nanoseconds , expected ):
156+ """Parametrized test with various input/output combinations."""
157+ assert format_time (nanoseconds ) == expected
158+
159+ def test_invalid_input_types (self ):
160+ """Test that function handles invalid input types appropriately."""
161+ with pytest .raises (TypeError ):
162+ format_time ("1000" )
163+
164+ with pytest .raises (TypeError ):
165+ format_time (1000.5 )
166+
167+ with pytest .raises (TypeError ):
168+ format_time (None )
169+
170+ def test_negative_values (self ):
171+ """Test behavior with negative values (if applicable)."""
172+ # This test depends on whether your function should handle negative values
173+ # You might want to modify based on expected behavior
174+ with pytest .raises ((ValueError , TypeError )) or pytest .warns ():
175+ format_time (- 1000 )
0 commit comments