diff --git a/NAMESPACE b/NAMESPACE index 507d28c..f0d4aa4 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,5 +1,6 @@ # Generated by roxygen2: do not edit by hand +S3method(all.equal,Bitset) export(Bitset) export(CategoricalVariable) export(DoubleVariable) diff --git a/R/bitset.R b/R/bitset.R index 556c560..fe11a03 100644 --- a/R/bitset.R +++ b/R/bitset.R @@ -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 diff --git a/tests/testthat/test-bitset.R b/tests/testthat/test-bitset.R index 4199bc2..53ed13c 100644 --- a/tests/testthat/test-bitset.R +++ b/tests/testthat/test-bitset.R @@ -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))) +})