from __future__ import annotations

import gc
import os
import time

import pytest
from inference.core.workflows.core_steps.common.query_language.errors import \
    InvalidInputTypeError
from inference.core.workflows.core_steps.common.query_language.operations.sequences.base import \
    sequence_apply


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_sequence_apply_with_list_and_simple_function():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test basic functionality: apply a simple function to each element in a list.'
    codeflash_return_value = codeflash_wrap(sequence_apply, 'tests.inference.unit_tests.test_sequence_apply__unit_test_1', None, 'test_sequence_apply_with_list_and_simple_function', 'sequence_apply', '1', codeflash_loop_index, [1, 2, 3], lambda x: x * 2, 'test_context')
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_sequence_apply_with_empty_list():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that applying a function to an empty list returns an empty list.'
    codeflash_return_value = codeflash_wrap(sequence_apply, 'tests.inference.unit_tests.test_sequence_apply__unit_test_1', None, 'test_sequence_apply_with_empty_list', 'sequence_apply', '1', codeflash_loop_index, [], lambda x: x * 2, 'test_context')
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_sequence_apply_with_tuple():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that sequence_apply works with tuples (iterable).'
    codeflash_return_value = codeflash_wrap(sequence_apply, 'tests.inference.unit_tests.test_sequence_apply__unit_test_1', None, 'test_sequence_apply_with_tuple', 'sequence_apply', '1', codeflash_loop_index, (1, 2, 3), lambda x: x + 1, 'test_context')
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_sequence_apply_with_string_sequence():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that sequence_apply works with strings (iterables of characters).'
    codeflash_return_value = codeflash_wrap(sequence_apply, 'tests.inference.unit_tests.test_sequence_apply__unit_test_1', None, 'test_sequence_apply_with_string_sequence', 'sequence_apply', '1', codeflash_loop_index, 'abc', lambda x: x.upper(), 'test_context')
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_sequence_apply_with_function_returning_different_type():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test function that transforms elements to a different type.'
    codeflash_return_value = codeflash_wrap(sequence_apply, 'tests.inference.unit_tests.test_sequence_apply__unit_test_1', None, 'test_sequence_apply_with_function_returning_different_type', 'sequence_apply', '1', codeflash_loop_index, [1, 2, 3], lambda x: str(x), 'test_context')
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_sequence_apply_with_kwargs():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that sequence_apply correctly passes kwargs to the function.'

    def add_n(x, n=0):
        return x + n
    codeflash_return_value = codeflash_wrap(sequence_apply, 'tests.inference.unit_tests.test_sequence_apply__unit_test_1', None, 'test_sequence_apply_with_kwargs', 'sequence_apply', '2', codeflash_loop_index, [1, 2, 3], add_n, 'test_context', n=10)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_sequence_apply_with_negative_numbers():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test sequence_apply with negative numbers in the sequence.'
    codeflash_return_value = codeflash_wrap(sequence_apply, 'tests.inference.unit_tests.test_sequence_apply__unit_test_1', None, 'test_sequence_apply_with_negative_numbers', 'sequence_apply', '1', codeflash_loop_index, [-1, -2, -3], lambda x: x * -1, 'test_context')
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_sequence_apply_with_mixed_numeric_types():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test sequence_apply with mixed int and float types.'
    codeflash_return_value = codeflash_wrap(sequence_apply, 'tests.inference.unit_tests.test_sequence_apply__unit_test_1', None, 'test_sequence_apply_with_mixed_numeric_types', 'sequence_apply', '1', codeflash_loop_index, [1, 2.5, 3], lambda x: x * 2, 'test_context')
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_sequence_apply_with_zero_values():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test sequence_apply with zero values in the sequence.'
    codeflash_return_value = codeflash_wrap(sequence_apply, 'tests.inference.unit_tests.test_sequence_apply__unit_test_1', None, 'test_sequence_apply_with_zero_values', 'sequence_apply', '1', codeflash_loop_index, [0, 1, 0, 2], lambda x: x + 5, 'test_context')
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_sequence_apply_identity_function():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test sequence_apply with an identity function that returns inputs unchanged.'
    input_list = [1, 2, 3]
    codeflash_return_value = codeflash_wrap(sequence_apply, 'tests.inference.unit_tests.test_sequence_apply__unit_test_1', None, 'test_sequence_apply_identity_function', 'sequence_apply', '2', codeflash_loop_index, input_list, lambda x: x, 'test_context')
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_sequence_apply_with_none_in_sequence():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that None values in a sequence are passed to the function.'
    codeflash_return_value = codeflash_wrap(sequence_apply, 'tests.inference.unit_tests.test_sequence_apply__unit_test_1', None, 'test_sequence_apply_with_none_in_sequence', 'sequence_apply', '1', codeflash_loop_index, [1, None, 3], lambda x: x if x is not None else 0, 'test_context')
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_sequence_apply_with_all_none_values():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test sequence with all None values.'
    codeflash_return_value = codeflash_wrap(sequence_apply, 'tests.inference.unit_tests.test_sequence_apply__unit_test_1', None, 'test_sequence_apply_with_all_none_values', 'sequence_apply', '1', codeflash_loop_index, [None, None], lambda x: x, 'test_context')
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_sequence_apply_with_boolean_values():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test sequence_apply with boolean values.'
    codeflash_return_value = codeflash_wrap(sequence_apply, 'tests.inference.unit_tests.test_sequence_apply__unit_test_1', None, 'test_sequence_apply_with_boolean_values', 'sequence_apply', '1', codeflash_loop_index, [True, False, True], lambda x: not x, 'test_context')
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_sequence_apply_with_single_element():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test sequence_apply with a single-element sequence.'
    codeflash_return_value = codeflash_wrap(sequence_apply, 'tests.inference.unit_tests.test_sequence_apply__unit_test_1', None, 'test_sequence_apply_with_single_element', 'sequence_apply', '1', codeflash_loop_index, [42], lambda x: x * 2, 'test_context')
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_sequence_apply_with_nested_lists():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test sequence_apply where elements are themselves lists.'
    codeflash_return_value = codeflash_wrap(sequence_apply, 'tests.inference.unit_tests.test_sequence_apply__unit_test_1', None, 'test_sequence_apply_with_nested_lists', 'sequence_apply', '1', codeflash_loop_index, [[1, 2], [3, 4]], lambda x: len(x), 'test_context')
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_sequence_apply_with_dict_values():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test sequence_apply with dictionaries as elements.'
    codeflash_return_value = codeflash_wrap(sequence_apply, 'tests.inference.unit_tests.test_sequence_apply__unit_test_1', None, 'test_sequence_apply_with_dict_values', 'sequence_apply', '1', codeflash_loop_index, [{'a': 1}, {'b': 2}], lambda x: len(x), 'test_context')
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_sequence_apply_with_complex_objects():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test sequence_apply with custom objects as elements.'

    class Point:

        def __init__(self, x, y):
            self.x = x
            self.y = y

        def sum_coords(self):
            return self.x + self.y
    points = [Point(1, 2), Point(3, 4), Point(5, 6)]
    codeflash_return_value = codeflash_wrap(sequence_apply, 'tests.inference.unit_tests.test_sequence_apply__unit_test_1', None, 'test_sequence_apply_with_complex_objects', 'sequence_apply', '3', codeflash_loop_index, points, lambda p: p.sum_coords(), 'test_context')
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_sequence_apply_with_generator_expression():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test sequence_apply with a generator (iterable but not a list/tuple).'
    codeflash_return_value = codeflash_wrap(sequence_apply, 'tests.inference.unit_tests.test_sequence_apply__unit_test_1', None, 'test_sequence_apply_with_generator_expression', 'sequence_apply', '1', codeflash_loop_index, (x for x in [1, 2, 3]), lambda x: x ** 2, 'test_context')
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_sequence_apply_with_range_object():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test sequence_apply with a range object.'
    codeflash_return_value = codeflash_wrap(sequence_apply, 'tests.inference.unit_tests.test_sequence_apply__unit_test_1', None, 'test_sequence_apply_with_range_object', 'sequence_apply', '1', codeflash_loop_index, range(3), lambda x: x * 10, 'test_context')
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_sequence_apply_with_set():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test sequence_apply with a set (unordered iterable).'
    codeflash_return_value = codeflash_wrap(sequence_apply, 'tests.inference.unit_tests.test_sequence_apply__unit_test_1', None, 'test_sequence_apply_with_set', 'sequence_apply', '1', codeflash_loop_index, {1, 2, 3}, lambda x: x + 1, 'test_context')
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_sequence_apply_with_float_values():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test sequence_apply with floating-point values.'
    codeflash_return_value = codeflash_wrap(sequence_apply, 'tests.inference.unit_tests.test_sequence_apply__unit_test_1', None, 'test_sequence_apply_with_float_values', 'sequence_apply', '1', codeflash_loop_index, [1.5, 2.5, 3.5], lambda x: x + 0.5, 'test_context')
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_sequence_apply_with_string_concatenation():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test sequence_apply with string concatenation.'
    codeflash_return_value = codeflash_wrap(sequence_apply, 'tests.inference.unit_tests.test_sequence_apply__unit_test_1', None, 'test_sequence_apply_with_string_concatenation', 'sequence_apply', '1', codeflash_loop_index, ['a', 'b', 'c'], lambda x: x + '!', 'test_context')
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_sequence_apply_with_empty_strings():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test sequence_apply with empty strings in the sequence.'
    codeflash_return_value = codeflash_wrap(sequence_apply, 'tests.inference.unit_tests.test_sequence_apply__unit_test_1', None, 'test_sequence_apply_with_empty_strings', 'sequence_apply', '1', codeflash_loop_index, ['', 'hello', ''], lambda x: len(x), 'test_context')
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_sequence_apply_with_very_long_function_name():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test sequence_apply works regardless of execution context string length.'
    long_context = 'a' * 1000
    codeflash_return_value = codeflash_wrap(sequence_apply, 'tests.inference.unit_tests.test_sequence_apply__unit_test_1', None, 'test_sequence_apply_with_very_long_function_name', 'sequence_apply', '2', codeflash_loop_index, [1, 2], lambda x: x, long_context)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_sequence_apply_non_sequence_integer_raises_error():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that passing a non-iterable (integer) raises InvalidInputTypeError.'
    with pytest.raises(InvalidInputTypeError) as exc_info:
        codeflash_return_value = codeflash_wrap(sequence_apply, 'tests.inference.unit_tests.test_sequence_apply__unit_test_1', None, 'test_sequence_apply_non_sequence_integer_raises_error', 'sequence_apply', '1_0', codeflash_loop_index, 42, lambda x: x, 'test_context')

def test_sequence_apply_non_sequence_none_raises_error():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that passing None as the sequence raises InvalidInputTypeError.'
    with pytest.raises(InvalidInputTypeError):
        codeflash_return_value = codeflash_wrap(sequence_apply, 'tests.inference.unit_tests.test_sequence_apply__unit_test_1', None, 'test_sequence_apply_non_sequence_none_raises_error', 'sequence_apply', '1_0', codeflash_loop_index, None, lambda x: x, 'test_context')

def test_sequence_apply_non_sequence_float_raises_error():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that passing a float as the sequence raises InvalidInputTypeError.'
    with pytest.raises(InvalidInputTypeError):
        codeflash_return_value = codeflash_wrap(sequence_apply, 'tests.inference.unit_tests.test_sequence_apply__unit_test_1', None, 'test_sequence_apply_non_sequence_float_raises_error', 'sequence_apply', '1_0', codeflash_loop_index, 3.14, lambda x: x, 'test_context')

def test_sequence_apply_error_includes_execution_context():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that InvalidInputTypeError includes the execution context.'
    context_name = 'my_special_context'
    with pytest.raises(InvalidInputTypeError) as exc_info:
        codeflash_return_value = codeflash_wrap(sequence_apply, 'tests.inference.unit_tests.test_sequence_apply__unit_test_1', None, 'test_sequence_apply_error_includes_execution_context', 'sequence_apply', '2_0', codeflash_loop_index, 123, lambda x: x, context_name)
    error = exc_info.value

def test_sequence_apply_with_function_raising_value_error():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that errors from the function are caught and wrapped.'

    def bad_function(x):
        raise ValueError('Custom error')
    with pytest.raises(InvalidInputTypeError):
        codeflash_return_value = codeflash_wrap(sequence_apply, 'tests.inference.unit_tests.test_sequence_apply__unit_test_1', None, 'test_sequence_apply_with_function_raising_value_error', 'sequence_apply', '2_0', codeflash_loop_index, [1, 2, 3], bad_function, 'test_context')

def test_sequence_apply_with_function_raising_type_error():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that TypeError from function is caught and wrapped.'

    def bad_function(x):
        raise TypeError('Type mismatch')
    with pytest.raises(InvalidInputTypeError):
        codeflash_return_value = codeflash_wrap(sequence_apply, 'tests.inference.unit_tests.test_sequence_apply__unit_test_1', None, 'test_sequence_apply_with_function_raising_type_error', 'sequence_apply', '2_0', codeflash_loop_index, [1, 2, 3], bad_function, 'test_context')

def test_sequence_apply_preserves_inner_error_on_value_error():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that the inner error from the function is preserved in InvalidInputTypeError.'

    def bad_function(x):
        raise ValueError('Original error message')
    with pytest.raises(InvalidInputTypeError) as exc_info:
        codeflash_return_value = codeflash_wrap(sequence_apply, 'tests.inference.unit_tests.test_sequence_apply__unit_test_1', None, 'test_sequence_apply_preserves_inner_error_on_value_error', 'sequence_apply', '2_0', codeflash_loop_index, [1, 2], bad_function, 'test_context')
    error = exc_info.value

def test_sequence_apply_preserves_inner_error_on_type_error():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that the inner error from TypeError is preserved in InvalidInputTypeError.'

    def bad_function(x):
        raise TypeError('Type problem')
    with pytest.raises(InvalidInputTypeError) as exc_info:
        codeflash_return_value = codeflash_wrap(sequence_apply, 'tests.inference.unit_tests.test_sequence_apply__unit_test_1', None, 'test_sequence_apply_preserves_inner_error_on_type_error', 'sequence_apply', '2_0', codeflash_loop_index, [1, 2], bad_function, 'test_context')
    error = exc_info.value

def test_sequence_apply_with_kwargs_and_multiple_params():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test sequence_apply with multiple kwargs.'

    def multiply_and_add(x, factor=1, offset=0):
        return x * factor + offset
    codeflash_return_value = codeflash_wrap(sequence_apply, 'tests.inference.unit_tests.test_sequence_apply__unit_test_1', None, 'test_sequence_apply_with_kwargs_and_multiple_params', 'sequence_apply', '2', codeflash_loop_index, [1, 2, 3], multiply_and_add, 'test_context', factor=2, offset=10)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_sequence_apply_with_special_characters_in_context():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test sequence_apply handles special characters in execution context.'
    context = 'context | with | special_chars_!@#$%'
    codeflash_return_value = codeflash_wrap(sequence_apply, 'tests.inference.unit_tests.test_sequence_apply__unit_test_1', None, 'test_sequence_apply_with_special_characters_in_context', 'sequence_apply', '2', codeflash_loop_index, [1, 2], lambda x: x, context)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_sequence_apply_with_1000_elements():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test sequence_apply with a large list of 1000 elements.'
    large_list = list(range(1000))
    codeflash_return_value = codeflash_wrap(sequence_apply, 'tests.inference.unit_tests.test_sequence_apply__unit_test_1', None, 'test_sequence_apply_with_1000_elements', 'sequence_apply', '2', codeflash_loop_index, large_list, lambda x: x * 2, 'test_context')
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_sequence_apply_with_large_list_string_operations():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test sequence_apply with 1000 string elements.'
    large_list = [f'item_{i}' for i in range(1000)]
    codeflash_return_value = codeflash_wrap(sequence_apply, 'tests.inference.unit_tests.test_sequence_apply__unit_test_1', None, 'test_sequence_apply_with_large_list_string_operations', 'sequence_apply', '2', codeflash_loop_index, large_list, lambda x: x.upper(), 'test_context')
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_sequence_apply_with_large_nested_structures():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test sequence_apply with 1000 elements that are dictionaries.'
    large_list = [{'id': i, 'value': i * 2} for i in range(1000)]
    codeflash_return_value = codeflash_wrap(sequence_apply, 'tests.inference.unit_tests.test_sequence_apply__unit_test_1', None, 'test_sequence_apply_with_large_nested_structures', 'sequence_apply', '2', codeflash_loop_index, large_list, lambda x: x['value'], 'test_context')
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_sequence_apply_chained_operations_on_large_list():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test multiple sequence_apply calls on a large list.'
    large_list = list(range(500))
    codeflash_return_value = codeflash_wrap(sequence_apply, 'tests.inference.unit_tests.test_sequence_apply__unit_test_1', None, 'test_sequence_apply_chained_operations_on_large_list', 'sequence_apply', '2', codeflash_loop_index, large_list, lambda x: x * 2, 'context_1')
    codeflash_output = codeflash_return_value
    result1 = codeflash_output
    codeflash_return_value = codeflash_wrap(sequence_apply, 'tests.inference.unit_tests.test_sequence_apply__unit_test_1', None, 'test_sequence_apply_chained_operations_on_large_list', 'sequence_apply', '4', codeflash_loop_index, result1, lambda x: str(x), 'context_2')
    codeflash_output = codeflash_return_value
    result2 = codeflash_output

def test_sequence_apply_with_1000_tuples():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test sequence_apply with 1000 tuples as elements.'
    large_list = [(i, i * 2) for i in range(1000)]
    codeflash_return_value = codeflash_wrap(sequence_apply, 'tests.inference.unit_tests.test_sequence_apply__unit_test_1', None, 'test_sequence_apply_with_1000_tuples', 'sequence_apply', '2', codeflash_loop_index, large_list, lambda x: x[0] + x[1], 'test_context')
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_sequence_apply_with_large_list_complex_function():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test sequence_apply with 1000 elements and a complex function.'

    def complex_function(x):
        return {'original': x, 'doubled': x * 2, 'squared': x ** 2, 'string': str(x)}
    large_list = list(range(1000))
    codeflash_return_value = codeflash_wrap(sequence_apply, 'tests.inference.unit_tests.test_sequence_apply__unit_test_1', None, 'test_sequence_apply_with_large_list_complex_function', 'sequence_apply', '3', codeflash_loop_index, large_list, complex_function, 'test_context')
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_sequence_apply_with_1000_none_conversions():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test sequence_apply converting 1000 optional values.'
    large_list = [i if i % 2 == 0 else None for i in range(1000)]
    codeflash_return_value = codeflash_wrap(sequence_apply, 'tests.inference.unit_tests.test_sequence_apply__unit_test_1', None, 'test_sequence_apply_with_1000_none_conversions', 'sequence_apply', '2', codeflash_loop_index, large_list, lambda x: 0 if x is None else x * 2, 'test_context')
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_sequence_apply_with_alternating_large_sequence():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test sequence_apply with alternating True/False values on large list.'
    large_list = [i % 2 == 0 for i in range(1000)]
    codeflash_return_value = codeflash_wrap(sequence_apply, 'tests.inference.unit_tests.test_sequence_apply__unit_test_1', None, 'test_sequence_apply_with_alternating_large_sequence', 'sequence_apply', '2', codeflash_loop_index, large_list, lambda x: 'even' if x else 'odd', 'test_context')
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_sequence_apply_with_1000_list_elements():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test sequence_apply where each element is a list of 1000 items.'
    large_list = [[j for j in range(100)] for i in range(100)]
    codeflash_return_value = codeflash_wrap(sequence_apply, 'tests.inference.unit_tests.test_sequence_apply__unit_test_1', None, 'test_sequence_apply_with_1000_list_elements', 'sequence_apply', '2', codeflash_loop_index, large_list, lambda x: sum(x), 'test_context')
    codeflash_output = codeflash_return_value
    result = codeflash_output
    expected_sum = sum(range(100))

def test_sequence_apply_large_list_with_kwargs():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test sequence_apply with large list and kwargs.'

    def weighted_sum(x, weight=1):
        return sum(x) * weight if isinstance(x, list) else x * weight
    large_list = [[i, i + 1, i + 2] for i in range(500)]
    codeflash_return_value = codeflash_wrap(sequence_apply, 'tests.inference.unit_tests.test_sequence_apply__unit_test_1', None, 'test_sequence_apply_large_list_with_kwargs', 'sequence_apply', '3', codeflash_loop_index, large_list, weighted_sum, 'test_context', weight=3)
    codeflash_output = codeflash_return_value
    result = codeflash_output