# ---- adfoControl ------------------------------------------------------------- test_that("adfoControl() returns correct class and key defaults", { ctl <- adfoControl() expect_s3_class(ctl, "adfoControl") expect_equal(ctl$grad, "none") expect_equal(ctl$maxeval, 500L) expect_equal(ctl$seed, 12345L) expect_equal(ctl$cores, 1L) expect_equal(ctl$n_restarts, 1L) expect_equal(ctl$workers, 1L) expect_equal(ctl$covMethod, "r") expect_equal(ctl$returnAdmr, FALSE) }) test_that("adfoControl(): grad != 'none' + BOBYQA switches to LBFGS", { ctl <- adfoControl(grad = "analytical") expect_equal(ctl$algorithm, "NLOPT_LD_LBFGS") }) test_that("adfoControl(): grad = 'fd' + BOBYQA switches to LBFGS", { ctl <- adfoControl(grad = "fd") expect_equal(ctl$algorithm, "NLOPT_LD_LBFGS") }) test_that("adfoControl(): grad = 'cfd' + BOBYQA switches to LBFGS", { ctl <- adfoControl(grad = "cfd") expect_equal(ctl$algorithm, "NLOPT_LD_LBFGS") }) test_that("adfoControl(): grad = 'none' keeps BOBYQA", { ctl <- adfoControl(grad = "none") expect_equal(ctl$algorithm, "NLOPT_LN_BOBYQA") }) test_that("adfoControl(): grad != 'none' + explicit non-BOBYQA algorithm kept", { ctl <- adfoControl(grad = "fd", algorithm = "NLOPT_LD_SLSQP") expect_equal(ctl$algorithm, "NLOPT_LD_SLSQP") }) test_that("adfoControl(): internal n_sim is 1L for .admRunRestarts() compat", { ctl <- adfoControl() expect_type(ctl$n_sim, "integer") expect_equal(ctl$n_sim, 1L) }) test_that("adfoControl(): internal sampling is 'sobol' for .admRunRestarts() compat", { ctl <- adfoControl() expect_equal(ctl$sampling, "sobol") }) test_that("adfoControl(): maxeval stored as integer", { ctl <- adfoControl(maxeval = 200) expect_type(ctl$maxeval, "integer") expect_equal(ctl$maxeval, 200L) }) test_that("adfoControl(): workers stored as integer", { ctl <- adfoControl(workers = 2) expect_type(ctl$workers, "integer") expect_equal(ctl$workers, 2L) }) test_that("adfoControl(): n_restarts stored as integer", { ctl <- adfoControl(n_restarts = 3) expect_type(ctl$n_restarts, "integer") expect_equal(ctl$n_restarts, 3L) }) test_that("adfoControl(): cov_h_outer default is eps^(1/5)", { ctl <- adfoControl() expect_equal(ctl$cov_h_outer, .Machine$double.eps^(1/5), tolerance = 1e-15) }) test_that("adfoControl(): studies stored as-is", { st <- list(s1 = list(E = 1, V = 1, n = 10L, times = 1)) ctl <- adfoControl(studies = st) expect_identical(ctl$studies, st) }) test_that("adfoControl(): returnAdmr stored correctly", { ctl <- adfoControl(returnAdmr = TRUE) expect_true(ctl$returnAdmr) }) test_that("adfoControl(): unknown argument errors with informative message", { expect_error(adfoControl(nonexistent_arg = 42), regexp = "unused argument") }) test_that("adfoControl(): invalid grad errors", { expect_error(adfoControl(grad = "newton")) }) test_that("adfoControl(): zero cores errors via checkmate", { expect_error(adfoControl(cores = 0), regexp = "cores") }) test_that("adfoControl(): negative maxeval errors via checkmate", { expect_error(adfoControl(maxeval = 0), regexp = "maxeval") }) test_that("adfoControl(): invalid covMethod errors", { expect_error(adfoControl(covMethod = "hessian")) }) test_that("adfoControl(): covMethod 'none' accepted", { ctl <- adfoControl(covMethod = "none") expect_equal(ctl$covMethod, "none") }) test_that("adfoControl(): grad_h, grad_bounds, cov_h stored", { ctl <- adfoControl(grad_h = 1e-5, grad_bounds = 3, cov_h = 5e-4) expect_equal(ctl$grad_h, 1e-5) expect_equal(ctl$grad_bounds, 3) expect_equal(ctl$cov_h, 5e-4) }) test_that("adfoControl(): getValidNlmixrCtl.adfo passes through adfoControl", { ctl <- adfoControl(maxeval = 99L) out <- admixr2:::getValidNlmixrCtl.adfo(ctl) expect_s3_class(out, "adfoControl") expect_equal(out$maxeval, 99L) }) test_that("adfoControl(): getValidNlmixrCtl.adfo returns default for NULL", { out <- admixr2:::getValidNlmixrCtl.adfo(NULL) expect_s3_class(out, "adfoControl") }) test_that("adfoControl(): getValidNlmixrCtl.adfo converts plain list", { out <- admixr2:::getValidNlmixrCtl.adfo(list(maxeval = 77L)) expect_s3_class(out, "adfoControl") expect_equal(out$maxeval, 77L) })