From 35c33a4347d0e79da69dd3512e89d722d1d006ea Mon Sep 17 00:00:00 2001 From: Denes Toth Date: Tue, 26 Mar 2024 18:36:33 +0100 Subject: [PATCH 1/3] Add a unit test for a special sortedness scenario with NA --- tests/testthat/test_checkNumeric.R | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/testthat/test_checkNumeric.R b/tests/testthat/test_checkNumeric.R index 848b7883..63f2893b 100644 --- a/tests/testthat/test_checkNumeric.R +++ b/tests/testthat/test_checkNumeric.R @@ -71,6 +71,8 @@ test_that("sorted works", { else expect_true(grepl("sorted", checkNumeric(xu, sorted = TRUE), fixed = TRUE)) } + ## see https://github.com/mllg/checkmate/issues/258 + expect_true(grepl("sorted", checkNumeric(c(3, NA, 2), sorted = TRUE), fixed = TRUE)) }) test_that("names check works", { From 783c793bbdfa0861629f5137d000235f10690782 Mon Sep 17 00:00:00 2001 From: Denes Toth Date: Tue, 26 Mar 2024 18:37:27 +0100 Subject: [PATCH 2/3] Use ISNA to test if a double is NA_REAL --- src/helper.c | 2 +- src/is_sorted.c | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/helper.c b/src/helper.c index 6dc149ff..dc1af963 100644 --- a/src/helper.c +++ b/src/helper.c @@ -84,7 +84,7 @@ R_xlen_t attribute_hidden as_length(SEXP x, const char *vname) { return (R_xlen_t) xi; case REALSXP:; double xr = REAL_RO(x)[0]; - if (xr == NA_REAL) + if (ISNA(xr)) error("Argument '%s' may not be missing", vname); if (xr < 0) error("Argument '%s' must be >= 0", vname); diff --git a/src/is_sorted.c b/src/is_sorted.c index 3e2298f7..1b1b9cbc 100644 --- a/src/is_sorted.c +++ b/src/is_sorted.c @@ -31,10 +31,10 @@ static Rboolean is_sorted_double(SEXP x) { R_xlen_t i = 0; const R_xlen_t n = xlength(x); const double * const xr = REAL_RO(x); - while(i < n && xr[i] == NA_REAL) i++; + while(i < n && ISNA(xr[i])) i++; for (R_xlen_t j = i + 1; j < n; j++) { - if (xr[j] != NA_REAL) { + if (!ISNA(xr[j])) { if (xr[i] > xr[j]) return FALSE; i = j; From 52452d3d88a836960e3802a2e3c21da05ba8da2d Mon Sep 17 00:00:00 2001 From: Denes Toth Date: Tue, 26 Mar 2024 18:42:04 +0100 Subject: [PATCH 3/3] Bump pkg version number and update NEWS --- DESCRIPTION | 2 +- NEWS.md | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 350d93fe..74a06625 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -4,7 +4,7 @@ Title: Fast and Versatile Argument Checks Description: Tests and assertions to perform frequent argument checks. A substantial part of the package was written in C to minimize any worries about execution time overhead. -Version: 2.3.1 +Version: 2.3.2 Authors@R: c( person("Michel", "Lang", NULL, "michellang@gmail.com", role = c("cre", "aut"), comment = c(ORCID = "0000-0001-9754-0393")), diff --git a/NEWS.md b/NEWS.md index bef65efe..85a44a55 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,6 @@ +# Version 2.3.1 +* Fixed a sortedness check bug for numeric vectors with NAs (#258). + # Version 2.3.1 * Fixed a sprintf format string for long integers.