test_that("r_impadapt2_censor generates valid IAT-II PCS samples", {
  set.seed(42)
  ge_cdf <- function(x, par) (1 - exp(-par[2] * x))^par[1]
  ge_pdf <- function(x, par) par[1] * par[2] * exp(-par[2] * x) * (1 - exp(-par[2] * x))^(par[1] - 1)
  ge_qdf <- function(p, par) - (1 / par[2]) * log(1 - p^(1 / par[1]))
  
  # Test Case I (Large T1)
  samp1 <- r_impadapt2_censor(
    n = 20, m = 10, T1 = 10.0, T2 = 15.0,
    R = c(rep(1, 10)),
    pdf = ge_pdf, cdf = ge_cdf, qdf = ge_qdf,
    par = c(1.5, 0.8)
  )
  expect_s3_class(samp1, "impadapt2_sample")
  expect_equal(samp1$case, "Case I")
  expect_equal(length(samp1$data), 10)
  expect_false(any(is.na(samp1$data)))
  expect_false(any(is.nan(samp1$data)))
  
  # Test Case II (Intermediate T1, large T2)
  samp2 <- r_impadapt2_censor(
    n = 25, m = 12, T1 = 0.5, T2 = 10.0,
    R = c(rep(1, 11), 2),
    pdf = ge_pdf, cdf = ge_cdf, qdf = ge_qdf,
    par = c(1.5, 0.8)
  )
  expect_s3_class(samp2, "impadapt2_sample")
  expect_false(any(is.na(samp2$data)))
  
  # Test Case III (Very small T1 and T2 so X_m > T2)
  samp3 <- r_impadapt2_censor(
    n = 40, m = 20, T1 = 0.01, T2 = 0.03,
    R = c(rep(1, 20)),
    pdf = ge_pdf, cdf = ge_cdf, qdf = ge_qdf,
    par = c(1.5, 0.8)
  )
  expect_s3_class(samp3, "impadapt2_sample")
  if (length(samp3$data) > 0) {
    expect_true(all(samp3$data <= samp3$T_star))
  }
  expect_false(any(is.na(samp3$data)))
})

test_that("mle_impadapt2_censor computes correct estimates without warnings or NAs", {
  set.seed(123)
  ge_cdf <- function(x, par) (1 - exp(-par[2] * x))^par[1]
  ge_pdf <- function(x, par) par[1] * par[2] * exp(-par[2] * x) * (1 - exp(-par[2] * x))^(par[1] - 1)
  ge_qdf <- function(p, par) - (1 / par[2]) * log(1 - p^(1 / par[1]))
  
  samp <- r_impadapt2_censor(
    n = 25, m = 12, T1 = 0.8, T2 = 2.0,
    R = c(rep(1, 11), 2),
    pdf = ge_pdf, cdf = ge_cdf, qdf = ge_qdf,
    par = c(1.5, 0.8)
  )
  
  fit <- mle_impadapt2_censor(
    data = samp, pdf = ge_pdf, cdf = ge_cdf,
    start = c(1.0, 1.0), eval_time = 0.5
  )
  
  expect_s3_class(fit, "impadapt2_mle")
  expect_false(any(is.na(fit$par)))
  expect_false(any(is.nan(fit$par)))
  expect_false(any(is.na(fit$std_er)))
  expect_true(is.numeric(fit$loglik))
  expect_true(is.numeric(fit$aic))
  expect_true(is.numeric(fit$bic))
  
  expect_no_error(print(fit))
  s <- summary(fit)
  expect_s3_class(s, "summary.impadapt2_mle")
  expect_no_error(print(s))
  expect_equal(length(coef(fit)), 2)
  expect_equal(dim(vcov(fit)), c(2, 2))
  expect_equal(dim(confint(fit)), c(2, 2))
  expect_s3_class(logLik(fit), "logLik")
  expect_true(is.numeric(AIC(fit)))
})

test_that("mpse_impadapt2_censor computes valid product of spacing estimates", {
  set.seed(123)
  ge_cdf <- function(x, par) (1 - exp(-par[2] * x))^par[1]
  ge_pdf <- function(x, par) par[1] * par[2] * exp(-par[2] * x) * (1 - exp(-par[2] * x))^(par[1] - 1)
  ge_qdf <- function(p, par) - (1 / par[2]) * log(1 - p^(1 / par[1]))
  
  samp <- r_impadapt2_censor(
    n = 25, m = 12, T1 = 0.8, T2 = 2.0,
    R = c(rep(1, 11), 2),
    pdf = ge_pdf, cdf = ge_cdf, qdf = ge_qdf,
    par = c(1.5, 0.8)
  )
  
  fit <- mpse_impadapt2_censor(
    data = samp, cdf = ge_cdf, pdf = ge_pdf,
    start = c(1.0, 1.0), eval_time = 0.5
  )
  
  expect_s3_class(fit, "impadapt2_mpse")
  expect_false(any(is.na(fit$par)))
  expect_false(any(is.nan(fit$par)))
  expect_false(any(is.na(fit$std_er)))
  expect_true(is.numeric(fit$log_ps))
  
  expect_no_error(print(fit))
  s <- summary(fit)
  expect_s3_class(s, "summary.impadapt2_mpse")
  expect_no_error(print(s))
  expect_equal(length(coef(fit)), 2)
  expect_equal(dim(vcov(fit)), c(2, 2))
  expect_equal(dim(confint(fit)), c(2, 2))
})

test_that("bayes_impadapt2_censor computes valid Bayesian MCMC estimates and HPD intervals", {
  set.seed(123)
  ge_cdf <- function(x, par) (1 - exp(-par[2] * x))^par[1]
  ge_pdf <- function(x, par) par[1] * par[2] * exp(-par[2] * x) * (1 - exp(-par[2] * x))^(par[1] - 1)
  ge_qdf <- function(p, par) - (1 / par[2]) * log(1 - p^(1 / par[1]))
  
  samp <- r_impadapt2_censor(
    n = 20, m = 10, T1 = 0.8, T2 = 2.0,
    R = c(rep(1, 10)),
    pdf = ge_pdf, cdf = ge_cdf, qdf = ge_qdf,
    par = c(1.5, 0.8)
  )
  
  fit <- bayes_impadapt2_censor(
    data = samp, pdf = ge_pdf, cdf = ge_cdf,
    start = c(1.2, 0.7), n_iter = 500, burn_in = 100, eval_time = 0.5
  )
  
  expect_s3_class(fit, "impadapt2_bayes")
  expect_false(any(is.na(fit$bayes_sel)))
  expect_false(any(is.nan(fit$bayes_sel)))
  expect_false(any(is.na(fit$bayes_linex)))
  expect_false(any(is.nan(fit$bayes_linex)))
  expect_false(any(is.na(fit$hpd_interval)))
  expect_true(fit$acceptance_rate > 0)
  
  expect_no_error(print(fit))
  s <- summary(fit)
  expect_s3_class(s, "summary.impadapt2_bayes")
  expect_no_error(print(s))
})

test_that("Diagnostic plotting functions execute without error", {
  set.seed(123)
  ge_cdf <- function(x, par) (1 - exp(-par[2] * x))^par[1]
  ge_pdf <- function(x, par) par[1] * par[2] * exp(-par[2] * x) * (1 - exp(-par[2] * x))^(par[1] - 1)
  ge_qdf <- function(p, par) - (1 / par[2]) * log(1 - p^(1 / par[1]))
  
  samp <- r_impadapt2_censor(
    n = 20, m = 10, T1 = 0.8, T2 = 2.0,
    R = c(rep(1, 10)),
    pdf = ge_pdf, cdf = ge_cdf, qdf = ge_qdf,
    par = c(1.5, 0.8)
  )
  
  pdf(NULL)
  expect_no_error(plot(samp))
  
  fit_mle <- mle_impadapt2_censor(data = samp, pdf = ge_pdf, cdf = ge_cdf, start = c(1.0, 1.0))
  expect_no_error(plot(fit_mle))
  
  fit_mpse <- mpse_impadapt2_censor(data = samp, cdf = ge_cdf, pdf = ge_pdf, start = c(1.0, 1.0))
  expect_no_error(plot(fit_mpse))
  
  fit_bayes <- bayes_impadapt2_censor(data = samp, pdf = ge_pdf, cdf = ge_cdf, start = c(1.0, 1.0), n_iter = 200, burn_in = 50)
  expect_no_error(plot(fit_bayes))
  dev.off()
})
