tests/Test-SS.R

require(ncar)
require(NonCompart)

# Regression tests for the steady-state report path: txtNCA/pdfNCA/rtfNCA with SS=TRUE and
# tau (NonCompart >= 0.8.4). The report must (1) carry the new AUCTAU and CAVG lines with
# their CDISC synonyms, (2) state the dosing interval it used, (3) print exactly what
# tblNCA computes for the same profile, and (4) refuse SS=TRUE without tau before any
# file is opened.

Fail = function(msg) stop(paste("Test Failed!", msg))
Value = function(txt, code) {
  ln = grep(paste0("^", code, " "), txt, value=TRUE)
  if (length(ln) != 1) Fail(paste("could not read", code, "from the report"))
  as.numeric(tail(strsplit(trimws(ln), " +")[[1]], 2)[1])
}

x = c(0, 1, 2, 4, 8, 12); y = c(2, 6, 10, 8, 4, 2)   # q12h at steady state, AUCtau = 66
d = data.frame(ID="A", Time=x, conc=y)

## 1. txtNCA: lines, synonyms and values --------------------------------------------------
txt = txtNCA(x, y, dose=100, concUnit="mg/L", SS=TRUE, tau=12)
if (!any(grepl("^Steady State: Yes \\(dosing interval tau = 12 h", txt))) Fail("the report must state the dosing interval")
if (!any(grepl("^AUCTAU +AUC Over Dosing Interval ", txt))) Fail("AUCTAU line with its CDISC synonym")
if (!any(grepl("^CAVG +Average Concentration ", txt))) Fail("CAVG line with its CDISC synonym")
if (Value(txt, "AUCTAU") != 66) Fail("AUCTAU printed value")
if (Value(txt, "CAVG") != 5.5) Fail("CAVG printed value")
if (Value(txt, "CLFO") != round(100/66, 4)) Fail("CLFO must be dose/AUCTAU")
if (any(grepl("^CLFP |^VZFP |^MRTEVIFO |^MRTEVIFP ", txt))) Fail("NA parameters must not be printed")

## 2. Report equals table ------------------------------------------------------------------
tab = tblNCA(d, key="ID", dose=100, concUnit="mg/L", SS=TRUE, tau=12)
for (code in c("AUCTAU", "CAVG", "CLFO", "VZFO", "AUCLST", "AUCIFO", "LAMZ", "LAMZHL", "CMAX")) {
  if (Value(txt, code) != Round(tab[[code]][1], 4)) Fail(paste(code, "in the report differs from tblNCA"))
}

## 3. tau unit conversion reaches the report -----------------------------------------------
txt2 = txtNCA(x, y, dose=100, concUnit="mg/L", SS=TRUE, tau=0.5, unitTau="d")
if (!any(grepl("tau = 0.5 d", txt2))) Fail("the report must print tau as given, with its unit")
if (Value(txt2, "AUCTAU") != 66 || Value(txt2, "CLFO") != round(100/66, 4)) Fail("tau in days must give the same values")

## 4. Without SS nothing changes -----------------------------------------------------------
txt0 = txtNCA(x, y, dose=100, concUnit="mg/L")
if (any(grepl("Steady State|^AUCTAU |^CAVG ", txt0))) Fail("SS=FALSE must not add steady-state lines")
w = tryCatch(txtNCA(x, y, dose=100, concUnit="mg/L", tau=12), warning=function(w) conditionMessage(w))
if (!is.character(w) || !grepl("ignored", w)) Fail("tau with SS=FALSE must warn")

## 5. SS=TRUE without tau stops, and before any file is written ----------------------------
e = tryCatch(txtNCA(x, y, dose=100, SS=TRUE), error=function(e) conditionMessage(e))
if (!is.character(e) || !grepl("requires tau", e)) Fail("txtNCA SS=TRUE without tau must stop")
f1 = file.path(tempdir(), "ncar-ss-missing-tau.pdf")
e = tryCatch(pdfNCA(f1, d, key="ID", dose=100, concUnit="mg/L", SS=TRUE), error=function(e) conditionMessage(e))
if (!is.character(e) || !grepl("requires tau", e)) Fail("pdfNCA SS=TRUE without tau must stop")
if (file.exists(f1)) Fail("pdfNCA must stop before opening the file")
e = tryCatch(pdfNCA(f1, d, key="ID", dose=100, concUnit="mg/L", SS=TRUE, tau=c(12, 24)), error=function(e) conditionMessage(e))
if (!is.character(e) || !grepl("Count of tau", e)) Fail("pdfNCA tau of the wrong length must stop")

## 6. pdfNCA and rtfNCA accept tau, per NCA, and produce files -----------------------------
d2 = rbind(d, data.frame(ID="B", Time=x, conc=2*y))
f2 = file.path(tempdir(), "ncar-ss.pdf")
pdfNCA(f2, d2, key="ID", dose=100, concUnit="mg/L", SS=TRUE, tau=c(12, 10))
if (!file.exists(f2) || file.info(f2)$size < 1000) Fail("pdfNCA with SS=TRUE did not write a pdf")
f3 = file.path(tempdir(), "ncar-ss.rtf")
rtfNCA(f3, d2, key="ID", dose=100, concUnit="mg/L", SS=TRUE, tau=0.5, unitTau="d")
if (!file.exists(f3)) Fail("rtfNCA with SS=TRUE did not write an rtf")
rtf = readLines(f3, warn=FALSE)
if (!any(grepl("tau = 0.5 d", rtf))) Fail("rtfNCA must carry the dosing interval into the report")
if (!any(grepl("AUC Over Dosing Interval", rtf))) Fail("rtfNCA must carry the AUCTAU line")
unlink(c(f2, f3))

cat("Test-SS.R (ncar): all steady-state report checks passed\n")

Try the ncar package in your browser

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

ncar documentation built on Sept. 25, 2026, 5:07 p.m.