1 2# POSIXct index 3test.diff_integer_POSIXt <- function() { 4 x <- .xts(1:5, 1:5 + 0.0) 5 dx <- xts(rbind(NA_integer_, diff(coredata(x))), index(x)) 6 checkIdentical(diff(x), dx) 7} 8test.diff_numeric_POSIXt <- function() { 9 x <- .xts(1:5 + 1.0, 1:5 + 0.0) 10 dx <- xts(rbind(NA_real_, diff(coredata(x))), index(x)) 11 checkIdentical(diff(x), dx) 12} 13test.diff_logical_POSIXt <- function() { 14 x <- .xts(1:5 > 2, 1:5 + 0.0) 15 dx <- xts(rbind(NA, diff(coredata(x))), index(x)) 16 checkIdentical(diff(x), dx) 17} 18 19# Date index 20test.diff_integer_Date <- function() { 21 x <- xts(1:5, as.Date("2016-01-01") - 5:1) 22 dx <- xts(rbind(NA_integer_, diff(coredata(x))), index(x)) 23 checkIdentical(diff(x), dx) 24} 25test.diff_numeric_Date <- function() { 26 x <- xts(1:5 + 1.0, as.Date("2016-01-01") - 5:1) 27 dx <- xts(rbind(NA_real_, diff(coredata(x))), index(x)) 28 checkIdentical(diff(x), dx) 29} 30test.diff_logical_Date <- function() { 31 x <- xts(1:5 > 2, as.Date("2016-01-01") - 5:1) 32 dx <- xts(rbind(NA, diff(coredata(x))), index(x)) 33 checkIdentical(diff(x), dx) 34} 35 36# Type-check failure errors 37test.diff_differences_NA <- function() { 38 x <- .xts(1:5, 1:5) 39 checkException(diff(x, 1L, "a"), "'differences' must be integer") 40} 41test.diff_lag_NA <- function() { 42 x <- .xts(1:5, 1:5) 43 checkException(diff(x, "a", 1L), "'lag' must be integer") 44} 45test.diff_differences_LT1 <- function() { 46 x <- .xts(1:5, 1:5) 47 checkException(diff(x, 1L, -1L), "'diff.xts' defined only for positive lag and differences arguments") 48} 49test.diff_lag_LT1 <- function() { 50 x <- .xts(1:5, 1:5) 51 checkException(diff(x, -1L, 1L), "'diff.xts' defined only for positive lag and differences arguments") 52} 53 54test.diff_logical_preserves_colnames <- function() { 55 cnames <- c("a", "b") 56 x <- .xts(matrix(rnorm(10) > 0, 5), 1:5, dimnames = list(NULL, cnames)) 57 y <- diff(x) 58 checkIdentical(colnames(y), cnames) 59} 60