test_that("Golden Section search finds minimum of quadratic function", { f <- function(x) (x - 2)^2 + 1 res <- sm.golden_section(f, lower = 0, upper = 5, tol = 1e-6, max_iter = 100, verbose = FALSE) # Check convergence expect_true(res$converged) # Check estimated minimum close to true minimum expect_equal(res$x_min, 2, tolerance = 1e-5) # Check function value at minimum expect_equal(res$f_min, 1, tolerance = 1e-5) # Iteration data frame should not be empty expect_true(nrow(res$iter_df) > 0) }) # Test 2: Minimum at interval boundary test_that("Golden Section search handles minimum at boundary", { f <- function(x) (x)^2 res <- sm.golden_section(f, lower = 0, upper = 5, tol = 1e-6, verbose = FALSE) expect_true(res$converged) expect_equal(res$x_min, 0, tolerance = 1e-5) expect_equal(res$f_min, 0, tolerance = 1e-5) }) # Test 3: Nonlinear function test_that("Golden Section search works on nonlinear function", { f <- function(x) sin(x) + (x - 1)^2 res <- sm.golden_section(f, lower = 0, upper = 5, tol = 1e-6, verbose = FALSE) expect_true(res$converged) # Minimum should lie within the interval expect_true(res$x_min >= 0 && res$x_min <= 5) # Function value should be finite expect_true(is.finite(res$f_min)) }) # Test 4: Verbose mode runs without error test_that("Golden Section search verbose mode runs without error", { f <- function(x) (x - 1)^2 expect_error(res <- sm.golden_section(f, lower = 0, upper = 2, verbose = TRUE), NA) })