Skip to content

Commit 9552ebc

Browse files
authored
Update complex tests for R-devel (4.4.0) (#1883)
* Update complex tests * NEWS bullet
1 parent 8bbd8c4 commit 9552ebc

File tree

5 files changed

+50
-13
lines changed

5 files changed

+50
-13
lines changed

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# vctrs (development version)
22

3+
* Fixed an issue with complex vector tests related to changes in R-devel
4+
(#1883).
5+
36
* Added a class to the `vec_locate_matches()` error that is thrown when an
47
overflow would otherwise occur (#1845).
58

tests/testthat/helper-vctrs.R

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,15 @@ expect_equal <- function(object, expected, ...,
5656
raw2 <- function(...) {
5757
as.raw(list_unchop(list2(...), ptype = integer()))
5858
}
59+
cpl2 <- function(...) {
60+
# R 4.4.0 changed `as.complex(NA_real/integer/logical)` so that it always uses
61+
# a `0` in the imaginary slot. While this is reasonable, it is annoying for
62+
# comparison purposes in tests, where we typically propagate the `NA`. As of
63+
# rlang 1.1.1, `cpl()` inherits this behavior change so we have a custom version
64+
# here that works the same on all R versions.
65+
# https://github.com/wch/r-source/commit/1a2aea9ac3c216fea718f33f712764afc34f6ee8
66+
out <- list2(...)
67+
out <- as.complex(out)
68+
out[is.na(out)] <- complex(real = NA_real_, imaginary = NA_real_)
69+
out
70+
}

tests/testthat/test-slice-assign.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -681,7 +681,7 @@ test_that("can assign shaped base vectors with compact seqs", {
681681
expect_identical(vec_assign_seq(mat(lgl(1, 0, 1)), NA, start, size, increasing), mat(lgl(1, NA, NA)))
682682
expect_identical(vec_assign_seq(mat(int(1, 2, 3)), NA, start, size, increasing), mat(int(1, NA, NA)))
683683
expect_identical(vec_assign_seq(mat(dbl(1, 2, 3)), NA, start, size, increasing), mat(dbl(1, NA, NA)))
684-
expect_identical(vec_assign_seq(mat(cpl(1, 2, 3)), NA, start, size, increasing), mat(cpl(1, NA, NA)))
684+
expect_identical(vec_assign_seq(mat(cpl2(1, 2, 3)), NA, start, size, increasing), mat(cpl2(1, NA, NA)))
685685
expect_identical(vec_assign_seq(mat(chr("1", "2", "3")), NA, start, size, increasing), mat(chr("1", NA, NA)))
686686
expect_identical(vec_assign_seq(mat(raw2(1, 2, 3)), raw2(1), start, size, increasing), mat(raw2(1, 1, 1)))
687687
expect_identical(vec_assign_seq(mat(list(1, 2, 3)), NA, start, size, increasing), mat(list(1, NULL, NULL)))
@@ -695,7 +695,7 @@ test_that("can assign shaped base vectors with decreasing compact seqs", {
695695
expect_identical(vec_assign_seq(mat(lgl(1, 0, 1)), NA, start, size, increasing), mat(lgl(1, NA, NA)))
696696
expect_identical(vec_assign_seq(mat(int(1, 2, 3)), NA, start, size, increasing), mat(int(1, NA, NA)))
697697
expect_identical(vec_assign_seq(mat(dbl(1, 2, 3)), NA, start, size, increasing), mat(dbl(1, NA, NA)))
698-
expect_identical(vec_assign_seq(mat(cpl(1, 2, 3)), NA, start, size, increasing), mat(cpl(1, NA, NA)))
698+
expect_identical(vec_assign_seq(mat(cpl2(1, 2, 3)), NA, start, size, increasing), mat(cpl2(1, NA, NA)))
699699
expect_identical(vec_assign_seq(mat(chr("1", "2", "3")), NA, start, size, increasing), mat(chr("1", NA, NA)))
700700
expect_identical(vec_assign_seq(mat(raw2(1, 2, 3)), raw2(1), start, size, increasing), mat(raw2(1, 1, 1)))
701701
expect_identical(vec_assign_seq(mat(list(1, 2, 3)), NA, start, size, increasing), mat(list(1, NULL, NULL)))

tests/testthat/test-slice.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ test_that("can subset with missing indices", {
3838
expect_identical(vec_slice(lgl(1, 0, 1), i), lgl(0, NA))
3939
expect_identical(vec_slice(int(1, 2, 3), i), int(2, NA))
4040
expect_identical(vec_slice(dbl(1, 2, 3), i), dbl(2, NA))
41-
expect_identical(vec_slice(cpl(1, 2, 3), i), cpl(2, NA))
41+
expect_identical(vec_slice(cpl2(1, 2, 3), i), cpl2(2, NA))
4242
expect_identical(vec_slice(chr("1", "2", "3"), i), c("2", NA))
4343
expect_identical(vec_slice(raw2(1, 2, 3), i), raw2(2, 0))
4444
expect_identical(vec_slice(list(1, 2, 3), i), list(2, NULL))

tests/testthat/test-type-bare.R

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -243,13 +243,27 @@ test_that("safe casts to complex works", {
243243
})
244244

245245
test_that("NA casts work as expected", {
246+
# This goes through a special path for <unspecified>
246247
expect_equal(vec_cast(lgl(NA), cpl()), NA_complex_)
247-
expect_equal(vec_cast(int(NA), cpl()), NA_complex_)
248248

249249
# TODO: Use our own cast routines here?
250-
# `as.complex(NA_real_)` and `Rf_CoerceVector(NA_real_)` coerce to
251-
# `complex(real = NA_real_, imaginary = 0)` for some reason, but this may
252-
# change in the future https://stat.ethz.ch/pipermail/r-devel/2023-April/082545.html
250+
# It isn't great that this logical `NA` cast returns a different `NA`
251+
# than the one above with just `lgl(NA)` (which is seen as unspecified). i.e.
252+
# check the `Im()` slot between the two in R >=4.4.0. We can fix this with our
253+
# own cast routines rather than using `vec_coerce_bare()`.
254+
expect_type(vec_cast(lgl(NA, TRUE), cpl()), "complex")
255+
expect_identical(is.na(vec_cast(lgl(NA, TRUE), cpl())), c(TRUE, FALSE))
256+
257+
# TODO: Use our own cast routines here?
258+
# `as.complex(NA/NA_real_/NA_integer_)` and `Rf_CoerceVector(NA/NA_real_/NA_integer_)`
259+
# have gone back and forth about what they return in the `Im()` slot. In some
260+
# R versions they return `0` and in others they return `NA_real_`.
261+
# https://stat.ethz.ch/pipermail/r-devel/2023-April/082545.html
262+
# https://stat.ethz.ch/pipermail/r-devel/2023-September/082864.html
263+
# expect_equal(vec_cast(int(NA), cpl()), NA_complex_)
264+
expect_type(vec_cast(int(NA), cpl()), "complex")
265+
expect_identical(is.na(vec_cast(int(NA), cpl())), TRUE)
266+
253267
# expect_equal(vec_cast(dbl(NA), cpl()), NA_complex_)
254268
expect_type(vec_cast(dbl(NA), cpl()), "complex")
255269
expect_identical(is.na(vec_cast(dbl(NA), cpl())), TRUE)
@@ -263,13 +277,21 @@ test_that("Shaped NA casts work as expected", {
263277
exp_mat <- mat(NA_complex_)
264278
to_mat <- matrix(cpl())
265279

266-
expect_equal(vec_cast(mat(lgl(NA)), to_mat), exp_mat)
267-
expect_equal(vec_cast(mat(int(NA)), to_mat), exp_mat)
268-
269280
# TODO: Use our own cast routines here?
270-
# `as.complex(NA_real_)` and `Rf_CoerceVector(NA_real_)` coerce to
271-
# `complex(real = NA_real_, imaginary = 0)` for some reason, but this may
272-
# change in the future https://stat.ethz.ch/pipermail/r-devel/2023-April/082545.html
281+
# `as.complex(NA/NA_real_/NA_integer_)` and `Rf_CoerceVector(NA/NA_real_/NA_integer_)`
282+
# have gone back and forth about what they return in the `Im()` slot. In some
283+
# R versions they return `0` and in others they return `NA_real_`.
284+
# https://stat.ethz.ch/pipermail/r-devel/2023-April/082545.html
285+
# https://stat.ethz.ch/pipermail/r-devel/2023-September/082864.html
286+
287+
# expect_equal(vec_cast(mat(lgl(NA)), to_mat), exp_mat)
288+
expect_type(vec_cast(mat(lgl(NA)), to_mat), "complex")
289+
expect_identical(is.na(vec_cast(mat(lgl(NA)), to_mat)), matrix(TRUE))
290+
291+
# expect_equal(vec_cast(mat(int(NA)), to_mat), exp_mat)
292+
expect_type(vec_cast(mat(int(NA)), to_mat), "complex")
293+
expect_identical(is.na(vec_cast(mat(int(NA)), to_mat)), matrix(TRUE))
294+
273295
# expect_equal(vec_cast(mat(dbl(NA)), to_mat), exp_mat)
274296
expect_type(vec_cast(mat(dbl(NA)), to_mat), "complex")
275297
expect_identical(is.na(vec_cast(mat(dbl(NA)), to_mat)), matrix(TRUE))

0 commit comments

Comments
 (0)