diff --git a/inst/tests/tests.Rraw b/inst/tests/tests.Rraw index 6bb1def697..8fdeb6d1eb 100644 --- a/inst/tests/tests.Rraw +++ b/inst/tests/tests.Rraw @@ -67,6 +67,19 @@ if (exists("test.data.table", .GlobalEnv, inherits=FALSE)) { which.first = data.table:::which.first which.last = data.table:::which.last `-.IDate` = data.table:::`-.IDate` + gsum = data.table:::gsum + gmean = data.table:::gmean + gvar = data.table:::gvar + gsd = data.table:::gsd + gmedian = data.table:::gmedian + gprod = data.table:::gprod + gmin = data.table:::gmin + gmax = data.table:::gmax + `g[` = data.table:::`g[` + gfirst = data.table:::gfirst + glast = data.table:::glast + ghead = data.table:::ghead + gtail = data.table:::gtail # Also, for functions that are masked by other packages, we need to map the data.table one. Or else, # the other package's function would be picked up. As above, we only need to do this because we desire @@ -18260,8 +18273,13 @@ for (fun in funs) { test(testnum, EVAL("DT[,",fun,"(i, na.rm='a'), g]"), error="na.rm must be TRUE or FALSE") testnum = testnum + 0.01 test(testnum, EVAL("DT[,",fun,"(f), g]"), error=sprintf("%s is not meaningful for factors.", fun)) + # directly calling gfuns triggers the nrow != length error + testnum = testnum + 0.01 + test(testnum, EVAL("DT[,g",fun,"(i), g]"), error=sprintf("nrow [6] != length(x) [3] in g%s",fun)) } -test(testnum+0.01, DT[, prod(l), g], error="GForce prod can only be applied to columns, not .SD or similar.") +# outside of loop because gfirst has no na.rm argument +test(testnum+0.01, DT[, gfirst(i), g], error="nrow [6] != length(x) [3] in gfirst") +test(testnum+0.02, DT[, prod(l), g], error="GForce prod can only be applied to columns, not .SD or similar.") # tables() error when called from inside a function(...), #5197 test(2221, (function(...) tables())(), output = "No objects of class data.table exist") diff --git a/src/gsumm.c b/src/gsumm.c index 5bb2620243..9d8b069dec 100644 --- a/src/gsumm.c +++ b/src/gsumm.c @@ -348,7 +348,8 @@ SEXP gsum(SEXP x, SEXP narmArg) double started = wallclock(); const bool verbose=GetVerbose(); if (verbose) Rprintf(_("This gsum (narm=%s) took ... "), narm?"TRUE":"FALSE"); - if (nrow != n) error(_("nrow [%d] != length(x) [%d] in %s"), nrow, n, "gsum"); + if (nrow != n) + error(_("nrow [%d] != length(x) [%d] in %s"), nrow, n, "gsum"); bool anyNA=false; SEXP ans; switch(TYPEOF(x)) { @@ -582,7 +583,8 @@ SEXP gmean(SEXP x, SEXP narmArg) double started = wallclock(); const bool verbose=GetVerbose(); if (verbose) Rprintf(_("This gmean took (narm=%s) ... "), narm?"TRUE":"FALSE"); // narm=TRUE only at this point - if (nrow != n) error(_("nrow [%d] != length(x) [%d] in %s"), nrow, n, "gmean"); + if (nrow != n) + error(_("nrow [%d] != length(x) [%d] in %s"), nrow, n, "gmean"); bool anyNA=false; SEXP ans=R_NilValue; int protecti=0; @@ -728,7 +730,8 @@ static SEXP gminmax(SEXP x, SEXP narm, const bool min) const int n = nosubset ? length(x) : irowslen; //clock_t start = clock(); SEXP ans; - if (nrow != n) error(_("nrow [%d] != length(x) [%d] in %s"), nrow, n, "gminmax"); + if (nrow != n) + error(_("nrow [%d] != length(x) [%d] in %s"), nrow, n, min?"gmin":"gmax"); // GForce guarantees each group has at least one value; i.e. we don't need to consider length-0 per group here switch(TYPEOF(x)) { case LGLSXP: case INTSXP: { @@ -863,7 +866,8 @@ SEXP gmedian(SEXP x, SEXP narmArg) { error(_("%s is not meaningful for factors."), "median"); const bool isInt64 = INHERITS(x, char_integer64), narm = LOGICAL(narmArg)[0]; const int n = (irowslen == -1) ? length(x) : irowslen; - if (nrow != n) error(_("nrow [%d] != length(x) [%d] in %s"), nrow, n, "gmedian"); + if (nrow != n) + error(_("nrow [%d] != length(x) [%d] in %s"), nrow, n, "gmedian"); SEXP ans = PROTECT(allocVector(REALSXP, ngrp)); double *ansd = REAL(ans); const bool nosubset = irowslen==-1; @@ -915,7 +919,8 @@ static SEXP gfirstlast(SEXP x, const bool first, const int w, const bool headw) const bool nosubset = irowslen == -1; const bool issorted = !isunsorted; // make a const-bool for use inside loops const int n = nosubset ? length(x) : irowslen; - if (nrow != n) error(_("nrow [%d] != length(x) [%d] in %s"), nrow, n, first?"gfirst":"glast"); + if (nrow != n) + error(_("nrow [%d] != length(x) [%d] in %s"), nrow, n, first?"gfirst":"glast"); if (w==1 && headw) error(_("Internal error: gfirstlast headw should only be true when w>1")); int anslen = ngrp; if (headw) { @@ -1018,7 +1023,8 @@ static SEXP gvarsd1(SEXP x, SEXP narmArg, bool isSD) if (inherits(x, "factor")) error(_("%s is not meaningful for factors."), isSD ? "sd" : "var"); const int n = (irowslen == -1) ? length(x) : irowslen; - if (nrow != n) error(_("nrow [%d] != length(x) [%d] in %s"), nrow, n, "gvar"); + if (nrow != n) + error(_("nrow [%d] != length(x) [%d] in %s"), nrow, n, isSD?"gsd":"gvar"); SEXP sub, ans = PROTECT(allocVector(REALSXP, ngrp)); double *ansd = REAL(ans); const bool nosubset = irowslen==-1; @@ -1115,7 +1121,8 @@ SEXP gprod(SEXP x, SEXP narmArg) { const int n = nosubset ? length(x) : irowslen; //clock_t start = clock(); SEXP ans; - if (nrow != n) error(_("nrow [%d] != length(x) [%d] in %s"), nrow, n, "gprod"); + if (nrow != n) + error(_("nrow [%d] != length(x) [%d] in %s"), nrow, n, "gprod"); long double *s = malloc(ngrp * sizeof(long double)); if (!s) error(_("Unable to allocate %d * %d bytes for gprod"), ngrp, sizeof(long double)); for (int i=0; i