Skip to content

Commit f885467

Browse files
committed
parametrized marker
1 parent 935583f commit f885467

File tree

1 file changed

+126
-0
lines changed

1 file changed

+126
-0
lines changed

tests/test_add_runtime_comments.py

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1783,6 +1783,132 @@ def test_runtime_comment_addition_with(self, test_config):
17831783
modified_source = result.generated_tests[0].generated_original_test_source
17841784
assert modified_source == expected
17851785

1786+
def test_runtime_comment_addition_lc(self, test_config):
1787+
"""Test basic functionality of adding runtime comments for list comprehension."""
1788+
# Create test source code
1789+
os.chdir(test_config.project_root_path)
1790+
test_source = """def test_bubble_sort():
1791+
i = 0
1792+
codeflash_output = [bubble_sort([3, 1, 2]) for _ in range(3)]
1793+
assert codeflash_output == [[1,2,3],[1,2,3],[1,2,3]]
1794+
i += 1
1795+
d = 5
1796+
"""
1797+
expected = """def test_bubble_sort():
1798+
i = 0
1799+
codeflash_output = [bubble_sort([3, 1, 2]) for _ in range(3)] # 500μs -> 300μs (66.7% faster)
1800+
assert codeflash_output == [[1,2,3],[1,2,3],[1,2,3]]
1801+
i += 1
1802+
d = 5
1803+
"""
1804+
generated_test = GeneratedTests(
1805+
generated_original_test_source=test_source,
1806+
instrumented_behavior_test_source="",
1807+
instrumented_perf_test_source="",
1808+
behavior_file_path=test_config.tests_root / "test_module__unit_test_0.py",
1809+
perf_file_path=test_config.tests_root / "test_perf.py",
1810+
)
1811+
generated_tests = GeneratedTestsList(generated_tests=[generated_test])
1812+
1813+
# Create test results
1814+
original_test_results = TestResults()
1815+
optimized_test_results = TestResults()
1816+
1817+
# Add test invocations with different runtimes
1818+
original_invocation1 = self.create_test_invocation("test_bubble_sort", 500_000, iteration_id='1_0') # 500μs
1819+
optimized_invocation1 = self.create_test_invocation("test_bubble_sort", 300_000, iteration_id='1_0') # 300μs
1820+
# longer runtime than minimum, will not contribute
1821+
original_invocation2 = self.create_test_invocation("test_bubble_sort", 600_000, iteration_id='1_1') # 500μs
1822+
optimized_invocation2 = self.create_test_invocation("test_bubble_sort", 400_000, iteration_id='1_1') # 300μs
1823+
original_invocation3 = self.create_test_invocation("test_bubble_sort", 700_000, iteration_id='1_2') # 500μs
1824+
optimized_invocation3 = self.create_test_invocation("test_bubble_sort", 500_000, iteration_id='1_2') # 300μs
1825+
1826+
original_test_results.add(original_invocation1)
1827+
optimized_test_results.add(optimized_invocation1)
1828+
original_test_results.add(original_invocation2)
1829+
optimized_test_results.add(optimized_invocation2)
1830+
original_test_results.add(original_invocation3)
1831+
optimized_test_results.add(optimized_invocation3)
1832+
original_runtimes = original_test_results.usable_runtime_data_by_test_case()
1833+
optimized_runtimes = optimized_test_results.usable_runtime_data_by_test_case()
1834+
# Test the functionality
1835+
result = add_runtime_comments_to_generated_tests(generated_tests, original_runtimes, optimized_runtimes)
1836+
1837+
# Check that comments were added
1838+
modified_source = result.generated_tests[0].generated_original_test_source
1839+
assert modified_source == expected
1840+
1841+
def test_runtime_comment_addition_parameterized(self, test_config):
1842+
"""Test basic functionality of adding runtime comments for list comprehension."""
1843+
# Create test source code
1844+
os.chdir(test_config.project_root_path)
1845+
test_source = """@pytest.mark.parametrize(
1846+
"input, expected_output",
1847+
[
1848+
([5, 4, 3, 2, 1, 0], [0, 1, 2, 3, 4, 5]),
1849+
([5.0, 4.0, 3.0, 2.0, 1.0, 0.0], [0.0, 1.0, 2.0, 3.0, 4.0, 5.0]),
1850+
(list(reversed(range(50))), list(range(50))),
1851+
],
1852+
)
1853+
def test_bubble_sort(input, expected_output):
1854+
i = 0
1855+
codeflash_output = bubble_sort(input)
1856+
assert codeflash_output == expected_output
1857+
i += 1
1858+
d = 5
1859+
"""
1860+
expected = """@pytest.mark.parametrize(
1861+
"input, expected_output",
1862+
[
1863+
([5, 4, 3, 2, 1, 0], [0, 1, 2, 3, 4, 5]),
1864+
([5.0, 4.0, 3.0, 2.0, 1.0, 0.0], [0.0, 1.0, 2.0, 3.0, 4.0, 5.0]),
1865+
(list(reversed(range(50))), list(range(50))),
1866+
],
1867+
)
1868+
def test_bubble_sort(input, expected_output):
1869+
i = 0
1870+
codeflash_output = bubble_sort(input) # 500μs -> 300μs (66.7% faster)
1871+
assert codeflash_output == expected_output
1872+
i += 1
1873+
d = 5
1874+
"""
1875+
generated_test = GeneratedTests(
1876+
generated_original_test_source=test_source,
1877+
instrumented_behavior_test_source="",
1878+
instrumented_perf_test_source="",
1879+
behavior_file_path=test_config.tests_root / "test_module__unit_test_0.py",
1880+
perf_file_path=test_config.tests_root / "test_perf.py",
1881+
)
1882+
generated_tests = GeneratedTestsList(generated_tests=[generated_test])
1883+
1884+
# Create test results
1885+
original_test_results = TestResults()
1886+
optimized_test_results = TestResults()
1887+
1888+
# Add test invocations with different runtimes
1889+
original_invocation1 = self.create_test_invocation("test_bubble_sort", 500_000, iteration_id='1_0') # 500μs
1890+
optimized_invocation1 = self.create_test_invocation("test_bubble_sort", 300_000, iteration_id='1_0') # 300μs
1891+
# longer runtime than minimum, will not contribute
1892+
original_invocation2 = self.create_test_invocation("test_bubble_sort", 600_000, iteration_id='1_1') # 500μs
1893+
optimized_invocation2 = self.create_test_invocation("test_bubble_sort", 400_000, iteration_id='1_1') # 300μs
1894+
original_invocation3 = self.create_test_invocation("test_bubble_sort", 700_000, iteration_id='1_2') # 500μs
1895+
optimized_invocation3 = self.create_test_invocation("test_bubble_sort", 500_000, iteration_id='1_2') # 300μs
1896+
1897+
original_test_results.add(original_invocation1)
1898+
optimized_test_results.add(optimized_invocation1)
1899+
original_test_results.add(original_invocation2)
1900+
optimized_test_results.add(optimized_invocation2)
1901+
original_test_results.add(original_invocation3)
1902+
optimized_test_results.add(optimized_invocation3)
1903+
original_runtimes = original_test_results.usable_runtime_data_by_test_case()
1904+
optimized_runtimes = optimized_test_results.usable_runtime_data_by_test_case()
1905+
# Test the functionality
1906+
result = add_runtime_comments_to_generated_tests(generated_tests, original_runtimes, optimized_runtimes)
1907+
1908+
# Check that comments were added
1909+
modified_source = result.generated_tests[0].generated_original_test_source
1910+
assert modified_source == expected
1911+
17861912
"""TODO Future tests"""
17871913
# def test_runtime_comment_addition_else(self, test_config):
17881914
# """Test basic functionality of adding runtime comments."""

0 commit comments

Comments
 (0)