Skip to content

Commit

Permalink
Throw an exception if a matrix with non-zero size is constructed with…
Browse files Browse the repository at this point in the history
… a nullptr.
  • Loading branch information
breyerml committed Nov 28, 2024
1 parent cd2a090 commit b563c6f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
6 changes: 6 additions & 0 deletions include/plssvm/matrix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,9 @@ matrix<T, layout_>::matrix(const plssvm::shape shape, const std::vector<value_ty
template <typename T, layout_type layout_>
matrix<T, layout_>::matrix(const plssvm::shape shape, const_pointer data) :
matrix{ shape } {
if (data == nullptr && this->size() > 0) {
throw matrix_exception{ "The provided data pointer may not be a nullptr if the matrix size is greater than 0!" };
}
if (this->size() > 0) {
// memcpy data to matrix
std::memcpy(this->data(), data, this->size() * sizeof(value_type));
Expand All @@ -505,6 +508,9 @@ matrix<T, layout_>::matrix(const plssvm::shape shape, const_pointer data) :
template <typename T, layout_type layout_>
matrix<T, layout_>::matrix(const plssvm::shape shape, const_pointer data, const plssvm::shape padding) :
matrix{ shape, padding } {
if (data == nullptr && this->size() > 0) {
throw matrix_exception{ "The provided data pointer may not be a nullptr if the matrix size is greater than 0!" };
}
if (this->size() > 0) {
// memcpy data row- or column-wise depending on the layout type to the matrix
this->opt_mismatched_padding_copy(this->data(), this->shape_padded(), data, this->shape());
Expand Down
20 changes: 20 additions & 0 deletions tests/matrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,16 @@ TYPED_TEST(Matrix, construct_with_size_and_ptr_empty) {
EXPECT_EQ(matr.shape_padded(), (plssvm::shape{ 0, 0 }));
}

TYPED_TEST(Matrix, construct_with_size_and_nullptr_ptr) {
using real_type = typename TestFixture::fixture_real_type;
constexpr plssvm::layout_type layout = TestFixture::fixture_layout;

// construct a matrix with a specific size and a nullptr
EXPECT_THROW_WHAT((plssvm::matrix<real_type, layout>{ plssvm::shape{ 2, 3 }, nullptr }),
plssvm::matrix_exception,
"The provided data pointer may not be a nullptr if the matrix size is greater than 0!");
}

TYPED_TEST(Matrix, construct_with_size_and_ptr_value_zero_num_rows) {
using real_type = typename TestFixture::fixture_real_type;
constexpr plssvm::layout_type layout = TestFixture::fixture_layout;
Expand Down Expand Up @@ -603,6 +613,16 @@ TYPED_TEST(Matrix, construct_with_size_and_ptr_empty_and_padding) {
EXPECT_TRUE(std::all_of(matr.data(), matr.data() + matr.size_padded(), [](const real_type val) { return val == real_type{ 0.0 }; }));
}

TYPED_TEST(Matrix, construct_with_size_and_nullptr_empty_and_padding) {
using real_type = typename TestFixture::fixture_real_type;
constexpr plssvm::layout_type layout = TestFixture::fixture_layout;

// construct a matrix with a specific size, padding and a nullptr
EXPECT_THROW_WHAT((plssvm::matrix<real_type, layout>{ plssvm::shape{ 2, 3 }, nullptr, plssvm::shape{ 4, 5 } }),
plssvm::matrix_exception,
"The provided data pointer may not be a nullptr if the matrix size is greater than 0!");
}

TYPED_TEST(Matrix, construct_with_size_and_ptr_empty_and_zero_padding) {
using real_type = typename TestFixture::fixture_real_type;
constexpr plssvm::layout_type layout = TestFixture::fixture_layout;
Expand Down

0 comments on commit b563c6f

Please sign in to comment.