test_that("cutting_range works with percentage", {
  forcing <- c(0, 5000, 10000, 15000, 20000, 26000)
  # rango mayor: 15000 - 0 = 15000; 50% -> 7500 a cada lado
  res <- cutting_range(forcing, threshold = 15000, typerange = "percentage",
                       range_value = 50)
  expect_type(res, "list")
  expect_length(res, 2)
  expect_equal(res[[1]], 15000 - 7500)
  expect_equal(res[[2]], 15000 + 7500)
})

test_that("cutting_range works with balanced units", {
  forcing <- seq(0, 30000, by = 1000)
  res <- cutting_range(forcing, threshold = 15000, typerange = "balunit",
                       range_value = 2000)
  expect_equal(res[[1]], 14000)
  expect_equal(res[[2]], 16000)
})

test_that("cutting_range works with unbalanced units", {
  forcing <- seq(0, 30000, by = 1000)
  res <- cutting_range(forcing, threshold = 15000, typerange = "unbalunit",
                       range_value = c(2000, 4000))
  expect_equal(res[[1]], 15000 - 1000)
  expect_equal(res[[2]], 15000 + 2000)
})

test_that("all_binom2num converts binary columns to 0/1 numeric", {
  df <- data.frame(
    y = rnorm(30),
    a = sample(c("0", "1"), 30, replace = TRUE),
    b = sample(c("yes", "no"), 30, replace = TRUE),
    stringsAsFactors = FALSE
  )
  out <- all_binom2num(df)
  expect_true(is.numeric(out$a))
  expect_true(is.numeric(out$b))
  expect_true(all(out$a %in% c(0, 1)))
  expect_true(all(out$b %in% c(0, 1)))
  # la columna continua no se toca
  expect_identical(out$y, df$y)
})

test_that("Tave computes the absolute difference in means", {
  x <- c(1, 2, 3, 10, 11, 12)
  z <- c(0, 0, 0, 1, 1, 1)
  expect_equal(Tave(x, z), abs(mean(x[z == 1]) - mean(x[z == 0])))
  expect_equal(Tave(x, z), 9)
  # simetría
  expect_equal(Tave(x, z), Tave(x, 1 - z))
})

test_that("plus moves probabilities away from 0 and 1", {
  expect_gt(plus(0), 0)
  expect_lt(plus(1), 1)
})
