diff --git a/inst/tests/benchmark.Rraw b/inst/tests/benchmark.Rraw index 957c297fa7..2dc40b63ee 100644 --- a/inst/tests/benchmark.Rraw +++ b/inst/tests/benchmark.Rraw @@ -190,20 +190,24 @@ DT = data.table(A=1:10,B=rnorm(10),C=paste("a",1:100010,sep="")) test(301.1, nrow(DT[,sum(B),by=C])==100010) # Test := by key, and that := to the key by key unsets the key. Make it non-trivial in size too. -options(datatable.optimize=0L) -set.seed(1) -DT = data.table(a=sample(1:100,1e6,replace=TRUE),b=sample(1:1000,1e6,replace=TRUE),key="a") -test(637.1, DT[,m:=sum(b),by=a][1:3], data.table(a=1L,b=c(156L,808L,848L),m=DT[J(1),sum(b)],key="a")) -test(637.2, key(DT[J(43L),a:=99L]), NULL) -setkey(DT,a) -test(637.3, key(DT[,a:=99L,by=a]), NULL) -options(datatable.optimize=2L) -set.seed(1) -DT = data.table(a=sample(1:100,1e6,replace=TRUE),b=sample(1:1000,1e6,replace=TRUE),key="a") -test(638.1, DT[,m:=sum(b),by=a][1:3], data.table(a=1L,b=c(156L,808L,848L),m=DT[J(1),sum(b)],key="a")) -test(638.2, key(DT[J(43L),a:=99L]), NULL) -setkey(DT,a) -test(638.3, key(DT[,a:=99L,by=a]), NULL) +local({ + old = options(datatable.optimize=0L); on.exit(options(old)) + set.seed(1) + DT = data.table(a=sample(1:100, 1e6, replace=TRUE), b=sample(1:1000, 1e6, replace=TRUE), key="a") + test(637.1, DT[, m:=sum(b), by=a][1:3], data.table(a=1L, b=c(156L, 808L, 848L), m=DT[J(1), sum(b)], key="a")) + test(637.2, key(DT[J(43L), a:=99L]), NULL) + setkey(DT, a) + test(637.3, key(DT[, a:=99L, by=a]), NULL) +}) +local({ + options(datatable.optimize=2L); on.exit(options(old)) + set.seed(1) + DT = data.table(a=sample(1:100, 1e6, replace=TRUE), b=sample(1:1000, 1e6, replace=TRUE), key="a") + test(638.1, DT[, m:=sum(b), by=a][1:3], data.table(a=1L, b=c(156L, 808L, 848L), m=DT[J(1), sum(b)], key="a")) + test(638.2, key(DT[J(43L), a:=99L]), NULL) + setkey(DT,a) + test(638.3, key(DT[, a:=99L, by=a]), NULL) +}) # Test X[Y] slowdown, #2216 # Many minutes in 1.8.2! Now well under 1s, but 10s for very wide tolerance for CRAN. We'd like CRAN to tell us if any changes diff --git a/inst/tests/nafill.Rraw b/inst/tests/nafill.Rraw index 7f4a32b608..45168c0bb8 100644 --- a/inst/tests/nafill.Rraw +++ b/inst/tests/nafill.Rraw @@ -160,14 +160,15 @@ names(dt) <- NULL test(4.36, colnamesInt(dt, "a"), error="has no names") # verbose -dt = data.table(a=c(1L, 2L, NA_integer_), b=c(1, 2, NA_real_)) -old=options(datatable.verbose=TRUE) -test(5.01, nafill(dt, "locf"), output="nafillInteger: took.*nafillDouble: took.*nafillR.*took") -test(5.02, setnafill(dt, "locf"), output="nafillInteger: took.*nafillDouble: took.*nafillR.*took") -if (test_bit64) { - test(5.03, nafill(as.integer64(c(NA,2,NA,3)), "locf"), as.integer64(c(NA,2,2,3)), output="nafillInteger64: took.*nafillR.*took") -} -options(old) +local({ + dt = data.table(a=c(1L, 2L, NA_integer_), b=c(1, 2, NA_real_)) + old = options(datatable.verbose=TRUE); on.exit(options(old)) + test(5.01, nafill(dt, "locf"), output="nafillInteger: took.*nafillDouble: took.*nafillR.*took") + test(5.02, setnafill(dt, "locf"), output="nafillInteger: took.*nafillDouble: took.*nafillR.*took") + if (test_bit64) { + test(5.03, nafill(as.integer64(c(NA,2,NA,3)), "locf"), as.integer64(c(NA,2,2,3)), output="nafillInteger64: took.*nafillR.*took") + } +}) # coerceAs int/numeric/int64 as used in nafill if (test_bit64) { @@ -250,59 +251,61 @@ if (test_bit64) { } # coerceAs verbose -options(datatable.verbose=2L) -input = 1 -# use levels= explicitly to avoid locale-related sorting of letters -xy_factor = factor(c("x", "y"), levels=c("x", "y")) -test(10.01, ans<-coerceAs(input, 1), 1, output="double[numeric] into double[numeric]") -test(10.02, address(input)!=address(ans)) -test(10.03, ans<-coerceAs(input, 1, copy=FALSE), 1, output="copy=false and input already of expected type and class double[numeric]") -test(10.04, address(input), address(ans)) -test(10.05, ans<-coerceAs(input, 1L), 1L, output="double[numeric] into integer[integer]") -test(10.06, address(input)!=address(ans)) -test(10.07, ans<-coerceAs(input, 1L, copy=FALSE), 1L, output="double[numeric] into integer[integer]", notOutput="copy=false") -test(10.08, address(input)!=address(ans)) -test(10.09, coerceAs("1", 1L), 1L, output="character[character] into integer[integer]", warning="Coercing.*character.*integer") -test(10.10, coerceAs("1", 1), 1, output="character[character] into double[numeric]", warning="Coercing.*character.*double") -test(10.11, coerceAs("a", factor("x")), factor("a", levels=c("x","a")), output="character[character] into integer[factor]") ## levels of 'as' are retained! -test(10.12, coerceAs("a", factor()), factor("a"), output="character[character] into integer[factor]") -test(10.13, coerceAs(1, factor("x")), factor("x"), output="double[numeric] into integer[factor]") -test(10.14, coerceAs(1, factor("x", levels=c("x","y"))), factor("x", levels=c("x","y")), output="double[numeric] into integer[factor]") -test(10.15, coerceAs(2, factor("x", levels=c("x","y"))), factor("y", levels=c("x","y")), output="double[numeric] into integer[factor]") -test(10.16, coerceAs(1:2, xy_factor), xy_factor, output="integer[integer] into integer[factor]") -test(10.17, coerceAs(1:3, xy_factor), output="integer[integer] into integer[factor]", error="factor numbers.*3 is outside the level range") -test(10.18, coerceAs(c(1,2,3), xy_factor), output="double[numeric] into integer[factor]", error="factor numbers.*3.000000 is outside the level range") -test(10.19, coerceAs(factor("x"), xy_factor), factor("x", levels=c("x","y")), output="integer[factor] into integer[factor]") -test(10.20, coerceAs(factor("x"), xy_factor, copy=FALSE), factor("x", levels=c("x","y")), output="input already of expected type and class") ## copy=F has copyMostAttrib -a = structure("a", class="a") -b = structure("b", class="b") -test(10.21, coerceAs(a, b), structure("a", class="b"), output="character[a] into character[b]") -a = structure(1L, class="a") -b = structure(2L, class="b") -test(10.22, coerceAs(a, b), structure(1L, class="b"), output="integer[a] into integer[b]") -a = structure(1, class="a") -b = structure(2, class="b") -test(10.23, coerceAs(a, b), structure(1, class="b"), output="double[a] into double[b]") -a = structure(1, class="a") -b = structure(2L, class="b") -test(10.24, coerceAs(a, b), structure(1L, class="b"), output="double[a] into integer[b]") -if (test_bit64) { - x = as.integer64(1L) - test(10.81, coerceAs(x, 1), 1, output="double[integer64] into double[numeric]") - test(10.82, coerceAs(x, 1L), 1L, output="double[integer64] into integer[integer]") - test(10.83, coerceAs(x, "1"), "1", output="double[integer64] into character[character]") - test(10.84, coerceAs(1, x), x, output="double[numeric] into double[integer64]") - test(10.85, coerceAs(1L, x), x, output="integer[integer] into double[integer64]") - test(10.86, coerceAs("1", x), x, output="character[character] into double[integer64]", warning="Coercing.*character") - options(datatable.verbose=3L) - test(10.87, coerceAs(x, 1L), 1L, output=c("double[integer64] into integer[integer]","Zero-copy coerce when assigning 'integer64' to 'integer'")) - test(10.88, coerceAs(1L, x), x, output=c("integer[integer] into double[integer64]","Zero-copy coerce when assigning 'integer' to 'integer64'")) - options(datatable.verbose=2L) - test(10.89, coerceAs(-2147483649, x), as.integer64(-2147483649), output="double[numeric] into double[integer64]") -} -# 10.91 tested nanotime moved to other.Rraw 27.21, #6139 +local({ + old = options(datatable.verbose=2L); on.exit(options(old)) + input = 1 + # use levels= explicitly to avoid locale-related sorting of letters + xy_factor = factor(c("x", "y"), levels=c("x", "y")) + test(10.01, ans<-coerceAs(input, 1), 1, output="double[numeric] into double[numeric]") + test(10.02, address(input)!=address(ans)) + test(10.03, ans<-coerceAs(input, 1, copy=FALSE), 1, output="copy=false and input already of expected type and class double[numeric]") + test(10.04, address(input), address(ans)) + test(10.05, ans<-coerceAs(input, 1L), 1L, output="double[numeric] into integer[integer]") + test(10.06, address(input)!=address(ans)) + test(10.07, ans<-coerceAs(input, 1L, copy=FALSE), 1L, output="double[numeric] into integer[integer]", notOutput="copy=false") + test(10.08, address(input)!=address(ans)) + test(10.09, coerceAs("1", 1L), 1L, output="character[character] into integer[integer]", warning="Coercing.*character.*integer") + test(10.10, coerceAs("1", 1), 1, output="character[character] into double[numeric]", warning="Coercing.*character.*double") + test(10.11, coerceAs("a", factor("x")), factor("a", levels=c("x","a")), output="character[character] into integer[factor]") ## levels of 'as' are retained! + test(10.12, coerceAs("a", factor()), factor("a"), output="character[character] into integer[factor]") + test(10.13, coerceAs(1, factor("x")), factor("x"), output="double[numeric] into integer[factor]") + test(10.14, coerceAs(1, factor("x", levels=c("x","y"))), factor("x", levels=c("x","y")), output="double[numeric] into integer[factor]") + test(10.15, coerceAs(2, factor("x", levels=c("x","y"))), factor("y", levels=c("x","y")), output="double[numeric] into integer[factor]") + test(10.16, coerceAs(1:2, xy_factor), xy_factor, output="integer[integer] into integer[factor]") + test(10.17, coerceAs(1:3, xy_factor), output="integer[integer] into integer[factor]", error="factor numbers.*3 is outside the level range") + test(10.18, coerceAs(c(1,2,3), xy_factor), output="double[numeric] into integer[factor]", error="factor numbers.*3.000000 is outside the level range") + test(10.19, coerceAs(factor("x"), xy_factor), factor("x", levels=c("x","y")), output="integer[factor] into integer[factor]") + test(10.20, coerceAs(factor("x"), xy_factor, copy=FALSE), factor("x", levels=c("x","y")), output="input already of expected type and class") ## copy=F has copyMostAttrib + a = structure("a", class="a") + b = structure("b", class="b") + test(10.21, coerceAs(a, b), structure("a", class="b"), output="character[a] into character[b]") + a = structure(1L, class="a") + b = structure(2L, class="b") + test(10.22, coerceAs(a, b), structure(1L, class="b"), output="integer[a] into integer[b]") + a = structure(1, class="a") + b = structure(2, class="b") + test(10.23, coerceAs(a, b), structure(1, class="b"), output="double[a] into double[b]") + a = structure(1, class="a") + b = structure(2L, class="b") + test(10.24, coerceAs(a, b), structure(1L, class="b"), output="double[a] into integer[b]") + if (test_bit64) { + x = as.integer64(1L) + test(10.81, coerceAs(x, 1), 1, output="double[integer64] into double[numeric]") + test(10.82, coerceAs(x, 1L), 1L, output="double[integer64] into integer[integer]") + test(10.83, coerceAs(x, "1"), "1", output="double[integer64] into character[character]") + test(10.84, coerceAs(1, x), x, output="double[numeric] into double[integer64]") + test(10.85, coerceAs(1L, x), x, output="integer[integer] into double[integer64]") + test(10.86, coerceAs("1", x), x, output="character[character] into double[integer64]", warning="Coercing.*character") + 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'")) + 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'")) + test(10.89, options=c(datatable.verbose=2L), + coerceAs(-2147483649, x), as.integer64(-2147483649), output="double[numeric] into double[integer64]") + } + # 10.91 tested nanotime moved to other.Rraw 27.21, #6139 +}) -options(datatable.verbose=FALSE) test(11.01, coerceAs(list(a=1), 1), error="is not atomic") test(11.02, coerceAs(1, list(a=1)), list(1)) test(11.03, coerceAs(sum, 1), error="is not atomic") @@ -328,6 +331,4 @@ test(11.09, coerceAs(1L, a), error="must not be matrix or array") test(99.1, data.table(a=1,b=2)[1,1, verbose=1], error="verbose must be logical or integer") test(99.2, data.table(a=1,b=2)[1,1, verbose=1:2], error="verbose must be length 1 non-NA") test(99.3, data.table(a=1,b=2)[1,1, verbose=NA], error="verbose must be length 1 non-NA") -options(datatable.verbose=1) -test(99.4, coerceAs(1, 2L), error="verbose option must be length 1 non-NA logical or integer") -options(datatable.verbose=FALSE) +test(99.4, options=c(datatable.verbose=1), coerceAs(1, 2L), error="verbose option must be length 1 non-NA logical or integer") diff --git a/inst/tests/other.Rraw b/inst/tests/other.Rraw index 720ddf1945..f115ab0f3a 100644 --- a/inst/tests/other.Rraw +++ b/inst/tests/other.Rraw @@ -507,13 +507,12 @@ if (loaded[["xts"]]) { # was 1465 in tests.Rraw, #5516 # 890 -- key argument for as.data.table.xts x = xts(1:10, as.Date(1:10, origin = "1970-01-01")) - old = options(datatable.verbose=FALSE) - test(18.18, capture.output(as.data.table(x, key="index")), + test(18.18, options=c(datatable.verbose=FALSE), + capture.output(as.data.table(x, key="index")), c(" index V1", " 1: 1970-01-02 1", " 2: 1970-01-03 2", " 3: 1970-01-04 3", " 4: 1970-01-05 4", " 5: 1970-01-06 5", " 6: 1970-01-07 6", " 7: 1970-01-08 7", " 8: 1970-01-09 8", " 9: 1970-01-10 9", "10: 1970-01-11 10")) - options(old) # as.data.table.xts(foo) had incorrect integer index with a column name called 'x', #4897 M = xts::as.xts(matrix(1, dimnames=list("2021-05-23", "x"))) # xts:: just to be extra robust; shouldn't be needed with rm(as.xts) above @@ -528,23 +527,24 @@ if (loaded[["xts"]]) { # was 1465 in tests.Rraw, #5516 # was 2108 in tests.Rraw, #5516 # first and last should no longer load xts namespace, #3857, below commented test for interactive validation when xts present but not loaded or attached # stopifnot("xts"%in%installed.packages(), !isNamespaceLoaded("xts")); library(data.table); x=as.POSIXct("2019-01-01"); last(x); stopifnot(!isNamespaceLoaded("xts")) -x = as.POSIXct("2019-09-09")+0:1 -old = options(datatable.verbose=TRUE) -test(19.01, last(x), x[length(x)], output="!is.xts(x)") -test(19.02, first(x), x[1L], output="!is.xts(x)") -if (loaded[["xts"]]) { - xt = xts(1:2, x) - test(19.03, last(xt, 2L), xt, output="using xts::last: is.xts(x)") - test(19.04, first(xt, 2L), xt, output="using xts::first: is.xts(x)") - xt = xts(matrix(1:4, 2L, 2L), x) - test(19.05, last(xt, 2L), xt, output="using xts::last: is.xts(x)") - test(19.06, first(xt, 2L), xt, output="using xts::first: is.xts(x)") -} -# first on empty df now match head(df, n=1L), #3858 -df = data.frame(a=integer(), b=integer()) -test(19.11, first(df), df, output="!is.xts(x)") -test(19.12, last(df), df, output="!is.xts(x)") -options(datatable.verbose=FALSE) # so the as.data.table() doesn't pollute output +local({ + x = as.POSIXct("2019-09-09")+0:1 + old = options(datatable.verbose=TRUE); on.exit(options(old)) + test(19.01, last(x), x[length(x)], output="!is.xts(x)") + test(19.02, first(x), x[1L], output="!is.xts(x)") + if (loaded[["xts"]]) { + xt = xts(1:2, x) + test(19.03, last(xt, 2L), xt, output="using xts::last: is.xts(x)") + test(19.04, first(xt, 2L), xt, output="using xts::first: is.xts(x)") + xt = xts(matrix(1:4, 2L, 2L), x) + test(19.05, last(xt, 2L), xt, output="using xts::last: is.xts(x)") + test(19.06, first(xt, 2L), xt, output="using xts::first: is.xts(x)") + } + # first on empty df now match head(df, n=1L), #3858 + df = data.frame(a=integer(), b=integer()) + test(19.11, first(df), df, output="!is.xts(x)") + test(19.12, last(df), df, output="!is.xts(x)") +}) # xts last-first dispatch fix #4053 x = 1:3 y = as.POSIXct(x, origin="1970-01-01") @@ -560,75 +560,76 @@ xt = structure( index = structure(c(1167782400, 1167868800, 1167955200), tzone = "UTC", tclass = "Date"), .Dim = c(3L, 6L), .Dimnames = list(NULL, c("SPY.Open", "SPY.High", "SPY.Low", "SPY.Close", "SPY.Volume", "SPY.Adjusted")) ) -options(datatable.verbose=TRUE) -if (loaded[["xts"]]) { - test(19.21, last(x, n=2L), 2:3, output="using xts::last: !is.xts(x) & nargs>1 & 'package:xts'%in%search()") - test(19.22, last(y, n=2L), y[2:3], output="using xts::last: !is.xts(x) & nargs>1 & 'package:xts'%in%search()") - test(19.23, last(x, n=1L), 3L, output="using xts::last: !is.xts(x) & nargs>1 & 'package:xts'%in%search()") - test(19.24, last(y, n=1L), y[3L], output="using xts::last: !is.xts(x) & nargs>1 & 'package:xts'%in%search()") - xt_last = structure( - c(141.330002, 141.399994, 140.380005, 140.539993, 76645300, 108.360008), - class = c("xts", "zoo"), .indexCLASS = "Date", tclass = "Date", .indexTZ = "UTC", tzone = "UTC", - index = structure(1167955200, tzone = "UTC", tclass = "Date"), - .Dim = c(1L, 6L), .Dimnames = list(NULL, c("SPY.Open", "SPY.High", "SPY.Low", "SPY.Close", "SPY.Volume", "SPY.Adjusted")) - ) - xt_last2 = structure( - c(141.229996, 141.330002, 142.050003, 141.399994, 140.610001, 140.380005, - 141.669998, 140.539993, 69620600, 76645300, 109.231255, 108.360008), - class = c("xts", "zoo"), .indexCLASS = "Date", tclass = "Date", .indexTZ = "UTC", tzone = "UTC", - index = structure(c(1167868800, 1167955200), tzone = "UTC", tclass = "Date"), - .Dim = c(2L, 6L), .Dimnames = list(NULL, c("SPY.Open", "SPY.High", "SPY.Low", "SPY.Close", "SPY.Volume", "SPY.Adjusted")) - ) - test(19.25, last(xt), xt_last, output="using xts::last: is.xts(x)") - test(19.26, last(xt, n=2L), xt_last2, output="using xts::last: is.xts(x)") - test(19.31, first(x, n=2L), 1:2, output="using xts::first: !is.xts(x) & nargs>1 & 'package:xts'%in%search()") - test(19.32, first(y, n=2L), y[1:2], output="using xts::first: !is.xts(x) & nargs>1 & 'package:xts'%in%search()") - test(19.33, first(x, n=1L), 1L, output="using xts::first: !is.xts(x) & nargs>1 & 'package:xts'%in%search()") - test(19.34, first(y, n=1L), y[1L], output="using xts::first: !is.xts(x) & nargs>1 & 'package:xts'%in%search()") - xt_first = structure( - c(142.25, 142.860001, 140.570007, 141.369995, 94807600, 108.999954), - class = c("xts", "zoo"), .indexCLASS = "Date", tclass = "Date", .indexTZ = "UTC", tzone = "UTC", - index = structure(1167782400, tzone = "UTC", tclass = "Date"), - .Dim = c(1L, 6L), .Dimnames = list(NULL, c("SPY.Open", "SPY.High", "SPY.Low", "SPY.Close", "SPY.Volume", "SPY.Adjusted")) - ) - xt_first2 = structure( - c(142.25, 141.229996, 142.860001, 142.050003, 140.570007, 140.610001, 141.369995, 141.669998, 94807600, 69620600, 108.999954, 109.231255), - class = c("xts", "zoo"), .indexCLASS = "Date", tclass = "Date", .indexTZ = "UTC", tzone = "UTC", - index = structure(c(1167782400, 1167868800), tzone = "UTC", tclass = "Date"), - .Dim = c(2L, 6L), .Dimnames = list(NULL, c("SPY.Open", "SPY.High", "SPY.Low", "SPY.Close", "SPY.Volume", "SPY.Adjusted")) - ) - test(19.35, first(xt), xt_first, output="using xts::first: is.xts(x)") - test(19.36, first(xt, n=2L), xt_first2, output="using xts::first: is.xts(x)") -} else { - test(19.21, last(x, n=2L), 2:3, output="using utils::tail: !is.xts(x) & nargs>1 & !'package:xts'%in%search()") - test(19.22, last(y, n=2L), y[2:3], output="using utils::tail: !is.xts(x) & nargs>1 & !'package:xts'%in%search()") - test(19.23, last(x, n=1L), 3L, output="using utils::tail: !is.xts(x) & nargs>1 & !'package:xts'%in%search()") - test(19.24, last(y, n=1L), y[3L], output="using utils::tail: !is.xts(x) & nargs>1 & !'package:xts'%in%search()") - test(19.25, last(xt), error="you should have 'xts' installed already") - test(19.26, last(xt, n=2L), error="you should have 'xts' installed already") - test(19.31, first(x, n=2L), 1:2, output="using utils::head: !is.xts(x) & nargs>1 & !'package:xts'%in%search()") - test(19.32, first(y, n=2L), y[1:2], output="using utils::head: !is.xts(x) & nargs>1 & !'package:xts'%in%search()") - test(19.33, first(x, n=1L), 1L, output="using utils::head: !is.xts(x) & nargs>1 & !'package:xts'%in%search()") - test(19.34, first(y, n=1L), y[1L], output="using utils::head: !is.xts(x) & nargs>1 & !'package:xts'%in%search()") - test(19.35, first(xt), error="you should have 'xts' installed already") - test(19.36, first(xt, n=2L), error="you should have 'xts' installed already") -} -test(19.41, last(x), 3L, output="using 'x[[length(x)]]': !is.xts(x) & !nargs>1 & is.null(dim(x))") -test(19.42, last(y), y[3L], output="using 'x[[length(x)]]': !is.xts(x) & !nargs>1 & is.null(dim(x))") -test(19.51, first(x), 1L, output="using 'x[[1L]]': !is.xts(x) & !nargs>1 & is.null(dim(x))") -test(19.52, first(y), y[1L], output="using 'x[[1L]]': !is.xts(x) & !nargs>1 & is.null(dim(x))") -test(19.61, last(df), structure(list(a=2L, b=2L), row.names=2L, class="data.frame"), output="using 'x[nrow(x),]': !is.xts(x) & !nargs>1 & is.data.frame(x)") -test(19.62, last(dt), data.table(a=2L, b=2L), output="using 'x[nrow(x),]': !is.xts(x) & !nargs>1 & is.data.frame(x)") -test(19.71, first(df), structure(list(a=1L, b=3L), row.names=1L, class="data.frame"), output="using 'x[1L,]': !is.xts(x) & !nargs>1 & is.data.frame(x)") -test(19.72, first(dt), data.table(a=1L, b=3L), output="using 'x[1L,]': !is.xts(x) & !nargs>1 & is.data.frame(x)") -# matrix/array utils::tail behavior is likely to change in future R, Michael is more in the topic -test(19.81, last(mx), structure(c(3L, 6L, 9L), .Dim = c(1L, 3L), .Dimnames = list("[3,]", NULL)), output="using utils::tail: !is.xts(x) & !nargs>1 & !is.null(dim(x)) & !is.data.frame(x)") -expected = if (base::getRversion() < "3.7.0") 27L else structure(c(3L, 6L, 9L, 12L, 15L, 18L, 21L, 24L, 27L), .Dim = c(1L, 3L, 3L), .Dimnames = list("[3,]", NULL, NULL)) #4127 -test(19.82, last(ar), expected, output="using utils::tail: !is.xts(x) & !nargs>1 & !is.null(dim(x)) & !is.data.frame(x)") -test(19.91, first(mx), structure(c(1L, 4L, 7L), .Dim = c(1L, 3L)), output="using utils::head: !is.xts(x) & !nargs>1 & !is.null(dim(x)) & !is.data.frame(x)") -expected = if (base::getRversion() < "3.7.0") 1L else structure(c(1L, 4L, 7L, 10L, 13L, 16L, 19L, 22L, 25L), .Dim = c(1L, 3L, 3L)) #4127 -test(19.92, first(ar), expected, output="using utils::head: !is.xts(x) & !nargs>1 & !is.null(dim(x)) & !is.data.frame(x)") -options(old) +local({ + old = options(datatable.verbose=TRUE); on.exit(options(old)) + if (loaded[["xts"]]) { + test(19.21, last(x, n=2L), 2:3, output="using xts::last: !is.xts(x) & nargs>1 & 'package:xts'%in%search()") + test(19.22, last(y, n=2L), y[2:3], output="using xts::last: !is.xts(x) & nargs>1 & 'package:xts'%in%search()") + test(19.23, last(x, n=1L), 3L, output="using xts::last: !is.xts(x) & nargs>1 & 'package:xts'%in%search()") + test(19.24, last(y, n=1L), y[3L], output="using xts::last: !is.xts(x) & nargs>1 & 'package:xts'%in%search()") + xt_last = structure( + c(141.330002, 141.399994, 140.380005, 140.539993, 76645300, 108.360008), + class = c("xts", "zoo"), .indexCLASS = "Date", tclass = "Date", .indexTZ = "UTC", tzone = "UTC", + index = structure(1167955200, tzone = "UTC", tclass = "Date"), + .Dim = c(1L, 6L), .Dimnames = list(NULL, c("SPY.Open", "SPY.High", "SPY.Low", "SPY.Close", "SPY.Volume", "SPY.Adjusted")) + ) + xt_last2 = structure( + c(141.229996, 141.330002, 142.050003, 141.399994, 140.610001, 140.380005, + 141.669998, 140.539993, 69620600, 76645300, 109.231255, 108.360008), + class = c("xts", "zoo"), .indexCLASS = "Date", tclass = "Date", .indexTZ = "UTC", tzone = "UTC", + index = structure(c(1167868800, 1167955200), tzone = "UTC", tclass = "Date"), + .Dim = c(2L, 6L), .Dimnames = list(NULL, c("SPY.Open", "SPY.High", "SPY.Low", "SPY.Close", "SPY.Volume", "SPY.Adjusted")) + ) + test(19.25, last(xt), xt_last, output="using xts::last: is.xts(x)") + test(19.26, last(xt, n=2L), xt_last2, output="using xts::last: is.xts(x)") + test(19.31, first(x, n=2L), 1:2, output="using xts::first: !is.xts(x) & nargs>1 & 'package:xts'%in%search()") + test(19.32, first(y, n=2L), y[1:2], output="using xts::first: !is.xts(x) & nargs>1 & 'package:xts'%in%search()") + test(19.33, first(x, n=1L), 1L, output="using xts::first: !is.xts(x) & nargs>1 & 'package:xts'%in%search()") + test(19.34, first(y, n=1L), y[1L], output="using xts::first: !is.xts(x) & nargs>1 & 'package:xts'%in%search()") + xt_first = structure( + c(142.25, 142.860001, 140.570007, 141.369995, 94807600, 108.999954), + class = c("xts", "zoo"), .indexCLASS = "Date", tclass = "Date", .indexTZ = "UTC", tzone = "UTC", + index = structure(1167782400, tzone = "UTC", tclass = "Date"), + .Dim = c(1L, 6L), .Dimnames = list(NULL, c("SPY.Open", "SPY.High", "SPY.Low", "SPY.Close", "SPY.Volume", "SPY.Adjusted")) + ) + xt_first2 = structure( + c(142.25, 141.229996, 142.860001, 142.050003, 140.570007, 140.610001, 141.369995, 141.669998, 94807600, 69620600, 108.999954, 109.231255), + class = c("xts", "zoo"), .indexCLASS = "Date", tclass = "Date", .indexTZ = "UTC", tzone = "UTC", + index = structure(c(1167782400, 1167868800), tzone = "UTC", tclass = "Date"), + .Dim = c(2L, 6L), .Dimnames = list(NULL, c("SPY.Open", "SPY.High", "SPY.Low", "SPY.Close", "SPY.Volume", "SPY.Adjusted")) + ) + test(19.35, first(xt), xt_first, output="using xts::first: is.xts(x)") + test(19.36, first(xt, n=2L), xt_first2, output="using xts::first: is.xts(x)") + } else { + test(19.21, last(x, n=2L), 2:3, output="using utils::tail: !is.xts(x) & nargs>1 & !'package:xts'%in%search()") + test(19.22, last(y, n=2L), y[2:3], output="using utils::tail: !is.xts(x) & nargs>1 & !'package:xts'%in%search()") + test(19.23, last(x, n=1L), 3L, output="using utils::tail: !is.xts(x) & nargs>1 & !'package:xts'%in%search()") + test(19.24, last(y, n=1L), y[3L], output="using utils::tail: !is.xts(x) & nargs>1 & !'package:xts'%in%search()") + test(19.25, last(xt), error="you should have 'xts' installed already") + test(19.26, last(xt, n=2L), error="you should have 'xts' installed already") + test(19.31, first(x, n=2L), 1:2, output="using utils::head: !is.xts(x) & nargs>1 & !'package:xts'%in%search()") + test(19.32, first(y, n=2L), y[1:2], output="using utils::head: !is.xts(x) & nargs>1 & !'package:xts'%in%search()") + test(19.33, first(x, n=1L), 1L, output="using utils::head: !is.xts(x) & nargs>1 & !'package:xts'%in%search()") + test(19.34, first(y, n=1L), y[1L], output="using utils::head: !is.xts(x) & nargs>1 & !'package:xts'%in%search()") + test(19.35, first(xt), error="you should have 'xts' installed already") + test(19.36, first(xt, n=2L), error="you should have 'xts' installed already") + } + test(19.41, last(x), 3L, output="using 'x[[length(x)]]': !is.xts(x) & !nargs>1 & is.null(dim(x))") + test(19.42, last(y), y[3L], output="using 'x[[length(x)]]': !is.xts(x) & !nargs>1 & is.null(dim(x))") + test(19.51, first(x), 1L, output="using 'x[[1L]]': !is.xts(x) & !nargs>1 & is.null(dim(x))") + test(19.52, first(y), y[1L], output="using 'x[[1L]]': !is.xts(x) & !nargs>1 & is.null(dim(x))") + test(19.61, last(df), structure(list(a=2L, b=2L), row.names=2L, class="data.frame"), output="using 'x[nrow(x),]': !is.xts(x) & !nargs>1 & is.data.frame(x)") + test(19.62, last(dt), data.table(a=2L, b=2L), output="using 'x[nrow(x),]': !is.xts(x) & !nargs>1 & is.data.frame(x)") + test(19.71, first(df), structure(list(a=1L, b=3L), row.names=1L, class="data.frame"), output="using 'x[1L,]': !is.xts(x) & !nargs>1 & is.data.frame(x)") + test(19.72, first(dt), data.table(a=1L, b=3L), output="using 'x[1L,]': !is.xts(x) & !nargs>1 & is.data.frame(x)") + # matrix/array utils::tail behavior is likely to change in future R, Michael is more in the topic + test(19.81, last(mx), structure(c(3L, 6L, 9L), .Dim = c(1L, 3L), .Dimnames = list("[3,]", NULL)), output="using utils::tail: !is.xts(x) & !nargs>1 & !is.null(dim(x)) & !is.data.frame(x)") + expected = if (base::getRversion() < "3.7.0") 27L else structure(c(3L, 6L, 9L, 12L, 15L, 18L, 21L, 24L, 27L), .Dim = c(1L, 3L, 3L), .Dimnames = list("[3,]", NULL, NULL)) #4127 + test(19.82, last(ar), expected, output="using utils::tail: !is.xts(x) & !nargs>1 & !is.null(dim(x)) & !is.data.frame(x)") + test(19.91, first(mx), structure(c(1L, 4L, 7L), .Dim = c(1L, 3L)), output="using utils::head: !is.xts(x) & !nargs>1 & !is.null(dim(x)) & !is.data.frame(x)") + expected = if (base::getRversion() < "3.7.0") 1L else structure(c(1L, 4L, 7L, 10L, 13L, 16L, 19L, 22L, 25L), .Dim = c(1L, 3L, 3L)) #4127 + test(19.92, first(ar), expected, output="using utils::head: !is.xts(x) & !nargs>1 & !is.null(dim(x)) & !is.data.frame(x)") +}) if (loaded[["xts"]]) { # was 2133 in tests.Rraw, #5516 # keep.rownames in as.data.table.xts() supports a string, #4232 @@ -681,11 +682,12 @@ if (loaded[["nanotime"]]) { # was 2080.01-05 in tests.Rraw, #5516 n = nanotime(1:4) n[2L] = NA - opt = options(datatable.verbose=TRUE) - test(24.1, between(n, nanotime(2), nanotime(10)), c(FALSE, NA, TRUE, TRUE), output="between parallel processing of integer64") - test(24.2, between(n, nanotime(3), nanotime(10), incbounds=FALSE), c(FALSE, NA, FALSE, TRUE), output="between parallel processing of integer64") - test(24.3, between(n, nanotime(3), nanotime(NA), incbounds=FALSE, NAbounds=NA), c(FALSE, NA, FALSE, NA), output="between parallel processing of integer64") - options(opt) + local({ + old = options(datatable.verbose=TRUE); on.exit(options(old)) + test(24.1, between(n, nanotime(2), nanotime(10)), c(FALSE, NA, TRUE, TRUE), output="between parallel processing of integer64") + test(24.2, between(n, nanotime(3), nanotime(10), incbounds=FALSE), c(FALSE, NA, FALSE, TRUE), output="between parallel processing of integer64") + test(24.3, between(n, nanotime(3), nanotime(NA), incbounds=FALSE, NAbounds=NA), c(FALSE, NA, FALSE, NA), output="between parallel processing of integer64") + }) test(24.4, between(1:10, nanotime(3), nanotime(6)), error="x is not integer64 but.*Please align classes") test(24.5, between(1:10, 3, nanotime(6)), error="x is not integer64 but.*Please align classes") @@ -718,14 +720,17 @@ if (loaded[["nanotime"]]) { test(27.14, lapply(nafill(l, fill=nanotime(0)), as.character), lapply(list(a=nanotime(c(1:2,0L,4:5)), b=nanotime(c(0L,2L,0L,4L,0L))), as.character)) # coerceAs verbose. Was 10.91-10.96 in nafill.Rraw, #6139 - x = nanotime(1L) - test(27.21, options=c(datatable.verbose=2L), coerceAs(x, 1), 1, output="double[nanotime] into double[numeric]") - test(27.22, options=c(datatable.verbose=2L), coerceAs(x, 1L), 1L, output="double[nanotime] into integer[integer]") - test(27.23, options=c(datatable.verbose=2L), substring(coerceAs(x, "1"),1,11) %in% c("1","1970-01-01T"), output="double[nanotime] into character[character]") - # ^ https://github.com/eddelbuettel/nanotime/issues/92; %in% so as not to break if nanotime adds as.character method - test(27.24, options=c(datatable.verbose=2L), coerceAs(1, x), x, output="double[numeric] into double[nanotime]") - test(27.25, options=c(datatable.verbose=2L), coerceAs(1L, x), x, output="integer[integer] into double[nanotime]") - test(27.26, options=c(datatable.verbose=2L), coerceAs("1", x), x, output="character[character] into double[nanotime]", warning="Coercing.*character") + local({ + x = nanotime(1L) + old = options(datatable.verbose=2L); on.exit(options(old)) + test(27.21, coerceAs(x, 1), 1, output="double[nanotime] into double[numeric]") + test(27.22, coerceAs(x, 1L), 1L, output="double[nanotime] into integer[integer]") + test(27.23, substring(coerceAs(x, "1"), 1, 11) %in% c("1", "1970-01-01T"), output="double[nanotime] into character[character]") + # ^ https://github.com/eddelbuettel/nanotime/issues/92; %in% so as not to break if nanotime adds as.character method + test(27.24, coerceAs(1, x), x, output="double[numeric] into double[nanotime]") + test(27.25, coerceAs(1L, x), x, output="integer[integer] into double[nanotime]") + test(27.26, coerceAs("1", x), x, output="character[character] into double[nanotime]", warning="Coercing.*character") + }) } # that plot works; moved from tests.Rraw 167 to here to save ram of loading graphics package and possible screen device issues on overloaded servers, #5517