Skip to content

Commit fbd657a

Browse files
committed
N.env.parent arg
1 parent 1e7a3e7 commit fbd657a

File tree

8 files changed

+47
-22
lines changed

8 files changed

+47
-22
lines changed

NEWS

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
Changes in version 2024.10.3
22

33
- atime_pkg generates files such as _test_name_.png with underscores instead of special characters such as > which are not supported on some file systems, including github actions (https://github.com/tdhock/atime/issues/62).
4+
- atime and atime_versions gain N.env.parent arg, which is set by atime_pkg to environment created for evaluation of atime/tests.R code. N.env.parent is the parent env of N.env, the environment in which code is run for a given data size N. So now setup in tests can refer to variables defined in atime/tests.R.
45

56
Changes in version 2024.9.27
67

R/atime.R

+5-2
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,12 @@ default_N <- function(){
8686
as.integer(2^seq(1, 20))
8787
}
8888

89-
atime <- function(N=default_N(), setup, expr.list=NULL, times=10, seconds.limit=0.01, verbose=FALSE, result=FALSE, ...){
89+
atime <- function(N=default_N(), setup, expr.list=NULL, times=10, seconds.limit=0.01, verbose=FALSE, result=FALSE, N.env.parent=NULL, ...){
9090
kilobytes <- mem_alloc <- . <- sizes <- NULL
9191
## above for CRAN NOTE.
92+
if(is.null(N.env.parent)){
93+
N.env.parent <- parent.frame()
94+
}
9295
if(!is.numeric(N)){
9396
stop("N should be a numeric vector")
9497
}
@@ -116,7 +119,7 @@ atime <- function(N=default_N(), setup, expr.list=NULL, times=10, seconds.limit=
116119
for(N.value in N){
117120
not.done.yet <- names(done.vec)[!done.vec]
118121
if(length(not.done.yet)){
119-
N.env <- new.env(parent=parent.frame())
122+
N.env <- new.env(parent=N.env.parent)
120123
N.env$N <- N.value
121124
eval(mc.args$setup, N.env)
122125
m.list <- list(quote(bench::mark), iterations=times,check=FALSE)

R/test.R

+22-11
Original file line numberDiff line numberDiff line change
@@ -253,10 +253,11 @@ atime_pkg_test_info <- function(pkg.path=".", tests.dir=NULL){
253253
names(test.env$version.colors) %in% names(abbrev2name),
254254
abbrev2name[names(test.env$version.colors)],
255255
names(test.env$version.colors))
256-
pkg.sha.args <- list(
256+
common.args <- list(
257+
N.env.parent=test.env,
257258
pkg.path=pkg.path,
258259
sha.vec=sha.vec)
259-
test.env$test.list <- inherit_args(test.env$test.list, pkg.sha.args)
260+
test.env$test.list <- inherit_args(test.env$test.list, common.args)
260261
test.env$test.call <- list()
261262
for(Test in names(test.env$test.list)){
262263
test.env$test.call[[Test]] <- as.call(c(
@@ -266,19 +267,29 @@ atime_pkg_test_info <- function(pkg.path=".", tests.dir=NULL){
266267
test.env
267268
}
268269

269-
atime_test <- function(...){
270-
as.list(match.call()[-1])
271-
}
272-
273-
atime_test_list <- function(..., N, setup, expr, times, seconds.limit, verbose, pkg.edit.fun, result, tests=NULL){
274-
could.copy <- intersect(names(formals(atime_versions)),names(formals()))
275-
mc <- as.list(match.call()[-1])
270+
get_test_args <- function(){
271+
s.parent <- sys.parent()
272+
pfun <- sys.function(s.parent)
273+
two.funs <- list(pfun, atime_versions)
274+
name.vecs <- lapply(two.funs, function(f)names(formals(f)))
275+
could.copy <- Reduce(intersect, name.vecs)
276+
mc <- as.list(match.call(pfun, sys.call(s.parent))[-1])
276277
common.names <- intersect(names(mc), could.copy)
277278
possible.uneval <- c("setup","expr")
278279
uneval.names <- intersect(common.names, possible.uneval)
279280
eval.names <- setdiff(common.names, possible.uneval)
280-
common.args <- mget(eval.names)
281-
common.args[uneval.names] <- mc[uneval.names]
281+
p.frame <- parent.frame()
282+
test.args <- mget(eval.names, p.frame)
283+
test.args[uneval.names] <- mc[uneval.names]
284+
test.args
285+
}
286+
287+
atime_test <- function(N, setup, expr, times, seconds.limit, verbose, pkg.edit.fun, result){
288+
get_test_args()
289+
}
290+
291+
atime_test_list <- function(..., N, setup, expr, times, seconds.limit, verbose, pkg.edit.fun, result, tests=NULL){
292+
common.args <- get_test_args()
282293
L <- c(tests, list(...))
283294
inherit_args(L, common.args)
284295
}

R/versions.R

+2-2
Original file line numberDiff line numberDiff line change
@@ -121,14 +121,14 @@ atime_versions_install <- function(Package, pkg.path, new.Package.vec, sha.vec,
121121
}#any to install
122122
}
123123

124-
atime_versions <- function(pkg.path, N=default_N(), setup, expr, sha.vec=NULL, times=10, seconds.limit=0.01, verbose=FALSE, pkg.edit.fun=pkg.edit.default, result=FALSE, ...){
124+
atime_versions <- function(pkg.path, N=default_N(), setup, expr, sha.vec=NULL, times=10, seconds.limit=0.01, verbose=FALSE, pkg.edit.fun=pkg.edit.default, result=FALSE, N.env.parent=NULL, ...){
125125
ver.args <- list(
126126
pkg.path, substitute(expr), sha.vec, verbose, pkg.edit.fun, ...)
127127
install.seconds <- system.time({
128128
ver.exprs <- do.call(atime_versions_exprs, ver.args)
129129
})[["elapsed"]]
130130
a.args <- list(
131-
N, substitute(setup), ver.exprs, times, seconds.limit, verbose, result)
131+
N, substitute(setup), ver.exprs, times, seconds.limit, verbose, result, N.env.parent)
132132
bench.seconds <- system.time({
133133
out.list <- do.call(atime, a.args)
134134
})[["elapsed"]]

inst/example_tests.R

+5-3
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,11 @@ edit.data.table <- function(old.Package, new.Package, sha, new.pkg.path){
3131
sprintf('useDynLib\\("?%s"?', Package_regex),
3232
paste0('useDynLib(', new.Package_))
3333
}
34+
gvar <- 5
3435
test.list <- atime::atime_test_list(
3536
pkg.edit.fun=edit.data.table,
36-
N=9,
37-
test_N_expr=atime::atime_test(N=2, expr=rnorm(N)),
38-
test_expr=atime::atime_test(expr=rnorm(N))
37+
N=c(9,90),
38+
test_N_expr=atime::atime_test(N=c(2,20), expr=rnorm(N)),
39+
test_expr=atime::atime_test(expr=rnorm(N)),
40+
global_var_in_setup=atime::atime_test(setup=rnorm(gvar), expr=atime:::.packageName)
3941
)

man/atime.Rd

+5-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
\usage{atime(
88
N, setup, expr.list=NULL, times=10, seconds.limit=0.01, verbose=FALSE,
9-
result=FALSE, ...)}
9+
result=FALSE, N.env.parent=NULL...)}
1010

1111
\arguments{
1212
\item{N}{numeric vector of at least two data sizes, default is \code{2^seq(2,20)}.}
@@ -18,7 +18,10 @@ result=FALSE, ...)}
1818
\item{verbose}{logical, print messages after every data size?}
1919
\item{result}{logical, save each result? If \code{TRUE}, and result is
2020
a data frame with one row, then the numeric column names will be
21-
saved as more units to analyze (in addition to kilobytes and seconds).}
21+
saved as more units to analyze (in addition to kilobytes and
22+
seconds).}
23+
\item{N.env.parent}{environment to use as parent of environment
24+
created for each data size N, or NULL to use default parent env.}
2225
\item{\dots}{named expressions to time.}
2326
}
2427

man/atime_versions.Rd

+3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ atime_versions(
88
pkg.path, N, setup, expr, sha.vec=NULL,
99
times=10, seconds.limit=0.01, verbose=FALSE,
1010
pkg.edit.fun=pkg.edit.default, result=FALSE,
11+
N.env.parent=NULL,
1112
...)
1213
}
1314
\arguments{
@@ -38,6 +39,8 @@ atime_versions(
3839
installation, should typically replace instances of PKG with
3940
PKG.SHA, default works with Rcpp packages.}
4041
\item{result}{logical, save results? (default FALSE)}
42+
\item{N.env.parent}{environment to use as parent of environment
43+
created for each data size N, or NULL to use default parent env.}
4144
\item{\dots}{named versions.}
4245
}
4346
\details{

tests/testthat/test-versions.R

+4-2
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,12 @@ test_that("pkg.edit.fun is a function", {
9595
test.env <- atime::atime_pkg_test_info(pkg.dir)
9696
test_N_expr <- test.env$test.list$test_N_expr
9797
expect_identical(test_N_expr$pkg.edit.fun, test.env$edit.data.table)
98-
expect_identical(test_N_expr$N, 2)
98+
expect_identical(test_N_expr$N, c(2,20))
9999
expect_identical(test_N_expr$expr, quote(rnorm(N)))
100100
test_expr <- test.env$test.list$test_expr
101101
expect_identical(test_expr$pkg.edit.fun, test.env$edit.data.table)
102-
expect_identical(test_expr$N, 9)
102+
expect_identical(test_expr$N, c(9,90))
103103
expect_identical(test_expr$expr, quote(rnorm(N)))
104+
e.res <- eval(test.env$test.call[["global_var_in_setup"]])
105+
expect_is(e.res, "atime")
104106
})

0 commit comments

Comments
 (0)