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

[c++] Unit-test resize for SparseNDArray and DenseNDArray #2947

Merged
merged 1 commit into from
Sep 3, 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
17 changes: 13 additions & 4 deletions libtiledbsoma/test/unit_soma_dense_ndarray.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

#define DIM_MAX 1000

TEST_CASE("SOMADenseNDArray: basic") {
TEST_CASE("SOMADenseNDArray: basic", "[SOMADenseNDArray]") {
int64_t dim_max = 1000;
auto use_current_domain = GENERATE(false, true);
// TODO this could be formatted with fmt::format which is part of internal
Expand Down Expand Up @@ -98,7 +98,7 @@ TEST_CASE("SOMADenseNDArray: basic") {
REQUIRE(schema->domain().has_dimension(dim_name));
REQUIRE(soma_dense->ndim() == 1);

// Once we have support for current domain in dense arrays
// TODO: Once we have support for current domain in dense arrays
// if (use_current_domain) {
// REQUIRE(soma_dense->shape() == std::vector<int64_t>{dim_max +
// 1});
Expand Down Expand Up @@ -129,11 +129,20 @@ TEST_CASE("SOMADenseNDArray: basic") {
REQUIRE(a0 == std::vector<int>(a0span.begin(), a0span.end()));
}
soma_dense->close();

soma_dense = SOMADenseNDArray::open(uri, OpenMode::read, ctx);
auto new_shape = std::vector<int64_t>({DIM_MAX * 2});
// * When use_current_domain is false: can't resize what has not
// been sized.
// * When use_current_domain is true: TODO: current domain not
// supported for dense arrays yet (see above).
REQUIRE_THROWS(soma_dense->resize(new_shape));
soma_dense->close();
}
}
}

TEST_CASE("SOMADenseNDArray: platform_config") {
TEST_CASE("SOMADenseNDArray: platform_config", "[SOMADenseNDArray]") {
int64_t dim_max = 1000;
auto use_current_domain = GENERATE(false, true);
// TODO this could be formatted with fmt::format which is part of internal
Expand Down Expand Up @@ -196,7 +205,7 @@ TEST_CASE("SOMADenseNDArray: platform_config") {
}
}

TEST_CASE("SOMADenseNDArray: metadata") {
TEST_CASE("SOMADenseNDArray: metadata", "[SOMADenseNDArray]") {
int64_t dim_max = 1000;
auto use_current_domain = GENERATE(false, true);
// TODO this could be formatted with fmt::format which is part of internal
Expand Down
54 changes: 42 additions & 12 deletions libtiledbsoma/test/unit_soma_sparse_ndarray.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@
*/

#include "common.h"
#define DIM_MAX 1000

TEST_CASE("SOMASparseNDArray: basic") {
TEST_CASE("SOMASparseNDArray: basic", "[SOMASparseNDArray]") {
int64_t dim_max = 1000;
auto use_current_domain = GENERATE(false, true);
// auto use_current_domain = GENERATE(false, true);
auto use_current_domain = GENERATE(true);
// TODO this could be formatted with fmt::format which is part of internal
// header spd/log/fmt/fmt.h and should not be used. In C++20, this can be
// replaced with std::format.
Expand All @@ -45,6 +45,7 @@ TEST_CASE("SOMASparseNDArray: basic") {
auto ctx = std::make_shared<SOMAContext>();
std::string uri = "mem://unit-test-sparse-ndarray-basic";
std::string dim_name = "soma_dim_0";
std::string attr_name = "soma_data";
tiledb_datatype_t tiledb_datatype = TILEDB_INT64;
std::string arrow_format = helper::to_arrow_format(tiledb_datatype);

Expand Down Expand Up @@ -79,7 +80,7 @@ TEST_CASE("SOMASparseNDArray: basic") {
REQUIRE(soma_sparse->is_sparse() == true);
REQUIRE(soma_sparse->soma_data_type() == arrow_format);
auto schema = soma_sparse->tiledb_schema();
REQUIRE(schema->has_attribute("soma_data"));
REQUIRE(schema->has_attribute(attr_name));
REQUIRE(schema->array_type() == TILEDB_SPARSE);
REQUIRE(schema->domain().has_dimension(dim_name));
REQUIRE(soma_sparse->ndim() == 1);
Expand All @@ -100,30 +101,59 @@ TEST_CASE("SOMASparseNDArray: basic") {
std::vector<int> a0(10, 1);

soma_sparse->open(OpenMode::write);
soma_sparse->set_column_data("soma_data", a0.size(), a0.data());
soma_sparse->set_column_data(dim_name, d0.size(), d0.data());
soma_sparse->set_column_data(attr_name, a0.size(), a0.data());
soma_sparse->write();
soma_sparse->close();

soma_sparse->open(OpenMode::read);
while (auto batch = soma_sparse->read_next()) {
auto arrbuf = batch.value();
auto d0span = arrbuf->at(dim_name)->data<int64_t>();
auto a0span = arrbuf->at("soma_data")->data<int>();
auto a0span = arrbuf->at(attr_name)->data<int>();
REQUIRE(d0 == std::vector<int64_t>(d0span.begin(), d0span.end()));
REQUIRE(a0 == std::vector<int>(a0span.begin(), a0span.end()));
}

// TODO on a subsequent PR if use_current_domain:
// * test write out of bounds, including assertion of exception type
// * test resize
// * test write within new bounds
std::vector<int64_t> d0b({dim_max, dim_max + 1});
std::vector<int64_t> a0b({30, 40});
soma_sparse->close();

// Try out-of-bounds write before resize.
// * Without current domain support: this should throw since it's
// outside the (immutable) doqain.
// * With current domain support: this should throw since it's outside
// the (mutable) current domain.
soma_sparse = SOMASparseNDArray::open(uri, OpenMode::write, ctx);
soma_sparse->set_column_data(dim_name, d0b.size(), d0b.data());
soma_sparse->set_column_data(attr_name, a0b.size(), a0b.data());
REQUIRE_THROWS(soma_sparse->write());
soma_sparse->close();

soma_sparse = SOMASparseNDArray::open(uri, OpenMode::write, ctx);
auto new_shape = std::vector<int64_t>({dim_max * 2});
if (!use_current_domain) {
// Without current-domain support: this should throw since
// one cannot resize what has not been sized.
REQUIRE_THROWS(soma_sparse->resize(new_shape));
} else {
soma_sparse->resize(new_shape);
}
soma_sparse->close();

// Try out-of-bounds write after resize.
if (use_current_domain) {
soma_sparse = SOMASparseNDArray::open(uri, OpenMode::write, ctx);
soma_sparse->set_column_data(dim_name, d0b.size(), d0b.data());
soma_sparse->set_column_data(attr_name, a0b.size(), a0b.data());
// Implicitly checking for no throw
soma_sparse->write();
soma_sparse->close();
}
}
}

TEST_CASE("SOMASparseNDArray: platform_config") {
TEST_CASE("SOMASparseNDArray: platform_config", "[SOMASparseNDArray]") {
int64_t dim_max = 1000;
auto use_current_domain = GENERATE(false, true);
// TODO this could be formatted with fmt::format which is part of internal
Expand Down Expand Up @@ -171,7 +201,7 @@ TEST_CASE("SOMASparseNDArray: platform_config") {
}
}

TEST_CASE("SOMASparseNDArray: metadata") {
TEST_CASE("SOMASparseNDArray: metadata", "[SOMASparseNDArray]") {
int64_t dim_max = 1000;
auto use_current_domain = GENERATE(false, true);
// TODO this could be formatted with fmt::format which is part of internal
Expand Down
Loading