Skip to content

Commit

Permalink
Add checks and tests for all arguments of rnbinom_mean_disp()
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesmbaazam committed Nov 6, 2023
1 parent f7d27a5 commit ccbb57c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
6 changes: 3 additions & 3 deletions R/utils.r
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ rgen_length <- function(n, x, prob) {
#' Negative binomial random numbers parametrized
#' in terms of mean and dispersion coefficient
#' @param n number of samples to draw
#' @param mn mean of distribution
#' @param mn mean of distribution; Must be > 0.
#' @param disp dispersion coefficient (var/mean); Must be > 1.
#' @return vector containing the random numbers
#' @author Flavio Finger
Expand All @@ -55,13 +55,13 @@ rgen_length <- function(n, x, prob) {
#' rnbinom_mean_disp(n = 5, mn = 4, disp = 2)
rnbinom_mean_disp <- function(n, mn, disp) {
checkmate::assert_number(
n, lower = 0, finite = TRUE, na.ok = FALSE
n, lower = 1, finite = TRUE, na.ok = FALSE
)
checkmate::assert_number(
disp, lower = 1, finite = TRUE, na.ok = FALSE
)
checkmate::assert_number(
mn, lower = 0, finite = TRUE, na.ok = FALSE
mn, lower = 1E-100, finite = TRUE, na.ok = FALSE
)
size <- mn / (disp - 1)
stats::rnbinom(n, size = size, mu = mn)
Expand Down
34 changes: 34 additions & 0 deletions tests/testthat/test-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,44 @@ test_that("Util functions work", {
})

test_that("Errors are thrown", {
# Checks on 'disp' argument
expect_error(
rnbinom_mean_disp(n = 5, mn = 4, disp = 0.9),
"is not >= 1"
)
expect_error(
rnbinom_mean_disp(n = 5, mn = 4, disp = NA),
"May not be NA"
)
expect_error(
rnbinom_mean_disp(n = 5, mn = 4, disp = Inf),
"Must be finite"
)
# Checks on 'n' argument
expect_error(
rnbinom_mean_disp(n = 0, mn = 4, disp = 2),
"is not >= 1"
)
expect_error(
rnbinom_mean_disp(n = NA, mn = 4, disp = 2),
"May not be NA"
)
expect_error(
rnbinom_mean_disp(n = Inf, mn = 4, disp = 2),
"Must be finite"
)
# Checks on 'mn' argument
expect_error(
rnbinom_mean_disp(n = 2, mn = 0, disp = 2)
)
expect_error(
rnbinom_mean_disp(n = 2, mn = NA, disp = 2),
"May not be NA"
)
expect_error(
rnbinom_mean_disp(n = 2, mn = Inf, disp = 2),
"Must be finite"
)
})

test_that("Errors are thrown", {
Expand Down

0 comments on commit ccbb57c

Please sign in to comment.