tests/testthat/helper-lifecycle.R

# Steps for zu_test_sequence(), the scripted-handle harness behind
# test-lifecycle.R. Each returns list(op, a, b, c); see zukomp_test_sequence()
# in src/zukomp_test.c for the encoding.

flush_code <- function(flush) {
  switch(flush, run = 0L, flush = 1L, finish = 2L,
         stop("unknown flush: ", flush))
}

# Offer the next `n_in` input bytes and `room` bytes of output space.
step_process <- function(n_in, room = 1e6, flush = "run") {
  list(1L, as.integer(n_in), as.integer(room), flush_code(flush))
}
step_finish <- function(n_in = 0, room = 1e6) {
  step_process(n_in, room, "finish")
}
# Reset under the creation options, and move the input cursor to `cursor`.
step_reset <- function(cursor = 0) list(2L, as.integer(cursor), 0L, 0L)
# The next process() that reaches the codec returns `status` instead.
step_inject <- function(status) {
  list(3L, zu_status_codes()[[status]], 0L, 0L)
}
step_free <- function() list(4L, 0L, 0L, 0L)
step_nullbuf <- function(flush = "run") list(5L, 0L, 0L, flush_code(flush))

# The codecs this build implements, in komp_codecs() order.
lifecycle_codecs <- function() {
  tab <- komp_codecs()
  tab$id[tab$available]
}

lifecycle_can_flush <- function(codec) {
  tab <- komp_codecs()
  isTRUE(tab$can_flush[tab$id == codec])
}

# Asserts the post-error contract (design 9) on steps `from`..`to` of a
# sequence result: every call returns `status`, and none of them consumes
# input or delivers output.
expect_sticky <- function(res, from, status, to = length(res$status),
                          info = NULL) {
  idx <- seq.int(from, to)
  idx <- idx[!is.na(res$status[idx])]
  testthat::expect_identical(res$status[idx], rep(status, length(idx)),
                             info = info)
  testthat::expect_identical(res$consumed[idx], rep(0, length(idx)),
                             info = info)
  testthat::expect_identical(res$produced[idx], rep(0, length(idx)),
                             info = info)
}

# `bytes` without its first `n`. Not bytes[-seq_len(n)], which is empty
# when n is 0.
drop_first <- function(bytes, n) {
  bytes[seq_len(length(bytes) - n) + n]
}

# Payloads for test-lifecycle.R. Helpers live here rather than at the top
# of the test file, which a shuffled run would reorder past their callers.
lc_payload <- function() new_payload("ascii", 3000L)
# A second, small, incompressible message for after a reset: it fits every
# limit the failing cases use, ratio 2 included.
lc_good <- function() new_payload("lcg", 200L)

# Fails a stream with `bad`, checks the failure is sticky across every kind
# of later call, then resets and decodes `good` through the same handle.
expect_decoder_fails_then_recovers <- function(codec, bad, status, ...,
                                               info = codec) {
  good_x <- lc_good()
  good <- komp_compress(good_x, codec)
  res <- zu_test_sequence(
    c(bad, good), codec, "decode",
    list(
      step_finish(length(bad)),        # 1: fails
      step_process(5),                 # 2: more input
      step_process(0, room = 0),       # 3: nothing at all
      step_finish(),                   # 4: finish
      step_process(5, flush = "flush"),# 5: a flush, even where unsupported
      step_nullbuf(),                  # 6: no buffer
      step_reset(length(bad)),         # 7: reset
      step_finish(length(good))        # 8: a valid message
    ),
    ...
  )
  if (is.null(status)) {
    expect_false(res$status[[1]] %in% c("ZU_OK", "ZU_NEED_INPUT",
                                        "ZU_NEED_OUTPUT", "ZU_STREAM_END"),
                 info = info)
    status <- res$status[[1]]
  } else {
    expect_identical(res$status[[1]], status, info = info)
  }
  expect_sticky(res, 2L, status, to = 6L, info = info)
  expect_identical(res$status[7:8], c("ZU_OK", "ZU_STREAM_END"), info = info)
  # The reset message is whole, and nothing after the failure leaked in.
  n_before <- sum(res$produced[1:6])
  expect_identical(drop_first(res$bytes, n_before), good_x, info = info)
  invisible(res)
}

Try the zukomp package in your browser

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

zukomp documentation built on Nov. 24, 2026, 1:06 a.m.