Skip to content

Commit

Permalink
Updated tests to use the new tatami_test library.
Browse files Browse the repository at this point in the history
Also a minor testfix, to coerce size_t's to uint64_t for type-safe writes.
  • Loading branch information
LTLA committed Dec 1, 2024
1 parent d8bb9c3 commit 0835a95
Show file tree
Hide file tree
Showing 7 changed files with 311 additions and 247 deletions.
18 changes: 5 additions & 13 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
include(FetchContent)
FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/afd902e992b720d1b3e106bc5e425a5768872265.zip
tatami_test
GIT_REPOSITORY https://github.com/tatami-inc/tatami_test
GIT_TAG master
)

# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)

# Avoid installing GoogleTest when installing this project.
option(INSTALL_GTEST "Enable installation of googletest." OFF)

FetchContent_MakeAvailable(googletest)

enable_testing()
FetchContent_MakeAvailable(tatami_test)

include(CheckIncludeFiles)
check_include_files(filesystem HAVE_CXX_FS)
Expand All @@ -35,7 +27,7 @@ macro(decorate_test target)

target_compile_options(${target} PRIVATE -Wall -Wextra -Wpedantic)

target_link_libraries(${target} gtest_main tatami_hdf5)
target_link_libraries(${target} tatami_hdf5 tatami_test)

if (NOT HAVE_CXX_FS)
target_link_libraries(${target} stdc++fs)
Expand Down
256 changes: 132 additions & 124 deletions tests/src/CompressedSparseMatrix.cpp

Large diffs are not rendered by default.

91 changes: 39 additions & 52 deletions tests/src/DenseMatrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include "tatami_hdf5/DenseMatrix.hpp"

#include "tatami_test/tatami_test.hpp"
#include "tatami_test/temp_file_path.hpp"
#include "temp_file_path.h"

#include <vector>
#include <random>
Expand Down Expand Up @@ -49,8 +49,7 @@ class DenseMatrixTestCore {
auto chunk_sizes = std::get<0>(params);
auto cache_fraction = std::get<1>(params);

fpath = tatami_test::temp_file_path("tatami-dense-test.h5");
tatami_test::remove_file_path(fpath);
fpath = temp_file_path("tatami-dense-test.h5");
H5::H5File fhandle(fpath, H5F_ACC_TRUNC);

hsize_t dims[2];
Expand All @@ -72,7 +71,13 @@ class DenseMatrixTestCore {

name = "stuff";
auto dhandle = fhandle.createDataSet(name, dtype, dspace, plist);
auto values = tatami_test::simulate_dense_vector<double>(NR * NC, 0, 100, /* seed = */ chunk_sizes.first * chunk_sizes.second + 100 * cache_fraction);
auto values = tatami_test::simulate_vector<double>(NR * NC, [&]{
tatami_test::SimulateVectorOptions opt;
opt.lower = 0;
opt.upper = 100;
opt.seed = chunk_sizes.first * chunk_sizes.second + 100 * cache_fraction;
return opt;
}());
dhandle.write(values.data(), H5::PredType::NATIVE_DOUBLE);

ref.reset(new tatami::DenseRowMatrix<double, int>(NR, NC, std::move(values)));
Expand Down Expand Up @@ -129,7 +134,7 @@ TEST_F(DenseMatrixUtilsTest, Basic) {
*************************************/

class DenseMatrixAccessFullTest :
public ::testing::TestWithParam<std::tuple<DenseMatrixTestCore::SimulationParameters, tatami_test::StandardTestAccessParameters> >,
public ::testing::TestWithParam<std::tuple<DenseMatrixTestCore::SimulationParameters, tatami_test::StandardTestAccessOptions> >,
public DenseMatrixTestCore {
protected:
void SetUp() {
Expand All @@ -138,25 +143,25 @@ class DenseMatrixAccessFullTest :
};

TEST_P(DenseMatrixAccessFullTest, Basic) {
auto params = tatami_test::convert_access_parameters(std::get<1>(GetParam()));
tatami_test::test_full_access(params, mat.get(), ref.get());
tatami_test::test_full_access(params, tmat.get(), tref.get());
auto opts = tatami_test::convert_test_access_options(std::get<1>(GetParam()));
tatami_test::test_full_access(*mat, *ref, opts);
tatami_test::test_full_access(*tmat, *tref, opts);
}

INSTANTIATE_TEST_SUITE_P(
DenseMatrix,
DenseMatrixAccessFullTest,
::testing::Combine(
DenseMatrixTestCore::create_combinations(),
tatami_test::standard_test_access_parameter_combinations()
tatami_test::standard_test_access_options_combinations()
)
);

/*************************************
*************************************/

class DenseSlicedTest :
public ::testing::TestWithParam<std::tuple<DenseMatrixTestCore::SimulationParameters, tatami_test::StandardTestAccessParameters, std::pair<double, double> > >,
public ::testing::TestWithParam<std::tuple<DenseMatrixTestCore::SimulationParameters, tatami_test::StandardTestAccessOptions, std::pair<double, double> > >,
public DenseMatrixTestCore {
protected:
void SetUp() {
Expand All @@ -166,32 +171,22 @@ class DenseSlicedTest :

TEST_P(DenseSlicedTest, Basic) {
auto tparam = GetParam();
auto params = tatami_test::convert_access_parameters(std::get<1>(tparam));
auto opts = tatami_test::convert_test_access_options(std::get<1>(tparam));
auto block = std::get<2>(tparam);

{
auto len = params.use_row ? ref->ncol() : ref->nrow();
size_t FIRST = block.first * len, LAST = block.second * len;
tatami_test::test_block_access(params, mat.get(), ref.get(), FIRST, LAST);
}

{
auto len = params.use_row ? tref->ncol() : tref->nrow();
size_t FIRST = block.first * len, LAST = block.second * len;
tatami_test::test_block_access(params, tmat.get(), tref.get(), FIRST, LAST);
}
tatami_test::test_block_access(*mat, *ref, block.first, block.second, opts);
tatami_test::test_block_access(*tmat, *tref, block.first, block.second, opts);
}

INSTANTIATE_TEST_SUITE_P(
DenseMatrix,
DenseSlicedTest,
::testing::Combine(
DenseMatrixTestCore::create_combinations(),
tatami_test::standard_test_access_parameter_combinations(),
tatami_test::standard_test_access_options_combinations(),
::testing::Values(
std::make_pair(0.0, 0.5),
std::make_pair(0.25, 0.75),
std::make_pair(0.51, 1.0)
std::make_pair(0.25, 0.5),
std::make_pair(0.51, 0.4)
)
)
);
Expand All @@ -200,65 +195,57 @@ INSTANTIATE_TEST_SUITE_P(
*************************************/

class DenseIndexedTest :
public ::testing::TestWithParam<std::tuple<DenseMatrixTestCore::SimulationParameters, tatami_test::StandardTestAccessParameters, std::pair<double, int> > >,
public ::testing::TestWithParam<std::tuple<DenseMatrixTestCore::SimulationParameters, tatami_test::StandardTestAccessOptions, std::pair<double, double> > >,
public DenseMatrixTestCore {
protected:
void SetUp() {
assemble(std::get<0>(GetParam()));
}
};

TEST_P(DenseIndexedTest, Basic) {
auto tparam = GetParam();
auto params = tatami_test::convert_access_parameters(std::get<1>(tparam));
auto opts = tatami_test::convert_test_access_options(std::get<1>(tparam));
auto index = std::get<2>(tparam);

{
auto len = params.use_row ? ref->ncol() : ref->nrow();
size_t FIRST = index.first * len, STEP = index.second;
tatami_test::test_indexed_access(params, mat.get(), ref.get(), FIRST, STEP);
}

{
auto len = params.use_row ? tref->ncol() : tref->nrow();
size_t FIRST = index.first * len, STEP = index.second;
tatami_test::test_indexed_access(params, tmat.get(), tref.get(), FIRST, STEP);
}
tatami_test::test_indexed_access(*mat, *ref, index.first, index.second, opts);
tatami_test::test_indexed_access(*tmat, *tref, index.first, index.second, opts);
}

INSTANTIATE_TEST_SUITE_P(
DenseMatrix,
DenseIndexedTest,
::testing::Combine(
DenseMatrixTestCore::create_combinations(),
tatami_test::standard_test_access_parameter_combinations(),
tatami_test::standard_test_access_options_combinations(),
::testing::Values(
std::make_pair(0.3, 5),
std::make_pair(0.11, 9),
std::make_pair(0.4, 7)
std::make_pair(0.3, 0.2),
std::make_pair(0.11, 0.11),
std::make_pair(0.4, 0.15)
)
)
);

/*************************************
*************************************/

class DenseMatrixCacheTypeTest : public ::testing::TestWithParam<std::tuple<bool, bool> >, public DenseMatrixTestCore {};
class DenseMatrixCacheTypeTest :
public ::testing::TestWithParam<std::tuple<bool, bool> >,
public DenseMatrixTestCore {};

TEST_P(DenseMatrixCacheTypeTest, CastToInt) {
assemble(SimulationParameters(std::make_pair(9, 13), 1));

tatami_hdf5::DenseMatrix<double, int, int> mat(fpath, name, false);
auto altref = tatami::convert_to_dense<double, int, int>(ref.get(), true);

tatami_test::TestAccessParameters params;
auto tparam = GetParam();
params.use_row = std::get<0>(tparam);
params.use_oracle = std::get<1>(tparam);
tatami_test::TestAccessOptions opts;
opts.use_row = std::get<0>(tparam);
opts.use_oracle = std::get<1>(tparam);

tatami_test::test_full_access(params, &mat, altref.get());
tatami_test::test_block_access(params, &mat, altref.get(), 5, 20);
tatami_test::test_indexed_access(params, &mat, altref.get(), 3, 5);
tatami_test::test_full_access(mat, *altref, opts);
tatami_test::test_block_access(mat, *altref, 0.2, 0.55, opts);
tatami_test::test_indexed_access(mat, *altref, 0.3, 0.2, opts);
}

INSTANTIATE_TEST_SUITE_P(
Expand Down
45 changes: 26 additions & 19 deletions tests/src/load_compressed_sparse_matrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "tatami_hdf5/load_compressed_sparse_matrix.hpp"

#include "tatami_test/tatami_test.hpp"
#include "tatami_test/temp_file_path.hpp"
#include "temp_file_path.h"

#include <vector>
#include <random>
Expand All @@ -14,20 +14,26 @@ TEST(LoadCompressedSparseMatrixTest, Basic) {
const size_t NR = 200, NC = 100;

// Dumping a sparse matrix.
auto fpath = tatami_test::temp_file_path("tatami-sparse-test.h5");
auto fpath = temp_file_path("tatami-sparse-test.h5");
std::string name = "stuff";
auto triplets = tatami_test::simulate_sparse_compressed<double>(NR, NC, 0.05, 0, 100);
auto triplets = tatami_test::simulate_compressed_sparse<double, int>(NR, NC, []{
tatami_test::SimulateCompressedSparseOptions opt;
opt.density = 0.05;
opt.lower = 0;
opt.upper = 100;
return opt;
}());

{
H5::H5File fhandle(fpath, H5F_ACC_TRUNC);
auto ghandle = fhandle.createGroup(name);

hsize_t dims = triplets.value.size();
hsize_t dims = triplets.data.size();
H5::DataSpace dspace(1, &dims);
{
H5::DataType dtype(H5::PredType::NATIVE_DOUBLE);
auto dhandle = ghandle.createDataSet("data", dtype, dspace);
dhandle.write(triplets.value.data(), H5::PredType::NATIVE_DOUBLE);
dhandle.write(triplets.data.data(), H5::PredType::NATIVE_DOUBLE);
}

{
Expand All @@ -37,11 +43,12 @@ TEST(LoadCompressedSparseMatrixTest, Basic) {
}

{
hsize_t ncp1 = triplets.ptr.size();
hsize_t ncp1 = triplets.indptr.size();
H5::DataSpace dspace(1, &ncp1);
H5::DataType dtype(H5::PredType::NATIVE_UINT64);
auto dhandle = ghandle.createDataSet("indptr", dtype, dspace);
dhandle.write(triplets.ptr.data(), H5::PredType::NATIVE_LONG);
std::vector<uint64_t> copy(triplets.indptr.begin(), triplets.indptr.end()); // making a copy as size_t doesn't have a HDF5 datatype.
dhandle.write(copy.data(), H5::PredType::NATIVE_UINT64);
}
}

Expand All @@ -51,12 +58,12 @@ TEST(LoadCompressedSparseMatrixTest, Basic) {
tatami::CompressedSparseRowMatrix<
double,
int,
decltype(triplets.value),
decltype(triplets.data),
decltype(triplets.index),
decltype(triplets.ptr)
> ref(NR, NC, triplets.value, triplets.index, triplets.ptr);
decltype(triplets.indptr)
> ref(NR, NC, triplets.data, triplets.index, triplets.indptr);

tatami_test::test_simple_row_access(mat.get(), &ref);
tatami_test::test_simple_row_access(*mat, ref);
}

// Pretending it's a CSC matrix.
Expand All @@ -65,12 +72,12 @@ TEST(LoadCompressedSparseMatrixTest, Basic) {
tatami::CompressedSparseColumnMatrix<
double,
int,
decltype(triplets.value),
decltype(triplets.data),
decltype(triplets.index),
decltype(triplets.ptr)
> ref(NC, NR, triplets.value, triplets.index, triplets.ptr);
decltype(triplets.indptr)
> ref(NC, NR, triplets.data, triplets.index, triplets.indptr);

tatami_test::test_simple_column_access(mat.get(), &ref);
tatami_test::test_simple_column_access(*mat, ref);
}

// Trying a variety of storage types.
Expand All @@ -83,7 +90,7 @@ TEST(LoadCompressedSparseMatrixTest, Basic) {
std::vector<uint64_t>
>(NR, NC, fpath, name + "/data", name + "/index", name + "/indptr", true);

std::vector<double> truncated = triplets.value;
std::vector<double> truncated = triplets.data;
for (auto& x : truncated) {
x = std::trunc(x);
}
Expand All @@ -93,9 +100,9 @@ TEST(LoadCompressedSparseMatrixTest, Basic) {
int,
decltype(truncated),
decltype(triplets.index),
decltype(triplets.ptr)
> ref(NR, NC, std::move(truncated), triplets.index, triplets.ptr);
decltype(triplets.indptr)
> ref(NR, NC, std::move(truncated), triplets.index, triplets.indptr);

tatami_test::test_simple_column_access(mat.get(), &ref);
tatami_test::test_simple_column_access(*mat, ref);
}
}
17 changes: 11 additions & 6 deletions tests/src/load_dense_matrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,22 @@
#include "tatami_hdf5/load_dense_matrix.hpp"

#include "tatami_test/tatami_test.hpp"
#include "tatami_test/temp_file_path.hpp"
#include "temp_file_path.h"

#include <vector>
#include <random>

TEST(LoadDenseMatrixTest, Basic) {
size_t NR = 200, NC = 100;
auto fpath = tatami_test::temp_file_path("tatami-dense-test.h5");
auto fpath = temp_file_path("tatami-dense-test.h5");
std::string name = "stuff";

std::vector<double> values = tatami_test::simulate_dense_vector<double>(NR * NC, 0, 100);
std::vector<double> values = tatami_test::simulate_vector<double>(NR * NC, []{
tatami_test::SimulateVectorOptions opt;
opt.lower = 0;
opt.upper = 100;
return opt;
}());

{
H5::H5File fhandle(fpath, H5F_ACC_TRUNC);
Expand All @@ -32,14 +37,14 @@ TEST(LoadDenseMatrixTest, Basic) {
{
auto mat = tatami_hdf5::load_dense_matrix<double, int>(fpath, name, false);
tatami::DenseRowMatrix<double, int> ref(NR, NC, values);
tatami_test::test_simple_row_access(mat.get(), &ref);
tatami_test::test_simple_row_access(*mat, ref);
}

// Pretending it's a column-major matrix.
{
auto mat = tatami_hdf5::load_dense_matrix<double, int, std::vector<double> >(fpath, name, true);
tatami::DenseColumnMatrix<double, int> ref(NC, NR, values);
tatami_test::test_simple_column_access(mat.get(), &ref);
tatami_test::test_simple_column_access(*mat, ref);
}

// Trying a different storage type.
Expand All @@ -52,6 +57,6 @@ TEST(LoadDenseMatrixTest, Basic) {
}
tatami::DenseRowMatrix<double, int> ref(NR, NC, std::move(truncated));

tatami_test::test_simple_column_access(mat.get(), &ref);
tatami_test::test_simple_column_access(*mat, ref);
}
}
Loading

0 comments on commit 0835a95

Please sign in to comment.