from __future__ import annotations

import gc
import os
import time

import numpy as np
import pytest
from inference.core.interfaces.camera.test_pattern_producer import \
    _generate_grid_colors


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
GRID_COLS = 16
GRID_ROWS = 9

def test_returns_numpy_array():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that the function returns a numpy array.'
    codeflash_return_value = codeflash_wrap(_generate_grid_colors, 'tests.inference.unit_tests.test__generate_grid_colors__unit_test_1', None, 'test_returns_numpy_array', '_generate_grid_colors', '1', codeflash_loop_index, seed=42)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_output_shape_is_correct():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that the output shape matches (GRID_ROWS, GRID_COLS, 3).'
    codeflash_return_value = codeflash_wrap(_generate_grid_colors, 'tests.inference.unit_tests.test__generate_grid_colors__unit_test_1', None, 'test_output_shape_is_correct', '_generate_grid_colors', '1', codeflash_loop_index, seed=42)
    codeflash_output = codeflash_return_value
    result = codeflash_output
    expected_shape = (GRID_ROWS, GRID_COLS, 3)

def test_output_dtype_is_uint8():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that the output dtype is uint8.'
    codeflash_return_value = codeflash_wrap(_generate_grid_colors, 'tests.inference.unit_tests.test__generate_grid_colors__unit_test_1', None, 'test_output_dtype_is_uint8', '_generate_grid_colors', '1', codeflash_loop_index, seed=42)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_all_values_in_valid_range():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that all color values are in the range [40, 220).'
    codeflash_return_value = codeflash_wrap(_generate_grid_colors, 'tests.inference.unit_tests.test__generate_grid_colors__unit_test_1', None, 'test_all_values_in_valid_range', '_generate_grid_colors', '1', codeflash_loop_index, seed=42)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_deterministic_with_same_seed():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that the same seed produces the same output (deterministic).'
    codeflash_return_value = codeflash_wrap(_generate_grid_colors, 'tests.inference.unit_tests.test__generate_grid_colors__unit_test_1', None, 'test_deterministic_with_same_seed', '_generate_grid_colors', '1', codeflash_loop_index, seed=12345)
    codeflash_output = codeflash_return_value
    result1 = codeflash_output
    codeflash_return_value = codeflash_wrap(_generate_grid_colors, 'tests.inference.unit_tests.test__generate_grid_colors__unit_test_1', None, 'test_deterministic_with_same_seed', '_generate_grid_colors', '3', codeflash_loop_index, seed=12345)
    codeflash_output = codeflash_return_value
    result2 = codeflash_output

def test_different_seeds_produce_different_results():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that different seeds produce different outputs.'
    codeflash_return_value = codeflash_wrap(_generate_grid_colors, 'tests.inference.unit_tests.test__generate_grid_colors__unit_test_1', None, 'test_different_seeds_produce_different_results', '_generate_grid_colors', '1', codeflash_loop_index, seed=111)
    codeflash_output = codeflash_return_value
    result1 = codeflash_output
    codeflash_return_value = codeflash_wrap(_generate_grid_colors, 'tests.inference.unit_tests.test__generate_grid_colors__unit_test_1', None, 'test_different_seeds_produce_different_results', '_generate_grid_colors', '3', codeflash_loop_index, seed=222)
    codeflash_output = codeflash_return_value
    result2 = codeflash_output

def test_basic_seed_zero():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test with seed value of 0.'
    codeflash_return_value = codeflash_wrap(_generate_grid_colors, 'tests.inference.unit_tests.test__generate_grid_colors__unit_test_1', None, 'test_basic_seed_zero', '_generate_grid_colors', '1', codeflash_loop_index, seed=0)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_basic_seed_one():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test with seed value of 1.'
    codeflash_return_value = codeflash_wrap(_generate_grid_colors, 'tests.inference.unit_tests.test__generate_grid_colors__unit_test_1', None, 'test_basic_seed_one', '_generate_grid_colors', '1', codeflash_loop_index, seed=1)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_large_seed_value():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test with a large seed value.'
    codeflash_return_value = codeflash_wrap(_generate_grid_colors, 'tests.inference.unit_tests.test__generate_grid_colors__unit_test_1', None, 'test_large_seed_value', '_generate_grid_colors', '1', codeflash_loop_index, seed=2147483647)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_negative_seed_value():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test with a negative seed value.'
    codeflash_return_value = codeflash_wrap(_generate_grid_colors, 'tests.inference.unit_tests.test__generate_grid_colors__unit_test_1', None, 'test_negative_seed_value', '_generate_grid_colors', '1', codeflash_loop_index, seed=-1)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_output_is_contiguous():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that the returned array is a valid numpy array.'
    codeflash_return_value = codeflash_wrap(_generate_grid_colors, 'tests.inference.unit_tests.test__generate_grid_colors__unit_test_1', None, 'test_output_is_contiguous', '_generate_grid_colors', '1', codeflash_loop_index, seed=42)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_total_elements_count():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that the total number of elements is GRID_ROWS * GRID_COLS * 3.'
    codeflash_return_value = codeflash_wrap(_generate_grid_colors, 'tests.inference.unit_tests.test__generate_grid_colors__unit_test_1', None, 'test_total_elements_count', '_generate_grid_colors', '1', codeflash_loop_index, seed=99)
    codeflash_output = codeflash_return_value
    result = codeflash_output
    expected_size = GRID_ROWS * GRID_COLS * 3
    actual_size = result.size

def test_each_pixel_has_three_channels():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that each pixel (row, col) has exactly 3 color channels.'
    codeflash_return_value = codeflash_wrap(_generate_grid_colors, 'tests.inference.unit_tests.test__generate_grid_colors__unit_test_1', None, 'test_each_pixel_has_three_channels', '_generate_grid_colors', '1', codeflash_loop_index, seed=50)
    codeflash_output = codeflash_return_value
    result = codeflash_output
    for i in range(GRID_ROWS):
        for j in range(GRID_COLS):
            pixel = result[i, j]

def test_seed_equals_max_int32():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test with seed equal to the maximum 32-bit signed integer.'
    codeflash_return_value = codeflash_wrap(_generate_grid_colors, 'tests.inference.unit_tests.test__generate_grid_colors__unit_test_1', None, 'test_seed_equals_max_int32', '_generate_grid_colors', '1', codeflash_loop_index, seed=2147483647)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_seed_equals_min_int32():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test with seed equal to a large negative integer.'
    codeflash_return_value = codeflash_wrap(_generate_grid_colors, 'tests.inference.unit_tests.test__generate_grid_colors__unit_test_1', None, 'test_seed_equals_min_int32', '_generate_grid_colors', '1', codeflash_loop_index, seed=-2147483648)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_output_no_values_below_40():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that no value in output is below the minimum (40).'
    codeflash_return_value = codeflash_wrap(_generate_grid_colors, 'tests.inference.unit_tests.test__generate_grid_colors__unit_test_1', None, 'test_output_no_values_below_40', '_generate_grid_colors', '1', codeflash_loop_index, seed=777)
    codeflash_output = codeflash_return_value
    result = codeflash_output
    min_value = np.min(result)

def test_output_no_values_at_or_above_220():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that no value in output is at or above 220.'
    codeflash_return_value = codeflash_wrap(_generate_grid_colors, 'tests.inference.unit_tests.test__generate_grid_colors__unit_test_1', None, 'test_output_no_values_at_or_above_220', '_generate_grid_colors', '1', codeflash_loop_index, seed=888)
    codeflash_output = codeflash_return_value
    result = codeflash_output
    max_value = np.max(result)

def test_multiple_calls_dont_interfere():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that calling the function multiple times produces independent results.'
    codeflash_return_value = codeflash_wrap(_generate_grid_colors, 'tests.inference.unit_tests.test__generate_grid_colors__unit_test_1', None, 'test_multiple_calls_dont_interfere', '_generate_grid_colors', '1', codeflash_loop_index, seed=555)
    codeflash_output = codeflash_return_value
    result1 = codeflash_output
    codeflash_return_value = codeflash_wrap(_generate_grid_colors, 'tests.inference.unit_tests.test__generate_grid_colors__unit_test_1', None, 'test_multiple_calls_dont_interfere', '_generate_grid_colors', '3', codeflash_loop_index, seed=555)
    codeflash_output = codeflash_return_value
    result2 = codeflash_output
    codeflash_return_value = codeflash_wrap(_generate_grid_colors, 'tests.inference.unit_tests.test__generate_grid_colors__unit_test_1', None, 'test_multiple_calls_dont_interfere', '_generate_grid_colors', '5', codeflash_loop_index, seed=666)
    codeflash_output = codeflash_return_value
    result3 = codeflash_output

def test_color_values_are_unsigned():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that all values are properly represented as unsigned integers.'
    codeflash_return_value = codeflash_wrap(_generate_grid_colors, 'tests.inference.unit_tests.test__generate_grid_colors__unit_test_1', None, 'test_color_values_are_unsigned', '_generate_grid_colors', '1', codeflash_loop_index, seed=42)
    codeflash_output = codeflash_return_value
    result = codeflash_output
    for val in result.flat:
        pass

def test_seed_negative_large_value():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test with a large negative seed.'
    codeflash_return_value = codeflash_wrap(_generate_grid_colors, 'tests.inference.unit_tests.test__generate_grid_colors__unit_test_1', None, 'test_seed_negative_large_value', '_generate_grid_colors', '1', codeflash_loop_index, seed=-999999999)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_seed_with_prime_number():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test with a prime number seed.'
    codeflash_return_value = codeflash_wrap(_generate_grid_colors, 'tests.inference.unit_tests.test__generate_grid_colors__unit_test_1', None, 'test_seed_with_prime_number', '_generate_grid_colors', '1', codeflash_loop_index, seed=1009)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_seed_with_power_of_two():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test with a power of two as seed.'
    codeflash_return_value = codeflash_wrap(_generate_grid_colors, 'tests.inference.unit_tests.test__generate_grid_colors__unit_test_1', None, 'test_seed_with_power_of_two', '_generate_grid_colors', '1', codeflash_loop_index, seed=1024)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_output_array_properties():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test various properties of the output numpy array.'
    codeflash_return_value = codeflash_wrap(_generate_grid_colors, 'tests.inference.unit_tests.test__generate_grid_colors__unit_test_1', None, 'test_output_array_properties', '_generate_grid_colors', '1', codeflash_loop_index, seed=42)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_boundary_seed_zero():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test with seed = 0 for boundary condition.'
    codeflash_return_value = codeflash_wrap(_generate_grid_colors, 'tests.inference.unit_tests.test__generate_grid_colors__unit_test_1', None, 'test_boundary_seed_zero', '_generate_grid_colors', '1', codeflash_loop_index, seed=0)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_reproducibility_across_multiple_calls():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test reproducibility: same seed always gives same output.'
    seed_val = 54321
    results = [_generate_grid_colors(seed=seed_val) for _ in range(5)]
    for i in range(1, len(results)):
        pass

def test_performance_with_multiple_generations():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that generating colors multiple times completes efficiently.'
    results = []
    for i in range(100):
        codeflash_return_value = codeflash_wrap(_generate_grid_colors, 'tests.inference.unit_tests.test__generate_grid_colors__unit_test_1', None, 'test_performance_with_multiple_generations', '_generate_grid_colors', '2_0', codeflash_loop_index, seed=i)
        codeflash_output = codeflash_return_value
        result = codeflash_output
        results.append(result)
    for result in results:
        pass
    for result in results:
        pass

def test_large_scale_value_distribution():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that generated values have reasonable distribution across range.'
    all_values = []
    for seed in range(50):
        codeflash_return_value = codeflash_wrap(_generate_grid_colors, 'tests.inference.unit_tests.test__generate_grid_colors__unit_test_1', None, 'test_large_scale_value_distribution', '_generate_grid_colors', '2_0', codeflash_loop_index, seed=seed)
        codeflash_output = codeflash_return_value
        result = codeflash_output
        all_values.extend(result.flat)
    all_values = np.array(all_values)
    min_val = np.min(all_values)
    max_val = np.max(all_values)
    value_range = max_val - min_val

def test_independence_of_multiple_seeds():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that many different seeds produce many different results.'
    results = [_generate_grid_colors(seed=i) for i in range(50)]
    unique_count = 0
    for i in range(len(results)):
        for j in range(i + 1, len(results)):
            if not np.array_equal(results[i], results[j]):
                unique_count += 1

def test_consistency_of_value_ranges():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that all generated grids have values in valid range.'
    for seed in range(200):
        codeflash_return_value = codeflash_wrap(_generate_grid_colors, 'tests.inference.unit_tests.test__generate_grid_colors__unit_test_1', None, 'test_consistency_of_value_ranges', '_generate_grid_colors', '1_0', codeflash_loop_index, seed=seed)
        codeflash_output = codeflash_return_value
        result = codeflash_output

def test_array_indexing_large_scale():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that array indexing works correctly for all elements.'
    codeflash_return_value = codeflash_wrap(_generate_grid_colors, 'tests.inference.unit_tests.test__generate_grid_colors__unit_test_1', None, 'test_array_indexing_large_scale', '_generate_grid_colors', '1', codeflash_loop_index, seed=42)
    codeflash_output = codeflash_return_value
    result = codeflash_output
    positions = [(0, 0, 0), (0, 0, 1), (0, 0, 2), (GRID_ROWS - 1, GRID_COLS - 1, 0), (GRID_ROWS - 1, GRID_COLS - 1, 1), (GRID_ROWS - 1, GRID_COLS - 1, 2), (GRID_ROWS // 2, GRID_COLS // 2, 1)]
    for pos in positions:
        val = result[pos]

def test_seed_sequence_coverage():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that sequential seeds produce varied outputs.'
    results = [_generate_grid_colors(seed=i) for i in range(1000)]
    for result in results:
        pass
    unique_results = sum((1 for i in range(1, len(results)) if not np.array_equal(results[0], results[i])))

def test_memory_efficiency_multiple_grids():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that memory is managed efficiently when creating many grids.'
    grids = []
    for seed in range(500):
        codeflash_return_value = codeflash_wrap(_generate_grid_colors, 'tests.inference.unit_tests.test__generate_grid_colors__unit_test_1', None, 'test_memory_efficiency_multiple_grids', '_generate_grid_colors', '2_0', codeflash_loop_index, seed=seed)
        codeflash_output = codeflash_return_value
        grid = codeflash_output
        grids.append(grid)
    for grid in grids:
        pass

def test_numerical_stability_across_many_seeds():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test numerical properties across a large range of seeds.'
    for seed in range(0, 500, 50):
        codeflash_return_value = codeflash_wrap(_generate_grid_colors, 'tests.inference.unit_tests.test__generate_grid_colors__unit_test_1', None, 'test_numerical_stability_across_many_seeds', '_generate_grid_colors', '1_0', codeflash_loop_index, seed=seed)
        codeflash_output = codeflash_return_value
        result = codeflash_output
        mean_val = np.mean(result)
        std_val = np.std(result)