Skip to content

Commit

Permalink
vec_equal_tol() checks equality of infinite values
Browse files Browse the repository at this point in the history
Fixes #789
  • Loading branch information
hadley committed Apr 1, 2019
1 parent 1e677b1 commit 992ddd8
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# testthat (development version)

* `expect_equal()` compares infinite values correctly (#789).

* `expect_output()` gains `width` argument, allowing you to control the
output width. This does not inherit from `getOption("width")`, ensuring
that tests return the same results regardless of environment (#805).
Expand Down
4 changes: 3 additions & 1 deletion R/compare.R
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,7 @@ vector_equal <- function(x, y) {
}

vector_equal_tol <- function(x, y, tolerance = .Machine$double.eps ^ 0.5) {
(is.na(x) & is.na(y)) | (!is.na(x) & !is.na(y) & abs(x - y) < tolerance)
(is.na(x) & is.na(y)) |
(!is.na(x) & !is.na(y)) & (x == y | abs(x - y) < tolerance)

}
12 changes: 12 additions & 0 deletions tests/testthat/test-compare.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,15 @@ test_that("list comparison truncates to max_diffs", {
lines2 <- strsplit(compare(x, y, max_diffs = 99)$message, "\n")[[1]]
expect_length(lines2, 100)
})

test_that("vector_equal_tol handles infinity", {
expect_true(vector_equal_tol(Inf, Inf))
expect_true(vector_equal_tol(-Inf, -Inf))
expect_false(vector_equal_tol(Inf, -Inf))
expect_false(vector_equal_tol(Inf, 0))
})

test_that("vector_equal_tol handles na", {
expect_true(vector_equal_tol(NA, NA))
expect_false(vector_equal_tol(NA, 0))
})

0 comments on commit 992ddd8

Please sign in to comment.