Skip to content

Commit a14b486

Browse files
committed
added batch tests for adaptive and partial
1 parent b3f81cd commit a14b486

File tree

2 files changed

+27
-21
lines changed

2 files changed

+27
-21
lines changed

inst/tests/froll.Rraw

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,7 +1223,7 @@ if (requireNamespace("zoo", quietly=TRUE)) {
12231223
}
12241224
#### adaptive moving average compare
12251225
num = 6009.0
1226-
arollfun = function(FUN, x, n, na.rm=FALSE, align=c("right","left"), fill=NA, nf.rm=FALSE) { ## partial cannot be used with adaptive
1226+
arollfun = function(FUN, x, n, na.rm=FALSE, align=c("right","left"), fill=NA, nf.rm=FALSE, partial=FALSE) {
12271227
# adaptive moving average in R
12281228
stopifnot((nx<-length(x))==length(n))
12291229
align = match.arg(align)
@@ -1234,11 +1234,15 @@ arollfun = function(FUN, x, n, na.rm=FALSE, align=c("right","left"), fill=NA, nf
12341234
for (i in seq_along(x)) {
12351235
if (i >= n[i])
12361236
ans[i] = f(x[(i-n[i]+1):i], na.rm=na.rm)
1237+
else if (partial)
1238+
ans[i] = f(x[1L:i], na.rm=na.rm)
12371239
}
12381240
} else {
12391241
for (i in seq_along(x)) {
12401242
if (i <= nx-n[i]+1)
12411243
ans[i] = f(x[i:(i+n[i]-1)], na.rm=na.rm)
1244+
else if (partial)
1245+
ans[i] = f(x[i:length(x)], na.rm=na.rm)
12421246
}
12431247
}
12441248
ans
@@ -1250,32 +1254,34 @@ afun_compare = function(x, n, funs=c("mean","sum","max"), algos=c("fast","exact"
12501254
for (align in c("right","left")) {
12511255
for (na.rm in c(FALSE, TRUE)) {
12521256
for (fill in c(NA_real_, 0)) {
1253-
for (has.nf in c(NA,TRUE,FALSE)) {
1254-
if (identical(has.nf, FALSE)) {
1255-
if (na.rm)
1256-
next ## errors "not make sense"
1257-
if (any(!is.finite(x)))
1258-
next ## do not test warnings (mean, sum) or incorrect expect results (max)
1257+
for (partial in c(FALSE,TRUE)) {
1258+
for (has.nf in c(NA,TRUE,FALSE)) {
1259+
if (identical(has.nf, FALSE)) {
1260+
if (na.rm)
1261+
next ## errors "not make sense"
1262+
if (any(!is.finite(x)))
1263+
next ## do not test warnings (mean, sum) or incorrect expect results (max)
1264+
}
1265+
for (algo in algos) {
1266+
num <<- num + num.step
1267+
eval(substitute(
1268+
test(.num, ignore.warning="no non-missing arguments",
1269+
arollfun(.fun, x, n, fill=.fill, na.rm=.na.rm, align=.align, partial=.partial),
1270+
frollfun(.fun, x, n, fill=.fill, na.rm=.na.rm, algo=.algo, adaptive=TRUE, align=.align, has.nf=.has.nf, partial=.partial)),
1271+
list(.num=num, .fun=fun, .fill=fill, .na.rm=na.rm, .algo=algo, .align=align, .has.nf=has.nf, .partial=partial)
1272+
))
1273+
}
12591274
}
1260-
for (algo in algos) {
1275+
if (base::getRversion() >= "3.4.0") { ## SET_GROWABLE_BIT
12611276
num <<- num + num.step
12621277
eval(substitute(
12631278
test(.num, ignore.warning="no non-missing arguments",
1264-
arollfun(.fun, x, n, fill=.fill, na.rm=.na.rm, align=.align),
1265-
frollfun(.fun, x, n, fill=.fill, na.rm=.na.rm, algo=.algo, adaptive=TRUE, align=.align, has.nf=.has.nf)),
1266-
list(.num=num, .fun=fun, .fill=fill, .na.rm=na.rm, .algo=algo, .align=align, .has.nf=has.nf)
1279+
frollapply(x, n, FUN=match.fun(.fun), fill=.fill, na.rm=.na.rm, adaptive=TRUE, align=.align, partial=.partial),
1280+
frollfun(.fun, x, n, fill=.fill, na.rm=.na.rm, adaptive=TRUE, align=.align, partial=.partial)),
1281+
list(.num=num, .fun=fun, .fill=fill, .na.rm=na.rm, .align=align, .partial=partial)
12671282
))
12681283
}
12691284
}
1270-
if (base::getRversion() >= "3.4.0") { ## SET_GROWABLE_BIT
1271-
num <<- num + num.step
1272-
eval(substitute(
1273-
test(.num, ignore.warning="no non-missing arguments",
1274-
frollapply(x, n, FUN=match.fun(.fun), fill=.fill, na.rm=.na.rm, adaptive=TRUE, align=.align),
1275-
frollfun(.fun, x, n, fill=.fill, na.rm=.na.rm, adaptive=TRUE, align=.align)),
1276-
list(.num=num, .fun=fun, .fill=fill, .na.rm=na.rm, .align=align)
1277-
))
1278-
}
12791285
}
12801286
}
12811287
}

man/froll.Rd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ ans1 = frollmean(x, n, partial=TRUE)
141141
an = function(n, len) c(seq.int(n), rep(n, len-n))
142142
ans2 = frollmean(x, an(n, length(x)), adaptive=TRUE)
143143
all.equal(ans1, ans2)
144-
# speed upby using partial only for incomplete observations
144+
# much faster by using partial only for incomplete observations
145145
ans3 = frollmean(x, n)
146146
ans3[seq.int(n-1L)] = frollmean(x[seq.int(n-1L)], n, partial=TRUE)
147147
all.equal(ans1, ans3)

0 commit comments

Comments
 (0)