n_iter <- 100 # was 1000 before
test_that("adjust_lambda default method works", {
  design <- setup_cpp(k = 3, p0 = 0.2)

  # Without simulated data
  set.seed(20230319)
  res1 <- adjust_lambda(design = design, n = 15, alpha = 0.05,
    design_params = list(tune_a = 1, tune_b = 1), iter = n_iter)
  toer1 <- toer(design, n = 15, lambda = res1$lambda, design_params =
      list(tune_a = 1, tune_b = 1), iter = n_iter)

  expect_lte(res1$toer, 0.05)
  expect_equal(res1$toer, toer1, tolerance = 0.5)

  skip_on_cran()
  set.seed(123)
  res2 <- adjust_lambda(design = design, n = 12, alpha = 0.05,
    design_params = list(tune_a = 2, tune_b = 2), iter = n_iter)
  toer2 <- toer(design, n = 12, lambda = res2$lambda, design_params =
      list(tune_a = 2, tune_b = 2), iter = n_iter)

  expect_lte(res2$toer, 0.05)
  expect_equal(res2$toer, toer2, tolerance = 0.5)

  set.seed(456)
  res3 <- adjust_lambda(design = design, n = 13, alpha = 0.05,
    design_params = list(tune_a = 4, tune_b = 1.5), iter = n_iter)
  toer3 <- toer(design, n = 13, lambda = res3$lambda, design_params =
      list(tune_a = 4, tune_b = 1.5), iter = n_iter)

  expect_lte(res3$toer, 0.05)
  expect_equal(res3$toer, toer3, tolerance = 0.5)

  # With simulated data
  simdata <- get_data(k = 3, n = 15, p = 0.2, iter = n_iter)
  res4 <- adjust_lambda(design = design, n = 15, p1 = 0.2, alpha = 0.05,
    design_params = list(tune_a = 1, tune_b = 1), iter = n_iter, prec_digits = 3,
    data = simdata)
  res5 <- adjust_lambda(design = design, n = 15, p1 = 0.2, alpha = 0.05,
    design_params = list(tune_a = 1, tune_b = 1), iter = n_iter, prec_digits = 3,
    data = simdata)

  # Check if results are equal when adjust_lambda is called two times with
  # the same simulated data
  expect_equal(res4$lambda, res5$lambda)
  expect_equal(res4$toer, res5$toer)

  # Check if selected lambda controls the TOER
  toer4 <- toer(design, n = 15, p1 = 0.2, lambda = res4$lambda,
    design_params = list(tune_a = 1, tune_b = 1), iter = n_iter, data = simdata)
  toer5 <- toer(design, n = 15, p1 = 0.2, lambda = res4$lambda - 0.01,
    design_params = list(tune_a = 1, tune_b = 1), iter = n_iter, data = simdata)

  expect_equal(res4$toer, toer4)
  expect_gt(toer5, 0.05)
})

test_that("adjust_lambda works for exnex", {
  design <- setup_exnex(k = 3, p0 = 0.2)

  # Without simulated data
  set.seed(125)
  res1 <- adjust_lambda(design = design, n = 15,
    design_params = list(tau_scale = 1, w_j = 0.5), iter = n_iter, n_mcmc = 2500)

  set.seed(125)
  res2 <- toer(design = design, n = 15, lambda = res1$lambda,
               design_params = list(tau_scale = 1, w_j = 0.5), iter = n_iter, n_mcmc = 2500)

  expect_lte(res1$toer, 0.05)
  expect_equal(res1$toer, res2)

  skip_on_cran()
  # With simulated data
  set.seed(125)
  simdata <- get_data(k = 3, n = 15, p = 0.2, iter = n_iter, type = "bhmbasket")
  set.seed(126)
  res3 <- adjust_lambda(design = design, n = 15,
    design_params = list(tau_scale = 1, w_j = 0.5), iter = n_iter, n_mcmc = 2500,
    data = simdata)
  set.seed(126)
  res4 <- adjust_lambda(design = design, n = 15,
    design_params = list(tau_scale = 1, w_j = 0.5), iter = n_iter, n_mcmc = 2500,
    data = simdata)

  # Check if results are equal when adjust_lambda is called two times with
  # the same simulated data
  expect_equal(res3$lambda, res4$lambda)
  expect_equal(res3$toer, res4$toer)

  # Check if selected lambda controls the TOER
  set.seed(126)
  toer1 <- toer(design, n = 15, p1 = 0.2, lambda = res3$lambda,
    design_params = list(tau_scale = 1, w_j = 0.5), data = simdata, iter = n_iter,
    n_mcmc = 2500)
  toer2 <- toer(design, n = 15, p1 = 0.2, lambda = res3$lambda - 0.01,
    design_params = list(tau_scale = 1, w_j = 0.5), data = simdata, iter = n_iter,
    n_mcmc = 2500)

  expect_equal(toer1, 0.05)
  expect_gt(toer2, 0.05)
})

test_that("adjust_lambda works for bhm", {
  design <- setup_bhm(k = 3, p0 = 0.2, p_target = 0.5)

  # Without simulated data
  set.seed(125)
  res1 <- adjust_lambda(design = design, n = 15,
    design_params = list(tau_scale = 1), iter = n_iter, n_mcmc = 2500)

  set.seed(125)
  res2 <- toer(design = design, n = 15, lambda = res1$lambda,
    design_params = list(tau_scale = 1), iter = n_iter, n_mcmc = 2500)

  expect_lte(res1$toer, 0.05)
  expect_equal(res1$toer, res2)

  skip_on_cran()
  # With simulated data
  set.seed(125)
  simdata <- get_data(k = 3, n = 15, p = 0.2, iter = n_iter, type = "bhmbasket")
  set.seed(126)
  res3 <- adjust_lambda(design = design, n = 15,
    design_params = list(tau_scale = 1), iter = n_iter, n_mcmc = 2500,
    data = simdata)
  set.seed(126)
  res4 <- adjust_lambda(design = design, n = 15,
    design_params = list(tau_scale = 1), iter = n_iter, n_mcmc = 2500,
    data = simdata)

  # Check if results are equal when adjust_lambda is called two times with
  # the same simulated data
  expect_equal(res3$lambda, res4$lambda)
  expect_equal(res3$toer, res4$toer)

  # Check if selected lambda controls the TOER
  set.seed(126)
  toer1 <- toer(design, n = 15, p1 = 0.2, lambda = res1$lambda,
    design_params = list(tau_scale = 1), data = simdata, iter = n_iter,
    n_mcmc = 2500)
  toer2 <- toer(design, n = 15, p1 = 0.2, lambda = res1$lambda - 0.01,
    design_params = list(tau_scale = 1), data = simdata, iter = n_iter,
    n_mcmc = 2500)

  expect_equal(toer1, 0.05, tolerance = 0.1)
  expect_gt(toer2, 0.05)
})
