tests/testthat/test-js-wrapper.R

context("wrapper")


test_that("force a vector of strings", {
  skip_if_no_js()
  gen <- odin(c("deriv(y) <- 0.5", "initial(y) <- 1"),
              target = "js")
  expect_equal(gen$public_methods$engine(), "js")
  mod <- gen$new()
  y <- mod$run(0:10)[, "y"]
  expect_equal(y, seq(1, by = 0.5, length.out = 11))
})


test_that("force a symbol of code", {
  skip_if_no_js()
  code <- quote({
    deriv(y) <- 0.5
    initial(y) <- 1
  })
  gen <- odin(code, target = "js")
  mod <- gen$new()
  y <- mod$run(0:10)[, "y"]
  expect_equal(y, seq(1, by = 0.5, length.out = 11))
})


test_that("allow initial conditions", {
  skip_if_no_js()
  code <- quote({
    deriv(y) <- 0.5
    initial(y) <- 1
  })
  gen <- odin(code, target = "js")
  mod <- gen$new()
  y <- mod$run(0:10, 2)[, "y"]
  expect_equal(y, seq(2, by = 0.5, length.out = 11))
})


test_that("return statistics", {
  skip_if_no_js()
  code <- quote({
    deriv(y) <- sin(y)
    initial(y) <- 1
  })
  gen <- odin(code, target = "js")
  mod <- gen$new()

  expect_null(attr(mod$run(0:10), "statistics"))
  res <- mod$run(0:10, return_statistics = TRUE)
  statistics <- attr(res, "statistics")
  expect_is(statistics, "integer")
  expect_equal(names(statistics),
               c("n_eval", "n_step", "n_accept", "n_reject"))
  expect_true(all(statistics) >= 0)
})
richfitz/odin documentation built on Feb. 23, 2024, 1:11 p.m.