Skip to content

Commit d3bdd11

Browse files
Add options= to test(), convert most Rraw scripts
1 parent 6c1fd83 commit d3bdd11

File tree

7 files changed

+160
-190
lines changed

7 files changed

+160
-190
lines changed

R/test.data.table.R

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,11 @@ gc_mem = function() {
249249
# nocov end
250250
}
251251

252-
test = function(num,x,y=TRUE,error=NULL,warning=NULL,message=NULL,output=NULL,notOutput=NULL,ignore.warning=NULL) {
252+
test = function(num,x,y=TRUE,error=NULL,warning=NULL,message=NULL,output=NULL,notOutput=NULL,ignore.warning=NULL,options=NULL) {
253+
if (!is.null(options)) {
254+
old_options <- do.call('options', as.list(options)) # as.list(): allow passing named character vector for convenience
255+
on.exit(options(old_options), add=TRUE)
256+
}
253257
# Usage:
254258
# i) tests that x equals y when both x and y are supplied, the most common usage
255259
# ii) tests that x is TRUE when y isn't supplied
@@ -280,7 +284,7 @@ test = function(num,x,y=TRUE,error=NULL,warning=NULL,message=NULL,output=NULL,no
280284
foreign = get("foreign", parent.frame())
281285
showProgress = get("showProgress", parent.frame())
282286
time = nTest = RSS = NULL # to avoid 'no visible binding' note
283-
if (num>0) on.exit( {
287+
if (num>0) on.exit( add=TRUE, {
284288
took = proc.time()[3L]-lasttime # so that prep time between tests is attributed to the following test
285289
timings[as.integer(num), `:=`(time=time+took, nTest=nTest+1L), verbose=FALSE]
286290
if (memtest) {

inst/tests/benchmark.Rraw

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,17 @@ test(655, abs(tt1["user.self"] - tt2["user.self"])<2.0) # unoptimized tt2 takes
8181
# Test for optimisation of 'order' to 'forder'.
8282
set.seed(45L)
8383
DT <- data.table(x=sample(1e2, 1e6,TRUE), y=sample(1e2, 1e6,TRUE))
84-
old = options(datatable.optimize=Inf)
85-
t1 = system.time(ans1 <- DT[order(x,-y)])[['elapsed']] # optimized to forder()
86-
t2 = system.time(ans2 <- DT[base_order(x,-y)])[['elapsed']] # not optimized
87-
test(1241.1, ans1, ans2)
88-
if (.devtesting) test(1241.2, t1 < t2+0.1)
89-
# 0.2 < 3.8 on Matt's laptop seems safe enough to test.
90-
# Even so, 1241.2 has been known to fail, perhaps if system swaps and this R sessions pauses or something?
91-
# We shouldn't have timing tests here that run on CRAN for this reason. Hence wrapping with .devtesting
92-
options(old)
84+
local({
85+
old = options(datatable.optimize=Inf)
86+
on.exit(options(old))
87+
t1 = system.time(ans1 <- DT[order(x,-y)])[['elapsed']] # optimized to forder()
88+
t2 = system.time(ans2 <- DT[base_order(x,-y)])[['elapsed']] # not optimized
89+
test(1241.1, ans1, ans2)
90+
if (.devtesting) test(1241.2, t1 < t2+0.1)
91+
# 0.2 < 3.8 on Matt's laptop seems safe enough to test.
92+
# Even so, 1241.2 has been known to fail, perhaps if system swaps and this R sessions pauses or something?
93+
# We shouldn't have timing tests here that run on CRAN for this reason. Hence wrapping with .devtesting
94+
})
9395

9496

9597
# fwrite showProgress test 1735. Turned off as too long/big for CRAN.

inst/tests/froll.Rraw

Lines changed: 39 additions & 57 deletions
Large diffs are not rendered by default.

inst/tests/nafill.Rraw

Lines changed: 44 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -154,13 +154,11 @@ test(4.24, colnamesInt(dt, "a"), error="has no names")
154154

155155
# verbose
156156
dt = data.table(a=c(1L, 2L, NA_integer_), b=c(1, 2, NA_real_))
157-
old=options(datatable.verbose=TRUE)
158-
test(5.01, nafill(dt, "locf"), output="nafillInteger: took.*nafillDouble: took.*nafillR.*took")
159-
test(5.02, setnafill(dt, "locf"), output="nafillInteger: took.*nafillDouble: took.*nafillR.*took")
157+
test(5.01, options = c(datatable.verbose = TRUE), nafill(dt, "locf"), output="nafillInteger: took.*nafillDouble: took.*nafillR.*took")
158+
test(5.02, options = c(datatable.verbose = TRUE), setnafill(dt, "locf"), output="nafillInteger: took.*nafillDouble: took.*nafillR.*took")
160159
if (test_bit64) {
161-
test(5.03, nafill(as.integer64(c(NA,2,NA,3)), "locf"), as.integer64(c(NA,2,2,3)), output="nafillInteger64: took.*nafillR.*took")
160+
test(5.03, options = c(datatable.verbose = TRUE), nafill(as.integer64(c(NA,2,NA,3)), "locf"), as.integer64(c(NA,2,2,3)), output="nafillInteger64: took.*nafillR.*took")
162161
}
163-
options(old)
164162

165163
# coerceAs int/numeric/int64 as used in nafill
166164
if (test_bit64) {
@@ -243,65 +241,61 @@ if (test_bit64) {
243241
}
244242

245243
# coerceAs verbose
246-
options(datatable.verbose=2L)
247244
input = 1
248-
test(10.01, ans<-coerceAs(input, 1), 1, output="double[numeric] into double[numeric]")
249-
test(10.02, address(input)!=address(ans))
250-
test(10.03, ans<-coerceAs(input, 1, copy=FALSE), 1, output="copy=false and input already of expected type and class double[numeric]")
251-
test(10.04, address(input), address(ans))
252-
test(10.05, ans<-coerceAs(input, 1L), 1L, output="double[numeric] into integer[integer]")
253-
test(10.06, address(input)!=address(ans))
254-
test(10.07, ans<-coerceAs(input, 1L, copy=FALSE), 1L, output="double[numeric] into integer[integer]", notOutput="copy=false")
255-
test(10.08, address(input)!=address(ans))
256-
test(10.09, coerceAs("1", 1L), 1L, output="character[character] into integer[integer]", warning="Coercing.*character.*integer")
257-
test(10.10, coerceAs("1", 1), 1, output="character[character] into double[numeric]", warning="Coercing.*character.*double")
258-
test(10.11, coerceAs("a", factor("x")), factor("a", levels=c("x","a")), output="character[character] into integer[factor]") ## levels of 'as' are retained!
259-
test(10.12, coerceAs("a", factor()), factor("a"), output="character[character] into integer[factor]")
260-
test(10.13, coerceAs(1, factor("x")), factor("x"), output="double[numeric] into integer[factor]")
261-
test(10.14, coerceAs(1, factor("x", levels=c("x","y"))), factor("x", levels=c("x","y")), output="double[numeric] into integer[factor]")
262-
test(10.15, coerceAs(2, factor("x", levels=c("x","y"))), factor("y", levels=c("x","y")), output="double[numeric] into integer[factor]")
263-
test(10.16, coerceAs(1:2, factor(c("x","y"))), factor(c("x","y")), output="integer[integer] into integer[factor]")
264-
test(10.17, coerceAs(1:3, factor(c("x","y"))), output="integer[integer] into integer[factor]", error="factor numbers.*3 is outside the level range")
265-
test(10.18, coerceAs(c(1,2,3), factor(c("x","y"))), output="double[numeric] into integer[factor]", error="factor numbers.*3.000000 is outside the level range")
266-
test(10.19, coerceAs(factor("x"), factor(c("x","y"))), factor("x", levels=c("x","y")), output="integer[factor] into integer[factor]")
267-
test(10.20, coerceAs(factor("x"), factor(c("x","y")), copy=FALSE), factor("x", levels=c("x","y")), output="input already of expected type and class") ## copy=F has copyMostAttrib
245+
test(10.01, options = c(datatable.verbose = 2L), ans<-coerceAs(input, 1), 1, output="double[numeric] into double[numeric]")
246+
test(10.02, options = c(datatable.verbose = 2L), address(input)!=address(ans))
247+
test(10.03, options = c(datatable.verbose = 2L), ans<-coerceAs(input, 1, copy=FALSE), 1, output="copy=false and input already of expected type and class double[numeric]")
248+
test(10.04, options = c(datatable.verbose = 2L), address(input), address(ans))
249+
test(10.05, options = c(datatable.verbose = 2L), ans<-coerceAs(input, 1L), 1L, output="double[numeric] into integer[integer]")
250+
test(10.06, options = c(datatable.verbose = 2L), address(input)!=address(ans))
251+
test(10.07, options = c(datatable.verbose = 2L), ans<-coerceAs(input, 1L, copy=FALSE), 1L, output="double[numeric] into integer[integer]", notOutput="copy=false")
252+
test(10.08, options = c(datatable.verbose = 2L), address(input)!=address(ans))
253+
test(10.09, options = c(datatable.verbose = 2L), coerceAs("1", 1L), 1L, output="character[character] into integer[integer]", warning="Coercing.*character.*integer")
254+
test(10.10, options = c(datatable.verbose = 2L), coerceAs("1", 1), 1, output="character[character] into double[numeric]", warning="Coercing.*character.*double")
255+
test(10.11, options = c(datatable.verbose = 2L), coerceAs("a", factor("x")), factor("a", levels=c("x","a")), output="character[character] into integer[factor]") ## levels of 'as' are retained!
256+
test(10.12, options = c(datatable.verbose = 2L), coerceAs("a", factor()), factor("a"), output="character[character] into integer[factor]")
257+
test(10.13, options = c(datatable.verbose = 2L), coerceAs(1, factor("x")), factor("x"), output="double[numeric] into integer[factor]")
258+
test(10.14, options = c(datatable.verbose = 2L), coerceAs(1, factor("x", levels=c("x","y"))), factor("x", levels=c("x","y")), output="double[numeric] into integer[factor]")
259+
test(10.15, options = c(datatable.verbose = 2L), coerceAs(2, factor("x", levels=c("x","y"))), factor("y", levels=c("x","y")), output="double[numeric] into integer[factor]")
260+
test(10.16, options = c(datatable.verbose = 2L), coerceAs(1:2, factor(c("x","y"))), factor(c("x","y")), output="integer[integer] into integer[factor]")
261+
test(10.17, options = c(datatable.verbose = 2L), coerceAs(1:3, factor(c("x","y"))), output="integer[integer] into integer[factor]", error="factor numbers.*3 is outside the level range")
262+
test(10.18, options = c(datatable.verbose = 2L), coerceAs(c(1,2,3), factor(c("x","y"))), output="double[numeric] into integer[factor]", error="factor numbers.*3.000000 is outside the level range")
263+
test(10.19, options = c(datatable.verbose = 2L), coerceAs(factor("x"), factor(c("x","y"))), factor("x", levels=c("x","y")), output="integer[factor] into integer[factor]")
264+
test(10.20, options = c(datatable.verbose = 2L), coerceAs(factor("x"), factor(c("x","y")), copy=FALSE), factor("x", levels=c("x","y")), output="input already of expected type and class") ## copy=F has copyMostAttrib
268265
a = structure("a", class="a")
269266
b = structure("b", class="b")
270-
test(10.21, coerceAs(a, b), structure("a", class="b"), output="character[a] into character[b]")
267+
test(10.21, options = c(datatable.verbose = 2L), coerceAs(a, b), structure("a", class="b"), output="character[a] into character[b]")
271268
a = structure(1L, class="a")
272269
b = structure(2L, class="b")
273-
test(10.22, coerceAs(a, b), structure(1L, class="b"), output="integer[a] into integer[b]")
270+
test(10.22, options = c(datatable.verbose = 2L), coerceAs(a, b), structure(1L, class="b"), output="integer[a] into integer[b]")
274271
a = structure(1, class="a")
275272
b = structure(2, class="b")
276-
test(10.23, coerceAs(a, b), structure(1, class="b"), output="double[a] into double[b]")
273+
test(10.23, options = c(datatable.verbose = 2L), coerceAs(a, b), structure(1, class="b"), output="double[a] into double[b]")
277274
a = structure(1, class="a")
278275
b = structure(2L, class="b")
279-
test(10.24, coerceAs(a, b), structure(1L, class="b"), output="double[a] into integer[b]")
276+
test(10.24, options = c(datatable.verbose = 2L), coerceAs(a, b), structure(1L, class="b"), output="double[a] into integer[b]")
280277
if (test_bit64) {
281278
x = as.integer64(1L)
282-
test(10.81, coerceAs(x, 1), 1, output="double[integer64] into double[numeric]")
283-
test(10.82, coerceAs(x, 1L), 1L, output="double[integer64] into integer[integer]")
284-
test(10.83, coerceAs(x, "1"), "1", output="double[integer64] into character[character]")
285-
test(10.84, coerceAs(1, x), x, output="double[numeric] into double[integer64]")
286-
test(10.85, coerceAs(1L, x), x, output="integer[integer] into double[integer64]")
287-
test(10.86, coerceAs("1", x), x, output="character[character] into double[integer64]", warning="Coercing.*character")
288-
options(datatable.verbose=3L)
289-
test(10.87, coerceAs(x, 1L), 1L, output=c("double[integer64] into integer[integer]","Zero-copy coerce when assigning 'integer64' to 'integer'"))
290-
test(10.88, coerceAs(1L, x), x, output=c("integer[integer] into double[integer64]","Zero-copy coerce when assigning 'integer' to 'integer64'"))
291-
options(datatable.verbose=2L)
292-
test(10.89, coerceAs(-2147483649, x), as.integer64(-2147483649), output="double[numeric] into double[integer64]")
279+
test(10.81, options = c(datatable.verbose = 2L), coerceAs(x, 1), 1, output="double[integer64] into double[numeric]")
280+
test(10.82, options = c(datatable.verbose = 2L), coerceAs(x, 1L), 1L, output="double[integer64] into integer[integer]")
281+
test(10.83, options = c(datatable.verbose = 2L), coerceAs(x, "1"), "1", output="double[integer64] into character[character]")
282+
test(10.84, options = c(datatable.verbose = 2L), coerceAs(1, x), x, output="double[numeric] into double[integer64]")
283+
test(10.85, options = c(datatable.verbose = 2L), coerceAs(1L, x), x, output="integer[integer] into double[integer64]")
284+
test(10.86, options = c(datatable.verbose = 2L), coerceAs("1", x), x, output="character[character] into double[integer64]", warning="Coercing.*character")
285+
test(10.87, options = c(datatable.verbose = 3L), coerceAs(x, 1L), 1L, output=c("double[integer64] into integer[integer]","Zero-copy coerce when assigning 'integer64' to 'integer'"))
286+
test(10.88, options = c(datatable.verbose = 3L), coerceAs(1L, x), x, output=c("integer[integer] into double[integer64]","Zero-copy coerce when assigning 'integer' to 'integer64'"))
287+
test(10.89, options = c(datatable.verbose = 2L), coerceAs(-2147483649, x), as.integer64(-2147483649), output="double[numeric] into double[integer64]")
293288
}
294289
if (test_nanotime) {
295290
x = nanotime(1L)
296-
test(10.91, coerceAs(x, 1), 1, output="double[nanotime] into double[numeric]")
297-
test(10.92, coerceAs(x, 1L), 1L, output="double[nanotime] into integer[integer]")
298-
test(10.93, substring(coerceAs(x, "1"),1,11) %in% c("1","1970-01-01T"), output="double[nanotime] into character[character]")
299-
# ^ https://github.com/eddelbuettel/nanotime/issues/92; %in% so as not to break if nanotime adds as.character method
300-
test(10.94, coerceAs(1, x), x, output="double[numeric] into double[nanotime]")
301-
test(10.95, coerceAs(1L, x), x, output="integer[integer] into double[nanotime]")
302-
test(10.96, coerceAs("1", x), x, output="character[character] into double[nanotime]", warning="Coercing.*character")
291+
test(10.91, options = c(datatable.verbose = 2L), coerceAs(x, 1), 1, output="double[nanotime] into double[numeric]")
292+
test(10.92, options = c(datatable.verbose = 2L), coerceAs(x, 1L), 1L, output="double[nanotime] into integer[integer]")
293+
test(10.93, options = c(datatable.verbose = 2L), substring(coerceAs(x, "1"),1,11) %in% c("1","1970-01-01T"), output="double[nanotime] into character[character]")
294+
# ^ https://github.com/eddelbuettel/nanotime/issues/92; %in% so as not to break if nanotime adds as.character method
295+
test(10.94, options = c(datatable.verbose = 2L), coerceAs(1, x), x, output="double[numeric] into double[nanotime]")
296+
test(10.95, options = c(datatable.verbose = 2L), coerceAs(1L, x), x, output="integer[integer] into double[nanotime]")
297+
test(10.96, options = c(datatable.verbose = 2L), coerceAs("1", x), x, output="character[character] into double[nanotime]", warning="Coercing.*character")
303298
}
304-
options(datatable.verbose=FALSE)
305299
test(11.01, coerceAs(list(a=1), 1), error="is not atomic")
306300
test(11.02, coerceAs(1, list(a=1)), list(1))
307301
test(11.03, coerceAs(sum, 1), error="is not atomic")
@@ -328,7 +322,4 @@ test(11.09, coerceAs(1L, a), error="must not be matrix or array")
328322
test(99.1, data.table(a=1,b=2)[1,1, verbose=1], error="verbose must be logical or integer")
329323
test(99.2, data.table(a=1,b=2)[1,1, verbose=1:2], error="verbose must be length 1 non-NA")
330324
test(99.3, data.table(a=1,b=2)[1,1, verbose=NA], error="verbose must be length 1 non-NA")
331-
options(datatable.verbose=1)
332-
test(99.4, coerceAs(1, 2L), error="verbose option must be length 1 non-NA logical or integer")
333-
options(datatable.verbose=FALSE)
334-
325+
test(99.4, options = c(datatable.verbose = 1), coerceAs(1, 2L), error="verbose option must be length 1 non-NA logical or integer")

0 commit comments

Comments
 (0)