Skip to content

Commit

Permalink
Test input validation
Browse files Browse the repository at this point in the history
  • Loading branch information
richfitz committed Dec 12, 2024
1 parent 2a6a3f2 commit 92438c3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
3 changes: 1 addition & 2 deletions src/random.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ struct input_array {
fixed = ncol == 1;
if (!fixed && static_cast<size_t>(ncol) != expected) {
if (expected == 1) {
cpp11::stop("Expected '%s' to have %d columns, not %d",
name, static_cast<int>(expected), ncol);
cpp11::stop("Expected '%s' to have 1 column, not %d", name, ncol);
} else {
cpp11::stop("Expected '%s' to have %d or 1 columns, not %d",
name, static_cast<int>(expected), ncol);
Expand Down
16 changes: 16 additions & 0 deletions tests/testthat/test-rng.R
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,22 @@ test_that("Can vary vector parameters by generator for multinomial", {
})


test_that("can validate multinomial inputs", {
r <- monty_rng_create(seed = 1, n_streams = 3)
expect_error(
monty_random_multinomial(5, matrix(1, 7, 2), r),
"Expected 'prob' to have 3 or 1 columns, not 2")
expect_error(
monty_random_multinomial(5, array(1, c(7, 2, 3)), r),
"Expected 'prob' to be a matrix")

r <- monty_rng_create(seed = 1, n_streams = 1)
expect_error(
monty_random_multinomial(5, matrix(1, 7, 2), r),
"Expected 'prob' to have 1 column, not 2")
})


test_that("deterministic rbinom returns mean", {
m <- 10
n <- as.numeric(sample(10, m, replace = TRUE))
Expand Down

0 comments on commit 92438c3

Please sign in to comment.