7:0
 )�    # unit tests for the following 'xts' methods:
# rbind
# cbind
#
info_msg <- "test.rbind_zero_length_non_zero_length_POSIXct_errors"
xpz <- xts( , as.POSIXct("2017-01-01"))
xp1 <- xts(1, as.POSIXct("2017-01-02"))
zpz <- as.zoo(xpz)
zp1 <- as.zoo(xp1)
zpe <- tryCatch(rbind(zpz, zp1), error = identity)
xpe <- tryCatch(rbind(xpz, xp1), error = identity)
expect_identical(zpe$message, xpe$message, info = info_msg)

info_msg <- "test.rbind_zero_length_non_zero_length_Date_errors"
xpz <- xts( , as.Date("2017-01-01"))
xp1 <- xts(1, as.Date("2017-01-02"))
zpz <- as.zoo(xpz)
zp1 <- as.zoo(xp1)
zpe <- tryCatch(rbind(zpz, zp1), error = identity)
xpe <- tryCatch(rbind(xpz, xp1), error = identity)
expect_identical(zpe$message, xpe$message, info = info_msg)


info_msg <- "test.rbind_no_dim_does_not_error"
d <- rep(0.1, 2)
i <- rep(581910048, 2)
xts_no_dim <-
  structure(d[1], class = c("xts", "zoo"),
            index = structure(i[1], tzone = "UTC", tclass = "Date"))
xts_out <-
  structure(d, class = c("xts", "zoo"), .Dim = 2:1,
            index = structure(i, tzone = "UTC", tclass = "Date"))
xts_rbind <- rbind(xts_no_dim, xts_no_dim)
expect_identical(xts_out, xts_rbind, info = info_msg)


# Test that as.Date.numeric() works at the top level (via zoo::as.Date()),
# and for functions defined in the xts namespace even if xts::as.Date.numeric()
# is not formally registered as an S3 method.
info_msg <- "test.as.Date.numeric"
# Define function that calls as.Date.numeric() ...
f <- function(d) {
  as.Date(d)
}
# ... in xts' namespace
environment(f) <- as.environment("package:xts")

dd <- as.Date("2017-12-13")
dn <- unclass(dd)
expect_identical(dd, as.Date(dn), info = info_msg)  # via zoo::as.Date()
expect_identical(dd, f(dn), info = info_msg)

# .subset.xts
# window.xts
# .toPOSIXct (indirectly)

info_msg <- "test.window"
# window function for xts series, use basic logic for testing & debugging
# null start and end not supported
window_dbg <- function(x, index. = index(x), start, end)
{
  start <- xts:::.toPOSIXct(start, tzone(x))
  end <- xts:::.toPOSIXct(end, tzone(x))
  index. <- as.POSIXct(index., tz=tzone(x))
  all.indexes <- .index(x)
  in.index <- all.indexes %in% as.numeric(index.)
  matches <- (in.index & all.indexes >= start & all.indexes <= end)
  x[matches,]
}

DAY = 24*3600
base <- as.POSIXct("2000-12-31")
dts <- base + c(1:10, 12:15, 17:20)*DAY
x <- xts(1:length(dts), dts)

# Range over gap
start <- base + 11*DAY
end <- base + 16*DAY
bin <- window(x, start = start, end = end)
reg <- window_dbg(x, start = start, end = end)
expect_identical(bin, reg, info = paste(info_msg, "- range over gap"))

# Range over one day
start <- base + 12*DAY
end <- base + 12*DAY
bin <- window(x, start = start, end = end)
reg <- window_dbg(x, start = start, end = end)
expect_identical(bin, reg, info = paste(info_msg, "- range over one day"))

# Empty Range over one day
start <- base + 11*DAY
end <- base + 11*DAY
bin <- window(x, start = start, end = end)
reg <- window_dbg(x, start = start, end = end)
expect_identical(bin, reg, info = paste(info_msg, "- empty Range over one day"))

# Range containing all dates
start <- base
end <- base + 21*DAY
bin <- window(x, start = start, end = end)
reg <- window_dbg(x, start = start, end = end)
expect_identical(bin, reg, info = paste(info_msg, "- range containing all dates"))

# Range past end
start <- base + 16*DAY
end <- base + 30*DAY
bin <- window(x, start = start, end = end)
reg <- window_dbg(x, start = start, end = end)
expect_identical(bin, reg, info = paste(info_msg, "- range past end"))

# Range before begin
start <- base
end <- base + 3*DAY
bin <- window(x, start = start, end = end)
reg <- window_dbg(x, start = start, end = end)
expect_identical(bin, reg, info = paste(info_msg, "- range before begin"))

# Test just start, end = NULL
start <- base + 13 * DAY
end <- base + 30*DAY
bin <- window(x, start = start)
reg <- window_dbg(x, start = start, end = end)
expect_identical(bin, reg, info = paste(info_msg, "- just start, end = NULL"))

# Test just start, end = NULL, empty range
start <- base +