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
  • Loading branch information
LTLA committed Nov 27, 2024
1 parent d714d7f commit d37a9fe
Show file tree
Hide file tree
Showing 9 changed files with 409 additions and 98 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(GoogleTest)

Expand All @@ -25,8 +17,8 @@ endif()
macro(decorate_test target)
target_link_libraries(
${target}
gtest_main
tatami_stats
tatami_test
)

target_compile_options(${target} PRIVATE -Wall -Wextra -Wpedantic -Werror)
Expand Down
58 changes: 44 additions & 14 deletions tests/src/counts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@

TEST(ComputingDimCounts, RowNaNCounts) {
size_t NR = 99, NC = 152;
auto dump = tatami_test::simulate_sparse_vector<double>(NR * NC, 0.1);
auto dump = tatami_test::simulate_vector<double>(NR * NC, []{
tatami_test::SimulateVectorOptions opt;
opt.density = 0.1;
opt.seed = 324987;
return opt;
}());
for (auto& x : dump) {
if (x == 0) {
x = std::numeric_limits<double>::quiet_NaN();
Expand Down Expand Up @@ -40,15 +45,20 @@ TEST(ComputingDimCounts, RowNaNCounts) {
EXPECT_EQ(ref, tatami_stats::counts::nan::by_row(sparse_column.get(), nopt));

// Checking same results from matrices that can yield unsorted indices.
std::shared_ptr<tatami::NumericMatrix> unsorted_row(new tatami_test::UnsortedWrapper<double, int>(sparse_row));
std::shared_ptr<tatami::NumericMatrix> unsorted_row(new tatami_test::ReversedIndicesWrapper<double, int>(sparse_row));
EXPECT_EQ(ref, tatami_stats::counts::nan::by_row(unsorted_row.get()));
std::shared_ptr<tatami::NumericMatrix> unsorted_column(new tatami_test::UnsortedWrapper<double, int>(sparse_column));
std::shared_ptr<tatami::NumericMatrix> unsorted_column(new tatami_test::ReversedIndicesWrapper<double, int>(sparse_column));
EXPECT_EQ(ref, tatami_stats::counts::nan::by_row(unsorted_column.get()));
}

TEST(ComputingDimCounts, ColumNaNCount) {
size_t NR = 79, NC = 62;
auto dump = tatami_test::simulate_sparse_vector<double>(NR * NC, 0.1);
auto dump = tatami_test::simulate_vector<double>(NR * NC, []{
tatami_test::SimulateVectorOptions opt;
opt.density = 0.1;
opt.seed = 28374928;
return opt;
}());
for (auto& x : dump) {
if (x == 0) {
x = std::numeric_limits<double>::quiet_NaN();
Expand Down Expand Up @@ -81,15 +91,20 @@ TEST(ComputingDimCounts, ColumNaNCount) {
EXPECT_EQ(ref, tatami_stats::counts::nan::by_column(sparse_column.get(), nopt));

// Checking same results from matrices that can yield unsorted indices.
std::shared_ptr<tatami::NumericMatrix> unsorted_row(new tatami_test::UnsortedWrapper<double, int>(sparse_row));
std::shared_ptr<tatami::NumericMatrix> unsorted_row(new tatami_test::ReversedIndicesWrapper<double, int>(sparse_row));
EXPECT_EQ(ref, tatami_stats::counts::nan::by_column(unsorted_row.get()));
std::shared_ptr<tatami::NumericMatrix> unsorted_column(new tatami_test::UnsortedWrapper<double, int>(sparse_column));
std::shared_ptr<tatami::NumericMatrix> unsorted_column(new tatami_test::ReversedIndicesWrapper<double, int>(sparse_column));
EXPECT_EQ(ref, tatami_stats::counts::nan::by_column(unsorted_column.get()));
}

TEST(ComputingDimCounts, RowZeroCounts) {
size_t NR = 99, NC = 152;
auto dump = tatami_test::simulate_sparse_vector<double>(NR * NC, 0.1);
auto dump = tatami_test::simulate_vector<double>(NR * NC, []{
tatami_test::SimulateVectorOptions opt;
opt.density = 0.1;
opt.seed = 958734;
return opt;
}());

auto dense_row = std::unique_ptr<tatami::NumericMatrix>(new tatami::DenseRowMatrix<double, int>(NR, NC, dump));
auto dense_column = tatami::convert_to_dense(dense_row.get(), false);
Expand Down Expand Up @@ -117,15 +132,20 @@ TEST(ComputingDimCounts, RowZeroCounts) {
EXPECT_EQ(ref, tatami_stats::counts::zero::by_row(sparse_column.get(), nopt));

// Checking same results from matrices that can yield unsorted indices.
std::shared_ptr<tatami::NumericMatrix> unsorted_row(new tatami_test::UnsortedWrapper<double, int>(sparse_row));
std::shared_ptr<tatami::NumericMatrix> unsorted_row(new tatami_test::ReversedIndicesWrapper<double, int>(sparse_row));
EXPECT_EQ(ref, tatami_stats::counts::zero::by_row(unsorted_row.get()));
std::shared_ptr<tatami::NumericMatrix> unsorted_column(new tatami_test::UnsortedWrapper<double, int>(sparse_column));
std::shared_ptr<tatami::NumericMatrix> unsorted_column(new tatami_test::ReversedIndicesWrapper<double, int>(sparse_column));
EXPECT_EQ(ref, tatami_stats::counts::zero::by_row(unsorted_column.get()));
}

TEST(ComputingDimVariances, RowZeroCountsWithNan) {
size_t NR = 52, NC = 83;
auto dump = tatami_test::simulate_sparse_vector<double>(NR * NC, 0.1);
auto dump = tatami_test::simulate_vector<double>(NR * NC, []{
tatami_test::SimulateVectorOptions opt;
opt.density = 0.1;
opt.seed = 3248927;
return opt;
}());
for (size_t r = 0; r < NR; ++r) { // Injecting an NaN at the start.
dump[r * NC] = std::numeric_limits<double>::quiet_NaN();
}
Expand All @@ -150,7 +170,12 @@ TEST(ComputingDimVariances, RowZeroCountsWithNan) {

TEST(ComputingDimCounts, ColumnZeroCounts) {
size_t NR = 79, NC = 62;
auto dump = tatami_test::simulate_sparse_vector<double>(NR * NC, 0.1);
auto dump = tatami_test::simulate_vector<double>(NR * NC, []{
tatami_test::SimulateVectorOptions opt;
opt.density = 0.1;
opt.seed = 93942;
return opt;
}());

auto dense_row = std::unique_ptr<tatami::NumericMatrix>(new tatami::DenseRowMatrix<double, int>(NR, NC, dump));
auto dense_column = tatami::convert_to_dense(dense_row.get(), false);
Expand Down Expand Up @@ -178,15 +203,20 @@ TEST(ComputingDimCounts, ColumnZeroCounts) {
EXPECT_EQ(ref, tatami_stats::counts::zero::by_column(sparse_column.get(), nopt));

// Checking same results from matrices that can yield unsorted indices.
std::shared_ptr<tatami::NumericMatrix> unsorted_row(new tatami_test::UnsortedWrapper<double, int>(sparse_row));
std::shared_ptr<tatami::NumericMatrix> unsorted_row(new tatami_test::ReversedIndicesWrapper<double, int>(sparse_row));
EXPECT_EQ(ref, tatami_stats::counts::zero::by_column(unsorted_row.get()));
std::shared_ptr<tatami::NumericMatrix> unsorted_column(new tatami_test::UnsortedWrapper<double, int>(sparse_column));
std::shared_ptr<tatami::NumericMatrix> unsorted_column(new tatami_test::ReversedIndicesWrapper<double, int>(sparse_column));
EXPECT_EQ(ref, tatami_stats::counts::zero::by_column(unsorted_column.get()));
}

TEST(ComputingDimVariances, ColumnZeroCountsWithNan) {
size_t NR = 82, NC = 33;
auto dump = tatami_test::simulate_sparse_vector<double>(NR * NC, 0.1);
auto dump = tatami_test::simulate_vector<double>(NR * NC, []{
tatami_test::SimulateVectorOptions opt;
opt.density = 0.1;
opt.seed = 87918;
return opt;
}());
for (size_t c = 0; c < NC; ++c) { // Injecting an NaN at the start.
dump[c] = std::numeric_limits<double>::quiet_NaN();
}
Expand Down
70 changes: 59 additions & 11 deletions tests/src/grouped_medians.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,15 @@ TEST(GroupedMedians, ByRow) {
// need to use the structural zeros. We also put all non-zero values on
// one side of zero, otherwise the structural zeros will dominate the
// median; in this case, we choose all-negative values.
auto simulated = tatami_test::simulate_sparse_vector<double>(NR * NC, 0.5, -10, -2);
auto simulated = tatami_test::simulate_vector<double>(NR * NC, []{
tatami_test::SimulateVectorOptions opt;
opt.density = 0.5;
opt.lower = -10;
opt.upper = -2;
opt.seed = 192836;
return opt;
}());

auto dense_row = std::shared_ptr<tatami::NumericMatrix>(new tatami::DenseRowMatrix<double, int>(NR, NC, std::move(simulated)));
auto dense_column = tatami::convert_to_dense(dense_row.get(), false);
auto sparse_row = tatami::convert_to_compressed_sparse(dense_row.get(), true);
Expand Down Expand Up @@ -47,17 +55,24 @@ TEST(GroupedMedians, ByRow) {
EXPECT_EQ(expected, tatami_stats::grouped_medians::by_row(sparse_column.get(), cgroups.data(), mopt));

// Checking same results from matrices that can yield unsorted indices.
std::shared_ptr<tatami::NumericMatrix> unsorted_row(new tatami_test::UnsortedWrapper<double, int>(sparse_row));
std::shared_ptr<tatami::NumericMatrix> unsorted_row(new tatami_test::ReversedIndicesWrapper<double, int>(sparse_row));
EXPECT_EQ(expected, tatami_stats::grouped_medians::by_row(unsorted_row.get(), cgroups.data()));
std::shared_ptr<tatami::NumericMatrix> unsorted_column(new tatami_test::UnsortedWrapper<double, int>(sparse_column));
std::shared_ptr<tatami::NumericMatrix> unsorted_column(new tatami_test::ReversedIndicesWrapper<double, int>(sparse_column));
EXPECT_EQ(expected, tatami_stats::grouped_medians::by_row(unsorted_column.get(), cgroups.data()));
}

TEST(GroupedMedians, ByRowWithNan) {
size_t NR = 99, NC = 155;

// We use a density of 0.5, plus sprinkling in some NaNs.
auto simulated = tatami_test::simulate_sparse_vector<double>(NR * NC, 0.5, -10, -2);
// We use a density of 0.5 as described above, plus sprinkling in some NaNs.
auto simulated = tatami_test::simulate_vector<double>(NR * NC, []{
tatami_test::SimulateVectorOptions opt;
opt.density = 0.5;
opt.lower = -10;
opt.upper = -2;
opt.seed = 1297836;
return opt;
}());
for (size_t r = 0; r < NR; ++r) {
simulated[r * NC + (r % NC)] = std::numeric_limits<double>::quiet_NaN();
}
Expand Down Expand Up @@ -95,7 +110,15 @@ TEST(GroupedMedians, ByColumn) {
size_t NR = 56, NC = 179;

// See above for why we use a density of 0.5. This time, we use all-positive values.
auto simulated = tatami_test::simulate_sparse_vector<double>(NR * NC, 0.5, 0.1, 2);
auto simulated = tatami_test::simulate_vector<double>(NR * NC, []{
tatami_test::SimulateVectorOptions opt;
opt.density = 0.5;
opt.lower = 0.1;
opt.upper = 2;
opt.seed = 239847;
return opt;
}());

auto dense_row = std::shared_ptr<tatami::NumericMatrix>(new tatami::DenseRowMatrix<double, int>(NR, NC, std::move(simulated)));
auto dense_column = tatami::convert_to_dense(dense_row.get(), false);
auto sparse_row = tatami::convert_to_compressed_sparse(dense_row.get(), true);
Expand Down Expand Up @@ -129,17 +152,25 @@ TEST(GroupedMedians, ByColumn) {
EXPECT_EQ(expected, tatami_stats::grouped_medians::by_column(sparse_column.get(), rgroups.data(), mopt));

// Checking same results from matrices that can yield unsorted indices.
std::shared_ptr<tatami::NumericMatrix> unsorted_row(new tatami_test::UnsortedWrapper<double, int>(sparse_row));
std::shared_ptr<tatami::NumericMatrix> unsorted_row(new tatami_test::ReversedIndicesWrapper<double, int>(sparse_row));
EXPECT_EQ(expected, tatami_stats::grouped_medians::by_column(unsorted_row.get(), rgroups.data()));
std::shared_ptr<tatami::NumericMatrix> unsorted_column(new tatami_test::UnsortedWrapper<double, int>(sparse_column));
std::shared_ptr<tatami::NumericMatrix> unsorted_column(new tatami_test::ReversedIndicesWrapper<double, int>(sparse_column));
EXPECT_EQ(expected, tatami_stats::grouped_medians::by_column(unsorted_column.get(), rgroups.data()));
}

TEST(GroupedMedians, ByColumnWithNan) {
size_t NR = 99, NC = 155;

// We use a density of 0.5, plus sprinkling in some NaNs.
auto simulated = tatami_test::simulate_sparse_vector<double>(NR * NC, 0.5, 1, 2);
auto simulated = tatami_test::simulate_vector<double>(NR * NC, []{
tatami_test::SimulateVectorOptions opt;
opt.density = 0.5;
opt.lower = 1;
opt.upper = 2;
opt.seed = 9428378;
return opt;
}());

for (size_t c = 0; c < NC; ++c) {
simulated[(c % NR) * NC + c] = std::numeric_limits<double>::quiet_NaN();
}
Expand Down Expand Up @@ -192,7 +223,16 @@ TEST(GroupedMedians, EdgeCases) {

TEST(GroupedMedians, NewType) {
size_t NR = 98, NC = 152;
auto dump = tatami_test::simulate_sparse_vector<double>(NR * NC, 0.1, /* lower = */ 1, /* upper = */ 100);

auto dump = tatami_test::simulate_vector<double>(NR * NC, []{
tatami_test::SimulateVectorOptions opt;
opt.density = 0.5; // see above for why we use a density of 0.5.
opt.lower = 1;
opt.upper = 100;
opt.seed = 2398472;
return opt;
}());

for (auto& d : dump) {
d = std::round(d);
}
Expand Down Expand Up @@ -230,7 +270,15 @@ TEST(GroupedMedians, DirtyOutputs) {
size_t NR = 56, NC = 179;

// See above for why we use a density of 0.5.
auto simulated = tatami_test::simulate_sparse_vector<double>(NR * NC, 0.5, -3, -0.5);
auto simulated = tatami_test::simulate_vector<double>(NR * NC, []{
tatami_test::SimulateVectorOptions opt;
opt.density = 0.5;
opt.lower = -3;
opt.upper = -0.5;
opt.seed = 1293786127;
return opt;
}());

auto dense_row = std::shared_ptr<tatami::NumericMatrix>(new tatami::DenseRowMatrix<double, int>(NR, NC, std::move(simulated)));
auto dense_column = tatami::convert_to_dense(dense_row.get(), false);
auto sparse_row = tatami::convert_to_compressed_sparse(dense_row.get(), true);
Expand Down
Loading

0 comments on commit d37a9fe

Please sign in to comment.