Skip to content

Commit

Permalink
between unit tests, minor reformat
Browse files Browse the repository at this point in the history
  • Loading branch information
jangorecki committed May 1, 2019
1 parent e18b095 commit 1259b66
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
16 changes: 8 additions & 8 deletions R/between.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,28 @@ between <- function(x,lower,upper,incbounds=TRUE) {
# POSIX special handling to auto coerce character
if (is.px(x) && !is.null(tz<-attr(x, "tzone", TRUE)) && nzchar(tz) &&
(is.character(lower) || is.character(upper))) {
try_posix_cast <- function(x, tz) tryCatch(
try_posix_cast <- function(x, tz) {tryCatch(
list(status=0L, value=as.POSIXct(x, tz = tz)),
error = function(e) list(status=1L, value=NULL, message=e[["message"]])
)
)}
if (is.character(lower)) {
ans = try_posix_cast(lower, tz)
if (ans$status==0L) lower = ans$value
else stop("'between' function the 'x' argument is a POSIX class while 'lower' was not, coercion to POSIX failed with:", ans$message)
else stop("'between' function the 'x' argument is a POSIX class while 'lower' was not, coercion to POSIX failed with: ", ans$message)
}
if (is.character(upper)) {
ans = try_posix_cast(upper, tz)
if (ans$status==0L) upper = ans$value
else stop("'between' function the 'x' argument is a POSIX class while 'upper' was not, coercion to POSIX failed with:", ans$message)
else stop("'between' function the 'x' argument is a POSIX class while 'upper' was not, coercion to POSIX failed with: ", ans$message)
}
stopifnot(is.px(x), is.px(lower), is.px(upper)) # nocov # internal
}
# POSIX check time zone match
if (is.px(x) && is.px(lower) && is.px(upper)) {
tz_match = function(x, y, z) ( # NULL match "", else all identical
((is.null(x) || !nzchar(x)) && (is.null(y) || !nzchar(y)) && (is.null(z) || !nzchar(z)))
|| (identical(x, y) && identical(x, z))
)
tz_match = function(x, y, z) { # NULL match "", else all identical
((is.null(x) || !nzchar(x)) && (is.null(y) || !nzchar(y)) && (is.null(z) || !nzchar(z))) ||
(identical(x, y) && identical(x, z))
}
if (!tz_match(attr(x, "tzone", TRUE), attr(lower, "tzone", TRUE), attr(upper, "tzone", TRUE))) {
stop("'between' function arguments have mismatched timezone attribute, align all arguments to same timezone")
}
Expand Down
22 changes: 18 additions & 4 deletions inst/tests/tests.Rraw
Original file line number Diff line number Diff line change
Expand Up @@ -14151,22 +14151,36 @@ up = as.POSIXct('2016-09-18 09:00:00')
test(2032.11, between(x, dn, up), output="between parallel processing of double using closed bounds with recycling took")
test(2032.12, between(x, dn, up, incbounds=FALSE), output="between parallel processing of double using open bounds with recycling took")

## also handling of string lower/upper bounds _only when x has a time zone_
# also handling of string lower/upper bounds _only when x has a time zone_
x = as.POSIXct("2016-09-18 07:00:00") + 0:10*60*15
dn = '2016-09-18 08:00:00'
up = '2016-09-18 09:00:00'
test(2032.13, between(x, dn, up), output = 'optimised between not available')
attr(x, 'tzone') = 'UTC'
test(2032.14, between(x, dn, up), output = 'between parallel processing of double')

### additional flexibility -- cast when one bound is already POSIXct
# additional flexibility -- cast when one bound is already POSIXct
up = as.POSIXct(up, tz="UTC")
test(2032.15, between(x, dn, up), output = 'between parallel processing of double')
options(old)

# exceptions in char to POSIX coercion
dn = 'aa2016-09-18 08:00:00'
test(2032.16, between(x, dn, up), error="coercion to POSIX failed")
dn = '2016-09-18 08:00:00'
up = 'bb2016-09-18 09:00:00'
test(2032.17, between(x, dn, up), error="coercion to POSIX failed")

# exceptions due to timezone mismatch
x = as.POSIXct("2016-09-18 07:00:00", tz="UTC") + 0:10*60*15
dn = as.POSIXct('2016-09-18 08:00:00')
up = '2016-09-18 09:00:00'
test(2032.18, between(x, dn, up), error="mismatched timezone attribute")

# `between` support `.` in RHS #2315
X = data.table(a = 1:5, b = 6:10, c = c(5:1))
test(2032.16, X[c %between% list(a, b)], X[c %between% .(a, b)])
options(old)
test(2032.19, X[c %between% list(a, b)], X[c %between% .(a, b)])



###################################
Expand Down

0 comments on commit 1259b66

Please sign in to comment.