diff --git a/inst/tests/tests.Rraw b/inst/tests/tests.Rraw index c349b99daf..6255f4f843 100644 --- a/inst/tests/tests.Rraw +++ b/inst/tests/tests.Rraw @@ -18249,3 +18249,16 @@ DT = data.table(A=letters[1:3]) test(2219.1, DT[2, A:=as.IDate("2021-02-03")], data.table(A=c("a","2021-02-03","c"))) if (test_bit64) test(2219.2, DT[3, A:=as.integer64("4611686018427387906")], data.table(A=c("a","2021-02-03","4611686018427387906"))) +# gforce improve coverage +DT = data.table(g=1:2, i=c(NA, 1:4, NA), f=factor(letters[1:6]), l=as.list(1:6)) +options(datatable.optimize = 2L) +funs = c("sum", "mean", "min", "max", "median", "var", "sd", "prod") +testnum = 2220 +for (fun in funs) { + testnum = testnum + 0.01 + 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)) +} +test(testnum+0.01, DT[, prod(l), g], error="GForce prod can only be applied to columns, not .SD or similar.") + diff --git a/src/gsumm.c b/src/gsumm.c index be3b0f7855..5bb2620243 100644 --- a/src/gsumm.c +++ b/src/gsumm.c @@ -348,8 +348,7 @@ 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)) { @@ -729,8 +728,7 @@ 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, "gminmax"); // 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: { @@ -865,8 +863,7 @@ 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; @@ -1021,8 +1018,7 @@ 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, "gvar"); SEXP sub, ans = PROTECT(allocVector(REALSXP, ngrp)); double *ansd = REAL(ans); const bool nosubset = irowslen==-1; @@ -1119,8 +1115,7 @@ 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