Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an all.equal implementation for Bitset. #192

Merged
merged 3 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
15 changes: 15 additions & 0 deletions R/bitset.R
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,21 @@
}
)

#' @export
#' @method all.equal Bitset
all.equal.Bitset <- function(target, current, ...) {
plietar marked this conversation as resolved.
Show resolved Hide resolved
if (!inherits(current, "Bitset")) {
return("'current' is not a Bitset")

Check warning on line 234 in R/bitset.R

View check run for this annotation

Codecov / codecov/patch

R/bitset.R#L234

Added line #L234 was not covered by tests
}

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)))
})
Loading