tests/testthat/test-rolling_window.R

context("rolling window functions")
# roll_list --------------------------------------------------------------

test_that("check roll_list", {
    x <- list(1, 2, 3, 4, 5, 6, c("a", "b"), NULL)
    test1 <- roll_list(x, window = 3, align = "right")
    test1 <- lapply(test1, unlist)
    test2 <- roll_list(as.list(letters), window = 11, align = "center")
    expect_equal(test1[6:8], list(4:6, c(5, 6, "a", "b"), c(6, "a", "b")))
    expect_equal(unlist(test2[[21]]), letters[16:26])

})


# rollapply_epi -----------------------------------------------------------


test_that("check rollaply_epi", {

    x <- c(4, 14, 14, 14, 10, 10, 10, 10, 5)
    n_days <- c(2, rep(7, times = (length(x) -2)), 3)
    y <- rollapply_epi(x, n_days = n_days)
    # should be true
    expect_equal(14, y[[1]])
    expect_equal(15/(10/7), y[[9]])

    expect_error(rollapply_epi(x,c(NA, n_days[-1])))
    expect_error(rollapply_epi(x,c(NA, n_days[-1]), na.rm = TRUE))
    expect_equal(rollapply_epi(c(NA, x),c(NA, n_days), na.rm = TRUE)[[1]],
                 14)

    # default returns NA when there is an NA in the window
    y2 <- rollapply_epi(c(NA, x[-1]), n_days = n_days)
    expect_equal(y2[1], NA_real_)

    # now remove NAs
    y3 <- rollapply_epi(c(NA, x[-1]), n_days = n_days, na.rm = TRUE)
    expect_equal(y3[1], 14)

})
MartinHoldrege/turnr documentation built on May 16, 2020, 10:39 a.m.