test_that("PIT boundary clipping and log(0) at i=0 work cleanly", {
  # Extreme u near 0 and 1
  u_ext <- c(1e-15, 0.2, 0.5, 0.8, 1 - 1e-15)
  n <- 10

  expect_silent({
    stats <- stat_edf_censored(u_ext, n)
  })

  expect_false(any(is.na(stats)))
  expect_false(any(is.nan(stats)))
  expect_false(any(is.infinite(stats)))
})

test_that("Malmquist transform handles boundary inputs without NaN or Inf", {
  u_bd <- c(1e-10, 0.5, 1 - 1e-10)
  n <- 5

  res <- transform_malmquist(u_bd, n, censoring = "right")
  expect_equal(length(res$W), 3)
  expect_false(any(is.na(res$W)))
  expect_false(any(is.infinite(res$W)))
})

test_that("Degenerate sample size r = 1 returns NA for T_rn and finite values for others", {
  u_single <- c(0.1)
  n <- 5

  stats <- stat_edf_censored(u_single, n)

  expect_true(is.na(stats["T_rn"]))
  expect_false(is.na(stats["D_rn"]))
  expect_false(is.na(stats["W2_rn"]))
})

test_that("Complete sample case r = n works without division by zero", {
  u_full <- c(0.1, 0.3, 0.5, 0.7, 0.9)
  n <- 5

  stats <- stat_edf_censored(u_full, n)

  expect_false(any(is.na(stats)))
  expect_false(any(is.nan(stats)))
})

test_that("Invalid inputs trigger clear error messages up front", {
  expect_error(stat_edf_censored(c(0.5, 0.2), n = 5), "sorted")
  expect_error(stat_edf_censored(c(0.1, 0.2), n = 1), "n` must be an integer >= length")
  expect_error(gof_censored(data = c(), n = 5, pdf = dunif, cdf = punif), "non-empty numeric vector")
})

test_that("Left censoring reduction runs correctly", {
  u_left <- c(0.6, 0.7, 0.8, 0.9)
  n <- 10

  res_mal <- transform_malmquist(u_left, n = 10, censoring = "left")
  expect_equal(length(res_mal$W), 4)
  expect_false(any(is.na(res_mal$W)))
})
