Skip to content

Commit

Permalink
Rename all fn to func
Browse files Browse the repository at this point in the history
  • Loading branch information
pratikunterwegs committed Jan 30, 2024
1 parent fe71615 commit 6e89af9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 36 deletions.
30 changes: 15 additions & 15 deletions R/function_checkers.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@
#' input returns a numeric vector output of the same length as the input, with
#' finite non-missing values \eqn{\geq} 0.0.
#'
#' @param fn A function. This is expected to be a function evaluating the
#' @param func A function. This is expected to be a function evaluating the
#' density of a distribution at numeric values.
#' @param n_req_args The number of required arguments, i.e., arguments without
#' default values.
#' @param n The number of elements over which to evaluate the function `fn`.
#' Defaults to 10, and `fn` is evaluated over `seq(n)`.
#' @param n The number of elements over which to evaluate the function `func`.
#' Defaults to 10, and `func` is evaluated over `seq(n)`.
#'
#' @return A logical for whether the function `fn` fulfils conditions specified
#' in the respective checks.
#' @return A logical for whether the function `func` fulfils conditions
#' specified in the respective checks.
#' @keywords internal
test_fn_req_args <- function(fn, n_req_args = 1) {
# NOTE: replacing checkmate::assert_count()
test_func_req_args <- function(func, n_req_args = 1) {
# check for a non-negative integerish value
stopifnot(
"`n_req_args` must be a single number" = (is.numeric(n_req_args)) &&
(length(n_req_args) == 1),
Expand All @@ -34,31 +34,31 @@ test_fn_req_args <- function(fn, n_req_args = 1) {
"`n_req_args` must be a natural number > 0" = (n_req_args > 0) &&
(n_req_args %% 1 == 0)
)
# NOTE: using formals(args(fn)) to allow checking args of builtin primitives
# for which formals(fn) would return NULL and cause the check to error
# NOTE: using formals(args(func)) to allow checking args of builtin primitives
# for which formals(func) would return NULL and cause the check to error
# NOTE: errors non-informatively for specials such as `if`
is.function(fn) &&
is.function(func) &&
Reduce(
x = Map(
function(x, y) {
is.name(x) && y != "..."
},
formals(args(fn)), names(formals(args(fn)))
formals(args(func)), names(formals(args(func)))
),
f = `+`
) == n_req_args
}

#' @name function_checkers
test_fn_num_out <- function(fn, n = 10) {
# NOTE: replacing checkmate::assert_count()
test_func_num_out <- function(func, n = 10) {
# check for a non-negative integerish value
stopifnot(
"`n` must be a single number" = (is.numeric(n)) && (length(n) == 1),
"`n` must be finite and non-missing" = (is.finite(n)) && (!is.na(n)),
"`n` must be a natural number > 0" = (n > 0) && (n %% 1 == 0),
"`fn` must be a function" = is.function(fn)
"`func` must be a function" = is.function(func)
)
out <- fn(seq(n))
out <- func(seq(n))

# return logical output of conditions
is.numeric(out) && (length(out) == n) && (!anyNA(out)) &&
Expand Down
42 changes: 21 additions & 21 deletions tests/testthat/test-function_checkers.R
Original file line number Diff line number Diff line change
@@ -1,60 +1,60 @@
#### Checks on test_fn_req_args() ####
#### Checks on test_func_req_args() ####
test_that("Function to check N required arguments", {
# Test function to check number of required arguments
expect_true(
test_fn_req_args(
test_func_req_args(
function(x) dgamma(x, 5, 1)
)
)
# `+` needs two arguments
expect_true(
test_fn_req_args(
test_func_req_args(
`+`, 2L
)
)
expect_false(
test_fn_req_args(
test_func_req_args(
`+`, 1L
)
)
expect_false(
test_fn_req_args(
test_func_req_args(
function(x, y, ...) dgamma(x, 5, 1, ...) + y
)
)
expect_true(
test_fn_req_args(
test_func_req_args(
dgamma, 2
)
)

# fails on poorly specified n
expect_error(
test_fn_req_args("dummy_fn", seq(10)),
test_func_req_args("dummy_fn", seq(10)),
regexp = "`n_req_args` must be a single number"
)
expect_error(
test_fn_req_args("dummy_fn", -1),
test_func_req_args("dummy_fn", -1),
regexp = "`n_req_args` must be a natural number > 0"
)
expect_error(
test_fn_req_args("dummy_fn", NA_real_),
test_func_req_args("dummy_fn", NA_real_),
regexp = "`n_req_args` must be finite and non-missing"
)
expect_error(
test_fn_req_args("dummy_fn", Inf),
test_func_req_args("dummy_fn", Inf),
regexp = "`n_req_args` must be finite and non-missing"
)

# expect TRUE when an appropriate primitive is passed
# NOTE: Passes as this primitive has one arg
expect_true(
test_fn_req_args(is.list)
test_func_req_args(is.list)
)

## functions with ellipsis
expect_true(
test_fn_req_args(
test_func_req_args(
function(x, ...) dgamma(x, 5, 1, ...)
)
)
Expand All @@ -64,35 +64,35 @@ test_that("Function to check N required arguments", {
test_that("Function to check numeric output", {
# well specified case
expect_true(
test_fn_num_out(
test_func_num_out(
function(x) dgamma(x, 5, 1)
)
)
# false on wrong input type
expect_error(
test_fn_num_out("dummy_fn"),
regexp = "`fn` must be a function"
test_func_num_out("dummy_fn"),
regexp = "`func` must be a function"
)
# fails on poorly specified n
expect_error(
test_fn_num_out("dummy_fn", seq(10)),
test_func_num_out("dummy_fn", seq(10)),
regexp = "`n` must be a single number"
)
expect_error(
test_fn_num_out("dummy_fn", -1),
test_func_num_out("dummy_fn", -1),
regexp = "`n` must be a natural number > 0"
)
expect_error(
test_fn_num_out("dummy_fn", NA_real_),
test_func_num_out("dummy_fn", NA_real_),
regexp = "`n` must be finite and non-missing"
)
expect_error(
test_fn_num_out("dummy_fn", Inf),
test_func_num_out("dummy_fn", Inf),
regexp = "`n` must be finite and non-missing"
)
# false on wrong return type
expect_false(
test_fn_num_out(
test_func_num_out(
function(x, ...) as.character(dgamma(x, 5, 1, ...))
)
)
Expand All @@ -102,6 +102,6 @@ test_that("Function to check numeric output", {
a[-1]
}
expect_false(
test_fn_num_out(fn)
test_func_num_out(fn)
)
})

0 comments on commit 6e89af9

Please sign in to comment.