From 7c73e5b5f8650d602c04368089ac61919258b9e2 Mon Sep 17 00:00:00 2001 From: Michael Chirico Date: Tue, 23 Jan 2018 12:40:47 +0800 Subject: [PATCH] more tests --- R/test.data.table.R | 17 ++++++++++------- inst/tests/tests.Rraw | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 7 deletions(-) diff --git a/R/test.data.table.R b/R/test.data.table.R index 4714e351d..1cb3b1ca0 100644 --- a/R/test.data.table.R +++ b/R/test.data.table.R @@ -3,8 +3,8 @@ test.data.table <- function(verbose=FALSE, pkg="pkg", silent=FALSE) { if (exists("test.data.table",.GlobalEnv,inherits=FALSE)) { # package developer if ("package:data.table" %in% search()) stop("data.table package is loaded. Unload or start a fresh R session.") - d = if (pkg %in% dir()) paste0(getwd(),"/",pkg) else Sys.getenv("CC_DIR") - d = paste0(d, "/inst/tests") + d = if (pkg %in% dir()) file.path(getwd(), pkg) else Sys.getenv("CC_DIR") + d = file.path(d, "inst/tests") } else { # R CMD check and user running test.data.table() d = paste0(getNamespaceInfo("data.table","path"),"/tests") @@ -40,13 +40,16 @@ test.data.table <- function(verbose=FALSE, pkg="pkg", silent=FALSE) { # whichfail = NULL # .devtesting = TRUE -compactprint <- function(DT, topn=2) { +# essentially toString.default +makeString = function (x) paste(x, collapse = ",") + +compactprint <- function(DT, topn=2L) { tt = vapply_1c(DT,function(x)class(x)[1L]) tt[tt=="integer64"] = "i64" - cn = paste(" [Key=",paste(key(DT),collapse=","), - " Types=",paste(substring(sapply(DT,typeof),1,3),collapse=","), - " Classes=",paste(substring(tt,1,3),collapse=","), - "]",sep="") + tt = substring(tt, 1L, 3L) + cn = paste0(" [Key=",makeString(key(DT)), + " Types=", makeString(substring(sapply(DT, typeof), 1L, 3L)), + " Classes=", makeString(tt), "]") if (nrow(DT)) { print(copy(DT)[,(cn):=""], topn=topn) } else { diff --git a/inst/tests/tests.Rraw b/inst/tests/tests.Rraw index 0e3dd3e01..3e3431656 100644 --- a/inst/tests/tests.Rraw +++ b/inst/tests/tests.Rraw @@ -11376,6 +11376,41 @@ test(1869.1, data.table(a = 1:4, short_s4_col), error = 'problem recycling.*try ## i must be a data.table when on is specified DT = data.table(a = 1:3) test(1869.2, DT[c(TRUE, FALSE), on = 'coefficients'], error = "not a data.table, but 'on'") +## missing tests for round.IDate +test_dates = c( + "2017-01-05", "2017-08-04", "2017-06-05", "2017-04-15", + "2017-06-11", "2017-10-04", "2017-04-19", "2017-01-11", + "2017-03-08", "2017-10-10" +) +test_dates = as.IDate(test_dates) +test(1869.3, round(test_dates, 'weeks'), + structure(c(17167L, 17377L, 17321L, 17272L, 17328L, + 17440L, 17272L, 17174L, 17230L, 17447L), + class = c("IDate", "Date"))) +test(1869.4, round(test_dates, 'months'), + structure(c(17167L, 17379L, 17318L, 17257L, 17318L, + 17440L, 17257L, 17167L, 17226L, 17440L), + class = c("IDate", "Date"))) +test(1869.5, round(test_dates, 'quarters'), + structure(c(17167L, 17348L, 17257L, 17257L, 17257L, + 17440L, 17257L, 17167L, 17167L, 17440L), + class = c("IDate", "Date"))) +test(1869.6, round(test_dates, 'years'), + structure(c(17167L, 17167L, 17167L, 17167L, 17167L, + 17167L, 17167L, 17167L, 17167L, 17167L), + class = c("IDate", "Date"))) +test(1869.7, round(test_dates, 'centuries'), + error = 'should be one of') +## missing a test of mday +test(1869.8, mday(test_dates), + c(5L, 4L, 5L, 15L, 11L, 4L, 19L, 11L, 8L, 10L)) +## META TEST of helper function compactprint from test.data.table +DT = data.table(a = 1, b = 2, key = 'a') +DT_out = gsub('\\s+$', '', capture.output(compactprint(DT))) +test(1869.9, DT_out, + c(" a b [Key=a Types=dou,dou Classes=num,num]", + "1: 1 2")) + ##########################