Skip to content

Commit

Permalink
BUG in reverse index
Browse files Browse the repository at this point in the history
The reverse index is incorrect.
The restriction that the maximum value inside the index cannot be greater than the sum of the index is incorrect.
In the reverse index, the maximum can be greater than the sum by one element.

Issue #108
  • Loading branch information
wilsonfreitas committed Feb 11, 2024
1 parent 2bb3ebc commit 6b435ea
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
3 changes: 2 additions & 1 deletion R/bizdays.R
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,12 @@ bizdays.Date <- function(from, to,
bdays[idx] <- -bdays[idx]
adj_vec <- as.integer(!(cal$is.bizday(as.integer(new.from)) |
cal$is.bizday(as.integer(new.to))))
adj_vec[idx] <- -adj_vec[idx]
bdays <- bdays - adj_vec
if (cal$financial) {
w <- (!cal$is.bizday(as.integer(new.from))) &
(!cal$is.bizday(as.integer(new.to))) &
bdays == -1
abs(bdays) == 1
bdays[w] <- 0
bdays
} else {
Expand Down
2 changes: 1 addition & 1 deletion R/calendar.R
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ NULL

rev_index <- function(idx) {
ridx <- cumsum(idx) + 1 - as.integer(idx)
ridx[ridx > sum(idx)] <- sum(idx)
# ridx[ridx > sum(idx)] <- sum(idx)
ridx
}

Expand Down
28 changes: 25 additions & 3 deletions tests/testthat/test-bizdays.R
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,40 @@ test_that("it should return negative bizdays", {
),
c(NA, -21, 252)
)
cal <- Calendar_(
cal <- create.calendar(
name = "test_neg_bizdays",
weekdays = c("saturday", "sunday"),
adjust.from = adjust.none, adjust.to = adjust.none
)
expect_equal(bizdays("2013-06-22", "2013-06-23", cal), 0)
expect_equal(bizdays("2013-06-23", "2013-06-22", cal), 0)
expect_equal(
bizdays(Sys.Date(), Sys.Date() + c(2, -1, 1, 1), "actual"),
c(2, -1, 1, 1)
)
})

test_that("it should check consistency in bizdays", {
cal <- create.calendar(
name = "test_consistency",
weekdays = c("saturday", "sunday"),
adjust.from = adjust.none, adjust.to = adjust.none
)
expect_equal(bizdays("2013-06-22", "2013-06-23", cal), 0)
expect_equal(bizdays("2013-06-23", "2013-06-22", cal), 0)

hol <- c("2024-01-01", "2024-03-29", "2024-04-01", "2024-05-06", "2024-05-27",
"2024-08-26", "2024-12-25", "2024-12-26")
cal <- bizdays::create.calendar(name = "nursery_calendar",
holidays = hol,
weekdays = c("monday", "tuesday", "wednesday",
"saturday", "sunday"),
start.date = as.Date("2024-01-01"),
end.date = as.Date("2024-12-31"),
financial = FALSE)
expect_equal(bizdays("2024-12-23", "2024-12-29", cal), 1)
expect_equal(bizdays("2024-12-29", "2024-12-23", cal), -1)
expect_true(is.bizday("2024-12-27", cal))
})

test_that("it should compute bizdays using double index approach", {
create.calendar(
name = "example1", weekdays = c("saturday", "sunday"),
Expand Down

0 comments on commit 6b435ea

Please sign in to comment.