from __future__ import annotations

import gc
import os
import time
import types
from unittest.mock import MagicMock, patch

import pytest
from inference.core.exceptions import WorkspaceLoadError
from inference.core.workflows.errors import PythonBlockError
from inference.core.workflows.execution_engine.v1.dynamic_blocks.block_scaffolding import \
    create_dynamic_module
from inference.core.workflows.execution_engine.v1.dynamic_blocks.entities import \
    PythonCode


def codeflash_wrap(codeflash_wrapped, codeflash_test_module_name, codeflash_test_class_name, codeflash_test_name, codeflash_function_name, codeflash_line_id, codeflash_loop_index, *args, **kwargs):
    test_id = f'{codeflash_test_module_name}:{codeflash_test_class_name}:{codeflash_test_name}:{codeflash_line_id}:{codeflash_loop_index}'
    if not hasattr(codeflash_wrap, 'index'):
        codeflash_wrap.index = {}
    if test_id in codeflash_wrap.index:
        codeflash_wrap.index[test_id] += 1
    else:
        codeflash_wrap.index[test_id] = 0
    codeflash_test_index = codeflash_wrap.index[test_id]
    invocation_id = f'{codeflash_line_id}_{codeflash_test_index}'
    test_stdout_tag = f"{codeflash_test_module_name}:{(codeflash_test_class_name + '.' if codeflash_test_class_name else '')}{codeflash_test_name}:{codeflash_function_name}:{codeflash_loop_index}:{invocation_id}"
    print(f'!$######{test_stdout_tag}######$!')
    exception = None
    gc.disable()
    try:
        counter = time.perf_counter_ns()
        return_value = codeflash_wrapped(*args, **kwargs)
        codeflash_duration = time.perf_counter_ns() - counter
    except Exception as e:
        codeflash_duration = time.perf_counter_ns() - counter
        exception = e
    gc.enable()
    print(f'!######{test_stdout_tag}:{codeflash_duration}######!')
    if exception:
        raise exception
    return return_value

def test_basic_module_creation_with_simple_function():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test creating a dynamic module with a simple run function.'
    python_code = PythonCode(type='PythonCode', run_function_code='def run():\n    return 42', run_function_name='run', imports=[])
    codeflash_return_value = codeflash_wrap(create_dynamic_module, 'tests.inference.unit_tests.test_create_dynamic_module__unit_test_1', None, 'test_basic_module_creation_with_simple_function', 'create_dynamic_module', '2', codeflash_loop_index, block_type_name='test_block', python_code=python_code, module_name='test_module', skip_class_eval=False)
    codeflash_output = codeflash_return_value
    module = codeflash_output

def test_module_creation_with_skip_class_eval_true():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test creating a stub module when skip_class_eval is True.'
    python_code = PythonCode(type='PythonCode', run_function_code='def run():\n    return 100', run_function_name='run', imports=[])
    codeflash_return_value = codeflash_wrap(create_dynamic_module, 'tests.inference.unit_tests.test_create_dynamic_module__unit_test_1', None, 'test_module_creation_with_skip_class_eval_true', 'create_dynamic_module', '2', codeflash_loop_index, block_type_name='test_block', python_code=python_code, module_name='stub_module', skip_class_eval=True)
    codeflash_output = codeflash_return_value
    module = codeflash_output

def test_module_creation_with_init_function():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test creating a module with both run and init functions.'
    python_code = PythonCode(type='PythonCode', run_function_code="def run():\n    return 'executed'", run_function_name='run', init_function_code="def init():\n    return {'key': 'value'}", init_function_name='init', imports=[])
    codeflash_return_value = codeflash_wrap(create_dynamic_module, 'tests.inference.unit_tests.test_create_dynamic_module__unit_test_1', None, 'test_module_creation_with_init_function', 'create_dynamic_module', '2', codeflash_loop_index, block_type_name='test_block', python_code=python_code, module_name='test_module_with_init', skip_class_eval=False)
    codeflash_output = codeflash_return_value
    module = codeflash_output

def test_module_with_imports():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test creating a module with additional imports.'
    python_code = PythonCode(type='PythonCode', run_function_code='def run():\n    return len([1, 2, 3])', run_function_name='run', imports=['from collections import Counter'], imports_list=[])
    codeflash_return_value = codeflash_wrap(create_dynamic_module, 'tests.inference.unit_tests.test_create_dynamic_module__unit_test_1', None, 'test_module_with_imports', 'create_dynamic_module', '2', codeflash_loop_index, block_type_name='test_block', python_code=python_code, module_name='test_module_imports', skip_class_eval=False)
    codeflash_output = codeflash_return_value
    module = codeflash_output

def test_custom_function_name():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test creating a module with a custom run function name.'
    python_code = PythonCode(type='PythonCode', run_function_code="def my_custom_func():\n    return 'custom'", run_function_name='my_custom_func', imports=[])
    codeflash_return_value = codeflash_wrap(create_dynamic_module, 'tests.inference.unit_tests.test_create_dynamic_module__unit_test_1', None, 'test_custom_function_name', 'create_dynamic_module', '2', codeflash_loop_index, block_type_name='test_block', python_code=python_code, module_name='custom_func_module', skip_class_eval=False)
    codeflash_output = codeflash_return_value
    module = codeflash_output

def test_module_with_global_state():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that module has shared globals dict accessible.'
    python_code = PythonCode(type='PythonCode', run_function_code="def run():\n    return globals.get('shared_var', 'not_set')", run_function_name='run', imports=[])
    codeflash_return_value = codeflash_wrap(create_dynamic_module, 'tests.inference.unit_tests.test_create_dynamic_module__unit_test_1', None, 'test_module_with_global_state', 'create_dynamic_module', '2', codeflash_loop_index, block_type_name='test_block', python_code=python_code, module_name='global_test', skip_class_eval=False)
    codeflash_output = codeflash_return_value
    module = codeflash_output

def test_empty_run_function_code():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test creating a module with empty run function code.'
    python_code = PythonCode(type='PythonCode', run_function_code='', run_function_name='run', imports=[])
    with pytest.raises(PythonBlockError):
        codeflash_return_value = codeflash_wrap(create_dynamic_module, 'tests.inference.unit_tests.test_create_dynamic_module__unit_test_1', None, 'test_empty_run_function_code', 'create_dynamic_module', '2_0', codeflash_loop_index, block_type_name='test_block', python_code=python_code, module_name='empty_module', skip_class_eval=False)

def test_syntax_error_in_function():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that syntax errors in code are caught and wrapped.'
    python_code = PythonCode(type='PythonCode', run_function_code='def run():\n    return 42\n  invalid syntax here', run_function_name='run', imports=[])
    with pytest.raises(PythonBlockError) as exc_info:
        codeflash_return_value = codeflash_wrap(create_dynamic_module, 'tests.inference.unit_tests.test_create_dynamic_module__unit_test_1', None, 'test_syntax_error_in_function', 'create_dynamic_module', '2_0', codeflash_loop_index, block_type_name='test_block', python_code=python_code, module_name='syntax_error_module', skip_class_eval=False)

def test_runtime_error_in_module_creation():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that runtime errors during module creation are wrapped.'
    python_code = PythonCode(type='PythonCode', run_function_code="def run():\n    raise ValueError('test error')\nrun()", run_function_name='run', imports=[])
    with pytest.raises(PythonBlockError):
        codeflash_return_value = codeflash_wrap(create_dynamic_module, 'tests.inference.unit_tests.test_create_dynamic_module__unit_test_1', None, 'test_runtime_error_in_module_creation', 'create_dynamic_module', '2_0', codeflash_loop_index, block_type_name='error_block', python_code=python_code, module_name='error_module', skip_class_eval=False)

def test_undefined_variable_reference():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that undefined variable references cause errors.'
    python_code = PythonCode(type='PythonCode', run_function_code='def run():\n    return undefined_var', run_function_name='run', imports=[])
    codeflash_return_value = codeflash_wrap(create_dynamic_module, 'tests.inference.unit_tests.test_create_dynamic_module__unit_test_1', None, 'test_undefined_variable_reference', 'create_dynamic_module', '2', codeflash_loop_index, block_type_name='test_block', python_code=python_code, module_name='undefined_module', skip_class_eval=False)
    codeflash_output = codeflash_return_value
    module = codeflash_output
    with pytest.raises(NameError):
        module.run()

def test_none_api_key():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test module creation with None api_key.'
    python_code = PythonCode(type='PythonCode', run_function_code="def run():\n    return 'works'", run_function_name='run', imports=[])
    codeflash_return_value = codeflash_wrap(create_dynamic_module, 'tests.inference.unit_tests.test_create_dynamic_module__unit_test_1', None, 'test_none_api_key', 'create_dynamic_module', '2', codeflash_loop_index, block_type_name='test_block', python_code=python_code, module_name='none_api_key_module', api_key=None, skip_class_eval=False)
    codeflash_output = codeflash_return_value
    module = codeflash_output

def test_function_with_multiple_statements():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test function with multiple statements.'
    python_code = PythonCode(type='PythonCode', run_function_code='def run():\n    x = 10\n    y = 20\n    return x + y', run_function_name='run', imports=[])
    codeflash_return_value = codeflash_wrap(create_dynamic_module, 'tests.inference.unit_tests.test_create_dynamic_module__unit_test_1', None, 'test_function_with_multiple_statements', 'create_dynamic_module', '2', codeflash_loop_index, block_type_name='test_block', python_code=python_code, module_name='multi_statement_module', skip_class_eval=False)
    codeflash_output = codeflash_return_value
    module = codeflash_output

def test_function_with_nested_scope():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test function with nested scopes.'
    python_code = PythonCode(type='PythonCode', run_function_code='def run():\n    def inner():\n        return 5\n    return inner() * 2', run_function_name='run', imports=[])
    codeflash_return_value = codeflash_wrap(create_dynamic_module, 'tests.inference.unit_tests.test_create_dynamic_module__unit_test_1', None, 'test_function_with_nested_scope', 'create_dynamic_module', '2', codeflash_loop_index, block_type_name='test_block', python_code=python_code, module_name='nested_scope_module', skip_class_eval=False)
    codeflash_output = codeflash_return_value
    module = codeflash_output

def test_function_with_list_comprehension():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test function using list comprehension.'
    python_code = PythonCode(type='PythonCode', run_function_code='def run():\n    return [x*2 for x in range(5)]', run_function_name='run', imports=[])
    codeflash_return_value = codeflash_wrap(create_dynamic_module, 'tests.inference.unit_tests.test_create_dynamic_module__unit_test_1', None, 'test_function_with_list_comprehension', 'create_dynamic_module', '2', codeflash_loop_index, block_type_name='test_block', python_code=python_code, module_name='comprehension_module', skip_class_eval=False)
    codeflash_output = codeflash_return_value
    module = codeflash_output

def test_init_with_skip_class_eval():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that init function is added as stub when skip_class_eval is True.'
    python_code = PythonCode(type='PythonCode', run_function_code='def run():\n    return 1', run_function_name='run', init_function_code="def init():\n    return {'data': 'value'}", init_function_name='init', imports=[])
    codeflash_return_value = codeflash_wrap(create_dynamic_module, 'tests.inference.unit_tests.test_create_dynamic_module__unit_test_1', None, 'test_init_with_skip_class_eval', 'create_dynamic_module', '2', codeflash_loop_index, block_type_name='test_block', python_code=python_code, module_name='init_stub_module', skip_class_eval=True)
    codeflash_output = codeflash_return_value
    module = codeflash_output

def test_empty_imports_list():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test creation with empty imports list.'
    python_code = PythonCode(type='PythonCode', run_function_code="def run():\n    return 'success'", run_function_name='run', imports=[])
    codeflash_return_value = codeflash_wrap(create_dynamic_module, 'tests.inference.unit_tests.test_create_dynamic_module__unit_test_1', None, 'test_empty_imports_list', 'create_dynamic_module', '2', codeflash_loop_index, block_type_name='test_block', python_code=python_code, module_name='empty_imports_module', skip_class_eval=False)
    codeflash_output = codeflash_return_value
    module = codeflash_output

def test_module_name_with_special_characters():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test module creation with special characters in name.'
    python_code = PythonCode(type='PythonCode', run_function_code='def run():\n    return True', run_function_name='run', imports=[])
    codeflash_return_value = codeflash_wrap(create_dynamic_module, 'tests.inference.unit_tests.test_create_dynamic_module__unit_test_1', None, 'test_module_name_with_special_characters', 'create_dynamic_module', '2', codeflash_loop_index, block_type_name='test_block', python_code=python_code, module_name='test_module_123_ABC', skip_class_eval=False)
    codeflash_output = codeflash_return_value
    module = codeflash_output

def test_init_function_code_none():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test when init_function_code is explicitly None.'
    python_code = PythonCode(type='PythonCode', run_function_code='def run():\n    return 42', run_function_name='run', init_function_code=None, init_function_name='init', imports=[])
    codeflash_return_value = codeflash_wrap(create_dynamic_module, 'tests.inference.unit_tests.test_create_dynamic_module__unit_test_1', None, 'test_init_function_code_none', 'create_dynamic_module', '2', codeflash_loop_index, block_type_name='test_block', python_code=python_code, module_name='none_init_module', skip_class_eval=False)
    codeflash_output = codeflash_return_value
    module = codeflash_output

def test_function_with_exception_handling():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test function that includes try-except blocks.'
    python_code = PythonCode(type='PythonCode', run_function_code="def run():\n    try:\n        return 1 / 0\n    except ZeroDivisionError:\n        return 'caught'", run_function_name='run', imports=[])
    codeflash_return_value = codeflash_wrap(create_dynamic_module, 'tests.inference.unit_tests.test_create_dynamic_module__unit_test_1', None, 'test_function_with_exception_handling', 'create_dynamic_module', '2', codeflash_loop_index, block_type_name='test_block', python_code=python_code, module_name='exception_module', skip_class_eval=False)
    codeflash_output = codeflash_return_value
    module = codeflash_output

def test_function_returning_dict():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test function returning a dictionary.'
    python_code = PythonCode(type='PythonCode', run_function_code="def run():\n    return {'key1': 'value1', 'key2': 42}", run_function_name='run', imports=[])
    codeflash_return_value = codeflash_wrap(create_dynamic_module, 'tests.inference.unit_tests.test_create_dynamic_module__unit_test_1', None, 'test_function_returning_dict', 'create_dynamic_module', '2', codeflash_loop_index, block_type_name='test_block', python_code=python_code, module_name='dict_module', skip_class_eval=False)
    codeflash_output = codeflash_return_value
    module = codeflash_output
    result = module.run()

def test_function_returning_none():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test function that explicitly returns None.'
    python_code = PythonCode(type='PythonCode', run_function_code='def run():\n    return None', run_function_name='run', imports=[])
    codeflash_return_value = codeflash_wrap(create_dynamic_module, 'tests.inference.unit_tests.test_create_dynamic_module__unit_test_1', None, 'test_function_returning_none', 'create_dynamic_module', '2', codeflash_loop_index, block_type_name='test_block', python_code=python_code, module_name='none_return_module', skip_class_eval=False)
    codeflash_output = codeflash_return_value
    module = codeflash_output

def test_function_with_string_operations():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test function with string operations.'
    python_code = PythonCode(type='PythonCode', run_function_code="def run():\n    s = 'hello'\n    return s.upper() + ' WORLD'", run_function_name='run', imports=[])
    codeflash_return_value = codeflash_wrap(create_dynamic_module, 'tests.inference.unit_tests.test_create_dynamic_module__unit_test_1', None, 'test_function_with_string_operations', 'create_dynamic_module', '2', codeflash_loop_index, block_type_name='test_block', python_code=python_code, module_name='string_module', skip_class_eval=False)
    codeflash_output = codeflash_return_value
    module = codeflash_output

def test_indentation_in_code():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that proper indentation is respected in function code.'
    python_code = PythonCode(type='PythonCode', run_function_code='def run():\n    if True:\n        x = 10\n    else:\n        x = 20\n    return x', run_function_name='run', imports=[])
    codeflash_return_value = codeflash_wrap(create_dynamic_module, 'tests.inference.unit_tests.test_create_dynamic_module__unit_test_1', None, 'test_indentation_in_code', 'create_dynamic_module', '2', codeflash_loop_index, block_type_name='test_block', python_code=python_code, module_name='indent_module', skip_class_eval=False)
    codeflash_output = codeflash_return_value
    module = codeflash_output

def test_large_function_with_many_operations():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test module with a large function performing many operations.'
    code_lines = ['def run():']
    code_lines.append('    result = 0')
    for i in range(100):
        code_lines.append(f'    result += {i}')
    code_lines.append('    return result')
    python_code = PythonCode(type='PythonCode', run_function_code='\n'.join(code_lines), run_function_name='run', imports=[])
    codeflash_return_value = codeflash_wrap(create_dynamic_module, 'tests.inference.unit_tests.test_create_dynamic_module__unit_test_1', None, 'test_large_function_with_many_operations', 'create_dynamic_module', '6', codeflash_loop_index, block_type_name='test_block', python_code=python_code, module_name='large_ops_module', skip_class_eval=False)
    codeflash_output = codeflash_return_value
    module = codeflash_output

def test_function_with_large_list():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test function creating and processing a large list.'
    python_code = PythonCode(type='PythonCode', run_function_code='def run():\n    lst = list(range(1000))\n    return len(lst)', run_function_name='run', imports=[])
    codeflash_return_value = codeflash_wrap(create_dynamic_module, 'tests.inference.unit_tests.test_create_dynamic_module__unit_test_1', None, 'test_function_with_large_list', 'create_dynamic_module', '2', codeflash_loop_index, block_type_name='test_block', python_code=python_code, module_name='large_list_module', skip_class_eval=False)
    codeflash_output = codeflash_return_value
    module = codeflash_output

def test_function_with_nested_loops():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test function with nested loops over large range.'
    python_code = PythonCode(type='PythonCode', run_function_code='def run():\n    count = 0\n    for i in range(100):\n        for j in range(10):\n            count += 1\n    return count', run_function_name='run', imports=[])
    codeflash_return_value = codeflash_wrap(create_dynamic_module, 'tests.inference.unit_tests.test_create_dynamic_module__unit_test_1', None, 'test_function_with_nested_loops', 'create_dynamic_module', '2', codeflash_loop_index, block_type_name='test_block', python_code=python_code, module_name='nested_loops_module', skip_class_eval=False)
    codeflash_output = codeflash_return_value
    module = codeflash_output

def test_function_with_large_dictionary():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test function creating and processing a large dictionary.'
    python_code = PythonCode(type='PythonCode', run_function_code='def run():\n    d = {i: i*2 for i in range(500)}\n    return len(d)', run_function_name='run', imports=[])
    codeflash_return_value = codeflash_wrap(create_dynamic_module, 'tests.inference.unit_tests.test_create_dynamic_module__unit_test_1', None, 'test_function_with_large_dictionary', 'create_dynamic_module', '2', codeflash_loop_index, block_type_name='test_block', python_code=python_code, module_name='large_dict_module', skip_class_eval=False)
    codeflash_output = codeflash_return_value
    module = codeflash_output

def test_function_with_string_concatenation_loop():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test function with string concatenation in a loop (1000 iterations).'
    python_code = PythonCode(type='PythonCode', run_function_code="def run():\n    s = ''\n    for i in range(100):\n        s += str(i)\n    return len(s)", run_function_name='run', imports=[])
    codeflash_return_value = codeflash_wrap(create_dynamic_module, 'tests.inference.unit_tests.test_create_dynamic_module__unit_test_1', None, 'test_function_with_string_concatenation_loop', 'create_dynamic_module', '2', codeflash_loop_index, block_type_name='test_block', python_code=python_code, module_name='string_concat_module', skip_class_eval=False)
    codeflash_output = codeflash_return_value
    module = codeflash_output
    result = module.run()

def test_function_with_recursive_computation():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test function with recursive computation on moderately large input.'
    python_code = PythonCode(type='PythonCode', run_function_code='def run():\n    def fib(n):\n        if n <= 1:\n            return n\n        return fib(n-1) + fib(n-2)\n    return fib(20)', run_function_name='run', imports=[])
    codeflash_return_value = codeflash_wrap(create_dynamic_module, 'tests.inference.unit_tests.test_create_dynamic_module__unit_test_1', None, 'test_function_with_recursive_computation', 'create_dynamic_module', '2', codeflash_loop_index, block_type_name='test_block', python_code=python_code, module_name='recursive_module', skip_class_eval=False)
    codeflash_output = codeflash_return_value
    module = codeflash_output

def test_multiple_modules_creation():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test creating multiple dynamic modules in sequence.'
    modules = []
    for i in range(100):
        python_code = PythonCode(type='PythonCode', run_function_code=f'def run():\n    return {i}', run_function_name='run', imports=[])
        codeflash_return_value = codeflash_wrap(create_dynamic_module, 'tests.inference.unit_tests.test_create_dynamic_module__unit_test_1', None, 'test_multiple_modules_creation', 'create_dynamic_module', '2_1', codeflash_loop_index, block_type_name=f'test_block_{i}', python_code=python_code, module_name=f'module_{i}', skip_class_eval=False)
        codeflash_output = codeflash_return_value
        module = codeflash_output
        modules.append(module)
    for i, mod in enumerate(modules):
        pass

def test_function_processing_large_range():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test function summing a large range.'
    python_code = PythonCode(type='PythonCode', run_function_code='def run():\n    return sum(range(1001))', run_function_name='run', imports=[])
    codeflash_return_value = codeflash_wrap(create_dynamic_module, 'tests.inference.unit_tests.test_create_dynamic_module__unit_test_1', None, 'test_function_processing_large_range', 'create_dynamic_module', '2', codeflash_loop_index, block_type_name='test_block', python_code=python_code, module_name='sum_range_module', skip_class_eval=False)
    codeflash_output = codeflash_return_value
    module = codeflash_output

def test_function_with_matrix_operations():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test function performing matrix-like operations on nested lists.'
    python_code = PythonCode(type='PythonCode', run_function_code='def run():\n    matrix = [[i+j for j in range(50)] for i in range(50)]\n    return len(matrix)', run_function_name='run', imports=[])
    codeflash_return_value = codeflash_wrap(create_dynamic_module, 'tests.inference.unit_tests.test_create_dynamic_module__unit_test_1', None, 'test_function_with_matrix_operations', 'create_dynamic_module', '2', codeflash_loop_index, block_type_name='test_block', python_code=python_code, module_name='matrix_module', skip_class_eval=False)
    codeflash_output = codeflash_return_value
    module = codeflash_output

def test_function_with_set_operations_large():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test function with set operations on large data.'
    python_code = PythonCode(type='PythonCode', run_function_code='def run():\n    s1 = set(range(500))\n    s2 = set(range(250, 750))\n    return len(s1 & s2)', run_function_name='run', imports=[])
    codeflash_return_value = codeflash_wrap(create_dynamic_module, 'tests.inference.unit_tests.test_create_dynamic_module__unit_test_1', None, 'test_function_with_set_operations_large', 'create_dynamic_module', '2', codeflash_loop_index, block_type_name='test_block', python_code=python_code, module_name='set_ops_module', skip_class_eval=False)
    codeflash_output = codeflash_return_value
    module = codeflash_output

def test_module_creation_with_many_imports():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test module creation with many import statements.'
    imports = [f'import math as m{i}' for i in range(50)]
    python_code = PythonCode(type='PythonCode', run_function_code="def run():\n    return 'success'", run_function_name='run', imports=imports)
    codeflash_return_value = codeflash_wrap(create_dynamic_module, 'tests.inference.unit_tests.test_create_dynamic_module__unit_test_1', None, 'test_module_creation_with_many_imports', 'create_dynamic_module', '3', codeflash_loop_index, block_type_name='test_block', python_code=python_code, module_name='many_imports_module', skip_class_eval=False)
    codeflash_output = codeflash_return_value
    module = codeflash_output

def test_function_with_complex_string_processing():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test function with complex string processing on large data.'
    python_code = PythonCode(type='PythonCode', run_function_code="def run():\n    text = ' '.join([str(i) for i in range(200)])\n    words = text.split()\n    return len(words)", run_function_name='run', imports=[])
    codeflash_return_value = codeflash_wrap(create_dynamic_module, 'tests.inference.unit_tests.test_create_dynamic_module__unit_test_1', None, 'test_function_with_complex_string_processing', 'create_dynamic_module', '2', codeflash_loop_index, block_type_name='test_block', python_code=python_code, module_name='string_processing_module', skip_class_eval=False)
    codeflash_output = codeflash_return_value
    module = codeflash_output

def test_init_and_run_both_complex():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test module with both complex init and run functions.'
    python_code = PythonCode(type='PythonCode', run_function_code='def run():\n    data = {i: i*2 for i in range(100)}\n    return sum(data.values())', run_function_name='run', init_function_code="def init():\n    result = {}\n    for i in range(50):\n        result[f'key_{i}'] = i\n    return result", init_function_name='init', imports=[])
    codeflash_return_value = codeflash_wrap(create_dynamic_module, 'tests.inference.unit_tests.test_create_dynamic_module__unit_test_1', None, 'test_init_and_run_both_complex', 'create_dynamic_module', '2', codeflash_loop_index, block_type_name='test_block', python_code=python_code, module_name='complex_init_run_module', skip_class_eval=False)
    codeflash_output = codeflash_return_value
    module = codeflash_output
    init_result = module.init()
    run_result = module.run()