Skip to content

Commit

Permalink
errors and warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
tdhock committed Oct 10, 2023
1 parent 36e2459 commit a8796e6
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 20 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: atime
Type: Package
Title: Asymptotic Timing
Version: 2023.4.27
Version: 2023.10.9
Authors@R: c(
person("Toby", "Hocking",
email="[email protected]",
Expand Down
7 changes: 7 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
Changes in version 2023.10.9

- default and errors for N in atime.
- warning if only one N evaluated in atime.
- error if no SHA specified in atime_versions.
- regex vignette/test uses re2 if present.

Changes in version 2023.4.27

- predict.references_best(kilobytes=1000) etc.
Expand Down
17 changes: 16 additions & 1 deletion R/atime.R
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@ atime_grid <- function

atime <- function(N, setup, expr.list=NULL, times=10, seconds.limit=0.01, verbose=FALSE, result=FALSE, ...){
kilobytes <- mem_alloc <- NULL
## above for CRAN NOTE.
if(missing(N)){
N <- as.integer(2^seq(1, 20))
}
if(!is.numeric(N)){
stop("N should be a numeric vector")
}
if(length(N)<2){
stop("length(N) should be at least 2")
}
formal.names <- names(formals())
mc.args <- as.list(match.call()[-1])
dots.list <- mc.args[!names(mc.args) %in% formal.names]
Expand Down Expand Up @@ -117,10 +127,15 @@ atime <- function(N, setup, expr.list=NULL, times=10, seconds.limit=0.01, verbos
metric.dt.list[[paste(N.value)]] <- N.stats
}
}
measurements <- rbindlist(metric.dt.list)
only.one <- measurements[, .(sizes=.N), by=expr.name][sizes==1]
if(nrow(only.one)){
warning("please increase max N or seconds.limit, because only one N was evaluated for expr.name: ", paste(only.one[["expr.name"]], collapse=", "))
}
structure(
list(
seconds.limit=seconds.limit,
measurements=do.call(rbind, metric.dt.list)),
measurements=measurements),
class="atime")
}

Expand Down
3 changes: 3 additions & 0 deletions R/versions.R
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ atime_versions_exprs <- function(pkg.path, expr, sha.vec=NULL, verbose=FALSE, pk
mc.args <- as.list(match.call()[-1])
dots.vec <- mc.args[!names(mc.args) %in% formal.names]
SHA.vec <- c(dots.vec, sha.vec)
if(length(SHA.vec)==0){
stop("need to specify at least one git SHA, in either sha.vec, or ...")
}
pkg.DESC <- file.path(pkg.path, "DESCRIPTION")
DESC.mat <- read.dcf(pkg.DESC)
Package <- DESC.mat[,"Package"]
Expand Down
3 changes: 1 addition & 2 deletions man/atime.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
N, setup, expr.list=NULL, times=10, seconds.limit=0.01, verbose=FALSE,
result=FALSE, ...)}
\arguments{
\item{N}{numeric vector of data sizes to vary.}
\item{N}{numeric vector of at least two data sizes, default is \code{2^seq(2,20)}.}
\item{setup}{expression to evaluate for every data size, before timings.}
\item{expr.list}{named list of expressions to time.}
\item{times}{number of times to evaluate each timed expression.}
Expand Down Expand Up @@ -45,7 +45,6 @@ plot(string.result)
## Example 2: split data table vs frame, constant factor difference.
library(data.table)
split.result <- atime::atime(
N=as.integer(10^seq(1, 7)),
setup={
set.seed(1)
DT <- data.table(
Expand Down
76 changes: 61 additions & 15 deletions tests/testthat/test-CRAN.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,48 @@
library(data.table)
library(testthat)

test_that("warning for only one N", {
expect_warning({
seconds.limit <- 0.001
atime.list <- atime::atime(
wait=Sys.sleep(seconds.limit),
TRE=regexpr(pattern, subject, perl=FALSE),
setup={
subject <- paste(rep("a", N), collapse="")
pattern <- paste(rep(c("a?", "a"), each=N), collapse="")
},
seconds.limit=seconds.limit)
},
"please increase max N or seconds.limit, because only one N was evaluated for expr.name: wait",
fixed=TRUE)
})

test_that("error for N wrong type", {
expect_error({
atime.list <- atime::atime(
PCRE=regexpr(pattern, subject, perl=TRUE),
TRE=regexpr(pattern, subject, perl=FALSE),
setup={
subject <- paste(rep("a", N), collapse="")
pattern <- paste(rep(c("a?", "a"), each=N), collapse="")
},
N="foo")
}, "N should be a numeric vector")
})

test_that("error for length(N)==1", {
expect_error({
atime.list <- atime::atime(
PCRE=regexpr(pattern, subject, perl=TRUE),
TRE=regexpr(pattern, subject, perl=FALSE),
setup={
subject <- paste(rep("a", N), collapse="")
pattern <- paste(rep(c("a?", "a"), each=N), collapse="")
},
N=100)
}, "length(N) should be at least 2", fixed=TRUE)
})

test_that("more.units error if not present", {
atime.list <- atime::atime(
PCRE=regexpr(pattern, subject, perl=TRUE),
Expand Down Expand Up @@ -105,7 +148,7 @@ test_that("atime_grid ok when THREADS used", {
test_that("error for expr.list not list", {
expr.list <- atime::atime_grid(
list(ENGINE=c(
##if(requireNamespace("re2"))"RE2",#uncomment when new nc on CRAN.
if(requireNamespace("re2"))"RE2",
"PCRE",
if(requireNamespace("stringi"))"ICU")),
nc=nc::capture_first_vec(subject, pattern, engine=ENGINE))
Expand Down Expand Up @@ -134,23 +177,27 @@ test_that("only one value in grid is OK", {
})

test_that("null is faster than wait", {
alist <- atime::atime(
N=1:2,
setup={},
wait=Sys.sleep(0.01),
null=NULL,
seconds.limit=0.001)
suppressWarnings({
alist <- atime::atime(
N=1:2,
setup={},
wait=Sys.sleep(0.01),
null=NULL,
seconds.limit=0.001)
})
expect_equal(nrow(alist$measurements[expr.name=="null"]), 2)
})

test_that("no error for results=FALSE", {
alist <- atime::atime(
N=1:2,
setup={},
wait=Sys.sleep(0.01),
null=NULL,
results=FALSE,
seconds.limit=0.001)
suppressWarnings({
alist <- atime::atime(
N=1:2,
setup={},
wait=Sys.sleep(0.01),
null=NULL,
results=FALSE,
seconds.limit=0.001)
})
expect_is(alist, "atime")
expect_equal(sort(alist$measurements$expr.name), c("null","null","results","results","wait"))
})
Expand Down Expand Up @@ -215,4 +262,3 @@ test_that("errors for predict method", {
}, "argument names should be unique, problem(count): foo(3), kilobytes(2)", fixed=TRUE)
})


19 changes: 19 additions & 0 deletions tests/testthat/test-versions.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
library(data.table)
library(testthat)
tdir <- tempfile()
dir.create(tdir)
git2r::clone("https://github.com/tdhock/binsegRcpp", tdir)
test_that("error if no versions specified", {
expect_error({
atime.list <- atime::atime_versions(
pkg.path=tdir,
N=2^seq(2, 20),
setup={
max.segs <- as.integer(N/2)
data.vec <- 1:N
},
expr=binsegRcpp::binseg_normal(data.vec, max.segs))
},
"need to specify at least one git SHA, in either sha.vec, or ...",
fixed=TRUE)
})
2 changes: 1 addition & 1 deletion vignettes/regex.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ C regex library is used:
```{r}
nc.exprs <- atime::atime_grid(
list(ENGINE=c(
##if(requireNamespace("re2"))"RE2",#uncomment when new nc on CRAN.
if(requireNamespace("re2"))"RE2",
"PCRE",
if(requireNamespace("stringi"))"ICU")),
nc=nc::capture_first_vec(subject, pattern, engine=ENGINE))
Expand Down

0 comments on commit a8796e6

Please sign in to comment.