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

Use correct NA value for user handling #101

Merged
merged 3 commits into from Feb 23, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
@@ -1,6 +1,6 @@
Package: odin.dust
Title: Compile Odin to Dust
Version: 0.2.15
Version: 0.2.16
Authors@R: c(person("Rich", "FitzJohn", role = c("aut", "cre"),
email = "rich.fitzjohn@gmail.com"),
person("John", "Lees", role = "aut"),
Expand Down
5 changes: 3 additions & 2 deletions R/generate_dust_equation.R
Expand Up @@ -92,8 +92,9 @@ generate_dust_equation_user <- function(eq, data_info, dat, rewrite, gpu) {
lhs <- rewrite(eq$lhs)
storage_type <- dust_type(data_info$storage_type)
is_integer <- if (storage_type == "int") "true" else "false"
min <- rewrite(eq$user$min %||% "NA_REAL")
max <- rewrite(eq$user$max %||% "NA_REAL")
na_value <- if (is_integer) "NA_INTEGER" else "NA_REAL"
min <- rewrite(eq$user$min %||% na_value)
max <- rewrite(eq$user$max %||% na_value)
previous <- lhs

if (eq$user$dim) {
Expand Down
11 changes: 11 additions & 0 deletions tests/testthat/helper-odin-dust.R
Expand Up @@ -24,3 +24,14 @@ user_wrapper <- function() {
sub_package_name <- function(text, name) {
gsub("{{name}}", name, text, fixed = TRUE)
}


## Quick fix for
## https://github.com/mrc-ide/odin.dust/issues/85
## Will think about this and implement as an API function later.
odin_dust_generate <- function(code) {
ir <- odin::odin_parse_(code)
options <- odin_dust_options()
dat <- generate_dust(ir, options)
odin_dust_code(dat)
}
21 changes: 21 additions & 0 deletions tests/testthat/test-user.R
Expand Up @@ -181,3 +181,24 @@ test_that("Correct if data exists and is provided", {
NA_real_, NA_real_),
list(v2, dim(a2)))
})


test_that("Generate correct types for user variables", {
## We were previously including NA_REAL as the default which is not
## the same as NA_INTEGER and causing weird errors on M1 (ARM) macs
## only.

code <- odin_dust_generate(quote({
var_a <- user(integer = TRUE)
var_b <- user(integer = FALSE)
initial(x) <- 1
update(x) <- x + var_a + var_b
}))

expect_match(
grep("user_get_scalar.+var_a", code, value = TRUE),
"NA_INTEGER, NA_INTEGER\\);$")
expect_match(
grep("user_get_scalar.+var_b", code, value = TRUE),
"NA_REAL, NA_REAL\\);$")
})