Error in `dbc2dbf(file, out)`: Decompression failed: Distance is too far back (invalid format)

test_that("End-to-end workflow: CSV -> DBF -> DBC -> DataFrame", {
# Using a mix of types: numeric, character, factor (will become char in DBF usually)
Name = c("Alice", "Bob", "Charlie", "David", "Eve", "Frank", "Grace", "Heidi", "Ivan", "Judy"),
Active = c(TRUE, FALSE, TRUE, TRUE, FALSE, TRUE, FALSE, TRUE, FALSE, TRUE),
write.csv(original_data, csv_file, row.names = FALSE)
# 3. Read CSV back (to ensure we work with what's on disk)
csv_data <- read.csv(csv_file, stringsAsFactors = FALSE)
# We need foreign package (suggested/imported by read.dbc)
# But write.dbf is not exported by read.dbc, so we load foreign namespace or check availability.
if (!requireNamespace("foreign", quietly = TRUE)) {
skip("foreign package not available for writing DBF")
# Note: DBF format has limitations (e.g., column names length, character width, no boolean type usually maps to Logical/Character)
# foreign::write.dbf might convert logicals to something else?
# ID (int) -> should remain int or numeric
# Name (char) -> should remain char or factor. read.dbc might return factors by default?
# read.dbc documentation says it calls foreign::read.dbf.
# Active (logical) -> DBF usually stores as 'T'/'F' or 1/0.
# foreign::read.dbf reads logicals correctly if they were stored as logicals.

"End-to-end workflow: CSV -> DBF -> DBC -> DataFrame"
"foreign package not available for writing DBF"

End-to-end workflow: CSV -> DBF -> DBC -> DataFrame

Error in `dbc2dbf(temp_dbc_new, temp_dbf_new)`: Decompression failed: Distance is too far back (invalid format)

test_that("dbf2dbc compresses simple data correctly", {
# Create a simple DBF-like file (header + data)
# Note: 1-based index in R. 8-9 is [9], [10]?
# Actually 32 is 0x20. So header[9] = 0x20.
test_that("dbf2dbc compresses sids.dbc correctly", {
input_dbc <- system.file("files/sids.dbc", package = "read.dbc")
temp_dbf_orig <- tempfile(fileext = ".dbf")
temp_dbc_new <- tempfile(fileext = ".dbc")
temp_dbf_new <- tempfile(fileext = ".dbf")
# 1. Decompress original sids.dbc -> temp_dbf_orig
# 2. Compress temp_dbf_orig -> temp_dbc_new
# 3. Decompress temp_dbc_new -> temp_dbf_new
# We can use tools::md5sum or read them as binary
bytes_orig <- readBin(con_orig, "raw", n = info_orig$size)
bytes_new <- readBin(con_new, "raw", n = info_new$size)
test_that("dbf2dbc compresses storm.dbc correctly", {
input_dbc <- system.file("files/storm.dbc", package = "read.dbc")
# 1. Decompress original storm.dbc -> temp_dbf_orig

"dbf2dbc compresses simple data correctly"