from __future__ import annotations

import gc
import os
import time
from typing import Any, Callable, Optional

import pytest
from inference.core.workflows.core_steps.visualizations.text_display.utils import \
    clamp_box

def codeflash_wrap(
    codeflash_wrapped: Callable[..., Any],
    codeflash_test_module_name: str,
    codeflash_test_class_name: Optional[str],
    codeflash_test_name: str,
    codeflash_function_name: str,
    codeflash_line_id: str,
    codeflash_loop_index: int,
    *args: Any,
    **kwargs: Any,
) -> str:
    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}"
    exception = None
    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}######$!")
    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_box_fits_within_image_top_left_corner():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    codeflash_return_value = codeflash_wrap(clamp_box, 'tests.inference.unit_tests.test_clamp_box__unit_test_0', None, 'test_box_fits_within_image_top_left_corner', 'clamp_box', '0', codeflash_loop_index, 0, 0, 10, 10, 100, 100)
    codeflash_output = codeflash_return_value

def test_box_fits_within_image_center():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    codeflash_return_value = codeflash_wrap(clamp_box, 'tests.inference.unit_tests.test_clamp_box__unit_test_0', None, 'test_box_fits_within_image_center', 'clamp_box', '0', codeflash_loop_index, 45, 45, 10, 10, 100, 100)
    codeflash_output = codeflash_return_value

def test_box_near_right_edge():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    codeflash_return_value = codeflash_wrap(clamp_box, 'tests.inference.unit_tests.test_clamp_box__unit_test_0', None, 'test_box_near_right_edge', 'clamp_box', '0', codeflash_loop_index, 95, 10, 10, 10, 100, 100)
    codeflash_output = codeflash_return_value

def test_box_near_bottom_edge():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    codeflash_return_value = codeflash_wrap(clamp_box, 'tests.inference.unit_tests.test_clamp_box__unit_test_0', None, 'test_box_near_bottom_edge', 'clamp_box', '0', codeflash_loop_index, 10, 95, 10, 10, 100, 100)
    codeflash_output = codeflash_return_value

def test_box_exactly_fills_image():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    codeflash_return_value = codeflash_wrap(clamp_box, 'tests.inference.unit_tests.test_clamp_box__unit_test_0', None, 'test_box_exactly_fills_image', 'clamp_box', '0', codeflash_loop_index, 0, 0, 100, 100, 100, 100)
    codeflash_output = codeflash_return_value

def test_box_width_larger_than_image():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    codeflash_return_value = codeflash_wrap(clamp_box, 'tests.inference.unit_tests.test_clamp_box__unit_test_0', None, 'test_box_width_larger_than_image', 'clamp_box', '0', codeflash_loop_index, 50, 10, 120, 10, 100, 100)
    codeflash_output = codeflash_return_value

def test_box_height_larger_than_image():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    codeflash_return_value = codeflash_wrap(clamp_box, 'tests.inference.unit_tests.test_clamp_box__unit_test_0', None, 'test_box_height_larger_than_image', 'clamp_box', '0', codeflash_loop_index, 10, 50, 10, 120, 100, 100)
    codeflash_output = codeflash_return_value

def test_box_width_and_height_larger_than_image():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    codeflash_return_value = codeflash_wrap(clamp_box, 'tests.inference.unit_tests.test_clamp_box__unit_test_0', None, 'test_box_width_and_height_larger_than_image', 'clamp_box', '0', codeflash_loop_index, 50, 50, 120, 120, 100, 100)
    codeflash_output = codeflash_return_value

def test_box_negative_position_clamped_to_zero():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    codeflash_return_value = codeflash_wrap(clamp_box, 'tests.inference.unit_tests.test_clamp_box__unit_test_0', None, 'test_box_negative_position_clamped_to_zero', 'clamp_box', '0', codeflash_loop_index, -10, -10, 10, 10, 100, 100)
    codeflash_output = codeflash_return_value

def test_box_position_exceeds_image_bounds():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    codeflash_return_value = codeflash_wrap(clamp_box, 'tests.inference.unit_tests.test_clamp_box__unit_test_0', None, 'test_box_position_exceeds_image_bounds', 'clamp_box', '0', codeflash_loop_index, 200, 200, 10, 10, 100, 100)
    codeflash_output = codeflash_return_value

def test_box_zero_width_and_height():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    codeflash_return_value = codeflash_wrap(clamp_box, 'tests.inference.unit_tests.test_clamp_box__unit_test_0', None, 'test_box_zero_width_and_height', 'clamp_box', '0', codeflash_loop_index, 10, 10, 0, 0, 100, 100)
    codeflash_output = codeflash_return_value

def test_box_width_equal_to_image_width():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    codeflash_return_value = codeflash_wrap(clamp_box, 'tests.inference.unit_tests.test_clamp_box__unit_test_0', None, 'test_box_width_equal_to_image_width', 'clamp_box', '0', codeflash_loop_index, 50, 10, 100, 10, 100, 100)
    codeflash_output = codeflash_return_value

def test_box_height_equal_to_image_height():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    codeflash_return_value = codeflash_wrap(clamp_box, 'tests.inference.unit_tests.test_clamp_box__unit_test_0', None, 'test_box_height_equal_to_image_height', 'clamp_box', '0', codeflash_loop_index, 10, 50, 10, 100, 100, 100)
    codeflash_output = codeflash_return_value

def test_box_at_maximum_possible_position():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    codeflash_return_value = codeflash_wrap(clamp_box, 'tests.inference.unit_tests.test_clamp_box__unit_test_0', None, 'test_box_at_maximum_possible_position', 'clamp_box', '0', codeflash_loop_index, 90, 90, 10, 10, 100, 100)
    codeflash_output = codeflash_return_value

def test_box_minimum_size_at_maximum_position():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    codeflash_return_value = codeflash_wrap(clamp_box, 'tests.inference.unit_tests.test_clamp_box__unit_test_0', None, 'test_box_minimum_size_at_maximum_position', 'clamp_box', '0', codeflash_loop_index, 99, 99, 1, 1, 100, 100)
    codeflash_output = codeflash_return_value

def test_box_position_and_size_zero():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    codeflash_return_value = codeflash_wrap(clamp_box, 'tests.inference.unit_tests.test_clamp_box__unit_test_0', None, 'test_box_position_and_size_zero', 'clamp_box', '0', codeflash_loop_index, 0, 0, 0, 0, 0, 0)
    codeflash_output = codeflash_return_value

def test_box_size_zero_with_nonzero_image():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    codeflash_return_value = codeflash_wrap(clamp_box, 'tests.inference.unit_tests.test_clamp_box__unit_test_0', None, 'test_box_size_zero_with_nonzero_image', 'clamp_box', '0', codeflash_loop_index, 50, 50, 0, 0, 100, 100)
    codeflash_output = codeflash_return_value

def test_box_size_equals_image_size_and_position_nonzero():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    codeflash_return_value = codeflash_wrap(clamp_box, 'tests.inference.unit_tests.test_clamp_box__unit_test_0', None, 'test_box_size_equals_image_size_and_position_nonzero', 'clamp_box', '0', codeflash_loop_index, 10, 10, 100, 100, 100, 100)
    codeflash_output = codeflash_return_value

def test_box_size_just_one_less_than_image():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    codeflash_return_value = codeflash_wrap(clamp_box, 'tests.inference.unit_tests.test_clamp_box__unit_test_0', None, 'test_box_size_just_one_less_than_image', 'clamp_box', '0', codeflash_loop_index, 99, 99, 99, 99, 100, 100)
    codeflash_output = codeflash_return_value

def test_box_size_one_with_large_image():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    codeflash_return_value = codeflash_wrap(clamp_box, 'tests.inference.unit_tests.test_clamp_box__unit_test_0', None, 'test_box_size_one_with_large_image', 'clamp_box', '0', codeflash_loop_index, 999, 999, 1, 1, 1000, 1000)
    codeflash_output = codeflash_return_value

def test_box_negative_size():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    codeflash_return_value = codeflash_wrap(clamp_box, 'tests.inference.unit_tests.test_clamp_box__unit_test_0', None, 'test_box_negative_size', 'clamp_box', '0', codeflash_loop_index, 10, 10, -10, -10, 100, 100)
    codeflash_output = codeflash_return_value

def test_image_size_zero():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    codeflash_return_value = codeflash_wrap(clamp_box, 'tests.inference.unit_tests.test_clamp_box__unit_test_0', None, 'test_image_size_zero', 'clamp_box', '0', codeflash_loop_index, 10, 10, 10, 10, 0, 0)
    codeflash_output = codeflash_return_value

def test_box_size_larger_than_zero_image():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    codeflash_return_value = codeflash_wrap(clamp_box, 'tests.inference.unit_tests.test_clamp_box__unit_test_0', None, 'test_box_size_larger_than_zero_image', 'clamp_box', '0', codeflash_loop_index, 10, 10, 10, 10, 0, 0)
    codeflash_output = codeflash_return_value

def test_large_box_and_image():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    codeflash_return_value = codeflash_wrap(clamp_box, 'tests.inference.unit_tests.test_clamp_box__unit_test_0', None, 'test_large_box_and_image', 'clamp_box', '0', codeflash_loop_index, 500, 500, 100, 100, 1000, 1000)
    codeflash_output = codeflash_return_value

def test_large_box_exceeds_image_bounds():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    codeflash_return_value = codeflash_wrap(clamp_box, 'tests.inference.unit_tests.test_clamp_box__unit_test_0', None, 'test_large_box_exceeds_image_bounds', 'clamp_box', '0', codeflash_loop_index, 950, 950, 100, 100, 1000, 1000)
    codeflash_output = codeflash_return_value

def test_large_box_larger_than_image():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    codeflash_return_value = codeflash_wrap(clamp_box, 'tests.inference.unit_tests.test_clamp_box__unit_test_0', None, 'test_large_box_larger_than_image', 'clamp_box', '0', codeflash_loop_index, 500, 500, 2000, 2000, 1000, 1000)
    codeflash_output = codeflash_return_value

def test_large_box_zero_size():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    codeflash_return_value = codeflash_wrap(clamp_box, 'tests.inference.unit_tests.test_clamp_box__unit_test_0', None, 'test_large_box_zero_size', 'clamp_box', '0', codeflash_loop_index, 999, 999, 0, 0, 1000, 1000)
    codeflash_output = codeflash_return_value

def test_large_box_negative_position():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    codeflash_return_value = codeflash_wrap(clamp_box, 'tests.inference.unit_tests.test_clamp_box__unit_test_0', None, 'test_large_box_negative_position', 'clamp_box', '0', codeflash_loop_index, -100, -100, 100, 100, 1000, 1000)
    codeflash_output = codeflash_return_value

def test_large_box_near_right_bottom_edge():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    codeflash_return_value = codeflash_wrap(clamp_box, 'tests.inference.unit_tests.test_clamp_box__unit_test_0', None, 'test_large_box_near_right_bottom_edge', 'clamp_box', '0', codeflash_loop_index, 995, 995, 10, 10, 1000, 1000)
    codeflash_output = codeflash_return_value

def test_large_box_width_equal_to_image():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    codeflash_return_value = codeflash_wrap(clamp_box, 'tests.inference.unit_tests.test_clamp_box__unit_test_0', None, 'test_large_box_width_equal_to_image', 'clamp_box', '0', codeflash_loop_index, 500, 500, 1000, 10, 1000, 1000)
    codeflash_output = codeflash_return_value

def test_large_box_height_equal_to_image():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    codeflash_return_value = codeflash_wrap(clamp_box, 'tests.inference.unit_tests.test_clamp_box__unit_test_0', None, 'test_large_box_height_equal_to_image', 'clamp_box', '0', codeflash_loop_index, 500, 500, 10, 1000, 1000, 1000)
    codeflash_output = codeflash_return_value

def test_many_boxes_in_bounds():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    for i in range(0, 1000, 100):
        codeflash_return_value = codeflash_wrap(clamp_box, 'tests.inference.unit_tests.test_clamp_box__unit_test_0', None, 'test_many_boxes_in_bounds', 'clamp_box', '0_0', codeflash_loop_index, i, i, 10, 10, 1000, 1000)
        codeflash_output = codeflash_return_value

def test_many_boxes_out_of_bounds():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    for i in range(900, 1100, 10):
        codeflash_return_value = codeflash_wrap(clamp_box, 'tests.inference.unit_tests.test_clamp_box__unit_test_0', None, 'test_many_boxes_out_of_bounds', 'clamp_box', '0_0', codeflash_loop_index, i, i, 100, 100, 1000, 1000)
        codeflash_output = codeflash_return_value

def test_many_boxes_larger_than_image():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    for w in range(1001, 1100, 10):
        for h in range(1001, 1100, 10):
            codeflash_return_value = codeflash_wrap(clamp_box, 'tests.inference.unit_tests.test_clamp_box__unit_test_0', None, 'test_many_boxes_larger_than_image', 'clamp_box', '0_0_0', codeflash_loop_index, 50, 50, w, h, 1000, 1000)
            codeflash_output = codeflash_return_value

def test_many_boxes_zero_size():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    for i in range(0, 1000, 100):
        codeflash_return_value = codeflash_wrap(clamp_box, 'tests.inference.unit_tests.test_clamp_box__unit_test_0', None, 'test_many_boxes_zero_size', 'clamp_box', '0_0', codeflash_loop_index, i, i, 0, 0, 1000, 1000)
        codeflash_output = codeflash_return_value

def test_mutation_sensitive_x_clamping():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    codeflash_return_value = codeflash_wrap(clamp_box, 'tests.inference.unit_tests.test_clamp_box__unit_test_0', None, 'test_mutation_sensitive_x_clamping', 'clamp_box', '0', codeflash_loop_index, 999, 10, 10, 10, 1000, 1000)
    codeflash_output = codeflash_return_value

def test_mutation_sensitive_y_clamping():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    codeflash_return_value = codeflash_wrap(clamp_box, 'tests.inference.unit_tests.test_clamp_box__unit_test_0', None, 'test_mutation_sensitive_y_clamping', 'clamp_box', '0', codeflash_loop_index, 10, 999, 10, 10, 1000, 1000)
    codeflash_output = codeflash_return_value