Skip to content

Commit

Permalink
Extend coverage for type checks
Browse files Browse the repository at this point in the history
  • Loading branch information
D-Se committed Nov 29, 2023
1 parent 9b3501c commit 838cc00
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 6 deletions.
4 changes: 3 additions & 1 deletion inst/tinytest/test_ifelse.R
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,6 @@ error(c(NA, NA) ? 1 ~ "a", "Type")
# misc
(TRUE ? lst[[2]] ~ lst[[3]]) := 2
num = "hi"
(num ? num) := FALSE
(num ? num) := FALSE

# new types
20 changes: 20 additions & 0 deletions inst/tinytest/test_isas.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,26 @@ for (i in seq_len(nrow(ask_is))) {
}
}

error(1 ? huh, "Abbreviation")
vals = list(
1, 1+0i, raw(1), ts(1), factor(1), ordered(1), table(1),
mtcars, mtcars, new.env(), c(1, 2), matrix(1), array(1:3, 4:6),
lapply, expression(1+1), getClass("MethodDefinition")
)
abbs2 = c(
"dbl", "cpl", "raw", "ts", "fct", "ord", "tab",
"df", "dfr", "env", "vec", "mtx", "arr",
"fun", "exp", "s4"
)
for (i in seq_along(vals)) {
eval(substitute(x ? y, list(x = vals[[i]], y = as.name(abbs2[i])))) := TRUE
}

# misc type check
(as.name("x") ? sym) := TRUE
(call("f") ? lng) := TRUE
(x ~ y ? fml) := TRUE

#### Type coercion ####
as_names <- paste0("as.", names)
base_as <- vapply(paste0("as.", names), function(fun) {
Expand Down
2 changes: 1 addition & 1 deletion man/ask.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
\alias{ask}
\title{Control \code{ask} package behavior in a session.}
\description{
OpenMP parallelisation powers vectorised-if. By default 50\% of available cores
OpenMP parallelisation powers ternary `?`. By default 50\% of available cores
are used, with as many threads.
}
\usage{
Expand Down
8 changes: 4 additions & 4 deletions src/isas.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ ABB str2abb(S abb) {
if (!strcmp(s, AbbCheckTable[i].str))
return (ABB) AbbCheckTable[i].abb;
}
err("Abbreviation not found"); // x ?~ bla
err("Abbreviation not found"); // x ? bla
}
S is(S x, S fml, bool negate) {
bool ans = false;
Expand Down Expand Up @@ -77,8 +77,8 @@ S is(S x, S fml, bool negate) {
//case ATMABB: ans = isAtomic(x); break;
// third-party
// case TBLABB: Rf_inherits(x, "tbl"); break;
case ANYABB:
default: ans = TYPEOF(x) == str2sexp(fml);
// case ANYABB: for completeness, but only in C API
default: ans = TYPEOF(x) == str2sexp(fml); // #nocov should never be reached
};
if(negate) ans = !ans;
return Rf_ScalarLogical(ans);
Expand All @@ -96,4 +96,4 @@ S isas(S x, S fml) {
}
default: return Rf_ScalarLogical(0); // NULL ? NULL opens help, always false
}
}
}

0 comments on commit 838cc00

Please sign in to comment.