tests/testthat/test_batchReduce.R

test_that("batchReduce", {
  reg = makeTestRegistry()
  xs = 1:20
  ids = batchReduce(function(aggr, x) aggr+x, xs, init = 0, chunks = chunk(seq_along(xs), n.chunks = 10), reg = reg)
  expect_data_table(ids, nrow = 10, key = "job.id")

  submitAndWait(ids = ids, reg = reg)
  y = reduceResults(fun = function(aggr, job, res) aggr+res, init = 0, reg = reg)
  expect_equal(y, sum(1:20))
})


test_that("batchReduce w/ more.args", {
  reg = makeTestRegistry()
  xs = 1:20
  chunks = sort(chunk(seq_along(xs), n.chunks = 10))
  ids = batchReduce(function(aggr, x, y) aggr+x+y, 1:20, init = 100, chunks = chunks, more.args = list(y=1), reg = reg)
  expect_data_table(ids, nrow = 10, key = "job.id")

  submitAndWait(reg = reg)

  expect_equal(as.integer(reduceResultsList(reg = reg)), viapply(split(xs, chunks), function(x) 100L + length(x) + sum(x), use.names = FALSE))
  y = reduceResults(fun=function(aggr, job, res) aggr+res, init = 0, reg = reg)
  expect_equal(y, sum(1:20) + 20 + uniqueN(chunks) * 100)
})

Try the batchtools package in your browser

Any scripts or data that you put into this service are public.

batchtools documentation built on April 20, 2023, 5:09 p.m.