Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bugs in reading dimensions #95

Merged
merged 1 commit into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: dust2
Title: Next Generation dust
Version: 0.1.11
Version: 0.1.12
Authors@R: c(person("Rich", "FitzJohn", role = c("aut", "cre"),
email = "[email protected]"),
person("Imperial College of Science, Technology and Medicine",
Expand Down
4 changes: 4 additions & 0 deletions inst/include/dust2/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ void read_real_vector(cpp11::list pars, size_t len, real_type * dest,
template <typename real_type, size_t rank>
void read_real_array(cpp11::list args, const dust2::array::dimensions<rank>& dim,
real_type * dest, const char *name, bool required);
template <size_t rank>
dust2::array::dimensions<rank> read_dimensions(cpp11::list args, const char * name);
template <>
dust2::array::dimensions<1> read_dimensions(cpp11::list args, const char * name);

}

Expand Down
11 changes: 6 additions & 5 deletions inst/include/dust2/r/helpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,20 +86,21 @@ void check_dimensions(cpp11::sexp value,


template <size_t rank>
dust2::array::dimensions<rank> read_dimensions(cpp11::sexp value,
dust2::array::dimensions<rank> read_dimensions(cpp11::list args,
const char * name) {
cpp11::sexp value = args[name];
check_rank(value, rank, name);
auto r_dim = cpp11::as_cpp<cpp11::integers>(value.attr("dim"));
return dust2::array::dimensions<rank>(r_dim.begin());
}

template <>
inline dust2::array::dimensions<1> read_dimensions(cpp11::sexp value,
inline dust2::array::dimensions<1> read_dimensions(cpp11::list args,
const char * name) {
SEXP value = args[name];
check_rank(value, 1, name);
size_t len = LENGTH(value);
const auto ret = dust2::array::dimensions<1>{len};
return ret; // dust2::array::dimensions<1>{len};
const size_t len = LENGTH(value);
return dust2::array::dimensions<1>{len};
}

inline double to_double(cpp11::sexp x, bool allow_na, const char * name) {
Expand Down
4 changes: 2 additions & 2 deletions src/cpp11.cpp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/test_helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ bool test_check_dimensions(cpp11::sexp value, cpp11::integers r_dim,


[[cpp11::register]]
std::vector<size_t> test_read_dimensions(cpp11::sexp value, int rank, const char * name) {
std::vector<size_t> test_read_dimensions(cpp11::list value, int rank, const char * name) {
if (rank == 1) {
const auto arr = dust2::r::read_dimensions<1>(value, name).dim;
return std::vector<size_t>(arr.begin(), arr.end());
Expand Down
20 changes: 10 additions & 10 deletions tests/testthat/test-helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -50,34 +50,34 @@ test_that("validate dimensions of an array", {


test_that("can read dimensions from vector", {
expect_equal(test_read_dimensions(0:3, 1, "x"), 4)
expect_error(test_read_dimensions(cbind(0:3), 1, "x"),
expect_equal(test_read_dimensions(list(x = 0:3), 1, "x"), 4)
expect_error(test_read_dimensions(list(x = cbind(0:3)), 1, "x"),
"Expected 'x' to be a vector, but was given a matrix")
expect_error(
test_read_dimensions(array(0:3, c(1, 2, 2)), 1, "x"),
test_read_dimensions(list(x = array(0:3, c(1, 2, 2))), 1, "x"),
"Expected 'x' to be a vector, but was given a 3-dimensional array")
})


test_that("can read dimensions from vector", {
expect_equal(test_read_dimensions(matrix(0, 2, 3), 2, "x"), c(2, 3))
expect_error(test_read_dimensions(1:6, 2, "x"),
expect_equal(test_read_dimensions(list(x = matrix(0, 2, 3)), 2, "x"), c(2, 3))
expect_error(test_read_dimensions(list(x = 1:6), 2, "x"),
"Expected 'x' to be a matrix, but was given a vector")
expect_error(
test_read_dimensions(array(0, 1:3), 2, "x"),
test_read_dimensions(list(x = array(0, 1:3)), 2, "x"),
"Expected 'x' to be a matrix, but was given a 3-dimensional array")
})


test_that("can read dimensions from array", {
expect_equal(test_read_dimensions(array(0, 2:4), 3, "x"), 2:4)
expect_equal(test_read_dimensions(list(x = array(0, 2:4)), 3, "x"), 2:4)
expect_error(
test_read_dimensions(1:6, 3, "x"),
test_read_dimensions(list(x = 1:6), 3, "x"),
"Expected 'x' to be a 3-dimensional array, but was given a vector")
expect_error(
test_read_dimensions(matrix(0, 2, 3), 3, "x"),
test_read_dimensions(list(x = matrix(0, 2, 3)), 3, "x"),
"Expected 'x' to be a 3-dimensional array, but was given a matrix")
expect_error(
test_read_dimensions(array(0, 1:4), 3, "x"),
test_read_dimensions(list(x = array(0, 1:4)), 3, "x"),
"Expected 'x' to be a 3-dimensional array, but was given a 4-dimensional")
})
Loading