From e764d02a521c2936e510179e4736b17521140a01 Mon Sep 17 00:00:00 2001 From: Matthew Kay Date: Sat, 20 Jul 2024 15:34:24 -0500 Subject: [PATCH 1/2] normalize test file names --- .../testthat/{test-dist_categorical.R => test-dist-categorical.R} | 0 tests/testthat/{test-dist_lognormal.R => test-dist-lognormal.R} | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename tests/testthat/{test-dist_categorical.R => test-dist-categorical.R} (100%) rename tests/testthat/{test-dist_lognormal.R => test-dist-lognormal.R} (100%) diff --git a/tests/testthat/test-dist_categorical.R b/tests/testthat/test-dist-categorical.R similarity index 100% rename from tests/testthat/test-dist_categorical.R rename to tests/testthat/test-dist-categorical.R diff --git a/tests/testthat/test-dist_lognormal.R b/tests/testthat/test-dist-lognormal.R similarity index 100% rename from tests/testthat/test-dist_lognormal.R rename to tests/testthat/test-dist-lognormal.R From a826dc8c7f7895e6d78ba4fdfaae6f293078d70b Mon Sep 17 00:00:00 2001 From: Matthew Kay Date: Sat, 20 Jul 2024 15:54:39 -0500 Subject: [PATCH 2/2] ensure functions returning NA_real_ use length of input as appropriate --- R/dist_categorical.R | 4 ++-- R/dist_missing.R | 6 +++--- tests/testthat/test-dist-categorical.R | 3 +++ tests/testthat/test-dist-missing.R | 18 ++++++++++++++++++ 4 files changed, 26 insertions(+), 5 deletions(-) create mode 100755 tests/testthat/test-dist-missing.R diff --git a/R/dist_categorical.R b/R/dist_categorical.R index b2f33873..0505b155 100644 --- a/R/dist_categorical.R +++ b/R/dist_categorical.R @@ -99,12 +99,12 @@ density.dist_categorical <- function(x, at, ...){ #' @export quantile.dist_categorical <- function(x, p, ...){ - NA_real_ + rep_len(NA_real_, length(p)) } #' @export cdf.dist_categorical <- function(x, q, ...){ - NA_real_ + rep_len(NA_real_, length(q)) } #' @export diff --git a/R/dist_missing.R b/R/dist_missing.R index 15e82925..549f6d77 100644 --- a/R/dist_missing.R +++ b/R/dist_missing.R @@ -40,7 +40,7 @@ format.dist_na <- function(x, ...) { #' @export density.dist_na <- function(x, at, ...){ - NA_real_ + rep_len(NA_real_, length(at)) } #' @export @@ -48,14 +48,14 @@ log_density.dist_na <- density.dist_na #' @export quantile.dist_na <- function(x, p, ...){ - NA_real_ + rep_len(NA_real_, length(p)) } #' @export log_quantile.dist_na <- quantile.dist_na #' @export cdf.dist_na <- function(x, q, ...){ - NA_real_ + rep_len(NA_real_, length(q)) } #' @export log_cdf.dist_na <- cdf.dist_na diff --git a/tests/testthat/test-dist-categorical.R b/tests/testthat/test-dist-categorical.R index dbb3b891..a78735bf 100644 --- a/tests/testthat/test-dist-categorical.R +++ b/tests/testthat/test-dist-categorical.R @@ -6,6 +6,7 @@ test_that("Categorical distribution", { # quantiles expect_true(all(is.na(quantile(dist, 0.5)))) expect_true(all(is.na(quantile(dist, 0.2)))) + expect_equal(quantile(dist, c(0.1, 0.9)), list(c(NA_real_, NA_real_))) # pdf expect_equal(density(dist, -1), NA_real_) @@ -13,9 +14,11 @@ test_that("Categorical distribution", { expect_equal(density(dist, 1), 0.4) expect_equal(density(dist, 2), 0.2) expect_equal(density(dist, 5), NA_real_) + expect_equal(density(dist, 3:5), list(c(0.3, 0.1, NA_real_))) # cdf expect_true(all(is.na(cdf(dist, 1)))) + expect_equal(cdf(dist, 1:2), list(c(NA_real_, NA_real_))) # stats expect_true(all(is.na(mean(dist)))) diff --git a/tests/testthat/test-dist-missing.R b/tests/testthat/test-dist-missing.R new file mode 100755 index 00000000..00aabb45 --- /dev/null +++ b/tests/testthat/test-dist-missing.R @@ -0,0 +1,18 @@ +test_that("Missing distribution", { + dist <- dist_missing() + + expect_equal(format(dist), "NA") + + # quantiles + expect_equal(quantile(dist, c(0.1, 0.9)), list(c(NA_real_, NA_real_))) + + # pdf + expect_equal(density(dist, 1:2), list(c(NA_real_, NA_real_))) + + # cdf + expect_equal(cdf(dist, 1:2), list(c(NA_real_, NA_real_))) + + # stats + expect_equal(mean(dist), NA_real_) + expect_equal(variance(dist), NA_real_) +})