package datascience

import "testing"

func TestPercentile(t *testing.T) {
	t.Run("slice vacio retorna cero", func(t *testing.T) {
		got := Percentile([]int64{}, 0.5)
		if got != 0 {
			t.Errorf("got %v, want 0", got)
		}
	})

	t.Run("un solo elemento retorna ese elemento", func(t *testing.T) {
		got := Percentile([]int64{42}, 0.5)
		if got != 42 {
			t.Errorf("got %v, want 42", got)
		}
	})

	t.Run("p0 retorna minimo", func(t *testing.T) {
		got := Percentile([]int64{10, 20, 30, 40, 50}, 0.0)
		if got != 10 {
			t.Errorf("got %v, want 10", got)
		}
	})

	t.Run("p100 retorna maximo", func(t *testing.T) {
		got := Percentile([]int64{10, 20, 30, 40, 50}, 1.0)
		if got != 50 {
			t.Errorf("got %v, want 50", got)
		}
	})

	t.Run("p50 retorna mediana de cinco elementos", func(t *testing.T) {
		got := Percentile([]int64{10, 20, 30, 40, 50}, 0.5)
		if got != 30 {
			t.Errorf("got %v, want 30", got)
		}
	})

	t.Run("p90 de diez elementos usa idx int truncado", func(t *testing.T) {
		// idx = int(9 * 0.9) = int(8.1) = 8 → sorted[8] = 9
		got := Percentile([]int64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, 0.9)
		if got != 9 {
			t.Errorf("got %v, want 9", got)
		}
	})

	t.Run("p99 de slice pequeno usa idx truncado a cero", func(t *testing.T) {
		// idx = int(1 * 0.99) = int(0.99) = 0 → sorted[0] = 100
		got := Percentile([]int64{100, 200}, 0.99)
		if got != 100 {
			t.Errorf("got %v, want 100", got)
		}
	})
}
