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

Release 0.1.15 #193

Merged
merged 2 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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")
})
Loading