Skip to content

Commit

Permalink
add more restrictive input checking
Browse files Browse the repository at this point in the history
  • Loading branch information
EmilHvitfeldt committed Apr 30, 2024
1 parent 40fc6cf commit f13d073
Show file tree
Hide file tree
Showing 3 changed files with 172 additions and 0 deletions.
47 changes: 47 additions & 0 deletions R/altrep.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,53 @@ new_sparse_real <- function(value, position, length) {
)
}

if (anyDuplicated(position) > 0) {
offenders <- which(duplicated(position))

cli::cli_abort(
c(
x = "{.arg position} must not contain any duplicate values.",
i = "Duplicate values at index: {offenders}."
)
)
}

if (is.unsorted(position)) {
cli::cli_abort(
"{.arg position} must be sorted in increasing order."
)
}

if (len_position > 0 && max(position) > length) {
offenders <- which(position > length)
cli::cli_abort(
c(
x = "{.arg position} value must not be larger than {.arg length}.",
i = "Offending values at index: {offenders}."
)
)
}

if (len_position > 0 && min(position) < 1) {
offenders <- which(position < 1)
cli::cli_abort(
c(
x = "{.arg position} value must positive.",
i = "Non-positive values at index: {offenders}."
)
)
}

if (any(value == 0)) {
offenders <- which(value == 0)
cli::cli_abort(
c(
x = "{.arg value} value must not be 0.",
i = "0 values at index: {offenders}."
)
)
}

x <- list(
val = value,
pos = position,
Expand Down
80 changes: 80 additions & 0 deletions tests/testthat/_snaps/altrep.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,83 @@
Error in `new_sparse_real()`:
! `value` (1) and `position` (6) must have the same length.

---

Code
new_sparse_real(1:4, c(1, 1, 5, 6), 10)
Condition
Error in `new_sparse_real()`:
x `position` must not contain any duplicate values.
i Duplicate values at index: 2.

---

Code
new_sparse_real(1:100, rep(1, 100), 100)
Condition
Error in `new_sparse_real()`:
x `position` must not contain any duplicate values.
i Duplicate values at index: 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, ..., 99, and 100.

---

Code
new_sparse_real(c(1, 2), c(3, 1), 5)
Condition
Error in `new_sparse_real()`:
! `position` must be sorted in increasing order.

---

Code
new_sparse_real(1, 10, 5)
Condition
Error in `new_sparse_real()`:
x `position` value must not be larger than `length`.
i Offending values at index: 1.

---

Code
new_sparse_real(rep(1, 50), seq(25, 74), 50)
Condition
Error in `new_sparse_real()`:
x `position` value must not be larger than `length`.
i Offending values at index: 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, ..., 49, and 50.

---

Code
new_sparse_real(1, 0, 5)
Condition
Error in `new_sparse_real()`:
x `position` value must positive.
i Non-positive values at index: 1.

---

Code
new_sparse_real(rep(1, 101), seq(-50, 50), 100)
Condition
Error in `new_sparse_real()`:
x `position` value must positive.
i Non-positive values at index: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, ..., 50, and 51.

---

Code
new_sparse_real(0, 1, 10)
Condition
Error in `new_sparse_real()`:
x `value` value must not be 0.
i 0 values at index: 1.

---

Code
new_sparse_real(rep(c(1, 0), 5), 1:10, 50)
Condition
Error in `new_sparse_real()`:
x `value` value must not be 0.
i 0 values at index: 2, 4, 6, 8, and 10.

45 changes: 45 additions & 0 deletions tests/testthat/test-altrep.R
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,52 @@ test_that("input checking is done correctly", {
error = TRUE,
new_sparse_real(1, 1:6, 10)
)

# duplicates in position
expect_snapshot(
error = TRUE,
new_sparse_real(1:4, c(1, 1, 5, 6), 10)
)
expect_snapshot(
error = TRUE,
new_sparse_real(1:100, rep(1, 100), 100)
)

# Ordered position
expect_snapshot(
error = TRUE,
new_sparse_real(c(1, 2), c(3, 1), 5)
)

# Too large position values
expect_snapshot(
error = TRUE,
new_sparse_real(1, 10, 5)
)
expect_snapshot(
error = TRUE,
new_sparse_real(rep(1, 50), seq(25, 74), 50)
)

# Too large position values
expect_snapshot(
error = TRUE,
new_sparse_real(1, 0, 5)
)
expect_snapshot(
error = TRUE,
new_sparse_real(rep(1, 101), seq(-50, 50), 100)
)

# Too large position values
expect_snapshot(
error = TRUE,
new_sparse_real(0, 1, 10)
)
expect_snapshot(
error = TRUE,
new_sparse_real(rep(c(1, 0), 5), 1:10, 50)
)
})

test_that("length() works with new_sparse_real()", {
Expand Down

0 comments on commit f13d073

Please sign in to comment.