tests/testthat/test-delarr-observe.R

test_that("explain infers chunk plans for N-d pipelines and reductions", {
  arr <- array(seq_len(24), dim = c(2, 3, 4))

  info_nd <- explain(delarr(arr) |> d_map(~ .x + 1), target_bytes = 100)
  expect_s3_class(info_nd, "delarr_explain")
  expect_equal(info_nd$input_dim, c(2, 3, 4))
  expect_equal(info_nd$output_dim, c(2, 3, 4))
  expect_equal(info_nd$chunk_margin, 3L)
  expect_equal(info_nd$chunk_size, 2L)
  expect_equal(info_nd$chunk_count, 2L)

  info_reduce <- explain(d_reduce(delarr(arr), sum, axis = 3L), target_bytes = 100)
  expect_true(info_reduce$has_reduce)
  expect_equal(info_reduce$reduce_dim, NULL)
  expect_equal(info_reduce$output_dim, c(2, 3, 1))
  expect_equal(info_reduce$chunk_margin, 3L)
  expect_equal(info_reduce$chunk_size, 2L)
  expect_equal(info_reduce$chunk_count, 2L)
})

test_that("explain falls back to a single full pass for generic N-d reductions", {
  arr <- array(seq_len(24), dim = c(2, 3, 4))

  info <- explain(
    d_reduce(delarr(arr), function(x, na.rm = FALSE) sum(x, na.rm = na.rm), axis = 3L),
    chunk_margin = 3L,
    target_bytes = 32
  )

  expect_true(info$has_reduce)
  expect_null(info$chunk_margin)
  expect_true(is.na(info$chunk_size))
  expect_equal(info$chunk_count, 1L)
})

test_that("print methods emit useful summaries", {
  arr <- array(seq_len(24), dim = c(2, 3, 4))

  info <- explain(d_reduce(delarr(arr), sum, axis = 3L), target_bytes = 100)
  info_output <- paste(capture.output(print(info)), collapse = "\n")
  expect_match(info_output, "<delarr_explain> in: 2x3x4  out: 2x3x1", fixed = TRUE)
  expect_match(info_output, "ops: 0  chunks: 2 (3=2)", fixed = TRUE)
  expect_match(info_output, "reduce:", fixed = TRUE)

  prof <- structure(
    list(
      reps = 2L,
      min_sec = 0.1,
      median_sec = 0.2,
      max_sec = 0.3,
      output_size_bytes = c(352, 352),
      output_class = "array"
    ),
    class = "delarr_profile"
  )
  prof_output <- paste(capture.output(print(prof)), collapse = "\n")
  expect_match(prof_output, "<delarr_profile> reps: 2", fixed = TRUE)
  expect_match(prof_output, "output size (bytes): 352", fixed = TRUE)
})

test_that("profile_collect validates reps and reports output size", {
  arr <- array(seq_len(24), dim = c(2, 3, 4))

  expect_error(profile_collect(delarr(arr), reps = 0L), "reps must be >= 1")

  prof <- profile_collect(delarr(arr), reps = 2L)
  expect_s3_class(prof, "delarr_profile")
  expect_equal(length(prof$output_size_bytes), 2L)
  expect_true(all(!is.na(prof$output_size_bytes)))
  expect_equal(prof$output_class, "array")
})

test_that("explain clears chunk margin when axis is blocked by centering", {
  arr <- array(as.double(1:24), dim = c(2, 3, 4))
  pipeline <- delarr(arr) |> d_center(axis = 2L) |> d_reduce(sum, axis = 2L)
  info <- explain(pipeline, chunk_margin = 1L, target_bytes = 100)
  expect_null(info$chunk_margin)
  expect_true(is.na(info$chunk_size))
})

test_that("print.delarr_explain shows operation plan when present", {
  mat <- matrix(1:12, 3, 4)
  info <- explain(delarr(mat) |> d_map(~ .x + 1) |> d_center("cols"))
  output <- paste(capture.output(print(info)), collapse = "\n")
  expect_match(output, "plan:", fixed = TRUE)
  expect_match(output, "map", fixed = TRUE)
  expect_match(output, "center", fixed = TRUE)
})

test_that("print.delarr_explain labels full-pass chunking", {
  arr <- array(seq_len(24), dim = c(2, 3, 4))
  info <- explain(
    d_reduce(
      delarr(arr),
      function(x, na.rm = FALSE) sum(x, na.rm = na.rm),
      axis = 3L
    ),
    chunk_margin = 3L,
    target_bytes = 32
  )
  output <- paste(capture.output(print(info)), collapse = "\n")
  expect_match(output, "chunks: 1 full", fixed = TRUE)
})

Try the delarr package in your browser

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

delarr documentation built on July 1, 2026, 1:06 a.m.