<?php

declare(strict_types=1);

namespace Drupal\Tests\native_observability_metrics\Unit\Service;

use Drupal\Tests\UnitTestCase;
use Drupal\native_observability_metrics\Service\PercentileCalculator;

/**
 * Covers percentile interpolation rules used by aggregated metrics.
 */
final class PercentileCalculatorTest extends UnitTestCase {

  /**
   * Verifies that an empty dataset produces a neutral numeric result.
   */
  public function testCalculateReturnsZeroForEmptyDataset(): void {
    $calculator = new PercentileCalculator();

    $this->assertSame(0.0, $calculator->calculate([], 95));
  }

  /**
   * Verifies interpolation across a sorted numeric dataset.
   */
  public function testCalculateInterpolatesExpectedPercentile(): void {
    $calculator = new PercentileCalculator();

    $result = $calculator->calculate([10, 20, 30, 40], 75);

    $this->assertSame(32.5, $result);
  }

  /**
   * Verifies that a percentile on an exact index returns that element.
   */
  public function testCalculateReturnsExactIndexedValue(): void {
    $calculator = new PercentileCalculator();

    $result = $calculator->calculate([5, 10, 15], 50);

    $this->assertSame(10.0, $result);
  }

}
