Skip to content

Commit 2f4bdd9

Browse files
authored
Merge branch 'main' into tracer_tracker
2 parents c12071e + 76df611 commit 2f4bdd9

19 files changed

+1322
-1044
lines changed

code_to_optimize/bubble_sort2.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
11
def sorter(arr):
22
arr.sort()
3-
return arr
4-
5-
6-
CACHED_TESTS = "import unittest\ndef sorter(arr):\n for i in range(len(arr)):\n for j in range(len(arr) - 1):\n if arr[j] > arr[j + 1]:\n temp = arr[j]\n arr[j] = arr[j + 1]\n arr[j + 1] = temp\n return arr\nclass SorterTestCase(unittest.TestCase):\n def test_empty_list(self):\n self.assertEqual(sorter([]), [])\n def test_single_element_list(self):\n self.assertEqual(sorter([5]), [5])\n def test_ascending_order_list(self):\n self.assertEqual(sorter([1, 2, 3, 4, 5]), [1, 2, 3, 4, 5])\n def test_descending_order_list(self):\n self.assertEqual(sorter([5, 4, 3, 2, 1]), [1, 2, 3, 4, 5])\n def test_random_order_list(self):\n self.assertEqual(sorter([3, 1, 4, 2, 5]), [1, 2, 3, 4, 5])\n def test_duplicate_elements_list(self):\n self.assertEqual(sorter([3, 1, 4, 2, 2, 5, 1]), [1, 1, 2, 2, 3, 4, 5])\n def test_negative_numbers_list(self):\n self.assertEqual(sorter([-5, -2, -8, -1, -3]), [-8, -5, -3, -2, -1])\n def test_mixed_data_types_list(self):\n self.assertEqual(sorter(['apple', 2, 'banana', 1, 'cherry']), [1, 2, 'apple', 'banana', 'cherry'])\n def test_large_input_list(self):\n self.assertEqual(sorter(list(range(1000, 0, -1))), list(range(1, 1001)))\n def test_list_with_none_values(self):\n self.assertEqual(sorter([None, 2, None, 1, None]), [None, None, None, 1, 2])\n def test_list_with_nan_values(self):\n self.assertEqual(sorter([float('nan'), 2, float('nan'), 1, float('nan')]), [1, 2, float('nan'), float('nan'), float('nan')])\n def test_list_with_complex_numbers(self):\n self.assertEqual(sorter([3 + 2j, 1 + 1j, 4 + 3j, 2 + 1j, 5 + 4j]), [1 + 1j, 2 + 1j, 3 + 2j, 4 + 3j, 5 + 4j])\n def test_list_with_custom_class_objects(self):\n class Person:\n def __init__(self, name, age):\n self.name = name\n self.age = age\n def __repr__(self):\n return f\"Person('{self.name}', {self.age})\"\n input_list = [Person('Alice', 25), Person('Bob', 30), Person('Charlie', 20)]\n expected_output = [Person('Charlie', 20), Person('Alice', 25), Person('Bob', 30)]\n self.assertEqual(sorter(input_list), expected_output)\n def test_list_with_uncomparable_elements(self):\n with self.assertRaises(TypeError):\n sorter([5, 'apple', 3, [1, 2, 3], 2])\n def test_list_with_custom_comparison_function(self):\n input_list = [5, 4, 3, 2, 1]\n expected_output = [5, 4, 3, 2, 1]\n self.assertEqual(sorter(input_list, reverse=True), expected_output)\nif __name__ == '__main__':\n unittest.main()"
3+
return arr

code_to_optimize/bubble_sort_deps.py

Lines changed: 0 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -9,142 +9,3 @@ def sorter_deps(arr):
99
dep2_swap(arr, j)
1010
return arr
1111

12-
13-
CACHED_TESTS = """import dill as pickle
14-
import os
15-
def _log__test__values(values, duration, test_name):
16-
iteration = os.environ["CODEFLASH_TEST_ITERATION"]
17-
with open(os.path.join(
18-
'/var/folders/ms/1tz2l1q55w5b7pp4wpdkbjq80000gn/T/codeflash_jk4pzz3w/',
19-
f'test_return_values_{iteration}.bin'), 'ab') as f:
20-
return_bytes = pickle.dumps(values)
21-
_test_name = f"{test_name}".encode("ascii")
22-
f.write(len(_test_name).to_bytes(4, byteorder='big'))
23-
f.write(_test_name)
24-
f.write(duration.to_bytes(8, byteorder='big'))
25-
f.write(len(return_bytes).to_bytes(4, byteorder='big'))
26-
f.write(return_bytes)
27-
import time
28-
import gc
29-
from code_to_optimize.bubble_sort_deps import sorter_deps
30-
import timeout_decorator
31-
import unittest
32-
33-
def dep1_comparer(arr, j: int) -> bool:
34-
return arr[j] > arr[j + 1]
35-
36-
def dep2_swap(arr, j):
37-
temp = arr[j]
38-
arr[j] = arr[j + 1]
39-
arr[j + 1] = temp
40-
41-
class TestSorterDeps(unittest.TestCase):
42-
43-
@timeout_decorator.timeout(15, use_signals=True)
44-
def test_integers(self):
45-
gc.disable()
46-
counter = time.perf_counter_ns()
47-
return_value = sorter_deps([5, 3, 2, 4, 1])
48-
duration = time.perf_counter_ns() - counter
49-
gc.enable()
50-
_log__test__values(
51-
return_value, duration,
52-
'code_to_optimize.tests.unittest.test_sorter_deps__unit_test_0:TestSorterDeps.test_integers:sorter_deps:0')
53-
gc.disable()
54-
counter = time.perf_counter_ns()
55-
return_value = sorter_deps([10, -3, 0, 2, 7])
56-
duration = time.perf_counter_ns() - counter
57-
gc.enable()
58-
_log__test__values(
59-
return_value, duration,
60-
('code_to_optimize.tests.unittest.test_sorter_deps__unit_test_0:'
61-
'TestSorterDeps.test_integers:sorter_deps:1'))
62-
63-
@timeout_decorator.timeout(15, use_signals=True)
64-
def test_floats(self):
65-
gc.disable()
66-
counter = time.perf_counter_ns()
67-
return_value = sorter_deps([3.2, 1.5, 2.7, 4.1, 1.0])
68-
duration = time.perf_counter_ns() - counter
69-
gc.enable()
70-
_log__test__values(return_value, duration,
71-
'code_to_optimize.tests.unittest.test_sorter_deps__unit_test_0:TestSorterDeps.test_floats:sorter_deps:0')
72-
gc.disable()
73-
counter = time.perf_counter_ns()
74-
return_value = sorter_deps([-1.1, 0.0, 3.14, 2.71, -0.5])
75-
duration = time.perf_counter_ns() - counter
76-
gc.enable()
77-
_log__test__values(return_value, duration,
78-
'code_to_optimize.tests.unittest.test_sorter_deps__unit_test_0:TestSorterDeps.test_floats:sorter_deps:1')
79-
80-
@timeout_decorator.timeout(15, use_signals=True)
81-
def test_identical_elements(self):
82-
gc.disable()
83-
counter = time.perf_counter_ns()
84-
return_value = sorter_deps([1, 1, 1, 1, 1])
85-
duration = time.perf_counter_ns() - counter
86-
gc.enable()
87-
_log__test__values(return_value, duration,
88-
('code_to_optimize.tests.unittest.test_sorter_deps__unit_test_0:'
89-
'TestSorterDeps.test_identical_elements:sorter_deps:0'))
90-
gc.disable()
91-
counter = time.perf_counter_ns()
92-
return_value = sorter_deps([3.14, 3.14, 3.14])
93-
duration = time.perf_counter_ns() - counter
94-
gc.enable()
95-
_log__test__values(return_value, duration,
96-
('code_to_optimize.tests.unittest.test_sorter_deps__unit_test_0:'
97-
'TestSorterDeps.test_identical_elements:sorter_deps:1'))
98-
99-
@timeout_decorator.timeout(15, use_signals=True)
100-
def test_single_element(self):
101-
gc.disable()
102-
counter = time.perf_counter_ns()
103-
return_value = sorter_deps([5])
104-
duration = time.perf_counter_ns() - counter
105-
gc.enable()
106-
_log__test__values(return_value, duration, 'code_to_optimize.tests.unittest.test_sorter_deps__unit_test_0:TestSorterDeps.test_single_element:sorter_deps:0')
107-
gc.disable()
108-
counter = time.perf_counter_ns()
109-
return_value = sorter_deps([-3.2])
110-
duration = time.perf_counter_ns() - counter
111-
gc.enable()
112-
_log__test__values(return_value, duration, 'code_to_optimize.tests.unittest.test_sorter_deps__unit_test_0:TestSorterDeps.test_single_element:sorter_deps:1')
113-
114-
@timeout_decorator.timeout(15, use_signals=True)
115-
def test_empty_array(self):
116-
gc.disable()
117-
counter = time.perf_counter_ns()
118-
return_value = sorter_deps([])
119-
duration = time.perf_counter_ns() - counter
120-
gc.enable()
121-
_log__test__values(return_value, duration, 'code_to_optimize.tests.unittest.test_sorter_deps__unit_test_0:TestSorterDeps.test_empty_array:sorter_deps:0')
122-
123-
@timeout_decorator.timeout(15, use_signals=True)
124-
def test_strings(self):
125-
gc.disable()
126-
counter = time.perf_counter_ns()
127-
return_value = sorter_deps(['apple', 'banana', 'cherry', 'date'])
128-
duration = time.perf_counter_ns() - counter
129-
gc.enable()
130-
_log__test__values(return_value, duration, 'code_to_optimize.tests.unittest.test_sorter_deps__unit_test_0:TestSorterDeps.test_strings:sorter_deps:0')
131-
gc.disable()
132-
counter = time.perf_counter_ns()
133-
return_value = sorter_deps(['dog', 'cat', 'elephant', 'ant'])
134-
duration = time.perf_counter_ns() - counter
135-
gc.enable()
136-
_log__test__values(return_value, duration, 'code_to_optimize.tests.unittest.test_sorter_deps__unit_test_0:TestSorterDeps.test_strings:sorter_deps:1')
137-
138-
@timeout_decorator.timeout(15, use_signals=True)
139-
def test_mixed_types(self):
140-
with self.assertRaises(TypeError):
141-
gc.disable()
142-
counter = time.perf_counter_ns()
143-
return_value = sorter_deps([1, 'two', 3.0, 'four'])
144-
duration = time.perf_counter_ns() - counter
145-
gc.enable()
146-
_log__test__values(return_value, duration, 'code_to_optimize.tests.unittest.test_sorter_deps__unit_test_0:TestSorterDeps.test_mixed_types:sorter_deps:0_0')
147-
if __name__ == '__main__':
148-
unittest.main()
149-
150-
"""

code_to_optimize/final_test_set/bubble_sort.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,4 @@ def sorter(arr):
55
temp = arr[j]
66
arr[j] = arr[j + 1]
77
arr[j + 1] = temp
8-
return arr
9-
10-
11-
CACHED_TESTS = "import unittest\ndef sorter(arr):\n for i in range(len(arr)):\n for j in range(len(arr) - 1):\n if arr[j] > arr[j + 1]:\n temp = arr[j]\n arr[j] = arr[j + 1]\n arr[j + 1] = temp\n return arr\nclass SorterTestCase(unittest.TestCase):\n def test_empty_list(self):\n self.assertEqual(sorter([]), [])\n def test_single_element_list(self):\n self.assertEqual(sorter([5]), [5])\n def test_ascending_order_list(self):\n self.assertEqual(sorter([1, 2, 3, 4, 5]), [1, 2, 3, 4, 5])\n def test_descending_order_list(self):\n self.assertEqual(sorter([5, 4, 3, 2, 1]), [1, 2, 3, 4, 5])\n def test_random_order_list(self):\n self.assertEqual(sorter([3, 1, 4, 2, 5]), [1, 2, 3, 4, 5])\n def test_duplicate_elements_list(self):\n self.assertEqual(sorter([3, 1, 4, 2, 2, 5, 1]), [1, 1, 2, 2, 3, 4, 5])\n def test_negative_numbers_list(self):\n self.assertEqual(sorter([-5, -2, -8, -1, -3]), [-8, -5, -3, -2, -1])\n def test_mixed_data_types_list(self):\n self.assertEqual(sorter(['apple', 2, 'banana', 1, 'cherry']), [1, 2, 'apple', 'banana', 'cherry'])\n def test_large_input_list(self):\n self.assertEqual(sorter(list(range(1000, 0, -1))), list(range(1, 1001)))\n def test_list_with_none_values(self):\n self.assertEqual(sorter([None, 2, None, 1, None]), [None, None, None, 1, 2])\n def test_list_with_nan_values(self):\n self.assertEqual(sorter([float('nan'), 2, float('nan'), 1, float('nan')]), [1, 2, float('nan'), float('nan'), float('nan')])\n def test_list_with_complex_numbers(self):\n self.assertEqual(sorter([3 + 2j, 1 + 1j, 4 + 3j, 2 + 1j, 5 + 4j]), [1 + 1j, 2 + 1j, 3 + 2j, 4 + 3j, 5 + 4j])\n def test_list_with_custom_class_objects(self):\n class Person:\n def __init__(self, name, age):\n self.name = name\n self.age = age\n def __repr__(self):\n return f\"Person('{self.name}', {self.age})\"\n input_list = [Person('Alice', 25), Person('Bob', 30), Person('Charlie', 20)]\n expected_output = [Person('Charlie', 20), Person('Alice', 25), Person('Bob', 30)]\n self.assertEqual(sorter(input_list), expected_output)\n def test_list_with_uncomparable_elements(self):\n with self.assertRaises(TypeError):\n sorter([5, 'apple', 3, [1, 2, 3], 2])\n def test_list_with_custom_comparison_function(self):\n input_list = [5, 4, 3, 2, 1]\n expected_output = [5, 4, 3, 2, 1]\n self.assertEqual(sorter(input_list, reverse=True), expected_output)\nif __name__ == '__main__':\n unittest.main()"
8+
return arr

code_to_optimize/use_cosine_similarity_from_other_file.py

Lines changed: 1 addition & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -9,110 +9,4 @@ def use_cosine_similarity(
99
top_k: Optional[int] = 5,
1010
score_threshold: Optional[float] = None,
1111
) -> Tuple[List[Tuple[int, int]], List[float]]:
12-
return cosine_similarity_top_k(X, Y, top_k, score_threshold)
13-
14-
15-
CACHED_TESTS = """import unittest
16-
import numpy as np
17-
from sklearn.metrics.pairwise import cosine_similarity
18-
from typing import List, Optional, Tuple, Union
19-
Matrix = Union[List[List[float]], List[np.ndarray], np.ndarray]
20-
def cosine_similarity_top_k(X: Matrix, Y: Matrix, top_k: Optional[int]=5, score_threshold: Optional[float]=None) -> Tuple[List[Tuple[int, int]], List[float]]:
21-
\"\"\"Row-wise cosine similarity with optional top-k and score threshold filtering.
22-
Args:
23-
X: Matrix.
24-
Y: Matrix, same width as X.
25-
top_k: Max number of results to return.
26-
score_threshold: Minimum cosine similarity of results.
27-
Returns:
28-
Tuple of two lists. First contains two-tuples of indices (X_idx, Y_idx),
29-
second contains corresponding cosine similarities.
30-
\"\"\"
31-
if len(X) == 0 or len(Y) == 0:
32-
return ([], [])
33-
score_array = cosine_similarity(X, Y)
34-
sorted_idxs = score_array.flatten().argsort()[::-1]
35-
top_k = top_k or len(sorted_idxs)
36-
top_idxs = sorted_idxs[:top_k]
37-
score_threshold = score_threshold or -1.0
38-
top_idxs = top_idxs[score_array.flatten()[top_idxs] > score_threshold]
39-
ret_idxs = [(x // score_array.shape[1], x % score_array.shape[1]) for x in top_idxs]
40-
scores = score_array.flatten()[top_idxs].tolist()
41-
return (ret_idxs, scores)
42-
def use_cosine_similarity(X: Matrix, Y: Matrix, top_k: Optional[int]=5, score_threshold: Optional[float]=None) -> Tuple[List[Tuple[int, int]], List[float]]:
43-
return cosine_similarity_top_k(X, Y, top_k, score_threshold)
44-
class TestUseCosineSimilarity(unittest.TestCase):
45-
def test_normal_scenario(self):
46-
X = [[1, 2, 3], [4, 5, 6]]
47-
Y = [[7, 8, 9], [10, 11, 12]]
48-
result = use_cosine_similarity(X, Y, top_k=1, score_threshold=0.5)
49-
self.assertEqual(result, ([(0, 1)], [0.9746318461970762]))
50-
def test_edge_case_empty_matrices(self):
51-
X = []
52-
Y = []
53-
result = use_cosine_similarity(X, Y)
54-
self.assertEqual(result, ([], []))
55-
def test_edge_case_different_widths(self):
56-
X = [[1, 2, 3]]
57-
Y = [[4, 5]]
58-
with self.assertRaises(ValueError):
59-
use_cosine_similarity(X, Y)
60-
def test_edge_case_negative_top_k(self):
61-
X = [[1, 2, 3]]
62-
Y = [[4, 5, 6]]
63-
with self.assertRaises(IndexError):
64-
use_cosine_similarity(X, Y, top_k=-1)
65-
def test_edge_case_zero_top_k(self):
66-
X = [[1, 2, 3]]
67-
Y = [[4, 5, 6]]
68-
result = use_cosine_similarity(X, Y, top_k=0)
69-
self.assertEqual(result, ([], []))
70-
def test_edge_case_negative_score_threshold(self):
71-
X = [[1, 2, 3]]
72-
Y = [[4, 5, 6]]
73-
result = use_cosine_similarity(X, Y, score_threshold=-1.0)
74-
self.assertEqual(result, ([(0, 0)], [0.9746318461970762]))
75-
def test_edge_case_large_score_threshold(self):
76-
X = [[1, 2, 3]]
77-
Y = [[4, 5, 6]]
78-
result = use_cosine_similarity(X, Y, score_threshold=2.0)
79-
self.assertEqual(result, ([], []))
80-
def test_exceptional_case_non_matrix_X(self):
81-
X = [1, 2, 3]
82-
Y = [[4, 5, 6]]
83-
with self.assertRaises(ValueError):
84-
use_cosine_similarity(X, Y)
85-
def test_exceptional_case_non_integer_top_k(self):
86-
X = [[1, 2, 3]]
87-
Y = [[4, 5, 6]]
88-
with self.assertRaises(TypeError):
89-
use_cosine_similarity(X, Y, top_k='5')
90-
def test_exceptional_case_non_float_score_threshold(self):
91-
X = [[1, 2, 3]]
92-
Y = [[4, 5, 6]]
93-
with self.assertRaises(TypeError):
94-
use_cosine_similarity(X, Y, score_threshold='0.5')
95-
def test_special_values_nan_in_matrices(self):
96-
X = [[1, 2, np.nan]]
97-
Y = [[4, 5, 6]]
98-
with self.assertRaises(ValueError):
99-
use_cosine_similarity(X, Y)
100-
def test_special_values_none_top_k(self):
101-
X = [[1, 2, 3]]
102-
Y = [[4, 5, 6]]
103-
result = use_cosine_similarity(X, Y, top_k=None)
104-
self.assertEqual(result, ([(0, 0)], [0.9746318461970762]))
105-
def test_special_values_none_score_threshold(self):
106-
X = [[1, 2, 3]]
107-
Y = [[4, 5, 6]]
108-
result = use_cosine_similarity(X, Y, score_threshold=None)
109-
self.assertEqual(result, ([(0, 0)], [0.9746318461970762]))
110-
def test_large_inputs(self):
111-
X = np.random.rand(1000, 1000)
112-
Y = np.random.rand(1000, 1000)
113-
result = use_cosine_similarity(X, Y, top_k=10, score_threshold=0.5)
114-
self.assertEqual(len(result[0]), 10)
115-
self.assertEqual(len(result[1]), 10)
116-
self.assertTrue(all((score > 0.5 for score in result[1])))
117-
if __name__ == '__main__':
118-
unittest.main()"""
12+
return cosine_similarity_top_k(X, Y, top_k, score_threshold)

codeflash/cli_cmds/cmd_init.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,7 @@ def configure_pyproject_toml(setup_info: SetupInfo) -> None:
595595
if formatter in ["black", "ruff"]:
596596
try:
597597
subprocess.run([formatter], capture_output=True, check=False)
598-
except FileNotFoundError:
598+
except (FileNotFoundError, NotADirectoryError):
599599
click.echo(f"⚠️ Formatter not found: {formatter}, please ensure it is installed")
600600
codeflash_section["formatter-cmds"] = formatter_cmds
601601
# Add the 'codeflash' section, ensuring 'tool' section exists

codeflash/code_utils/coverage_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
def extract_dependent_function(main_function: str, code_context: CodeOptimizationContext) -> str | Literal[False]:
1414
"""Extract the single dependent function from the code context excluding the main function."""
15-
ast_tree = ast.parse(code_context.code_to_optimize_with_helpers)
15+
ast_tree = ast.parse(code_context.testgen_context_code)
1616

1717
dependent_functions = {node.name for node in ast_tree.body if isinstance(node, ast.FunctionDef)}
1818

0 commit comments

Comments
 (0)