From efac0a6073f320ab12c6e5e911c4c9a73d87b690 Mon Sep 17 00:00:00 2001 From: Kyle Baron Date: Fri, 2 Aug 2024 07:04:51 -0500 Subject: [PATCH] refactor / comment tests; explicit namespace references --- inst/base/mrgsolve-evtools.h | 16 +++----- inst/maintenance/unit-cpp/test-evtools.R | 49 ++++++++++++++---------- 2 files changed, 35 insertions(+), 30 deletions(-) diff --git a/inst/base/mrgsolve-evtools.h b/inst/base/mrgsolve-evtools.h index cc0ecf9c..98272135 100644 --- a/inst/base/mrgsolve-evtools.h +++ b/inst/base/mrgsolve-evtools.h @@ -13,7 +13,7 @@ mrgsolve::evdata bolus(const double amt, const int cmt) { } void bolus(databox& self, const double amt, const int cmt) { - mrgsolve::evdata ev = bolus(amt, cmt); + mrgsolve::evdata ev = evt::bolus(amt, cmt); self.mevector.push_back(ev); return; } @@ -29,7 +29,7 @@ mrgsolve::evdata infuse(const double amt, const int cmt, const double rate) { } void infuse(databox& self, const double amt, const int cmt, const double rate) { - mrgsolve::evdata ev = infuse(amt, cmt, rate); + mrgsolve::evdata ev = evt::infuse(amt, cmt, rate); self.mevector.push_back(ev); return; } @@ -44,7 +44,7 @@ mrgsolve::evdata replace(const double amt, const int cmt) { } void replace(databox& self, const double amt, const int cmt) { - mrgsolve::evdata ev = replace(amt, cmt); + mrgsolve::evdata ev = evt::replace(amt, cmt); self.mevector.push_back(ev); return; } @@ -58,29 +58,25 @@ mrgsolve::evdata reset() { mrgsolve::evdata reset(const double amt, const int cmt, const double rate = 0) { - mrgsolve::evdata ev(0, 4); + mrgsolve::evdata ev = evt::reset(); ev.cmt = cmt; ev.amt = amt; ev.rate = rate; - ev.now = true; - ev.check_unique = false; return ev; } void reset(databox& self) { - evt::ev ev = reset(); + mrgsolve::evdata ev = evt::reset(); self.mevector.push_back(ev); return; } void reset(databox& self, const double amt, const int cmt, const double rate = 0) { - evt::ev ev(0, 4); + mrgsolve::evdata ev = evt::reset(); ev.cmt = cmt; ev.amt = amt; ev.rate = rate; - ev.now = true; - ev.check_unique = false; self.mevector.push_back(ev); return; } diff --git a/inst/maintenance/unit-cpp/test-evtools.R b/inst/maintenance/unit-cpp/test-evtools.R index 28eb660b..43c84737 100644 --- a/inst/maintenance/unit-cpp/test-evtools.R +++ b/inst/maintenance/unit-cpp/test-evtools.R @@ -61,26 +61,26 @@ if(mode==8 && givedose) { evt::retime(rep, reptime); self.push(rep); } -if(mode==9 && givedose) { +if(mode==9 && givedose) { // reset evt::reset(self); -} -if(mode==10 && givedose) { +} +if(mode==10 && givedose) { // reset, non-self evt::ev res = evt::reset(); res.time = 15; self.push(res); } -if(mode==11 && givedose) { +if(mode==11 && givedose) { // reset with bolus evt::reset(self, 100, 3); } -if(mode==12 && givedose) { +if(mode==12 && givedose) { // reset with infusion evt::reset(self, 100, 1, 20); } -if(mode==13 && givedose) { +if(mode==13 && givedose) { // reset with bolus, non-self evt::ev res = evt::reset(100, 3); self.push(res); } -if(mode==14 && givedose) { - evt::ev res = evt::reset(100, 1, 20); +if(mode==14 && givedose) { // reset with infusion, non-self + evt::ev res = evt::reset(100, 1, 100.0/5.0); self.push(res); } ' @@ -167,13 +167,17 @@ test_that("evtools - reset", { out <- mrgsim(mod) expect_true(all(out$A==0)) expect_true(all(out$C==0)) - x <- filter(out, time %in% c(0,5)) + # B starts at 50 and then is reset at time==5 + x <- filter(out, time %in% c(0, 5)) expect_true(all(x$B==50)) + # Verify value of B just before reset x <- filter(out, time==4) expect_equal(x$B, 50*exp(-0.1*4), tolerance = 1e-2) mod <- param(mod, mode = 10) - out2 <- mrgsim(mod) - expect_equal(out@data, out2@data) + # Identical results if the self or non-self function is called + out1 <- mrgsim(mod, param = list(mode = 9)) + out2 <- mrgsim(mod, param = list(mode = 10)) + expect_equal(out1@data, out2@data) }) test_that("evtools - reset with bolus", { @@ -187,28 +191,33 @@ test_that("evtools - reset with bolus", { expect_equal(x$C, 0) x <- filter(out, time > 5) expect_true(all(x$C==100)) + + # Identical results if the self method called or non-self + out1 <- mrgsim(mod, param = list(mode = 11)) + out2 <- mrgsim(mod, param = list(mode = 12)) + + expect_identical(out1@data, out3@data) }) test_that("evtools - reset with infusion", { mod <- param(mod, dosetime = 5, B0 = 50, mode = 12) - out <- mrgsim(mod, rtol = 1e-12) - x <- filter(out, time %in% c(0,5)) + out <- mrgsim(mod, rtol = 1e-12, delta = 0.25) + # B should be 50 to start and the reset time + x <- filter(out, time %in% c(0, 5)) expect_true(all(x$B==50)) expect_true(all(x$A==0)) + # Check values of A and B just before reset x <- filter(out, time == 4.75) expect_equal(x$A, 0) expect_equal(x$B, 50*exp(-0.1*4.75), tolerance = 1e-2) - # Maximum A is at the end of the infusion + # Check the infusion; Amax should be at the end of the 5 hr infusion x <- filter(out, A==max(A)) expect_equal(x$time, 10) - # Identical results - out1 <- mrgsim(mod, param = list(mode = 11)) - out2 <- mrgsim(mod, param = list(mode = 12)) - out3 <- mrgsim(mod, param = list(mode = 13)) - out4 <- mrgsim(mod, param = list(mode = 14)) + # Identical results if the self method called or non-self + out1 <- mrgsim(mod, param = list(mode = 13)) + out2 <- mrgsim(mod, param = list(mode = 14)) expect_identical(out1@data, out3@data) - expect_identical(out2@data, out4@data) })