from __future__ import annotations

import gc
import inspect
import os
import time
from pathlib import Path

import cv2 as cv
import dill as pickle
import numpy as np
import pytest
import supervision as sv
from inference.core.workflows.core_steps.classical_cv.mask_area_measurement.v1 import (
    OUTPUT_KEY, MaskAreaMeasurementBlockV1, get_detection_area)


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}######!')
    iteration = os.environ['CODEFLASH_TEST_ITERATION']
    with Path('{codeflash_run_tmp_dir_client_side}', f'test_return_values_{iteration}.bin').open('ab') as f:
        pickled_values = pickle.dumps((args, kwargs, exception)) if exception else pickle.dumps((args, kwargs, return_value))
        _test_name = f"{codeflash_test_module_name}:{(codeflash_test_class_name + '.' if codeflash_test_class_name else '')}{codeflash_test_name}:{codeflash_function_name}:{codeflash_line_id}".encode('ascii')
        f.write(len(_test_name).to_bytes(4, byteorder='big'))
        f.write(_test_name)
        f.write(codeflash_duration.to_bytes(8, byteorder='big'))
        f.write(len(pickled_values).to_bytes(4, byteorder='big'))
        f.write(pickled_values)
        f.write(codeflash_loop_index.to_bytes(8, byteorder='big'))
        f.write(len(invocation_id).to_bytes(4, byteorder='big'))
        f.write(invocation_id.encode('ascii'))
    if exception:
        raise exception
    return return_value

def test_run_single_detection_with_bbox_only():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test run method with a single detection without segmentation mask.'
    xyxy = np.array([[10.0, 20.0, 50.0, 80.0]])
    detections = sv.Detections(xyxy=xyxy)
    block = MaskAreaMeasurementBlockV1()
    _call__bound__arguments = inspect.signature(block.run).bind(predictions=detections)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(block.run, 'tests.inference.unit_tests.test_run__unit_test_1', None, 'test_run_single_detection_with_bbox_only', 'run', '4', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_run_multiple_detections_bbox_only():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test run method with multiple detections without segmentation masks.'
    xyxy = np.array([[0.0, 0.0, 10.0, 10.0], [5.0, 5.0, 15.0, 20.0], [100.0, 100.0, 110.0, 110.0]])
    detections = sv.Detections(xyxy=xyxy)
    block = MaskAreaMeasurementBlockV1()
    _call__bound__arguments = inspect.signature(block.run).bind(predictions=detections)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(block.run, 'tests.inference.unit_tests.test_run__unit_test_1', None, 'test_run_multiple_detections_bbox_only', 'run', '4', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_run_single_detection_with_mask():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test run method with a single detection including a segmentation mask.'
    mask = np.zeros((100, 100), dtype=bool)
    mask[20:50, 30:70] = True
    xyxy = np.array([[20.0, 20.0, 70.0, 50.0]])
    detections = sv.Detections(xyxy=xyxy, mask=np.array([mask]))
    block = MaskAreaMeasurementBlockV1()
    _call__bound__arguments = inspect.signature(block.run).bind(predictions=detections)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(block.run, 'tests.inference.unit_tests.test_run__unit_test_1', None, 'test_run_single_detection_with_mask', 'run', '6', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_run_multiple_detections_with_masks():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test run method with multiple detections with segmentation masks.'
    mask1 = np.zeros((100, 100), dtype=bool)
    mask2 = np.zeros((100, 100), dtype=bool)
    mask1[10:40, 10:40] = True
    mask2[50:80, 50:80] = True
    xyxy = np.array([[10.0, 10.0, 40.0, 40.0], [50.0, 50.0, 80.0, 80.0]])
    detections = sv.Detections(xyxy=xyxy, mask=np.array([mask1, mask2]))
    block = MaskAreaMeasurementBlockV1()
    _call__bound__arguments = inspect.signature(block.run).bind(predictions=detections)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(block.run, 'tests.inference.unit_tests.test_run__unit_test_1', None, 'test_run_multiple_detections_with_masks', 'run', '8', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_get_detection_area_with_bbox_only():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test get_detection_area helper function with bbox-only detection.'
    xyxy = np.array([[10.0, 10.0, 50.0, 50.0]])
    detections = sv.Detections(xyxy=xyxy)
    area = get_detection_area(detections, 0)

def test_get_detection_area_with_mask():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test get_detection_area helper function with mask.'
    mask = np.zeros((100, 100), dtype=bool)
    mask[20:60, 30:70] = True
    xyxy = np.array([[20.0, 20.0, 70.0, 60.0]])
    detections = sv.Detections(xyxy=xyxy, mask=np.array([mask]))
    area = get_detection_area(detections, 0)

def test_run_returns_dict_with_correct_key():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that run method returns a dictionary with the correct key.'
    xyxy = np.array([[0.0, 0.0, 5.0, 5.0]])
    detections = sv.Detections(xyxy=xyxy)
    block = MaskAreaMeasurementBlockV1()
    _call__bound__arguments = inspect.signature(block.run).bind(predictions=detections)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(block.run, 'tests.inference.unit_tests.test_run__unit_test_1', None, 'test_run_returns_dict_with_correct_key', 'run', '4', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_run_empty_detections():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test run method with empty detections object.'
    xyxy = np.empty((0, 4), dtype=np.float32)
    detections = sv.Detections(xyxy=xyxy)
    block = MaskAreaMeasurementBlockV1()
    _call__bound__arguments = inspect.signature(block.run).bind(predictions=detections)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(block.run, 'tests.inference.unit_tests.test_run__unit_test_1', None, 'test_run_empty_detections', 'run', '4', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_run_zero_area_bbox():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test with detection having zero area bbox (single point or line).'
    xyxy = np.array([[10.0, 10.0, 10.0, 10.0]])
    detections = sv.Detections(xyxy=xyxy)
    block = MaskAreaMeasurementBlockV1()
    _call__bound__arguments = inspect.signature(block.run).bind(predictions=detections)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(block.run, 'tests.inference.unit_tests.test_run__unit_test_1', None, 'test_run_zero_area_bbox', 'run', '4', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_run_very_small_bbox():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test with very small bounding box (fractional coordinates).'
    xyxy = np.array([[0.0, 0.0, 0.5, 0.5]])
    detections = sv.Detections(xyxy=xyxy)
    block = MaskAreaMeasurementBlockV1()
    _call__bound__arguments = inspect.signature(block.run).bind(predictions=detections)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(block.run, 'tests.inference.unit_tests.test_run__unit_test_1', None, 'test_run_very_small_bbox', 'run', '4', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_run_large_bbox():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test with very large bounding box.'
    xyxy = np.array([[0.0, 0.0, 10000.0, 10000.0]])
    detections = sv.Detections(xyxy=xyxy)
    block = MaskAreaMeasurementBlockV1()
    _call__bound__arguments = inspect.signature(block.run).bind(predictions=detections)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(block.run, 'tests.inference.unit_tests.test_run__unit_test_1', None, 'test_run_large_bbox', 'run', '4', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_get_detection_area_mask_with_empty_contours():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test get_detection_area when mask has no contours (empty mask).'
    mask = np.zeros((100, 100), dtype=bool)
    xyxy = np.array([[10.0, 10.0, 50.0, 50.0]])
    detections = sv.Detections(xyxy=xyxy, mask=np.array([mask]))
    area = get_detection_area(detections, 0)

def test_get_detection_area_mask_with_zero_area_contour():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test get_detection_area with a mask that has zero area contours.'
    mask = np.zeros((100, 100), dtype=np.uint8)
    mask[50, 10:90] = 1
    xyxy = np.array([[10.0, 40.0, 90.0, 60.0]])
    detections = sv.Detections(xyxy=xyxy, mask=np.array([mask.astype(bool)]))
    area = get_detection_area(detections, 0)

def test_run_negative_coordinates():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test with negative bounding box coordinates.'
    xyxy = np.array([[-50.0, -50.0, 50.0, 50.0]])
    detections = sv.Detections(xyxy=xyxy)
    block = MaskAreaMeasurementBlockV1()
    _call__bound__arguments = inspect.signature(block.run).bind(predictions=detections)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(block.run, 'tests.inference.unit_tests.test_run__unit_test_1', None, 'test_run_negative_coordinates', 'run', '4', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_run_inverted_bbox_coordinates():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test with inverted bbox coordinates (x2 < x1).'
    xyxy = np.array([[50.0, 50.0, 10.0, 100.0]])
    detections = sv.Detections(xyxy=xyxy)
    block = MaskAreaMeasurementBlockV1()
    _call__bound__arguments = inspect.signature(block.run).bind(predictions=detections)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(block.run, 'tests.inference.unit_tests.test_run__unit_test_1', None, 'test_run_inverted_bbox_coordinates', 'run', '4', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_get_detection_area_mask_single_pixel():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test get_detection_area with mask containing single pixel.'
    mask = np.zeros((100, 100), dtype=bool)
    mask[50, 50] = True
    xyxy = np.array([[45.0, 45.0, 55.0, 55.0]])
    detections = sv.Detections(xyxy=xyxy, mask=np.array([mask]))
    area = get_detection_area(detections, 0)

def test_run_detection_at_image_boundary():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test detection positioned at image boundaries.'
    xyxy = np.array([[0.0, 0.0, 100.0, 100.0]])
    detections = sv.Detections(xyxy=xyxy)
    block = MaskAreaMeasurementBlockV1()
    _call__bound__arguments = inspect.signature(block.run).bind(predictions=detections)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(block.run, 'tests.inference.unit_tests.test_run__unit_test_1', None, 'test_run_detection_at_image_boundary', 'run', '4', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_run_rectangular_mask_vs_bbox_fallback():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that mask area is used when mask exists, not bbox fallback.'
    mask = np.zeros((200, 200), dtype=bool)
    mask[50:100, 50:100] = True
    xyxy = np.array([[0.0, 0.0, 200.0, 200.0]])
    detections = sv.Detections(xyxy=xyxy, mask=np.array([mask]))
    area = get_detection_area(detections, 0)

def test_run_many_detections_bbox_only():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test run method with 100 detections.'
    xyxy = np.array([[float(i * 10), float(i * 10), float(i * 10 + 50), float(i * 10 + 50)] for i in range(100)])
    detections = sv.Detections(xyxy=xyxy)
    block = MaskAreaMeasurementBlockV1()
    _call__bound__arguments = inspect.signature(block.run).bind(predictions=detections)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(block.run, 'tests.inference.unit_tests.test_run__unit_test_1', None, 'test_run_many_detections_bbox_only', 'run', '4', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    result = codeflash_output
    for area in result[OUTPUT_KEY]:
        pass

def test_run_many_detections_with_masks():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test run method with 50 detections with masks.'
    masks = []
    xyxy_list = []
    for i in range(50):
        mask = np.zeros((100, 100), dtype=bool)
        y_start = i % 10 * 5
        x_start = i // 10 * 10
        mask[y_start:y_start + 20, x_start:x_start + 20] = True
        masks.append(mask)
        xyxy_list.append([float(x_start), float(y_start), float(x_start + 30), float(y_start + 30)])
    xyxy = np.array(xyxy_list)
    detections = sv.Detections(xyxy=xyxy, mask=np.array(masks))
    block = MaskAreaMeasurementBlockV1()
    _call__bound__arguments = inspect.signature(block.run).bind(predictions=detections)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(block.run, 'tests.inference.unit_tests.test_run__unit_test_1', None, 'test_run_many_detections_with_masks', 'run', '7', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    result = codeflash_output
    for area in result[OUTPUT_KEY]:
        pass

def test_run_1000_detections_bbox_only():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test run method with 1000 detections for performance.'
    xyxy = np.array([[float(i), float(i), float(i + 10), float(i + 10)] for i in range(1000)])
    detections = sv.Detections(xyxy=xyxy)
    block = MaskAreaMeasurementBlockV1()
    _call__bound__arguments = inspect.signature(block.run).bind(predictions=detections)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(block.run, 'tests.inference.unit_tests.test_run__unit_test_1', None, 'test_run_1000_detections_bbox_only', 'run', '4', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_run_mixed_mask_and_bbox_detections():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test with some detections having masks and others not.'
    masks_list = [np.ones((100, 100), dtype=bool) * False, np.ones((100, 100), dtype=bool) * False, np.ones((100, 100), dtype=bool) * False]
    masks_list[0][20:40, 20:40] = True
    masks_list[1][30:60, 30:60] = True
    masks_list[2][10:50, 10:50] = True
    all_masks = masks_list + [np.zeros((100, 100), dtype=bool), np.zeros((100, 100), dtype=bool)]
    xyxy = np.array([[10.0, 10.0, 50.0, 50.0], [20.0, 20.0, 80.0, 80.0], [0.0, 0.0, 100.0, 100.0], [0.0, 0.0, 100.0, 100.0], [50.0, 50.0, 100.0, 100.0]])
    detections = sv.Detections(xyxy=xyxy, mask=np.array(all_masks))
    block = MaskAreaMeasurementBlockV1()
    _call__bound__arguments = inspect.signature(block.run).bind(predictions=detections)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(block.run, 'tests.inference.unit_tests.test_run__unit_test_1', None, 'test_run_mixed_mask_and_bbox_detections', 'run', '9', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_run_large_masks():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test with large resolution masks (1000x1000).'
    mask = np.zeros((1000, 1000), dtype=bool)
    mask[100:900, 100:900] = True
    xyxy = np.array([[0.0, 0.0, 1000.0, 1000.0]])
    detections = sv.Detections(xyxy=xyxy, mask=np.array([mask]))
    area = get_detection_area(detections, 0)

def test_run_many_detections_with_various_shapes():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test 100 detections with different aspect ratios.'
    xyxy = np.array([[float(i * 5), float(i * 2), float(i * 5 + 100), float(i * 2 + 10)] if i % 2 == 0 else [float(i * 5), float(i * 2), float(i * 5 + 10), float(i * 2 + 100)] for i in range(100)])
    detections = sv.Detections(xyxy=xyxy)
    block = MaskAreaMeasurementBlockV1()
    _call__bound__arguments = inspect.signature(block.run).bind(predictions=detections)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(block.run, 'tests.inference.unit_tests.test_run__unit_test_1', None, 'test_run_many_detections_with_various_shapes', 'run', '4', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_get_detection_area_index_boundaries():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test get_detection_area with different index values.'
    xyxy = np.array([[0.0, 0.0, 10.0, 10.0], [20.0, 20.0, 40.0, 40.0], [50.0, 50.0, 80.0, 80.0]])
    detections = sv.Detections(xyxy=xyxy)
    area0 = get_detection_area(detections, 0)
    area1 = get_detection_area(detections, 1)
    area2 = get_detection_area(detections, 2)

def test_run_preserves_order_of_areas():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that areas are returned in the same order as input detections.'
    xyxy = np.array([[0.0, 0.0, 10.0, 10.0], [0.0, 0.0, 5.0, 5.0], [0.0, 0.0, 20.0, 20.0]])
    detections = sv.Detections(xyxy=xyxy)
    block = MaskAreaMeasurementBlockV1()
    _call__bound__arguments = inspect.signature(block.run).bind(predictions=detections)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(block.run, 'tests.inference.unit_tests.test_run__unit_test_1', None, 'test_run_preserves_order_of_areas', 'run', '4', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_run_returns_float_values():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that all returned areas are float type.'
    xyxy = np.array([[0.0, 0.0, 5.0, 5.0], [10.0, 10.0, 20.0, 20.0]])
    detections = sv.Detections(xyxy=xyxy)
    block = MaskAreaMeasurementBlockV1()
    _call__bound__arguments = inspect.signature(block.run).bind(predictions=detections)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(block.run, 'tests.inference.unit_tests.test_run__unit_test_1', None, 'test_run_returns_float_values', 'run', '4', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    result = codeflash_output
    for area in result[OUTPUT_KEY]:
        pass

def test_run_with_integer_coordinates():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test run with integer coordinates in detection bboxes.'
    xyxy = np.array([[10, 10, 30, 40]], dtype=np.int32)
    detections = sv.Detections(xyxy=xyxy.astype(np.float32))
    block = MaskAreaMeasurementBlockV1()
    _call__bound__arguments = inspect.signature(block.run).bind(predictions=detections)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(block.run, 'tests.inference.unit_tests.test_run__unit_test_1', None, 'test_run_with_integer_coordinates', 'run', '4', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_run_with_float32_coordinates():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test run with float32 precision coordinates.'
    xyxy = np.array([[10.5, 20.5, 50.7, 80.3]], dtype=np.float32)
    detections = sv.Detections(xyxy=xyxy)
    block = MaskAreaMeasurementBlockV1()
    _call__bound__arguments = inspect.signature(block.run).bind(predictions=detections)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(block.run, 'tests.inference.unit_tests.test_run__unit_test_1', None, 'test_run_with_float32_coordinates', 'run', '4', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    result = codeflash_output
    expected = (50.7 - 10.5) * (80.3 - 20.5)

def test_run_complex_polygon_mask():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test with a complex polygon mask (L-shaped).'
    mask = np.zeros((100, 100), dtype=bool)
    mask[10:60, 10:40] = True
    mask[40:60, 10:60] = True
    xyxy = np.array([[0.0, 0.0, 100.0, 100.0]])
    detections = sv.Detections(xyxy=xyxy, mask=np.array([mask]))
    area = get_detection_area(detections, 0)

def test_run_circular_mask_approximation():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test with approximately circular mask.'
    mask = np.zeros((100, 100), dtype=bool)
    center_x, center_y = (50, 50)
    radius = 25
    for i in range(100):
        for j in range(100):
            if (i - center_y) ** 2 + (j - center_x) ** 2 <= radius ** 2:
                mask[i, j] = True
    xyxy = np.array([[25.0, 25.0, 75.0, 75.0]])
    detections = sv.Detections(xyxy=xyxy, mask=np.array([mask]))
    area = get_detection_area(detections, 0)