tests/testthat/helper-compare.R

# Compares two text snapshot files allowing small numeric differences.
#
# The files generated by write_cal/write_prj/write_nax contain thousands of
# model values. Even after rounding, tiny BLAS differences across platforms
# (e.g. OpenBLAS on Linux vs Accelerate on macOS) can flip the last rounded
# digit of a handful of values, so exact byte comparison is too strict.
# This comparison checks the file structure and all non-numeric content
# exactly, and numeric tokens within a tolerance.
compare_file_numeric <- function(old, new, tol = 1e-4) {
  read_tokens <- function(path) {
    txt <- readChar(path, file.info(path)$size, useBytes = TRUE)
    strsplit(txt, '["\t\r\n;, ]+')[[1]]
  }
  old_tokens <- read_tokens(old)
  new_tokens <- read_tokens(new)
  if (length(old_tokens) != length(new_tokens)) {
    return(FALSE)
  }
  old_num <- suppressWarnings(as.numeric(old_tokens))
  new_num <- suppressWarnings(as.numeric(new_tokens))
  is_num <- is.finite(old_num) & is.finite(new_num)
  if (!identical(old_tokens[!is_num], new_tokens[!is_num])) {
    return(FALSE)
  }
  diffs <- abs(old_num[is_num] - new_num[is_num])
  scales <- pmax(abs(old_num[is_num]), abs(new_num[is_num]))
  all(diffs <= tol | diffs <= tol * scales)
}

Try the proximetricsR package in your browser

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

proximetricsR documentation built on Sept. 4, 2026, 5:08 p.m.