Skip to content

Commit

Permalink
Testing that get_phi() returns no negative or totally zero result.
Browse files Browse the repository at this point in the history
Close #283
  • Loading branch information
gustavdelius committed Dec 1, 2023
1 parent 0f0e088 commit 0aa6d22
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
13 changes: 10 additions & 3 deletions R/setPredKernel.R
Original file line number Diff line number Diff line change
Expand Up @@ -320,13 +320,20 @@ get_phi <- function(species_params, ppmr) {
}
pars <- c(ppmr = list(ppmr), as.list(species_params[i, args]))
phi <- do.call(pred_kernel_func_name, args = pars)
if (any(is.na(phi)) &&
(species_params$interaction_resource[i] > 0 ||
any(interaction[i, ] > 0))) {

if (any(is.na(phi))) {
stop("The function ", pred_kernel_func_name,
" returned NA. Did you correctly specify all required",
" parameters in the species parameter dataframe?")
}
if (any(phi < 0)) {
stop("The function ", pred_kernel_func_name,
" returned negative values.")
}
if (all(phi == 0)) {
stop("The function ", pred_kernel_func_name,
" returned a zero predation kernel.")
}
phis[i, ] <- phi
}
return(phis)
Expand Down
9 changes: 9 additions & 0 deletions tests/testthat/test-setPredKernel.R
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,13 @@ test_that("get_phi works", {
expect_identical(phi[1, 1], 0)
expect_identical(phi[1, 2], 1)
expect_identical(phi[1, 5], 0)
# call with invalid parameters
NS_species_params$ppmr_max <- 1
expect_error(get_phi(NS_species_params, 1:5),
"ppmr_min not less than ppmr_max")
NS_species_params$pred_kernel_type <- "lognormal"
NS_species_params$sigma <- 0
expect_error(get_phi(NS_species_params, 1:5),
"The function lognormal_pred_kernel returned a zero predation kernel")
})

0 comments on commit 0aa6d22

Please sign in to comment.