tests/testthat/test-partitionSites.R

outline <- terra::vect(system.file("extdata", "example_outline.shp", package = "rLakeHabitat"))
dat <- read.csv(system.file("extdata", "example_depths.csv", package = "rLakeHabitat"))

dem <- interpBathy(outline, dat, "x", "y", "z", zeros = FALSE, separation = 10, res = 10, method = "IDW", nmax = 8)


# input validation

test_that("partitionSites errors on non-SpatRaster dem", {
  expect_error(
    partitionSites(dem = as.matrix(1), depth_bins = c(0, 5, Inf), n_per_bin = 2),
    "dem must be a SpatRaster"
  )
})

test_that("partitionSites errors on invalid depth_bins", {
  expect_error(
    partitionSites(dem, depth_bins = 5, n_per_bin = 2),
    "depth_bins must be a numeric vector of at least 2 bin edges"
  )
  expect_error(
    partitionSites(dem, depth_bins = c("a", "b"), n_per_bin = 2),
    "depth_bins must be a numeric vector of at least 2 bin edges"
  )
})

test_that("partitionSites errors on mismatched n_per_bin length", {
  expect_error(
    partitionSites(dem, depth_bins = c(0, 5, 10, Inf), n_per_bin = c(2, 3)),
    "n_per_bin must be a single value"
  )
})

test_that("partitionSites errors on negative n_per_bin", {
  expect_error(
    partitionSites(dem, depth_bins = c(0, 5, Inf), n_per_bin = -1),
    "n_per_bin values must be non-negative"
  )
})

test_that("partitionSites errors on invalid seed", {
  expect_error(
    partitionSites(dem, depth_bins = c(0, 5, Inf), n_per_bin = 2, seed = "abc"),
    "seed must be numeric"
  )
})

# n_per_bin recycling

test_that("partitionSites recycles a single n_per_bin value across all bins", {
  depth_range <- terra::minmax(dem)[, 1]
  mid <- mean(depth_range)
  bins <- c(depth_range[1], mid, depth_range[2] + 1)

  result <- partitionSites(dem, depth_bins = bins, n_per_bin = 3, plot = FALSE, seed = 123)

  expect_type(result, "list")
  expect_named(result, c("locations", "map"))
  expect_s3_class(result$locations, "data.frame")
  expect_named(result$locations, c("x", "y", "depth_bin", "bin_index"))

  expect_equal(nrow(result$locations), 6)
  expect_equal(sort(unique(result$locations$bin_index)), c(1, 2))
})

test_that("partitionSites accepts a per-bin n_per_bin vector", {
  depth_range <- terra::minmax(dem)[, 1]
  mid <- mean(depth_range)
  bins <- c(depth_range[1], mid, depth_range[2] + 1)

  result <- partitionSites(dem, depth_bins = bins, n_per_bin = c(4, 2), plot = FALSE, seed = 123)

  expect_equal(nrow(result$locations), 6)
  expect_equal(sum(result$locations$bin_index == 1), 4)
  expect_equal(sum(result$locations$bin_index == 2), 2)
})

# min_spacing truncation

test_that("partitionSites returns exactly n_per_bin locations even when min_spacing is set", {
  depth_range <- terra::minmax(dem)[, 1]
  bins <- c(depth_range[1], depth_range[2] + 1)

  result <- partitionSites(dem, depth_bins = bins, n_per_bin = 5,
                           min_spacing = 20, plot = FALSE, seed = 123)

  expect_lte(nrow(result$locations), 5)
  expect_gt(nrow(result$locations), 0)
})

# map output

test_that("partitionSites returns NULL map when plot = FALSE and a recordedplot when plot = TRUE", {
  depth_range <- terra::minmax(dem)[, 1]
  bins <- c(depth_range[1], depth_range[2] + 1)

  result_no_plot <- partitionSites(dem, depth_bins = bins, n_per_bin = 2, plot = FALSE, seed = 123)
  expect_null(result_no_plot$map)

  result_plot <- partitionSites(dem, depth_bins = bins, n_per_bin = 2, plot = TRUE, seed = 123)
  expect_s3_class(result_plot$map, "recordedplot")
})

test_that("partitionSites is reproducible with a fixed seed", {
  depth_range <- terra::minmax(dem)[, 1]
  bins <- c(depth_range[1], depth_range[2] + 1)

  r1 <- partitionSites(dem, depth_bins = bins, n_per_bin = 3, plot = FALSE, seed = 7)
  r2 <- partitionSites(dem, depth_bins = bins, n_per_bin = 3, plot = FALSE, seed = 7)
  expect_equal(r1$locations, r2$locations)
})

Try the rLakeHabitat package in your browser

Any scripts or data that you put into this service are public.

rLakeHabitat documentation built on July 30, 2026, 5:11 p.m.