Skip to content

Commit

Permalink
Add an all.equal implementation for Bitset.
Browse files Browse the repository at this point in the history
Calling `all.equal` (or equivalently `expect_equal`) on bitsets has
never worked correctly. For some reason, externalptr always compare as
equal, thus, under the R6 implementation, any bitset with the same max
size was treated as equal.

With the recent switch to a named list and bitsets would sometimes be
considered different, even where their contents were the same. More
precisely, the arguments of `Bitset$new` now happen to be captured by
all of the methods' environments and were being included in the
comparison. A bitset created with `new(size=N)` would always be
different compared to a bitset created with `new(from=ptr)`.

malariasimulation has some tests that use `mockery::expect_args` to
compare bitset arguments, and these tests are now broken by the new
individual version.
  • Loading branch information
plietar committed Mar 11, 2024
1 parent a3ce0e0 commit ff99bf1
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Generated by roxygen2: do not edit by hand

S3method(all.equal,Bitset)
export(Bitset)
export(CategoricalVariable)
export(DoubleVariable)
Expand Down
11 changes: 11 additions & 0 deletions R/bitset.R
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,17 @@ Bitset <- list(
}
)

#' @export
#' @method all.equal Bitset
all.equal.Bitset <- function(target, current, ...) {
if (target$max_size != current$max_size) {
return(paste0(
"Bitset capacity differs (", target$max_size, " vs ",
current$max_size, ")"))
}
all.equal(target$to_vector(), current$to_vector(), ...)
}

#' @title Filter a bitset
#' @description This non-modifying function returns a new \code{\link{Bitset}}
#' object of the same maximum size as the original but which only contains
Expand Down
35 changes: 35 additions & 0 deletions tests/testthat/test-bitset.R
Original file line number Diff line number Diff line change
Expand Up @@ -358,3 +358,38 @@ test_that("bitset sampling has correctly distributed size", {
expect_gt(p, threshold)
}
})

test_that("bitset equality works", {
a <- Bitset$new(10)
b <- Bitset$new(10)
c <- Bitset$new(10)
d <- Bitset$new(10)
e <- Bitset$new(10)
f <- Bitset$new(10)

c$insert(c(1, 2, 3))
d$insert(c(1, 2, 3))
e$insert(c(4, 5, 6))
f$insert(c(4, 7))

expect_equal(a, b)
expect_equal(c, d)

expect_false(isTRUE(all.equal(a, c)))
expect_false(isTRUE(all.equal(a, e)))
expect_false(isTRUE(all.equal(a, f)))
expect_false(isTRUE(all.equal(c, e)))
expect_false(isTRUE(all.equal(c, f)))
expect_false(isTRUE(all.equal(e, f)))

expect_equal(a, a$copy())
expect_equal(c, c$copy())
expect_equal(e, e$copy())
expect_equal(f, f$copy())
})

test_that("bitset with different sizes are not equal", {
a <- Bitset$new(10)
b <- Bitset$new(11)
expect_false(isTRUE(all.equal(a, b)))
})

0 comments on commit ff99bf1

Please sign in to comment.