Skip to content

Commit

Permalink
Merge pull request #193 from mrc-ide/release/0.1.15
Browse files Browse the repository at this point in the history
Release 0.1.15
  • Loading branch information
plietar authored Mar 20, 2024
2 parents a3ce0e0 + 7e48608 commit 4bef32e
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 1 deletion.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
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
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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.
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 @@ 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
Expand Down
41 changes: 41 additions & 0 deletions tests/testthat/test-bitset.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
})

0 comments on commit 4bef32e

Please sign in to comment.