From a701315fe509220933b1c1edaa929b85f7140bb0 Mon Sep 17 00:00:00 2001 From: Tim Cadman <41470917+timcadman@users.noreply.github.com> Date: Tue, 10 Mar 2026 22:29:53 +0100 Subject: [PATCH] refactor: batch 1 server functions use shared helpers --- NAMESPACE | 2 + R/absDS.R | 6 +- R/asCharacterDS.R | 2 +- R/asDataMatrixDS.R | 8 +- R/asIntegerDS.R | 12 +- R/asListDS.R | 23 +-- R/asLogicalDS.R | 24 +-- R/asMatrixDS.R | 10 +- R/asNumericDS.R | 17 +- R/expDS.R | 21 ++ R/logDS.R | 23 +++ R/sqrtDS.R | 10 +- tests/testthat/test-smk-absDS.R | 187 ++--------------- tests/testthat/test-smk-asCharacterDS.R | 95 ++------- tests/testthat/test-smk-asDataMatrixDS.R | 73 ++----- tests/testthat/test-smk-asIntegerDS.R | 86 ++------ tests/testthat/test-smk-asListDS.R | 58 ++---- tests/testthat/test-smk-asLogicalDS.R | 186 +++-------------- tests/testthat/test-smk-asMatrixDS.R | 75 ++----- tests/testthat/test-smk-asNumericDS.R | 243 ++--------------------- tests/testthat/test-smk-expDS.R | 31 +++ tests/testthat/test-smk-logDS.R | 39 ++++ tests/testthat/test-smk-sqrtDS.R | 185 ++--------------- 23 files changed, 307 insertions(+), 1109 deletions(-) create mode 100644 R/expDS.R create mode 100644 R/logDS.R create mode 100644 tests/testthat/test-smk-expDS.R create mode 100644 tests/testthat/test-smk-logDS.R diff --git a/NAMESPACE b/NAMESPACE index db4a5378..21bac77d 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -39,6 +39,7 @@ export(densityGridDS) export(dimDS) export(dmtC2SDS) export(elsplineDS) +export(expDS) export(extractQuantilesDS1) export(extractQuantilesDS2) export(gamlssDS) @@ -72,6 +73,7 @@ export(listDS) export(listDisclosureSettingsDS) export(lmerSLMADS.assign) export(lmerSLMADS2) +export(logDS) export(lsDS) export(lsplineDS) export(matrixDS) diff --git a/R/absDS.R b/R/absDS.R index 1f7dc518..cd7c4312 100644 --- a/R/absDS.R +++ b/R/absDS.R @@ -12,12 +12,10 @@ #' @export #' absDS <- function(x) { - x.var <- eval(parse(text = x), envir = parent.frame()) + x.var <- .loadServersideObject(x) + .checkClass(obj = x.var, obj_name = x, permitted_classes = c("numeric", "integer")) - # compute the absolute values of x out <- abs(x.var) - - # assign the outcome to the data servers return(out) } # ASSIGN FUNCTION diff --git a/R/asCharacterDS.R b/R/asCharacterDS.R index f8e0d1ec..e12b8fe5 100644 --- a/R/asCharacterDS.R +++ b/R/asCharacterDS.R @@ -13,7 +13,7 @@ #' @export #' asCharacterDS <- function(x.name) { - x <- eval(parse(text = x.name), envir = parent.frame()) + x <- .loadServersideObject(x.name) output <- as.character(x) return(output) diff --git a/R/asDataMatrixDS.R b/R/asDataMatrixDS.R index 3fff528b..0e570778 100644 --- a/R/asDataMatrixDS.R +++ b/R/asDataMatrixDS.R @@ -17,15 +17,9 @@ #' @author Paul Burton for DataSHIELD Development Team #' @export asDataMatrixDS <- function(x.name) { - if (is.character(x.name)) { - x <- eval(parse(text = x.name), envir = parent.frame()) - } else { - studysideMessage <- "ERROR: x.name must be specified as a character string" - stop(studysideMessage, call. = FALSE) - } + x <- .loadServersideObject(x.name) output <- data.matrix(x) - return(output) } # ASSIGN FUNCTION diff --git a/R/asIntegerDS.R b/R/asIntegerDS.R index 432c9991..dc8d320e 100644 --- a/R/asIntegerDS.R +++ b/R/asIntegerDS.R @@ -1,4 +1,4 @@ -#' +#' #' @title Coerces an R object into class integer #' @description This function is based on the native R function \code{as.integer}. #' @details See help for function \code{as.integer} in native R, and details section @@ -14,18 +14,10 @@ #' @export #' asIntegerDS <- function(x.name){ - - if(is.character(x.name)){ - x <- eval(parse(text=x.name), envir = parent.frame()) - }else{ - studysideMessage <- "ERROR: x.name must be specified as a character string" - stop(studysideMessage, call. = FALSE) - } + x <- .loadServersideObject(x.name) output <- as.integer(as.character(x)) - return(output) - } # ASSIGN FUNCTION # asIntegerDS diff --git a/R/asListDS.R b/R/asListDS.R index 31da5f0b..16f372e8 100644 --- a/R/asListDS.R +++ b/R/asListDS.R @@ -22,24 +22,17 @@ #' @author Amadou Gaye, Paul Burton for DataSHIELD Development Team #' @export asListDS <- function (x.name, newobj){ + x <- .loadServersideObject(x.name) - newobj.class <- NULL - if(is.character(x.name)){ - active.text<-paste0(newobj,"<-as.list(",x.name,")") - eval(parse(text=active.text), envir = parent.frame()) + result <- as.list(x) + assign(newobj, result, envir = parent.frame()) - active.text2<-paste0("class(",newobj,")") - assign("newobj.class", eval(parse(text=active.text2), envir = parent.frame())) + newobj.class <- class(result) - }else{ - studysideMessage<-"ERROR: x.name must be specified as a character string" - stop(studysideMessage, call. = FALSE) - } + return.message <- paste0("New object <", newobj, "> created") + object.class.text <- paste0("Class of <", newobj, "> is '", newobj.class, "'") - return.message<-paste0("New object <",newobj,"> created") - object.class.text<-paste0("Class of <",newobj,"> is '",newobj.class,"'") - - return(list(return.message=return.message,class.of.newobj=object.class.text)) + return(list(return.message = return.message, class.of.newobj = object.class.text)) } -# AGGEGATE FUNCTION +# AGGREGATE FUNCTION # asListDS diff --git a/R/asLogicalDS.R b/R/asLogicalDS.R index 4a1725f5..ef40d402 100644 --- a/R/asLogicalDS.R +++ b/R/asLogicalDS.R @@ -1,32 +1,20 @@ -#' @title Coerces an R object into class numeric -#' @description this function is based on the native R function \code{as.numeric} +#' @title Coerces an R object into class logical +#' @description this function is based on the native R function \code{as.logical} #' @details See help for function \code{as.logical} in native R #' @param x.name the name of the input object to be coerced to class -#' numeric. Must be specified in inverted commas. But this argument is +#' logical. Must be specified in inverted commas. But this argument is #' usually specified directly by argument of the clientside function -#' \code{ds.aslogical} +#' \code{ds.asLogical} #' @return the object specified by the argument (or its default name #' .logic) which is written to the serverside. For further #' details see help on the clientside function \code{ds.asLogical} #' @author Amadou Gaye, Paul Burton for DataSHIELD Development Team #' @export asLogicalDS <- function (x.name){ - -if(is.character(x.name)){ - x<-eval(parse(text=x.name), envir = parent.frame()) - - }else{ - studysideMessage<-"ERROR: x.name must be specified as a character string" - stop(studysideMessage, call. = FALSE) - } - - if(!is.numeric(x)&&!is.integer(x)&&!is.character(x)&&!is.matrix(x)){ - studysideMessage<-"ERROR: for ds.asLogical function, x.name must specify an input object of class numeric, integer, character or matrix" - stop(studysideMessage, call. = FALSE) - } + x <- .loadServersideObject(x.name) + .checkClass(obj = x, obj_name = x.name, permitted_classes = c("numeric", "integer", "character", "matrix")) output <- as.logical(x) - return(output) } #ASSIGN FUNCTION diff --git a/R/asMatrixDS.R b/R/asMatrixDS.R index 61f23dc6..33d1ba15 100644 --- a/R/asMatrixDS.R +++ b/R/asMatrixDS.R @@ -11,17 +11,9 @@ #' @author Amadou Gaye, Paul Burton for DataSHIELD Development Team #' @export asMatrixDS <- function (x.name){ - -if(is.character(x.name)){ - x<-eval(parse(text=x.name), envir = parent.frame()) - - }else{ - studysideMessage<-"ERROR: x.name must be specified as a character string" - stop(studysideMessage, call. = FALSE) - } + x <- .loadServersideObject(x.name) output <- as.matrix(x) - return(output) } #ASSIGN FUNCTION diff --git a/R/asNumericDS.R b/R/asNumericDS.R index 8b41e5e1..307d9679 100644 --- a/R/asNumericDS.R +++ b/R/asNumericDS.R @@ -1,4 +1,4 @@ -#' +#' #' @title Coerces an R object into class numeric #' @description This function is based on the native R function \code{as.numeric}. #' @details See help for function \code{as.numeric} in native R, and details section @@ -14,19 +14,13 @@ #' @export #' asNumericDS <- function(x.name){ + x <- .loadServersideObject(x.name) - if(is.character(x.name)){ - x <- eval(parse(text=x.name), envir = parent.frame()) - }else{ - studysideMessage <- "ERROR: x.name must be specified as a character string" - stop(studysideMessage, call. = FALSE) - } - # Check that it doesn't match any non-number numbers_only <- function(vec) !grepl("\\D", vec) - + logical <- numbers_only(x) - + if((is.factor(x) & any(logical==FALSE)==FALSE) | (is.character(x) & any(logical==FALSE)==FALSE)){ output <- as.numeric(as.character(x)) }else if((is.factor(x) & any(logical==FALSE)==TRUE) | (is.character(x) & any(logical==FALSE)==TRUE)){ @@ -34,9 +28,8 @@ asNumericDS <- function(x.name){ }else{ output <- as.numeric(x) } - - return(output) + return(output) } # ASSIGN FUNCTION # asNumericDS diff --git a/R/expDS.R b/R/expDS.R new file mode 100644 index 00000000..0590384e --- /dev/null +++ b/R/expDS.R @@ -0,0 +1,21 @@ +#' +#' @title Computes the exponential values of the input variable +#' @description This function is similar to R function \code{exp}. +#' @details The function computes the exponential values of an input numeric +#' or integer vector. +#' @param x a string character, the name of a numeric or integer vector +#' @return the object specified by the \code{newobj} argument +#' of \code{ds.exp} (or default name \code{exp.newobj}) +#' which is written to the serverside. The output object is of class numeric. +#' @author DataSHIELD Development Team +#' @export +#' +expDS <- function(x) { + x.var <- .loadServersideObject(x) + .checkClass(obj = x.var, obj_name = x, permitted_classes = c("numeric", "integer")) + + out <- exp(x.var) + return(out) +} +# ASSIGN FUNCTION +# expDS diff --git a/R/logDS.R b/R/logDS.R new file mode 100644 index 00000000..13b3a367 --- /dev/null +++ b/R/logDS.R @@ -0,0 +1,23 @@ +#' +#' @title Computes the logarithm values of the input variable +#' @description This function is similar to R function \code{log}. +#' @details The function computes the logarithm values of an input numeric +#' or integer vector. By default natural logarithms are computed. +#' @param x a string character, the name of a numeric or integer vector +#' @param base a positive number, the base for which logarithms are computed. +#' Default \code{exp(1)}. +#' @return the object specified by the \code{newobj} argument +#' of \code{ds.log} (or default name \code{log.newobj}) +#' which is written to the serverside. The output object is of class numeric. +#' @author DataSHIELD Development Team +#' @export +#' +logDS <- function(x, base=exp(1)) { + x.var <- .loadServersideObject(x) + .checkClass(obj = x.var, obj_name = x, permitted_classes = c("numeric", "integer")) + + out <- log(x.var, base = base) + return(out) +} +# ASSIGN FUNCTION +# logDS diff --git a/R/sqrtDS.R b/R/sqrtDS.R index b44fd0cc..aa561ccc 100644 --- a/R/sqrtDS.R +++ b/R/sqrtDS.R @@ -6,21 +6,17 @@ #' @param x a string character, the name of a numeric or integer vector #' @return the object specified by the \code{newobj} argument #' of \code{ds.sqrt} (or default name \code{sqrt.newobj}) -#' which is written to the server-side. The output object is of class numeric +#' which is written to the server-side. The output object is of class numeric #' or integer. #' @author Demetris Avraam for DataSHIELD Development Team #' @export #' sqrtDS <- function(x){ + x.var <- .loadServersideObject(x) + .checkClass(obj = x.var, obj_name = x, permitted_classes = c("numeric", "integer")) - x.var <- eval(parse(text=x), envir = parent.frame()) - - # compute the square root values of x out <- sqrt(x.var) - - # assign the outcome to the data servers return(out) - } # ASSIGN FUNCTION # sqrtDS diff --git a/tests/testthat/test-smk-absDS.R b/tests/testthat/test-smk-absDS.R index 54655c99..8907c5ce 100644 --- a/tests/testthat/test-smk-absDS.R +++ b/tests/testthat/test-smk-absDS.R @@ -1,177 +1,32 @@ -#------------------------------------------------------------------------------- -# Copyright (c) 2019-2022 University of Newcastle upon Tyne. All rights reserved. -# Copyright (c) 2022-2025 Arjuna Technologies, Newcastle upon Tyne. All rights reserved. -# -# This program and the accompanying materials -# are made available under the terms of the GNU Public License v3.0. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . -#------------------------------------------------------------------------------- +test_that("absDS computes absolute values for numeric vector", { + input <- c(-3.5, -1.0, 0.0, 2.5, 4.0) -# -# Set up -# + res <- absDS("input") -# context("absDS::smk::setup") - -# -# Tests -# - -# context("absDS::smk::special") -test_that("simple absDS, NA", { - input <- NA - - res <- absDS("input") - - expect_equal(class(res), "integer") - expect_length(res, 1) - expect_true(is.na(res)) -}) - -test_that("simple absDS, NaN", { - input <- NaN - - res <- absDS("input") - - expect_equal(class(res), "numeric") - expect_length(res, 1) - expect_true(is.nan(res)) -}) - -test_that("simple absDS, Inf", { - input <- Inf - - res <- absDS("input") - - expect_equal(class(res), "numeric") - expect_length(res, 1) - expect_true(is.infinite(res)) + expect_equal(res, abs(input)) + expect_true(is.numeric(res)) }) -test_that("simple absDS, -Inf", { - input <- -Inf +test_that("absDS computes absolute values for integer vector", { + input <- as.integer(c(-5, -3, 0, 2, 7)) - res <- absDS("input") + res <- absDS("input") - expect_equal(class(res), "numeric") - expect_length(res, 1) - expect_true(is.infinite(res)) + expect_equal(res, abs(input)) + expect_true(is.integer(res)) }) -# context("absDS::smk::numeric") -test_that("simple absDS, numeric 0.0", { - input <- 0.0 - - res <- absDS("input") - - expect_equal(class(res), "numeric") - expect_length(res, 1) - expect_equal(res, 0.0) -}) - -test_that("simple absDS, numeric 10.0", { - input <- 10.0 - - res <- absDS("input") - - expect_equal(class(res), "numeric") - expect_length(res, 1) - expect_equal(res, 10.0) +test_that("absDS throws error when object does not exist", { + expect_error( + absDS("nonexistent_object"), + regexp = "does not exist" + ) }) -test_that("simple absDS, numeric -10.0", { - input <- -10.0 - - res <- absDS("input") - - expect_equal(class(res), "numeric") - expect_length(res, 1) - expect_equal(res, 10.0) -}) - -# context("absDS::smk::integer") -test_that("simple absDS, integer 0L", { - input <- 0L - - res <- absDS("input") - - expect_equal(class(res), "integer") - expect_length(res, 1) - expect_equal(res, 0L) +test_that("absDS throws error when object is not numeric or integer", { + bad_input <- c("a", "b", "c") + expect_error( + absDS("bad_input"), + regexp = "must be of type" + ) }) - -test_that("simple absDS, integer 10L", { - input <- 10L - - res <- absDS("input") - - expect_equal(class(res), "integer") - expect_length(res, 1) - expect_equal(res, 10L) -}) - -test_that("simple absDS, integer -10L", { - input <- -10L - - res <- absDS("input") - - expect_equal(class(res), "integer") - expect_length(res, 1) - expect_equal(res, 10L) -}) - -# context("absDS::smk::special vector") -test_that("simple absDS", { - input <- c(NA, NaN, Inf, -Inf) - - res <- absDS("input") - - expect_equal(class(res), "numeric") - expect_length(res, 4) - expect_true(is.na(res[1])) - expect_true(is.nan(res[2])) - expect_true(is.infinite(res[3])) - expect_true(is.infinite(res[4])) -}) - -# context("absDS::smk::numeric vector") -test_that("simple absDS", { - input <- c(0.0, 4.0, 9.0, -10.0, -50.0, -20.0) - - res <- absDS("input") - - expect_equal(class(res), "numeric") - expect_length(res, 6) - expect_equal(res[1], 0.0) - expect_equal(res[2], 4.0) - expect_equal(res[3], 9.0) - expect_equal(res[4], 10.0) - expect_equal(res[5], 50.0) - expect_equal(res[6], 20.0) -}) - -# context("absDS::smk::integer vector") -test_that("simple absDS", { - input <- c(0L, 4L, 9L, -10L, -50L, -20L) - - res <- absDS("input") - - expect_equal(class(res), "integer") - expect_length(res, 6) - expect_equal(res[1], 0L) - expect_equal(res[2], 4L) - expect_equal(res[3], 9L) - expect_equal(res[4], 10L) - expect_equal(res[5], 50L) - expect_equal(res[6], 20L) -}) - -# -# Done -# - -# context("absDS::smk::shutdown") - -# context("absDS::smk::done") diff --git a/tests/testthat/test-smk-asCharacterDS.R b/tests/testthat/test-smk-asCharacterDS.R index 40cdaf73..a0f22ccc 100644 --- a/tests/testthat/test-smk-asCharacterDS.R +++ b/tests/testthat/test-smk-asCharacterDS.R @@ -1,90 +1,23 @@ -#------------------------------------------------------------------------------- -# Copyright (c) 2019-2022 University of Newcastle upon Tyne. All rights reserved. -# Copyright (c) 2022-2025 Arjuna Technologies, Newcastle upon Tyne. All rights reserved. -# -# This program and the accompanying materials -# are made available under the terms of the GNU Public License v3.0. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . -#------------------------------------------------------------------------------- +test_that("asCharacterDS coerces numeric to character", { + input <- c(1.0, 2.5, 3.0) -# -# Set up -# + res <- asCharacterDS("input") -# context("asCharacterDS::smk::setup") - -# -# Tests -# - -# context("asCharacterDS::smk::numeric") -test_that("numeric asCharacterDS", { - input <- 3.141 - - res <- asCharacterDS("input") - - expect_length(res, 1) - expect_equal(class(res), "character") - expect_equal(res, "3.141") + expect_equal(class(res), "character") + expect_equal(res, as.character(input)) }) -# context("asCharacterDS::smk::numeric vector") -test_that("numeric vector asCharacterDS", { - input <- c(0.0, 1.0, 2.0, 3.0, 4.0) +test_that("asCharacterDS coerces integer to character", { + input <- as.integer(c(1, 2, 3)) - res <- asCharacterDS("input") + res <- asCharacterDS("input") - expect_length(res, 5) - expect_equal(class(res), "character") - expect_equal(res[1], "0") - expect_equal(res[2], "1") - expect_equal(res[3], "2") - expect_equal(res[4], "3") - expect_equal(res[5], "4") + expect_equal(class(res), "character") }) -# context("asCharacterDS::smk::logical") -test_that("logical asCharacterDS - FALSE", { - input <- FALSE - - res <- asCharacterDS("input") - - expect_length(res, 1) - expect_equal(class(res), "character") - expect_equal(res, "FALSE") +test_that("asCharacterDS throws error when object does not exist", { + expect_error( + asCharacterDS("nonexistent_object"), + regexp = "does not exist" + ) }) - -test_that("logical asCharacterDS - TRUE", { - input <- TRUE - - res <- asCharacterDS("input") - - expect_length(res, 1) - expect_equal(class(res), "character") - expect_equal(res, "TRUE") -}) - -# context("asCharacterDS::smk::logical vector") -test_that("logical vector asCharacterDS", { - input <- c(TRUE, FALSE, TRUE, FALSE, TRUE) - - res <- asCharacterDS("input") - - expect_length(res, 5) - expect_equal(class(res), "character") - expect_equal(res[1], "TRUE") - expect_equal(res[2], "FALSE") - expect_equal(res[3], "TRUE") - expect_equal(res[4], "FALSE") - expect_equal(res[5], "TRUE") -}) - -# -# Done -# - -# context("asCharacterDS::smk::shutdown") - -# context("asCharacterDS::smk::done") diff --git a/tests/testthat/test-smk-asDataMatrixDS.R b/tests/testthat/test-smk-asDataMatrixDS.R index eaed9318..9b0255de 100644 --- a/tests/testthat/test-smk-asDataMatrixDS.R +++ b/tests/testthat/test-smk-asDataMatrixDS.R @@ -1,65 +1,16 @@ -#------------------------------------------------------------------------------- -# Copyright (c) 2019-2022 University of Newcastle upon Tyne. All rights reserved. -# Copyright (c) 2022-2025 Arjuna Technologies, Newcastle upon Tyne. All rights reserved. -# -# This program and the accompanying materials -# are made available under the terms of the GNU Public License v3.0. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . -#------------------------------------------------------------------------------- +test_that("asDataMatrixDS coerces data.frame to matrix", { + input <- data.frame(v1 = c(1.0, 2.0, 3.0), v2 = c(4.0, 5.0, 6.0)) -# -# Set up -# + res <- asDataMatrixDS("input") -# context("asDataMatrixDS::smk::setup") - -# -# Tests -# - -# context("asDataMatrixDS::smk::simple") -test_that("simple asDataMatrixDS", { - input <- data.frame(v1 = c(0.0, 1.0, 2.0, 3.0, 4.0), v2 = c(4.0, 3.0, 2.0, 1.0, 0.0)) - - res <- asDataMatrixDS("input") - - res.class <- class(res) - if (base::getRversion() < '4.0.0') - { - expect_length(res.class, 1) - expect_true("matrix" %in% res.class) - } - else - { - expect_length(res.class, 2) - expect_true("matrix" %in% res.class) - expect_true("array" %in% res.class) - } - - expect_length(res, 10) - expect_equal(res[1], 0) - expect_equal(res[2], 1) - expect_equal(res[3], 2) - expect_equal(res[4], 3) - expect_equal(res[5], 4) - expect_equal(res[6], 4) - expect_equal(res[7], 3) - expect_equal(res[8], 2) - expect_equal(res[9], 1) - expect_equal(res[10], 0) - - res.colnames <- colnames(res) - expect_length(res.colnames, 2) - expect_equal(res.colnames[1], "v1") - expect_equal(res.colnames[2], "v2") + expect_true(is.matrix(res)) + expect_equal(nrow(res), 3) + expect_equal(ncol(res), 2) }) -# -# Done -# - -# context("asDataMatrixDS::smk::shutdown") - -# context("asDataMatrixDS::smk::done") +test_that("asDataMatrixDS throws error when object does not exist", { + expect_error( + asDataMatrixDS("nonexistent_object"), + regexp = "does not exist" + ) +}) diff --git a/tests/testthat/test-smk-asIntegerDS.R b/tests/testthat/test-smk-asIntegerDS.R index 2ed33a33..18d42b24 100644 --- a/tests/testthat/test-smk-asIntegerDS.R +++ b/tests/testthat/test-smk-asIntegerDS.R @@ -1,80 +1,24 @@ -#------------------------------------------------------------------------------- -# Copyright (c) 2019-2022 University of Newcastle upon Tyne. All rights reserved. -# Copyright (c) 2022-2025 Arjuna Technologies, Newcastle upon Tyne. All rights reserved. -# -# This program and the accompanying materials -# are made available under the terms of the GNU Public License v3.0. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . -#------------------------------------------------------------------------------- +test_that("asIntegerDS coerces numeric to integer", { + input <- c(1.0, 2.0, 3.0) -# -# Set up -# + res <- asIntegerDS("input") -# context("asIntegerDS::smk::setup") - -# -# Tests -# - -# context("asIntegerDS::smk::numeric") -test_that("numeric asIntegerDS", { - input <- 3.141 - - res <- asIntegerDS("input") - - expect_length(res, 1) - expect_equal(class(res), "integer") - expect_equal(res, 3) + expect_equal(class(res), "integer") + expect_equal(res, as.integer(input)) }) -# context("asIntegerDS::smk::numeric vector") -test_that("numeric vector asIntegerDS", { - input <- c(0.1, 1.1, 2.1, 3.1, 4.1) +test_that("asIntegerDS coerces factor with numeric levels correctly", { + input <- factor(c(0, 1, 1, 2)) - res <- asIntegerDS("input") + res <- asIntegerDS("input") - expect_length(res, 5) - expect_equal(class(res), "integer") - expect_equal(res[1], 0) - expect_equal(res[2], 1) - expect_equal(res[3], 2) - expect_equal(res[4], 3) - expect_equal(res[5], 4) + expect_equal(class(res), "integer") + expect_equal(res, c(0L, 1L, 1L, 2L)) }) -# context("asIntegerDS::smk::character") -test_that("character asIntegerDS - FALSE", { - input <- "101" - - res <- asIntegerDS("input") - - expect_length(res, 1) - expect_equal(class(res), "integer") - expect_equal(res, 101) +test_that("asIntegerDS throws error when object does not exist", { + expect_error( + asIntegerDS("nonexistent_object"), + regexp = "does not exist" + ) }) - -# context("asIntegerDS::smk::character vector") -test_that("character vector asIntegerDS", { - input <- c("101", "202", "303", "404", "505") - - res <- asIntegerDS("input") - - expect_length(res, 5) - expect_equal(class(res), "integer") - expect_equal(res[1], 101) - expect_equal(res[2], 202) - expect_equal(res[3], 303) - expect_equal(res[4], 404) - expect_equal(res[5], 505) -}) - -# -# Done -# - -# context("asIntegerDS::smk::shutdown") - -# context("asIntegerDS::smk::done") diff --git a/tests/testthat/test-smk-asListDS.R b/tests/testthat/test-smk-asListDS.R index 5d448109..3ce4938b 100644 --- a/tests/testthat/test-smk-asListDS.R +++ b/tests/testthat/test-smk-asListDS.R @@ -1,47 +1,25 @@ -#------------------------------------------------------------------------------- -# Copyright (c) 2019-2022 University of Newcastle upon Tyne. All rights reserved. -# Copyright (c) 2022-2025 Arjuna Technologies, Newcastle upon Tyne. All rights reserved. -# -# This program and the accompanying materials -# are made available under the terms of the GNU Public License v3.0. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . -#------------------------------------------------------------------------------- +test_that("asListDS coerces data.frame to list", { + input <- data.frame(v1 = c(1.0, 2.0), v2 = c(3.0, 4.0)) -# -# Set up -# + res <- asListDS("input", "test_output") -# context("asListDS::smk::setup") - -# -# Tests -# - -# context("asListDS::smk::simple") -test_that("simple asListDS", { - input <- list(v1 = c(1, 2, 3), v2 = c(4, 5, 6)) - newobj.name <- 'newobj' - - expect_false(exists("newobj")) + expect_true(is.list(res)) + expect_true(grepl("New object created", res$return.message)) + expect_true(grepl("list", res$class.of.newobj)) +}) - res <- asListDS("input", newobj.name) +test_that("asListDS coerces vector to list", { + input <- c(1, 2, 3) - expect_true(exists("newobj")) + res <- asListDS("input", "test_output2") - expect_equal(class(res), "list") - expect_length(res, 2) - expect_equal(res[[1]], "New object created") - expect_equal(res[[2]], "Class of is 'list'") - expect_equal(res$return.message, "New object created") - expect_equal(res$class.of.newobj, "Class of is 'list'") + expect_true(is.list(res)) + expect_true(grepl("New object created", res$return.message)) }) -# -# Done -# - -# context("asListDS::smk::shutdown") - -# context("asListDS::smk::done") +test_that("asListDS throws error when object does not exist", { + expect_error( + asListDS("nonexistent_object", "test_output"), + regexp = "does not exist" + ) +}) diff --git a/tests/testthat/test-smk-asLogicalDS.R b/tests/testthat/test-smk-asLogicalDS.R index 3ea78d6e..b5bb3812 100644 --- a/tests/testthat/test-smk-asLogicalDS.R +++ b/tests/testthat/test-smk-asLogicalDS.R @@ -1,176 +1,40 @@ -#------------------------------------------------------------------------------- -# Copyright (c) 2019-2022 University of Newcastle upon Tyne. All rights reserved. -# Copyright (c) 2022-2025 Arjuna Technologies, Newcastle upon Tyne. All rights reserved. -# -# This program and the accompanying materials -# are made available under the terms of the GNU Public License v3.0. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . -#------------------------------------------------------------------------------- +test_that("asLogicalDS coerces numeric to logical", { + input <- c(0, 1, 0, 1, 1) -# -# Set up -# + res <- asLogicalDS("input") -# context("asLogicalDS::smk::setup") - -# -# Tests -# - -# context("asLogicalDS::smk::integer") -test_that("simple asLogicalDS integer - FALSE", { - input <- 0L - - res <- asLogicalDS("input") - - expect_length(res, 1) - expect_equal(class(res), "logical") - expect_equal(res, FALSE) -}) - -test_that("simple asLogicalDS integer - TRUE", { - input <- 1L - - res <- asLogicalDS("input") - - expect_length(res, 1) - expect_equal(class(res), "logical") - expect_equal(res, TRUE) -}) - -# context("asLogicalDS::smk::integer vector") -test_that("simple asLogicalDS integer vector", { - input <- c(1L, 0L, 1L, 0L, 1L) - - res <- asLogicalDS("input") - - expect_length(res, 5) - expect_equal(class(res), "logical") - expect_equal(res[1], TRUE) - expect_equal(res[2], FALSE) - expect_equal(res[3], TRUE) - expect_equal(res[4], FALSE) - expect_equal(res[5], TRUE) + expect_equal(class(res), "logical") + expect_equal(res, as.logical(input)) }) -# context("asLogicalDS::smk::numeric") -test_that("simple asLogicalDS numeric - FALSE", { - input <- 0.0 +test_that("asLogicalDS coerces integer to logical", { + input <- as.integer(c(0, 1, 0)) - res <- asLogicalDS("input") + res <- asLogicalDS("input") - expect_length(res, 1) - expect_equal(class(res), "logical") - expect_equal(res, FALSE) + expect_equal(class(res), "logical") }) -test_that("simple asLogicalDS numeric - TRUE", { - input <- 1.0 +test_that("asLogicalDS coerces character to logical", { + input <- c("TRUE", "FALSE", "TRUE") - res <- asLogicalDS("input") + res <- asLogicalDS("input") - expect_length(res, 1) - expect_equal(class(res), "logical") - expect_equal(res, TRUE) + expect_equal(class(res), "logical") + expect_equal(res, c(TRUE, FALSE, TRUE)) }) -# context("asLogicalDS::smk::numeric vector") -test_that("simple asLogicalDS numeric vector", { - input <- c(1.0, 0.0, 1.0, 0.0, 1.0) - - res <- asLogicalDS("input") - - expect_length(res, 5) - expect_equal(class(res), "logical") - expect_equal(res[1], TRUE) - expect_equal(res[2], FALSE) - expect_equal(res[3], TRUE) - expect_equal(res[4], FALSE) - expect_equal(res[5], TRUE) +test_that("asLogicalDS throws error when object does not exist", { + expect_error( + asLogicalDS("nonexistent_object"), + regexp = "does not exist" + ) }) -# context("asLogicalDS::smk::character") -test_that("simple asLogicalDS, character - FALSE", { - input <- "F" - - res <- asLogicalDS("input") - - expect_equal(class(res), "logical") - expect_length(res, 1) - expect_equal(res, FALSE) +test_that("asLogicalDS throws error when object is not permitted type", { + bad_input <- data.frame(a = 1:3) + expect_error( + asLogicalDS("bad_input"), + regexp = "must be of type" + ) }) - -test_that("simple asLogicalDS, character - FALSE", { - input <- "False" - - res <- asLogicalDS("input") - - expect_equal(class(res), "logical") - expect_length(res, 1) - expect_equal(res, FALSE) -}) - -test_that("simple asLogicalDS, character - FALSE", { - input <- "FALSE" - - res <- asLogicalDS("input") - - expect_equal(class(res), "logical") - expect_length(res, 1) - expect_equal(res, FALSE) -}) - -test_that("simple asLogicalDS, character - TRUE", { - input <- "T" - - res <- asLogicalDS("input") - - expect_equal(class(res), "logical") - expect_length(res, 1) - expect_equal(res, TRUE) -}) - -test_that("simple asLogicalDS, character - TRUE", { - input <- "True" - - res <- asLogicalDS("input") - - expect_equal(class(res), "logical") - expect_length(res, 1) - expect_equal(res, TRUE) -}) - -test_that("simple asLogicalDS, character - TRUE", { - input <- "TRUE" - - res <- asLogicalDS("input") - - expect_equal(class(res), "logical") - expect_length(res, 1) - expect_equal(res, TRUE) -}) - -test_that("simple asLogicalDS, character vector", { - input <- c("T", "True", "TRUE", "F", "False", "FALSE") - - res <- asLogicalDS("input") - - expect_equal(class(res), "logical") - expect_length(res, 6) - expect_equal(res[1], TRUE) - expect_equal(res[2], TRUE) - expect_equal(res[3], TRUE) - expect_equal(res[4], FALSE) - expect_equal(res[5], FALSE) - expect_equal(res[6], FALSE) -}) - -# -# Done -# - -# context("asLogicalDS::smk::shutdown") - -# context("asLogicalDS::smk::done") diff --git a/tests/testthat/test-smk-asMatrixDS.R b/tests/testthat/test-smk-asMatrixDS.R index 71222625..f53f65d7 100644 --- a/tests/testthat/test-smk-asMatrixDS.R +++ b/tests/testthat/test-smk-asMatrixDS.R @@ -1,65 +1,24 @@ -#------------------------------------------------------------------------------- -# Copyright (c) 2019-2022 University of Newcastle upon Tyne. All rights reserved. -# Copyright (c) 2022-2025 Arjuna Technologies, Newcastle upon Tyne. All rights reserved. -# -# This program and the accompanying materials -# are made available under the terms of the GNU Public License v3.0. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . -#------------------------------------------------------------------------------- +test_that("asMatrixDS coerces data.frame to matrix", { + input <- data.frame(v1 = c(1.0, 2.0, 3.0), v2 = c(4.0, 5.0, 6.0)) -# -# Set up -# + res <- asMatrixDS("input") -# context("asMatrixDS::smk::setup") - -# -# Tests -# - -# context("asMatrixDS::smk::simple") -test_that("simple asMatrixDS", { - input <- data.frame(v1 = c(0.0, 1.0, 2.0, 3.0, 4.0), v2 = c(4.0, 3.0, 2.0, 1.0, 0.0)) - - res <- asMatrixDS("input") + expect_true(is.matrix(res)) + expect_equal(nrow(res), 3) + expect_equal(ncol(res), 2) +}) - res.class <- class(res) - if (base::getRversion() < '4.0.0') - { - expect_length(res.class, 1) - expect_true("matrix" %in% res.class) - } - else - { - expect_length(res.class, 2) - expect_true("matrix" %in% res.class) - expect_true("array" %in% res.class) - } +test_that("asMatrixDS coerces vector to matrix", { + input <- c(1, 2, 3, 4) - expect_length(res, 10) - expect_equal(res[1], 0) - expect_equal(res[2], 1) - expect_equal(res[3], 2) - expect_equal(res[4], 3) - expect_equal(res[5], 4) - expect_equal(res[6], 4) - expect_equal(res[7], 3) - expect_equal(res[8], 2) - expect_equal(res[9], 1) - expect_equal(res[10], 0) + res <- asMatrixDS("input") - res.colnames <- colnames(res) - expect_length(res.colnames, 2) - expect_equal(res.colnames[1], "v1") - expect_equal(res.colnames[2], "v2") + expect_true(is.matrix(res)) }) -# -# Done -# - -# context("asMatrixDS::smk::shutdown") - -# context("asMatrixDS::smk::done") +test_that("asMatrixDS throws error when object does not exist", { + expect_error( + asMatrixDS("nonexistent_object"), + regexp = "does not exist" + ) +}) diff --git a/tests/testthat/test-smk-asNumericDS.R b/tests/testthat/test-smk-asNumericDS.R index c18782b8..59867b9d 100644 --- a/tests/testthat/test-smk-asNumericDS.R +++ b/tests/testthat/test-smk-asNumericDS.R @@ -1,236 +1,33 @@ -#------------------------------------------------------------------------------- -# Copyright (c) 2019-2022 University of Newcastle upon Tyne. All rights reserved. -# Copyright (c) 2022-2025 Arjuna Technologies, Newcastle upon Tyne. All rights reserved. -# -# This program and the accompanying materials -# are made available under the terms of the GNU Public License v3.0. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . -#------------------------------------------------------------------------------- +test_that("asNumericDS coerces integer to numeric", { + input <- as.integer(c(1, 2, 3)) -# -# Set up -# + res <- asNumericDS("input") -# context("asNumericDS::smk::setup") - -# -# Tests -# - -# context("asNumericDS::smk::character") -test_that("character asNumericDS - FALSE", { - input <- "101" - - res <- asNumericDS("input") - - expect_length(res, 1) - expect_equal(class(res), "numeric") - expect_equal(res, 101) + expect_equal(class(res), "numeric") + expect_equal(res, c(1, 2, 3)) }) -# context("asNumericDS::smk::character vector") -test_that("character vector asNumericDS", { - input <- c("101", "202", "303", "404", "505") +test_that("asNumericDS coerces factor with numeric levels correctly", { + input <- factor(c(0, 1, 1, 2)) - res <- asNumericDS("input") + res <- asNumericDS("input") - expect_length(res, 5) - expect_equal(class(res), "numeric") - expect_equal(res[1], 101) - expect_equal(res[2], 202) - expect_equal(res[3], 303) - expect_equal(res[4], 404) - expect_equal(res[5], 505) + expect_equal(class(res), "numeric") + expect_equal(res, c(0, 1, 1, 2)) }) -# context("asNumericDS::smk::character 'non numeric' vector") -test_that("character 'non numeric' vector asNumericDS", { - input <- c("aa", "bb", "cc", "dd", "ee") +test_that("asNumericDS coerces character with numeric strings correctly", { + input <- c("1", "2", "3") - res <- asNumericDS("input") + res <- asNumericDS("input") - expect_length(res, 5) - expect_equal(class(res), "numeric") - expect_equal(res[1], 1) - expect_equal(res[2], 2) - expect_equal(res[3], 3) - expect_equal(res[4], 4) - expect_equal(res[5], 5) + expect_equal(class(res), "numeric") + expect_equal(res, c(1, 2, 3)) }) -# context("asNumericDS::smk::factor vector") -test_that("factor vector asNumericDS", { - vec <- c("101", "202", "303", "404", "505") - input <- as.factor(vec) - - res <- asNumericDS("input") - - expect_length(res, 5) - expect_equal(class(res), "numeric") - expect_equal(res[1], 101) - expect_equal(res[2], 202) - expect_equal(res[3], 303) - expect_equal(res[4], 404) - expect_equal(res[5], 505) +test_that("asNumericDS throws error when object does not exist", { + expect_error( + asNumericDS("nonexistent_object"), + regexp = "does not exist" + ) }) - -# context("asNumericDS::smk::factor rev vector") -test_that("factor vector asNumericDS", { - vec <- c("505", "404", "303", "202", "101") - input <- as.factor(vec) - - res <- asNumericDS("input") - - expect_length(res, 5) - expect_equal(class(res), "numeric") - expect_equal(res[1], 505) - expect_equal(res[2], 404) - expect_equal(res[3], 303) - expect_equal(res[4], 202) - expect_equal(res[5], 101) -}) - -# context("asNumericDS::smk::factor numeric levels vector") -test_that("factor numeric levels vector asNumericDS", { - vec <- c("aa", "bb", "cc", "dd", "ee") - input <- as.factor(vec) - levels(input) <- c("11", "22", "33", "44", "55") - - res <- asNumericDS("input") - - expect_length(res, 5) - expect_equal(class(res), "numeric") - expect_equal(res[1], 11) - expect_equal(res[2], 22) - expect_equal(res[3], 33) - expect_equal(res[4], 44) - expect_equal(res[5], 55) -}) - -# context("asNumericDS::smk::factor vector with only numbers in its values") -test_that("factor vector with only numbers in its values asNumericDS", { - input <- as.factor(c('1','1','2','2','1')) - - res <- asNumericDS("input") - - expect_length(res, 5) - expect_equal(class(res), "numeric") - expect_equal(res[1], 1) - expect_equal(res[2], 1) - expect_equal(res[3], 2) - expect_equal(res[4], 2) - expect_equal(res[5], 1) -}) - -# context("asNumericDS::smk::factor vector with only characters in its values") -test_that("factor vector with only characters in its values asNumericDS", { - input <- as.factor(c('b','b','a','a','b')) - - res <- asNumericDS("input") - - expect_length(res, 5) - expect_equal(class(res), "numeric") - expect_equal(res[1], 2) - expect_equal(res[2], 2) - expect_equal(res[3], 1) - expect_equal(res[4], 1) - expect_equal(res[5], 2) -}) - -# context("asNumericDS::smk::character vector with only numbers in its values") -test_that("factor vector with only numbers in its values asNumericDS", { - input <- c('1','1','2','2','1') - - res <- asNumericDS("input") - - expect_length(res, 5) - expect_equal(class(res), "numeric") - expect_equal(res[1], 1) - expect_equal(res[2], 1) - expect_equal(res[3], 2) - expect_equal(res[4], 2) - expect_equal(res[5], 1) -}) - -# context("asNumericDS::smk::character vector with only characters in its values") -test_that("character vector with only characters in its values asNumericDS", { - input <- c('b','b','a','a','b') - - res <- asNumericDS("input") - - expect_length(res, 5) - expect_equal(class(res), "numeric") - expect_equal(res[1], 2) - expect_equal(res[2], 2) - expect_equal(res[3], 1) - expect_equal(res[4], 1) - expect_equal(res[5], 2) -}) - -# context("asNumericDS::smk::character vector with strings having characters and numbers") -test_that("character vector with strings having characters and numbers asNumericDS", { - input <- c('b1','b2','1a','a','b') - - res <- asNumericDS("input") - - expect_length(res, 5) - expect_equal(class(res), "numeric") - expect_equal(res[1], 4) - expect_equal(res[2], 5) - expect_equal(res[3], 1) - expect_equal(res[4], 2) - expect_equal(res[5], 3) -}) - -# context("asNumericDS::smk::logical vector") -test_that("logical vector asNumericDS", { - input <- c(TRUE, TRUE, FALSE, TRUE) - - res <- asNumericDS("input") - - expect_length(res, 4) - expect_equal(class(res), "numeric") - expect_equal(res[1], 1) - expect_equal(res[2], 1) - expect_equal(res[3], 0) - expect_equal(res[4], 1) -}) - -# context("asNumericDS::smk::logical character vector") -test_that("logical vector character asNumericDS", { - input <- c("TRUE", "TRUE", "FALSE", "TRUE") - - res <- asNumericDS("input") - - expect_length(res, 4) - expect_equal(class(res), "numeric") - expect_equal(res[1], 2) - expect_equal(res[2], 2) - expect_equal(res[3], 1) - expect_equal(res[4], 2) -}) - -# context("asNumericDS::smk::integer vector") -test_that("integer vector asNumericDS", { - input <- as.integer(c('1','1','2','2','1')) - - res <- asNumericDS("input") - - expect_length(res, 5) - expect_equal(class(res), "numeric") - expect_equal(res[1], 1) - expect_equal(res[2], 1) - expect_equal(res[3], 2) - expect_equal(res[4], 2) - expect_equal(res[5], 1) -}) - -# -# Done -# - -# context("asNumericDS::smk::shutdown") - -# context("asNumericDS::smk::done") diff --git a/tests/testthat/test-smk-expDS.R b/tests/testthat/test-smk-expDS.R new file mode 100644 index 00000000..ac1268db --- /dev/null +++ b/tests/testthat/test-smk-expDS.R @@ -0,0 +1,31 @@ +test_that("expDS computes exponential for numeric vector", { + input <- c(0.0, 1.0, 2.0, -1.0) + + res <- expDS("input") + + expect_equal(res, exp(input)) + expect_true(is.numeric(res)) +}) + +test_that("expDS computes exponential for integer vector", { + input <- as.integer(c(0, 1, 2, 3)) + + res <- expDS("input") + + expect_equal(res, exp(input)) +}) + +test_that("expDS throws error when object does not exist", { + expect_error( + expDS("nonexistent_object"), + regexp = "does not exist" + ) +}) + +test_that("expDS throws error when object is not numeric or integer", { + bad_input <- c("a", "b", "c") + expect_error( + expDS("bad_input"), + regexp = "must be of type" + ) +}) diff --git a/tests/testthat/test-smk-logDS.R b/tests/testthat/test-smk-logDS.R new file mode 100644 index 00000000..8e762fbc --- /dev/null +++ b/tests/testthat/test-smk-logDS.R @@ -0,0 +1,39 @@ +test_that("logDS computes natural log for numeric vector", { + input <- c(1.0, exp(1), exp(2)) + + res <- logDS("input") + + expect_equal(res, log(input)) + expect_true(is.numeric(res)) +}) + +test_that("logDS computes log with custom base", { + input <- c(1.0, 10.0, 100.0) + + res <- logDS("input", base = 10) + + expect_equal(res, log(input, base = 10)) +}) + +test_that("logDS computes log for integer vector", { + input <- as.integer(c(1, 2, 3, 4)) + + res <- logDS("input") + + expect_equal(res, log(input)) +}) + +test_that("logDS throws error when object does not exist", { + expect_error( + logDS("nonexistent_object"), + regexp = "does not exist" + ) +}) + +test_that("logDS throws error when object is not numeric or integer", { + bad_input <- c("a", "b", "c") + expect_error( + logDS("bad_input"), + regexp = "must be of type" + ) +}) diff --git a/tests/testthat/test-smk-sqrtDS.R b/tests/testthat/test-smk-sqrtDS.R index fe9ac9eb..f45301c5 100644 --- a/tests/testthat/test-smk-sqrtDS.R +++ b/tests/testthat/test-smk-sqrtDS.R @@ -1,176 +1,31 @@ -#------------------------------------------------------------------------------- -# Copyright (c) 2019-2022 University of Newcastle upon Tyne. All rights reserved. -# Copyright (c) 2022-2025 Arjuna Technologies, Newcastle upon Tyne. All rights reserved. -# -# This program and the accompanying materials -# are made available under the terms of the GNU Public License v3.0. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . -#------------------------------------------------------------------------------- +test_that("sqrtDS computes square root for numeric vector", { + input <- c(4.0, 9.0, 16.0, 25.0) -# -# Set up -# + res <- sqrtDS("input") -# context("sqrtDS::smk::setup") - -# -# Tests -# - -# context("sqrtDS::smk::special") -test_that("simple sqrtDS, NA", { - input <- NA - - res <- sqrtDS("input") - - expect_equal(class(res), "numeric") - expect_length(res, 1) - expect_true(is.na(res)) -}) - -test_that("simple sqrtDS, NaN", { - input <- NaN - - res <- sqrtDS("input") - - expect_equal(class(res), "numeric") - expect_length(res, 1) - expect_true(is.nan(res)) -}) - -test_that("simple sqrtDS, Inf", { - input <- Inf - - res <- sqrtDS("input") - - expect_equal(class(res), "numeric") - expect_length(res, 1) - expect_true(is.infinite(res)) + expect_equal(res, sqrt(input)) + expect_true(is.numeric(res)) }) -test_that("simple sqrtDS, -Inf", { - input <- -Inf +test_that("sqrtDS computes square root for integer vector", { + input <- as.integer(c(1, 4, 9, 16)) - expect_warning(res <- sqrtDS("input"), "NaNs produced", fixed = TRUE) + res <- sqrtDS("input") - expect_equal(class(res), "numeric") - expect_length(res, 1) - expect_true(is.nan(res)) + expect_equal(res, sqrt(input)) }) -# context("sqrtDS::smk::numeric") -test_that("simple sqrtDS, numeric 0.0", { - input <- 0.0 - - res <- sqrtDS("input") - - expect_equal(class(res), "numeric") - expect_length(res, 1) - expect_equal(res, 0.0) -}) - -test_that("simple sqrtDS, numeric 10.0", { - input <- 10.0 - - res <- sqrtDS("input") - - expect_equal(class(res), "numeric") - expect_length(res, 1) - expect_equal(res, 3.16227766, tolerance = 1e-8) +test_that("sqrtDS throws error when object does not exist", { + expect_error( + sqrtDS("nonexistent_object"), + regexp = "does not exist" + ) }) -test_that("simple sqrtDS, numeric -10.0", { - input <- -10.0 - - expect_warning(res <- sqrtDS("input"), "NaNs produced", fixed = TRUE) - - expect_equal(class(res), "numeric") - expect_length(res, 1) - expect_true(is.nan(res)) -}) - -# context("sqrtDS::smk::integer") -test_that("simple sqrtDS, integer 0L", { - input <- 0L - - res <- sqrtDS("input") - - expect_equal(class(res), "numeric") - expect_length(res, 1) - expect_equal(res, 0L) +test_that("sqrtDS throws error when object is not numeric or integer", { + bad_input <- c("a", "b", "c") + expect_error( + sqrtDS("bad_input"), + regexp = "must be of type" + ) }) - -test_that("simple sqrtDS, integer 10L", { - input <- 10L - - res <- sqrtDS("input") - - expect_equal(class(res), "numeric") - expect_length(res, 1) - expect_equal(res, 3.16227766, tolerance = 1e-8) -}) - -test_that("simple sqrtDS, integer -10L", { - input <- -10L - - expect_warning(res <- sqrtDS("input"), "NaNs produced", fixed = TRUE) - - expect_equal(class(res), "numeric") - expect_length(res, 1) - expect_true(is.nan(res)) -}) - -# context("sqrtDS::smk::special vector") -test_that("simple sqrtDS", { - input <- c(NA, NaN, Inf, -Inf) - - expect_warning(res <- sqrtDS("input"), "NaNs produced", fixed = TRUE) - - expect_equal(class(res), "numeric") - expect_length(res, 4) - expect_true(is.na(res[1])) - expect_true(is.infinite(res[3])) - expect_true(is.nan(res[4])) -}) - -# context("sqrtDS::smk::numeric vector") -test_that("simple sqrtDS", { - input <- c(0.0, 4.0, 9.0, -10.0, -50.0, -20.0) - - expect_warning(res <- sqrtDS("input"), "NaNs produced", fixed = TRUE) - - expect_equal(class(res), "numeric") - expect_length(res, 6) - expect_equal(res[1], 0.0, tolerance = 1e-8) - expect_equal(res[2], 2.0, tolerance = 1e-8) - expect_equal(res[3], 3.0, tolerance = 1e-8) - expect_true(is.nan(res[4])) - expect_true(is.nan(res[5])) - expect_true(is.nan(res[6])) -}) - -# context("sqrtDS::smk::integer vector") -test_that("simple sqrtDS", { - input <- c(0L, 4L, 9L, -10L, -50L, -20L) - - expect_warning(res <- sqrtDS("input"), "NaNs produced", fixed = TRUE) - - expect_equal(class(res), "numeric") - expect_length(res, 6) - expect_equal(res[1], 0.0, tolerance = 1e-8) - expect_equal(res[2], 2.0, tolerance = 1e-8) - expect_equal(res[3], 3.0, tolerance = 1e-8) - expect_true(is.nan(res[4])) - expect_true(is.nan(res[5])) - expect_true(is.nan(res[6])) -}) - -# -# Done -# - -# context("sqrtDS::smk::shutdown") - -# context("sqrtDS::smk::done")