Skip to content

Commit

Permalink
Test fixes for dev waldo (#1829)
Browse files Browse the repository at this point in the history
* Test fixes for dev waldo

Which now actually compares the imaginary part of complex numbers, and flags NaN and NA_real_ as different

* Just test type and missingness in weird `as.complex(NA_real_)` case

In case it changes in base R in the future

* NEWS bullet

* Link to r-devel post

---------

Co-authored-by: DavisVaughan <davis@rstudio.com>
  • Loading branch information
hadley and DavisVaughan authored Apr 14, 2023
1 parent 77f5622 commit d27033e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# vctrs (development version)

* Fixed tests to maintain compatibility with the next version of waldo (#1829).

# vctrs 0.6.1

* Fixed a test related to `c.sfc()` changes in sf 1.0-10 (#1817).
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-set.R
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ test_that("works with complex missing values", {
imaginary = c(NA_real_, NaN, NA_real_, NaN)
)
expect_identical(vec_set_symmetric_difference(na, na), complex())
expect_identical(vec_set_symmetric_difference(na[-2], na[-4]), na[c(2, 4)])
expect_identical(vec_set_symmetric_difference(na[-2], na[-4]), na[c(4, 2)])
})

test_that("works correctly with unspecified logical vectors", {
Expand Down
27 changes: 19 additions & 8 deletions tests/testthat/test-type-bare.R
Original file line number Diff line number Diff line change
Expand Up @@ -243,25 +243,36 @@ test_that("safe casts to complex works", {
})

test_that("NA casts work as expected", {
exp <- cpl(NA)
to <- cpl()
expect_equal(vec_cast(lgl(NA), cpl()), NA_complex_)
expect_equal(vec_cast(int(NA), cpl()), NA_complex_)

expect_equal(vec_cast(lgl(NA), to), exp)
expect_equal(vec_cast(int(NA), to), exp)
expect_equal(vec_cast(dbl(NA), to), exp)
# TODO: Use our own cast routines here?
# `as.complex(NA_real_)` and `Rf_CoerceVector(NA_real_)` coerce to
# `complex(real = NA_real_, imaginary = 0)` for some reason, but this may
# change in the future https://stat.ethz.ch/pipermail/r-devel/2023-April/082545.html
# expect_equal(vec_cast(dbl(NA), cpl()), NA_complex_)
expect_type(vec_cast(dbl(NA), cpl()), "complex")
expect_identical(is.na(vec_cast(dbl(NA), cpl())), TRUE)

# This used to be allowed
expect_error(vec_cast(list(NA), to), class = "vctrs_error_incompatible_type")
expect_error(vec_cast(list(NA), cpl()), class = "vctrs_error_incompatible_type")
})

test_that("Shaped NA casts work as expected", {
mat <- matrix
exp_mat <- mat(cpl(NA))
exp_mat <- mat(NA_complex_)
to_mat <- matrix(cpl())

expect_equal(vec_cast(mat(lgl(NA)), to_mat), exp_mat)
expect_equal(vec_cast(mat(int(NA)), to_mat), exp_mat)
expect_equal(vec_cast(mat(dbl(NA)), to_mat), exp_mat)

# TODO: Use our own cast routines here?
# `as.complex(NA_real_)` and `Rf_CoerceVector(NA_real_)` coerce to
# `complex(real = NA_real_, imaginary = 0)` for some reason, but this may
# change in the future https://stat.ethz.ch/pipermail/r-devel/2023-April/082545.html
# expect_equal(vec_cast(mat(dbl(NA)), to_mat), exp_mat)
expect_type(vec_cast(mat(dbl(NA)), to_mat), "complex")
expect_identical(is.na(vec_cast(mat(dbl(NA)), to_mat)), matrix(TRUE))

# This used to be allowed
expect_error(vec_cast(mat(list(NA)), to_mat), class = "vctrs_error_incompatible_type")
Expand Down

0 comments on commit d27033e

Please sign in to comment.