diff --git a/DESCRIPTION b/DESCRIPTION index 27aaa95..c7a3f78 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: individual Title: Framework for Specifying and Simulating Individual Based Models -Version: 0.1.14 +Version: 0.1.15 Authors@R: c( person( given = "Giovanni", 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/NEWS.md b/NEWS.md index f01b153..e4d72cf 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,7 @@ +# individual 0.1.15 + + * Added an `all.equal` implementation for bitsets. + # individual 0.1.14 * Added a flag to the Event constructor to tweak the restore semantics. diff --git a/R/bitset.R b/R/bitset.R index 556c560..d0f9351 100644 --- a/R/bitset.R +++ b/R/bitset.R @@ -227,6 +227,21 @@ Bitset <- list( } ) +#' @export +#' @method all.equal Bitset +all.equal.Bitset <- function(target, current, ...) { + if (!inherits(current, "Bitset")) { + return("'current' is not a Bitset") + } + + 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..8b2fb55 100644 --- a/tests/testthat/test-bitset.R +++ b/tests/testthat/test-bitset.R @@ -358,3 +358,44 @@ 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("bitsets with different capacities are not equal", { + a <- Bitset$new(10) + b <- Bitset$new(11) + expect_match(all.equal(a, b), "Bitset capacity differs") +}) + +test_that("bitset is not equal to other types", { + a <- Bitset$new(10) + a$insert(c(1,4,5)) + expect_equal(all.equal(a, c(1,4,5)), "'current' is not a Bitset") +})