Skip to content

Commit

Permalink
Bug fix and more tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
NicChr committed Apr 17, 2024
1 parent 7811325 commit 116558d
Show file tree
Hide file tree
Showing 2 changed files with 219 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/lag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,12 @@ SEXP cpp_lag(SEXP x, int k, SEXP fill, bool set, bool recursive) {
Rf_unprotect(n_protections);
return out;
} else {
if (k > size){
k = size;
}
if (k < -size){
k = -size;
}
SEXP fill_value = Rf_protect(Rf_coerceVector(fill_size >= 1 ? fill : R_NilValue, VECSXP));
++n_protections;
SEXP out = Rf_protect(set ? x : Rf_allocVector(VECSXP, size));
Expand Down
213 changes: 213 additions & 0 deletions tests/testthat/test-lag.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ test_that("lags and leads", {
names(c) <- sample(letters, length(c), TRUE)
names(d) <- sample(letters, length(d), TRUE)
f <- as.list(b)
g <- a > 0

expect_identical(
lag_(a, 0), a
Expand Down Expand Up @@ -155,8 +156,220 @@ test_that("lags and leads", {
lag_(f, -10^6, recursive = FALSE), base_lag(f, -10^6)
)

expect_identical(
lag_(g, 0), g
)
expect_identical(
lag_(g, 1, recursive = FALSE), base_lag(g, 1)
)
expect_identical(
lag_(g, -1, recursive = FALSE), base_lag(g, -1)
)
expect_identical(
lag_(g, 3, recursive = FALSE), base_lag(g, 3)
)
expect_identical(
lag_(g, -3, recursive = FALSE), base_lag(g, -3)
)
expect_identical(
lag_(g, 10^6, recursive = FALSE), base_lag(g, 10^6)
)
expect_identical(
lag_(g, -10^6, recursive = FALSE), base_lag(g, -10^6)
)

expect_identical(
lag_(iris, 7),
as.data.frame(lapply(iris, base_lag, 7)),
)
})

test_that("lags and lead with set = TRUE", {
base_lag <- function(x, lag = 1L, check = TRUE){
lagged_indices <- seq_along(x) - lag
lagged_indices[lagged_indices < 1L] <- NA_integer_
x[lagged_indices]
}
set.seed(876123)
a <- rnorm(10^5)
b <- sample(-12:123, 10^5, TRUE)
c <- sample(do.call(paste0, expand.grid(letters, letters, letters)), 10^5, TRUE)
d <- complex(fill_with_na(rnorm(10^5, 10^3)),
fill_with_na(rnorm(10^5, 10^3)))
e <- vapply(sample(letters, 10^5, TRUE), charToRaw, raw(1))
a <- fill_with_na(a, 10^3)
names(a) <- sample(letters, length(a), TRUE)
b <- fill_with_na(b, 10^3)
names(b) <- sample(letters, length(b), TRUE)
c <- fill_with_na(c, 10^3)
names(c) <- sample(letters, length(c), TRUE)
names(d) <- sample(letters, length(d), TRUE)
f <- as.list(b)
g <- a > 0

set_lag <- function(x, ...){
lag_(x, ..., set = TRUE)
}

expect_identical(
set_lag(r_copy(a), 0), a
)
expect_identical(
set_lag(r_copy(a), 1), base_lag(a, 1)
)
expect_identical(
set_lag(r_copy(a), -1), base_lag(a, -1)
)
expect_identical(
set_lag(r_copy(a), 3), base_lag(a, 3)
)
expect_identical(
set_lag(r_copy(a), -3), base_lag(a, -3)
)
expect_identical(
set_lag(r_copy(a), 10^6), base_lag(a, 10^6)
)
expect_identical(
set_lag(r_copy(a), -10^6), base_lag(a, -10^6)
)

expect_identical(
set_lag(r_copy(b), 0), b
)
expect_identical(
set_lag(r_copy(b), 1), base_lag(b, 1)
)
expect_identical(
set_lag(r_copy(b), -1), base_lag(b, -1)
)
expect_identical(
set_lag(r_copy(b), 3), base_lag(b, 3)
)
expect_identical(
set_lag(r_copy(b), -3), base_lag(b, -3)
)
expect_identical(
set_lag(r_copy(b), 10^6), base_lag(b, 10^6)
)
expect_identical(
set_lag(r_copy(b), -10^6), base_lag(b, -10^6)
)


expect_identical(
set_lag(r_copy(c), 0), c
)
expect_identical(
set_lag(r_copy(c), 1), base_lag(c, 1)
)
expect_identical(
set_lag(r_copy(c), -1), base_lag(c, -1)
)
expect_identical(
set_lag(r_copy(c), 3), base_lag(c, 3)
)
expect_identical(
set_lag(r_copy(c), -3), base_lag(c, -3)
)
expect_identical(
set_lag(r_copy(c), 10^6), base_lag(c, 10^6)
)
expect_identical(
set_lag(r_copy(c), -10^6), base_lag(c, -10^6)
)


expect_identical(
set_lag(r_copy(d), 0), d
)
expect_identical(
set_lag(r_copy(d), 1), base_lag(d, 1)
)
expect_identical(
set_lag(r_copy(d), -1), base_lag(d, -1)
)
expect_identical(
set_lag(r_copy(d), 3), base_lag(d, 3)
)
expect_identical(
set_lag(r_copy(d), -3), base_lag(d, -3)
)
expect_identical(
set_lag(r_copy(d), 10^6), base_lag(d, 10^6)
)
expect_identical(
set_lag(r_copy(d), -10^6), base_lag(d, -10^6)
)


expect_identical(
set_lag(r_copy(e), 0), e
)
expect_identical(
set_lag(r_copy(e), 1), base_lag(e, 1)
)
expect_identical(
set_lag(r_copy(e), -1), base_lag(e, -1)
)
expect_identical(
set_lag(r_copy(e), 3), base_lag(e, 3)
)
expect_identical(
set_lag(r_copy(e), -3), base_lag(e, -3)
)
expect_identical(
set_lag(r_copy(e), 10^6), base_lag(e, 10^6)
)
expect_identical(
set_lag(r_copy(e), -10^6), base_lag(e, -10^6)
)

expect_identical(
set_lag(r_copy(f), 0), f
)
expect_identical(
set_lag(r_copy(f), 1, recursive = FALSE), base_lag(f, 1)
)
expect_identical(
set_lag(r_copy(f), -1, recursive = FALSE), base_lag(f, -1)
)
expect_identical(
set_lag(r_copy(f), 3, recursive = FALSE), base_lag(f, 3)
)
expect_identical(
set_lag(r_copy(f), -3, recursive = FALSE), base_lag(f, -3)
)
expect_identical(
set_lag(r_copy(f), 10^6, recursive = FALSE), base_lag(f, 10^6)
)
expect_identical(
set_lag(r_copy(f), -10^6, recursive = FALSE), base_lag(f, -10^6)
)

expect_identical(
set_lag(r_copy(g), 0), g
)
expect_identical(
set_lag(r_copy(g), 1, recursive = FALSE), base_lag(g, 1)
)
expect_identical(
set_lag(r_copy(g), -1, recursive = FALSE), base_lag(g, -1)
)
expect_identical(
set_lag(r_copy(g), 3, recursive = FALSE), base_lag(g, 3)
)
expect_identical(
set_lag(r_copy(g), -3, recursive = FALSE), base_lag(g, -3)
)
expect_identical(
set_lag(r_copy(g), 10^6, recursive = FALSE), base_lag(g, 10^6)
)
expect_identical(
set_lag(r_copy(g), -10^6, recursive = FALSE), base_lag(g, -10^6)
)

expect_identical(
set_lag(r_copy(iris), 7),
as.data.frame(lapply(iris, base_lag, 7)),
)
})

0 comments on commit 116558d

Please sign in to comment.