test_that("dmtl, pmtl, qmtl, rmtl, smtl, hmtl work correctly", {
  alpha <- 1.5
  
  # PDF integrates to 1
  integral_val <- stats::integrate(function(x) dmtl(x, alpha), lower = 0, upper = Inf)$value
  expect_equal(integral_val, 1, tolerance = 1e-4)
  
  # CDF and Quantile inverse property
  p_seq <- c(0.1, 0.25, 0.5, 0.75, 0.9)
  q_vals <- qmtl(p_seq, alpha)
  p_recalc <- pmtl(q_vals, alpha)
  expect_equal(p_seq, p_recalc, tolerance = 1e-5)
  
  # Survival function
  expect_equal(smtl(q_vals, alpha), 1 - p_seq, tolerance = 1e-5)
  
  # Hazard function non-negative and finite
  hrf_val <- hmtl(x = 1, alpha = alpha)
  expect_true(is.numeric(hrf_val) && hrf_val > 0 && is.finite(hrf_val))
  
  # Random generation
  set.seed(42)
  rnd <- rmtl(n = 100, alpha = alpha)
  expect_equal(length(rnd), 100)
  expect_true(all(rnd > 0))
  expect_false(any(is.na(rnd)))
  expect_false(any(is.nan(rnd)))
})

test_that("Classical estimation functions work without warnings, NAs, or NaNs", {
  set.seed(123)
  true_alpha <- 2.0
  x_sample <- rmtl(n = 100, alpha = true_alpha)
  
  # MLE
  res_mle <- mle_mtl(x = x_sample)
  expect_equal(res_mle$estimate, true_alpha, tolerance = 0.3)
  expect_false(is.na(res_mle$estimate))
  expect_false(is.nan(res_mle$estimate))
  
  # OLS, WLS, CVM, MPS
  res_ols <- ols_mtl(x = x_sample)
  expect_true(res_ols$estimate > 0)
  
  res_wls <- wls_mtl(x = x_sample)
  expect_true(res_wls$estimate > 0)
  
  res_cvm <- cvm_mtl(x = x_sample)
  expect_true(res_cvm$estimate > 0)
  
  res_mps <- mps_mtl(x = x_sample)
  expect_true(res_mps$estimate > 0)
  
  # Fit all
  df_all <- fit_mtl(x = x_sample, method = "all")
  expect_equal(nrow(df_all), 5)
  expect_false(any(is.na(df_all$Estimate)))
})

test_that("Bayesian estimation functions work properly", {
  set.seed(123)
  x_sample <- rmtl(n = 50, alpha = 2.0)
  
  res_bayes <- bayes_mtl(x = x_sample, prior = "noninformative", loss = "all", n.draws = 2000, burn.in = 500)
  expect_true(res_bayes$estimates["SELF"] > 0)
  expect_true(res_bayes$hpd["Lower"] < res_bayes$hpd["Upper"])
  expect_false(any(is.na(res_bayes$estimates)))
})

test_that("Censoring functions operate without errors", {
  set.seed(123)
  alpha_val <- 2.0
  
  # Random censoring
  c_rand <- rcensor_mtl(n = 30, alpha = alpha_val, scheme = "random")
  mle_rand <- mle_censor_mtl(x = c_rand$x, status = c_rand$status, scheme = "random")
  expect_true(mle_rand$estimate > 0)
  
  # Type-I censoring
  c_t1 <- rcensor_mtl(n = 30, alpha = alpha_val, scheme = "type1", T_censor = 1.0)
  mle_t1 <- mle_censor_mtl(x = c_t1$x, status = c_t1$status, scheme = "type1")
  expect_true(mle_t1$estimate > 0)
  
  # Progressive Type-II censoring
  R_sch <- c(2, 0, 1, 0, 2)
  c_prog <- rcensor_mtl(n = 10, alpha = alpha_val, scheme = "progressive2", m = 5, R = R_sch)
  mle_prog <- mle_censor_mtl(x = c_prog$x, scheme = "progressive2", R = R_sch)
  expect_true(mle_prog$estimate > 0)
})

test_that("Statistical properties functions return expected outputs", {
  alpha_val <- 2.5
  
  expect_true(mode_mtl(alpha = alpha_val) > 0)
  expect_true(mean_mtl(alpha = alpha_val) > 0)
  
  q_sum <- quantiles_mtl(alpha = alpha_val)
  expect_true(q_sum["Median"] > 0)
  
  md <- meandev_mtl(alpha = alpha_val)
  expect_true(md["MD_mean"] > 0)
  
  ineq <- inequality_mtl(y = 1, alpha = alpha_val)
  expect_true(ineq["Lorenz"] > 0 && ineq["Lorenz"] <= 1)
  
  expect_true(mrl_mtl(y = 1, alpha = alpha_val) > 0)
  expect_true(mpl_mtl(y = 1, alpha = alpha_val) > 0)
  
  expect_true(ssr_mtl(alpha1 = 2, alpha2 = 3) == 0.4)
  
  os <- order_stats_mtl(y = 1, alpha = alpha_val, n = 5, u = 3, type = "pdf")
  expect_true(os > 0)
})
