From f7d27a5d39643c45cb3427089dd133433de31615 Mon Sep 17 00:00:00 2001 From: jamesaazam Date: Mon, 6 Nov 2023 13:11:50 +0000 Subject: [PATCH] Check that x is below 0 --- R/utils.r | 3 +++ tests/testthat/test-utils.R | 12 +++++++++--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/R/utils.r b/R/utils.r index adc16df..d50a9bf 100644 --- a/R/utils.r +++ b/R/utils.r @@ -6,6 +6,9 @@ #' @author Sebastian Funk #' @keywords internal complementary_logprob <- function(x) { + checkmate::assert_numeric( + x, lower = -Inf, upper = 0 + ) tryCatch(log1p(-sum(exp(x))), error = function(e) -Inf) } diff --git a/tests/testthat/test-utils.R b/tests/testthat/test-utils.R index c5444ad..235e459 100644 --- a/tests/testthat/test-utils.R +++ b/tests/testthat/test-utils.R @@ -14,7 +14,13 @@ test_that("Errors are thrown", { ) }) -test_that("Warnings are thrown", { - expect_warning(complementary_logprob(0.1), "NaNs produced") - expect_warning(complementary_logprob(Inf), "NaNs produced") +test_that("Errors are thrown", { + expect_error( + complementary_logprob(0.1), + "is not <= 0" + ) + expect_error( + complementary_logprob(Inf), + "is not <= 0" + ) })