Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure NA returns use input vector length as needed #115

Merged
merged 2 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions R/dist_categorical.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions R/dist_missing.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,22 @@ format.dist_na <- function(x, ...) {

#' @export
density.dist_na <- function(x, at, ...){
NA_real_
rep_len(NA_real_, length(at))
}

#' @export
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@ 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_)
expect_equal(density(dist, 0), NA_real_)
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))))
Expand Down
18 changes: 18 additions & 0 deletions tests/testthat/test-dist-missing.R
Original file line number Diff line number Diff line change
@@ -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_)
})
Loading