test_that("Fibonacci search finds minimum of quadratic function", { f <- function(x) (x - 3)^2 + 2 res <- sm.fibonacci_search(f, a = 0, b = 6, 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, 3, tolerance = 1e-5) # Check function value at minimum expect_equal(res$f_min, 2, tolerance = 1e-5) # Check iteration data frame is not empty expect_true(nrow(res$iter_df) > 0) }) # Test 2: Minimum at the edge of interval test_that("Fibonacci search handles minimum at interval boundary", { f <- function(x) (x)^2 res <- sm.fibonacci_search(f, a = 0, b = 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: More complex 1D function test_that("Fibonacci search works on nonlinear function", { f <- function(x) sin(x) + (x-2)^2 res <- sm.fibonacci_search(f, a = 0, b = 5, tol = 1e-6, verbose = FALSE) expect_true(res$converged) # The minimum should be in [0,5] expect_true(res$x_min >= 0 && res$x_min <= 5) # Function value should be finite expect_true(is.finite(res$f_min)) }) # Test 4: Check verbose output does not fail test_that("Fibonacci search verbose mode runs without error", { f <- function(x) (x - 1)^2 expect_error(res <- sm.fibonacci_search(f, a = 0, b = 2, verbose = TRUE), NA) })