diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index e069462..b5a6016 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -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) @@ -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) diff --git a/tests/src/counts.cpp b/tests/src/counts.cpp index 9139209..fe68516 100644 --- a/tests/src/counts.cpp +++ b/tests/src/counts.cpp @@ -7,7 +7,12 @@ TEST(ComputingDimCounts, RowNaNCounts) { size_t NR = 99, NC = 152; - auto dump = tatami_test::simulate_sparse_vector(NR * NC, 0.1); + auto dump = tatami_test::simulate_vector(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::quiet_NaN(); @@ -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 unsorted_row(new tatami_test::UnsortedWrapper(sparse_row)); + std::shared_ptr unsorted_row(new tatami_test::ReversedIndicesWrapper(sparse_row)); EXPECT_EQ(ref, tatami_stats::counts::nan::by_row(unsorted_row.get())); - std::shared_ptr unsorted_column(new tatami_test::UnsortedWrapper(sparse_column)); + std::shared_ptr unsorted_column(new tatami_test::ReversedIndicesWrapper(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(NR * NC, 0.1); + auto dump = tatami_test::simulate_vector(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::quiet_NaN(); @@ -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 unsorted_row(new tatami_test::UnsortedWrapper(sparse_row)); + std::shared_ptr unsorted_row(new tatami_test::ReversedIndicesWrapper(sparse_row)); EXPECT_EQ(ref, tatami_stats::counts::nan::by_column(unsorted_row.get())); - std::shared_ptr unsorted_column(new tatami_test::UnsortedWrapper(sparse_column)); + std::shared_ptr unsorted_column(new tatami_test::ReversedIndicesWrapper(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(NR * NC, 0.1); + auto dump = tatami_test::simulate_vector(NR * NC, []{ + tatami_test::SimulateVectorOptions opt; + opt.density = 0.1; + opt.seed = 958734; + return opt; + }()); auto dense_row = std::unique_ptr(new tatami::DenseRowMatrix(NR, NC, dump)); auto dense_column = tatami::convert_to_dense(dense_row.get(), false); @@ -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 unsorted_row(new tatami_test::UnsortedWrapper(sparse_row)); + std::shared_ptr unsorted_row(new tatami_test::ReversedIndicesWrapper(sparse_row)); EXPECT_EQ(ref, tatami_stats::counts::zero::by_row(unsorted_row.get())); - std::shared_ptr unsorted_column(new tatami_test::UnsortedWrapper(sparse_column)); + std::shared_ptr unsorted_column(new tatami_test::ReversedIndicesWrapper(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(NR * NC, 0.1); + auto dump = tatami_test::simulate_vector(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::quiet_NaN(); } @@ -150,7 +170,12 @@ TEST(ComputingDimVariances, RowZeroCountsWithNan) { TEST(ComputingDimCounts, ColumnZeroCounts) { size_t NR = 79, NC = 62; - auto dump = tatami_test::simulate_sparse_vector(NR * NC, 0.1); + auto dump = tatami_test::simulate_vector(NR * NC, []{ + tatami_test::SimulateVectorOptions opt; + opt.density = 0.1; + opt.seed = 93942; + return opt; + }()); auto dense_row = std::unique_ptr(new tatami::DenseRowMatrix(NR, NC, dump)); auto dense_column = tatami::convert_to_dense(dense_row.get(), false); @@ -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 unsorted_row(new tatami_test::UnsortedWrapper(sparse_row)); + std::shared_ptr unsorted_row(new tatami_test::ReversedIndicesWrapper(sparse_row)); EXPECT_EQ(ref, tatami_stats::counts::zero::by_column(unsorted_row.get())); - std::shared_ptr unsorted_column(new tatami_test::UnsortedWrapper(sparse_column)); + std::shared_ptr unsorted_column(new tatami_test::ReversedIndicesWrapper(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(NR * NC, 0.1); + auto dump = tatami_test::simulate_vector(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::quiet_NaN(); } diff --git a/tests/src/grouped_medians.cpp b/tests/src/grouped_medians.cpp index 96471f9..7d002c9 100644 --- a/tests/src/grouped_medians.cpp +++ b/tests/src/grouped_medians.cpp @@ -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(NR * NC, 0.5, -10, -2); + auto simulated = tatami_test::simulate_vector(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(new tatami::DenseRowMatrix(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); @@ -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 unsorted_row(new tatami_test::UnsortedWrapper(sparse_row)); + std::shared_ptr unsorted_row(new tatami_test::ReversedIndicesWrapper(sparse_row)); EXPECT_EQ(expected, tatami_stats::grouped_medians::by_row(unsorted_row.get(), cgroups.data())); - std::shared_ptr unsorted_column(new tatami_test::UnsortedWrapper(sparse_column)); + std::shared_ptr unsorted_column(new tatami_test::ReversedIndicesWrapper(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(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(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::quiet_NaN(); } @@ -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(NR * NC, 0.5, 0.1, 2); + auto simulated = tatami_test::simulate_vector(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(new tatami::DenseRowMatrix(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); @@ -129,9 +152,9 @@ 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 unsorted_row(new tatami_test::UnsortedWrapper(sparse_row)); + std::shared_ptr unsorted_row(new tatami_test::ReversedIndicesWrapper(sparse_row)); EXPECT_EQ(expected, tatami_stats::grouped_medians::by_column(unsorted_row.get(), rgroups.data())); - std::shared_ptr unsorted_column(new tatami_test::UnsortedWrapper(sparse_column)); + std::shared_ptr unsorted_column(new tatami_test::ReversedIndicesWrapper(sparse_column)); EXPECT_EQ(expected, tatami_stats::grouped_medians::by_column(unsorted_column.get(), rgroups.data())); } @@ -139,7 +162,15 @@ 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(NR * NC, 0.5, 1, 2); + auto simulated = tatami_test::simulate_vector(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::quiet_NaN(); } @@ -192,7 +223,16 @@ TEST(GroupedMedians, EdgeCases) { TEST(GroupedMedians, NewType) { size_t NR = 98, NC = 152; - auto dump = tatami_test::simulate_sparse_vector(NR * NC, 0.1, /* lower = */ 1, /* upper = */ 100); + + auto dump = tatami_test::simulate_vector(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); } @@ -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(NR * NC, 0.5, -3, -0.5); + auto simulated = tatami_test::simulate_vector(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(new tatami::DenseRowMatrix(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); diff --git a/tests/src/grouped_sums.cpp b/tests/src/grouped_sums.cpp index 97cb6d1..3148d7e 100644 --- a/tests/src/grouped_sums.cpp +++ b/tests/src/grouped_sums.cpp @@ -9,8 +9,13 @@ TEST(GroupedSums, ByRow) { size_t NR = 99, NC = 155; + auto simulated = tatami_test::simulate_vector(NR * NC, []{ + tatami_test::SimulateVectorOptions opt; + opt.density = 0.2; + opt.seed = 239847612; + return opt; + }()); - auto simulated = tatami_test::simulate_sparse_vector(NR * NC, 0.2); auto dense_row = std::shared_ptr(new tatami::DenseRowMatrix(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); @@ -44,9 +49,9 @@ TEST(GroupedSums, ByRow) { EXPECT_EQ(expected, tatami_stats::grouped_sums::by_row(sparse_column.get(), cgroups.data(), sopt)); // Checking same results from matrices that can yield unsorted indices. - std::shared_ptr unsorted_row(new tatami_test::UnsortedWrapper(sparse_row)); + std::shared_ptr unsorted_row(new tatami_test::ReversedIndicesWrapper(sparse_row)); EXPECT_EQ(expected, tatami_stats::grouped_sums::by_row(unsorted_row.get(), cgroups.data())); - std::shared_ptr unsorted_column(new tatami_test::UnsortedWrapper(sparse_column)); + std::shared_ptr unsorted_column(new tatami_test::ReversedIndicesWrapper(sparse_column)); EXPECT_EQ(expected, tatami_stats::grouped_sums::by_row(unsorted_column.get(), cgroups.data())); } @@ -54,7 +59,14 @@ TEST(GroupedSums, ByRowWithNan) { size_t NR = 99, NC = 155; // Sprinkling in some NaNs. - auto simulated = tatami_test::simulate_sparse_vector(NR * NC, 0.5, -10, -2); + auto simulated = tatami_test::simulate_vector(NR * NC, []{ + tatami_test::SimulateVectorOptions opt; + opt.density = 0.15; + opt.lower = -10; + opt.upper = -2; + opt.seed = 23098472; + return opt; + }()); for (size_t r = 0; r < NR; ++r) { simulated[r * NC + (r % NC)] = std::numeric_limits::quiet_NaN(); } @@ -91,7 +103,13 @@ TEST(GroupedSums, ByRowWithNan) { TEST(GroupedSums, ByColumn) { size_t NR = 56, NC = 179; - auto simulated = tatami_test::simulate_sparse_vector(NR * NC, 0.25); + auto simulated = tatami_test::simulate_vector(NR * NC, []{ + tatami_test::SimulateVectorOptions opt; + opt.density = 0.25; + opt.seed = 593871; + return opt; + }()); + auto dense_row = std::shared_ptr(new tatami::DenseRowMatrix(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); @@ -125,9 +143,9 @@ TEST(GroupedSums, ByColumn) { EXPECT_EQ(expected, tatami_stats::grouped_sums::by_column(sparse_column.get(), rgroups.data(), sopt)); // Checking same results from matrices that can yield unsorted indices. - std::shared_ptr unsorted_row(new tatami_test::UnsortedWrapper(sparse_row)); + std::shared_ptr unsorted_row(new tatami_test::ReversedIndicesWrapper(sparse_row)); EXPECT_EQ(expected, tatami_stats::grouped_sums::by_column(unsorted_row.get(), rgroups.data())); - std::shared_ptr unsorted_column(new tatami_test::UnsortedWrapper(sparse_column)); + std::shared_ptr unsorted_column(new tatami_test::ReversedIndicesWrapper(sparse_column)); EXPECT_EQ(expected, tatami_stats::grouped_sums::by_column(unsorted_column.get(), rgroups.data())); } @@ -135,7 +153,14 @@ TEST(GroupedSums, ByColumnWithNan) { size_t NR = 99, NC = 155; // Sprinkling in some NaNs. - auto simulated = tatami_test::simulate_sparse_vector(NR * NC, 0.5, 1, 2); + auto simulated = tatami_test::simulate_vector(NR * NC, []{ + tatami_test::SimulateVectorOptions opt; + opt.density = 0.18; + opt.lower = 1; + opt.upper = 2; + opt.seed = 1923871; + return opt; + }()); for (size_t c = 0; c < NC; ++c) { simulated[(c % NR) * NC + c] = std::numeric_limits::quiet_NaN(); } @@ -188,7 +213,14 @@ TEST(GroupedSums, EdgeCases) { TEST(GroupedSums, NewType) { size_t NR = 98, NC = 152; - auto dump = tatami_test::simulate_sparse_vector(NR * NC, 0.1, /* lower = */ 1, /* upper = */ 100); + auto dump = tatami_test::simulate_vector(NR * NC, []{ + tatami_test::SimulateVectorOptions opt; + opt.density = 0.1; + opt.lower = 1; + opt.upper = 100; + opt.seed = 717879; + return opt; + }()); for (auto& d : dump) { d = std::round(d); } @@ -225,7 +257,13 @@ TEST(GroupedSums, NewType) { TEST(GroupedSums, DirtyOutputs) { size_t NR = 56, NC = 179; - auto simulated = tatami_test::simulate_sparse_vector(NR * NC, 0.3); + auto simulated = tatami_test::simulate_vector(NR * NC, []{ + tatami_test::SimulateVectorOptions opt; + opt.density = 0.3; + opt.seed = 192191; + return opt; + }()); + auto dense_row = std::shared_ptr(new tatami::DenseRowMatrix(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); diff --git a/tests/src/grouped_variances.cpp b/tests/src/grouped_variances.cpp index 5d9311b..152fff1 100644 --- a/tests/src/grouped_variances.cpp +++ b/tests/src/grouped_variances.cpp @@ -18,7 +18,13 @@ void compare_double_vectors_of_vectors(const L_& left, const R_& right) { TEST(GroupedVariances, ByRow) { size_t NR = 99, NC = 155; - auto simulated = tatami_test::simulate_sparse_vector(NR * NC, 0.2); + auto simulated = tatami_test::simulate_vector(NR * NC, []{ + tatami_test::SimulateVectorOptions opt; + opt.density = 0.2; + opt.seed = 1298191; + return opt; + }()); + auto dense_row = std::shared_ptr(new tatami::DenseRowMatrix(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); @@ -52,9 +58,9 @@ TEST(GroupedVariances, ByRow) { compare_double_vectors_of_vectors(expected, tatami_stats::grouped_variances::by_row(sparse_column.get(), cgroups.data(), sopt)); // Checking same results from matrices that can yield unsorted indices. - std::shared_ptr unsorted_row(new tatami_test::UnsortedWrapper(sparse_row)); + std::shared_ptr unsorted_row(new tatami_test::ReversedIndicesWrapper(sparse_row)); compare_double_vectors_of_vectors(expected, tatami_stats::grouped_variances::by_row(unsorted_row.get(), cgroups.data())); - std::shared_ptr unsorted_column(new tatami_test::UnsortedWrapper(sparse_column)); + std::shared_ptr unsorted_column(new tatami_test::ReversedIndicesWrapper(sparse_column)); compare_double_vectors_of_vectors(expected, tatami_stats::grouped_variances::by_row(unsorted_column.get(), cgroups.data())); } @@ -62,7 +68,14 @@ TEST(GroupedVariances, ByRowWithNan) { size_t NR = 99, NC = 155; // Sprinkling in some NaNs. - auto simulated = tatami_test::simulate_sparse_vector(NR * NC, 0.5, -10, -2); + auto simulated = tatami_test::simulate_vector(NR * NC, []{ + tatami_test::SimulateVectorOptions opt; + opt.density = 0.2; + opt.lower = -10; + opt.upper = -2; + opt.seed = 52827; + return opt; + }()); for (size_t r = 0; r < NR; ++r) { simulated[r * NC + (r % NC)] = std::numeric_limits::quiet_NaN(); } @@ -99,7 +112,13 @@ TEST(GroupedVariances, ByRowWithNan) { TEST(GroupedVariances, ByColumn) { size_t NR = 56, NC = 179; - auto simulated = tatami_test::simulate_sparse_vector(NR * NC, 0.25); + auto simulated = tatami_test::simulate_vector(NR * NC, []{ + tatami_test::SimulateVectorOptions opt; + opt.density = 0.25; + opt.seed = 83828; + return opt; + }()); + auto dense_row = std::shared_ptr(new tatami::DenseRowMatrix(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); @@ -133,9 +152,9 @@ TEST(GroupedVariances, ByColumn) { compare_double_vectors_of_vectors(expected, tatami_stats::grouped_variances::by_column(sparse_column.get(), rgroups.data(), sopt)); // Checking same results from matrices that can yield unsorted indices. - std::shared_ptr unsorted_row(new tatami_test::UnsortedWrapper(sparse_row)); + std::shared_ptr unsorted_row(new tatami_test::ReversedIndicesWrapper(sparse_row)); compare_double_vectors_of_vectors(expected, tatami_stats::grouped_variances::by_column(unsorted_row.get(), rgroups.data())); - std::shared_ptr unsorted_column(new tatami_test::UnsortedWrapper(sparse_column)); + std::shared_ptr unsorted_column(new tatami_test::ReversedIndicesWrapper(sparse_column)); compare_double_vectors_of_vectors(expected, tatami_stats::grouped_variances::by_column(unsorted_column.get(), rgroups.data())); } @@ -143,7 +162,14 @@ TEST(GroupedVariances, ByColumnWithNan) { size_t NR = 99, NC = 155; // Sprinkling in some NaNs. - auto simulated = tatami_test::simulate_sparse_vector(NR * NC, 0.5, 1, 2); + auto simulated = tatami_test::simulate_vector(NR * NC, []{ + tatami_test::SimulateVectorOptions opt; + opt.density = 0.3; + opt.lower = 1; + opt.upper = 2; + opt.seed = 191188; + return opt; + }()); for (size_t c = 0; c < NC; ++c) { simulated[(c % NR) * NC + c] = std::numeric_limits::quiet_NaN(); } @@ -210,7 +236,14 @@ TEST(GroupedVariances, EdgeCases) { TEST(GroupedVariances, NewType) { size_t NR = 198, NC = 52; - auto dump = tatami_test::simulate_sparse_vector(NR * NC, 0.1, /* lower = */ 1, /* upper = */ 100); + auto dump = tatami_test::simulate_vector(NR * NC, []{ + tatami_test::SimulateVectorOptions opt; + opt.density = 0.1; + opt.lower = 1; + opt.upper = 100; + opt.seed = 28928289; + return opt; + }()); for (auto& d : dump) { d = std::round(d); } @@ -247,7 +280,13 @@ TEST(GroupedVariances, NewType) { TEST(GroupedVariances, DirtyOutputs) { int NR = 56, NC = 179; - auto simulated = tatami_test::simulate_sparse_vector(NR * NC, 0.3); + auto simulated = tatami_test::simulate_vector(NR * NC, []{ + tatami_test::SimulateVectorOptions opt; + opt.density = 0.3; + opt.seed = 18761283; + return opt; + }()); + auto dense_row = std::shared_ptr(new tatami::DenseRowMatrix(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); diff --git a/tests/src/medians.cpp b/tests/src/medians.cpp index 9484b04..ddc8aef 100644 --- a/tests/src/medians.cpp +++ b/tests/src/medians.cpp @@ -205,7 +205,16 @@ TEST(ComputingDimMedians, SparseMedians) { // 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-positive values. - auto dense_row = std::unique_ptr(new tatami::DenseRowMatrix(NR, NC, tatami_test::simulate_sparse_vector(NR * NC, 0.5, 1, 10))); + auto vec = tatami_test::simulate_vector(NR * NC, []{ + tatami_test::SimulateVectorOptions opt; + opt.density = 0.5; + opt.lower = 1; + opt.upper = 10; + opt.seed = 734686273; + return opt; + }()); + + auto dense_row = std::unique_ptr(new tatami::DenseRowMatrix(NR, NC, vec)); auto dense_column = tatami::convert_to_dense(dense_row.get(), false); auto sparse_row = tatami::convert_to_compressed_sparse(dense_row.get(), true); auto sparse_column = tatami::convert_to_compressed_sparse(dense_row.get(), false); @@ -237,10 +246,10 @@ TEST(ComputingDimMedians, SparseMedians) { EXPECT_EQ(cref, tatami_stats::medians::by_column(sparse_column.get(), mopt)); // Checking same results from matrices that can yield unsorted indices. - std::shared_ptr unsorted_row(new tatami_test::UnsortedWrapper(sparse_row)); + std::shared_ptr unsorted_row(new tatami_test::ReversedIndicesWrapper(sparse_row)); EXPECT_EQ(rref, tatami_stats::medians::by_row(unsorted_row.get())); EXPECT_EQ(cref, tatami_stats::medians::by_column(unsorted_row.get())); - std::shared_ptr unsorted_column(new tatami_test::UnsortedWrapper(sparse_column)); + std::shared_ptr unsorted_column(new tatami_test::ReversedIndicesWrapper(sparse_column)); EXPECT_EQ(rref, tatami_stats::medians::by_row(unsorted_column.get())); EXPECT_EQ(cref, tatami_stats::medians::by_column(unsorted_column.get())); } @@ -249,7 +258,14 @@ TEST(ComputingMedians, WithNan) { size_t NR = 152, NC = 183; { - auto dump = tatami_test::simulate_sparse_vector(NR * NC, 0.5); + auto dump = tatami_test::simulate_vector(NR * NC, []{ + tatami_test::SimulateVectorOptions opt; + opt.density = 0.5; // using a density of 0.5 with all-positive values to make things interesting, see above. + opt.lower = 1; + opt.upper = 5; + opt.seed = 182761; + return opt; + }()); for (size_t c = 0; c < NC; ++c) { // Injecting an NaN at the start of each column. dump[c] = std::numeric_limits::quiet_NaN(); } @@ -272,7 +288,14 @@ TEST(ComputingMedians, WithNan) { } { - auto dump = tatami_test::simulate_sparse_vector(NR * NC, 0.5); + auto dump = tatami_test::simulate_vector(NR * NC, []{ + tatami_test::SimulateVectorOptions opt; + opt.density = 0.5; // using a density of 0.5 with all-negative values to make things interesting, see above. + opt.lower = -10; + opt.upper = -2; + opt.seed = 19988; + return opt; + }()); for (size_t r = 0; r < NR; ++r) { // Injecting an NaN at the start of each row. dump[r * NC] = std::numeric_limits::quiet_NaN(); } @@ -339,7 +362,13 @@ class MedianTriangularTest : public ::testing::TestWithParam { TEST_P(MedianTriangularTest, Positive) { size_t order = GetParam(); - auto dump = tatami_test::simulate_dense_vector(order * order, 0.1, 1); + auto dump = tatami_test::simulate_vector(order * order, [&]{ + tatami_test::SimulateVectorOptions opt; + opt.lower = 1; // all non-zeros are positive. + opt.upper = 5; + opt.seed = 191874 + order; + return opt; + }()); triangularize(order, dump); auto dense_row = std::unique_ptr(new tatami::DenseRowMatrix(order, order, dump)); @@ -361,9 +390,14 @@ TEST_P(MedianTriangularTest, Positive) { } TEST_P(MedianTriangularTest, Negative) { - // Seeing what happens if all non-zeros are less than zero. size_t order = GetParam(); - auto dump = tatami_test::simulate_dense_vector(order * order, -2, -0.1); + auto dump = tatami_test::simulate_vector(order * order, [&]{ + tatami_test::SimulateVectorOptions opt; + opt.lower = -2; + opt.upper = -0.1; // Seeing what happens if all non-zeros are less than zero. + opt.seed = 812763 + order; + return opt; + }()); triangularize(order, dump); auto dense_row = std::unique_ptr(new tatami::DenseRowMatrix(order, order, dump)); @@ -385,9 +419,15 @@ TEST_P(MedianTriangularTest, Negative) { } TEST_P(MedianTriangularTest, Mixed) { - // Mixing up the ratios of non-zeros on both sides of zero. size_t order = GetParam(); - auto dump = tatami_test::simulate_dense_vector(order * order, -2, 2); + auto dump = tatami_test::simulate_vector(order * order, [&]{ + tatami_test::SimulateVectorOptions opt; + // Mixing up the ratios of non-zeros on both sides of zero. + opt.lower = -2; + opt.upper = 2; + opt.seed = 283764283 + order; + return opt; + }()); triangularize(order, dump); auto dense_row = std::unique_ptr(new tatami::DenseRowMatrix(order, order, dump)); @@ -428,10 +468,18 @@ TEST(ComputingDimMedians, RowMediansNaN) { TEST(ComputingDimMedians, NewType) { size_t NR = 198, NC = 52; - auto dump = tatami_test::simulate_sparse_vector(NR * NC, 0.1, /* lower = */ 1, /* upper = */ 100); + auto dump = tatami_test::simulate_vector(NR * NC, []{ + tatami_test::SimulateVectorOptions opt; + opt.density = 0.5; // using 0.5 to make things interesting, see above. + opt.lower = 1; + opt.upper = 100; + opt.seed = 293876423; + return opt; + }()); for (auto& d : dump) { d = std::round(d); } + auto ref = std::unique_ptr(new tatami::DenseRowMatrix(NR, NC, dump)); auto rexpected = tatami_stats::medians::by_row(ref.get()); auto cexpected = tatami_stats::medians::by_column(ref.get()); @@ -455,7 +503,15 @@ TEST(ComputingDimMedians, NewType) { TEST(ComputingDimMedians, DirtyOutput) { size_t NR = 99, NC = 152; - auto dump = tatami_test::simulate_sparse_vector(NR * NC, 0.5, 1, 10); // see comments above about why we use 0.5. + auto dump = tatami_test::simulate_vector(NR * NC, []{ + tatami_test::SimulateVectorOptions opt; + opt.density = 0.5; // using 0.5 to make things interesting, see above. + opt.lower = 1; + opt.upper = 10; + opt.seed = 187181; + return opt; + }()); + auto dense_row = std::unique_ptr(new tatami::DenseRowMatrix(NR, NC, dump)); auto dense_column = tatami::convert_to_dense(dense_row.get(), false); auto sparse_row = tatami::convert_to_compressed_sparse(dense_row.get(), true); diff --git a/tests/src/ranges.cpp b/tests/src/ranges.cpp index 1f2ba6f..c995e95 100644 --- a/tests/src/ranges.cpp +++ b/tests/src/ranges.cpp @@ -5,18 +5,22 @@ #include "tatami_stats/ranges.hpp" #include "tatami_test/tatami_test.hpp" -class ComputingDimExtremesTest : public ::testing::TestWithParam > { -protected: - static std::vector simulate(size_t NR, size_t NC, std::pair limits) { - return tatami_test::simulate_sparse_vector(NR * NC, 0.1, limits.first, limits.second); - } -}; +class ComputingDimExtremesTest : public ::testing::TestWithParam > {}; /********************************************/ TEST_P(ComputingDimExtremesTest, RowRanges) { size_t NR = 75, NC = 62; - auto dump = simulate(NR, NC, GetParam()); + auto limits = GetParam(); + auto dump = tatami_test::simulate_vector(NR * NC, [&]{ + tatami_test::SimulateVectorOptions opt; + opt.density = 0.1; + opt.lower = limits.first; + opt.upper = limits.second; + opt.seed = 1239817 + limits.first * 1000 + limits.second; + return opt; + }()); + auto dense_row = std::unique_ptr(new tatami::DenseRowMatrix(NR, NC, dump)); auto dense_column = tatami::convert_to_dense(dense_row.get(), false); auto sparse_row = tatami::convert_to_compressed_sparse(dense_row.get(), true); @@ -49,9 +53,9 @@ TEST_P(ComputingDimExtremesTest, RowRanges) { EXPECT_EQ(ref, tatami_stats::ranges::by_row(sparse_column.get(), ropt)); // Checking same results from matrices that can yield unsorted indices. - std::shared_ptr unsorted_row(new tatami_test::UnsortedWrapper(sparse_row)); + std::shared_ptr unsorted_row(new tatami_test::ReversedIndicesWrapper(sparse_row)); EXPECT_EQ(ref, tatami_stats::ranges::by_row(unsorted_row.get())); - std::shared_ptr unsorted_column(new tatami_test::UnsortedWrapper(sparse_column)); + std::shared_ptr unsorted_column(new tatami_test::ReversedIndicesWrapper(sparse_column)); EXPECT_EQ(ref, tatami_stats::ranges::by_row(unsorted_column.get())); // Checking correct behavior with dirty output buffers, and sometimes @@ -90,7 +94,15 @@ TEST_P(ComputingDimExtremesTest, RowRanges) { TEST_P(ComputingDimExtremesTest, RowRangesWithNan) { size_t NR = 52, NC = 83; - auto dump = tatami_test::simulate_sparse_vector(NR * NC, 0.1); + auto limits = GetParam(); + auto dump = tatami_test::simulate_vector(NR * NC, [&]{ + tatami_test::SimulateVectorOptions opt; + opt.density = 0.1; + opt.lower = limits.first; + opt.upper = limits.second; + opt.seed = 543343 + limits.first * 1000 + limits.second; + return opt; + }()); for (size_t r = 0; r < NR; ++r) { // Injecting an NaN at the start. dump[r * NC] = std::numeric_limits::quiet_NaN(); } @@ -124,7 +136,16 @@ TEST_P(ComputingDimExtremesTest, RowRangesWithNan) { TEST_P(ComputingDimExtremesTest, ColumnRanges) { size_t NR = 111, NC = 52; - auto dump = simulate(NR, NC, GetParam()); + auto limits = GetParam(); + auto dump = tatami_test::simulate_vector(NR * NC, [&]{ + tatami_test::SimulateVectorOptions opt; + opt.density = 0.1; + opt.lower = limits.first; + opt.upper = limits.second; + opt.seed = 9919998 + limits.first * 1000 + limits.second; + return opt; + }()); + auto dense_row = std::unique_ptr(new tatami::DenseRowMatrix(NR, NC, dump)); auto dense_column = tatami::convert_to_dense(dense_row.get(), false); auto sparse_row = tatami::convert_to_compressed_sparse(dense_row.get(), true); @@ -157,15 +178,23 @@ TEST_P(ComputingDimExtremesTest, ColumnRanges) { EXPECT_EQ(ref, tatami_stats::ranges::by_column(sparse_column.get(), ropt)); // Checking same results from matrices that can yield unsorted indices. - std::shared_ptr unsorted_row(new tatami_test::UnsortedWrapper(sparse_row)); + std::shared_ptr unsorted_row(new tatami_test::ReversedIndicesWrapper(sparse_row)); EXPECT_EQ(ref, tatami_stats::ranges::by_column(unsorted_row.get())); - std::shared_ptr unsorted_column(new tatami_test::UnsortedWrapper(sparse_column)); + std::shared_ptr unsorted_column(new tatami_test::ReversedIndicesWrapper(sparse_column)); EXPECT_EQ(ref, tatami_stats::ranges::by_column(unsorted_column.get())); } TEST_P(ComputingDimExtremesTest, ColumnRangesWithNan) { size_t NR = 52, NC = 83; - auto dump = tatami_test::simulate_sparse_vector(NR * NC, 0.1); + auto limits = GetParam(); + auto dump = tatami_test::simulate_vector(NR * NC, [&]{ + tatami_test::SimulateVectorOptions opt; + opt.density = 0.1; + opt.lower = limits.first; + opt.upper = limits.second; + opt.seed = 1919191 + limits.first * 1000 + limits.second; + return opt; + }()); for (size_t c = 0; c < NC; ++c) { // Injecting an NaN at the start. dump[c] = std::numeric_limits::quiet_NaN(); } @@ -284,10 +313,18 @@ TEST(ComputingDimExtremes, NoZeros) { TEST(ComputingDimExtremes, NewType) { size_t NR = 198, NC = 52; - auto dump = tatami_test::simulate_sparse_vector(NR * NC, 0.1, /* lower = */ 1, /* upper = */ 100); + auto dump = tatami_test::simulate_vector(NR * NC, []{ + tatami_test::SimulateVectorOptions opt; + opt.density = 0.1; + opt.lower = 1; + opt.upper = 100; + opt.seed = 29842; + return opt; + }()); for (auto& d : dump) { d = std::round(d); } + auto ref = std::unique_ptr(new tatami::DenseRowMatrix(NR, NC, dump)); auto rexpected = tatami_stats::ranges::by_row(ref.get()); auto cexpected = tatami_stats::ranges::by_column(ref.get()); diff --git a/tests/src/sums.cpp b/tests/src/sums.cpp index 46b9bf5..12c7c68 100644 --- a/tests/src/sums.cpp +++ b/tests/src/sums.cpp @@ -8,7 +8,13 @@ TEST(ComputingDimSums, RowSums) { size_t NR = 99, NC = 152; - auto dump = tatami_test::simulate_sparse_vector(NR * NC, 0.1); + auto dump = tatami_test::simulate_vector(NR * NC, []{ + tatami_test::SimulateVectorOptions opt; + opt.density = 0.1; + opt.seed = 238947239; + return opt; + }()); + auto dense_row = std::unique_ptr(new tatami::DenseRowMatrix(NR, NC, dump)); auto dense_column = tatami::convert_to_dense(dense_row.get(), false); auto sparse_row = tatami::convert_to_compressed_sparse(dense_row.get(), true); @@ -38,7 +44,12 @@ TEST(ComputingDimSums, RowSums) { TEST(ComputingDimSums, RowSumsWithNan) { size_t NR = 52, NC = 83; - auto dump = tatami_test::simulate_sparse_vector(NR * NC, 0.1); + auto dump = tatami_test::simulate_vector(NR * NC, []{ + tatami_test::SimulateVectorOptions opt; + opt.density = 0.1; + opt.seed = 992929; + return opt; + }()); for (size_t r = 0; r < NR; ++r) { // Injecting an NaN at the start. dump[r * NC] = std::numeric_limits::quiet_NaN(); } @@ -68,10 +79,15 @@ TEST(ComputingDimSums, RowSumsWithNan) { EXPECT_TRUE(is_all_nan(tatami_stats::sums::by_row(sparse_column.get()))); } - TEST(ComputingDimSums, ColumnSums) { size_t NR = 79, NC = 62; - auto dump = tatami_test::simulate_sparse_vector(NR * NC, 0.1); + auto dump = tatami_test::simulate_vector(NR * NC, []{ + tatami_test::SimulateVectorOptions opt; + opt.density = 0.1; + opt.seed = 12919010; + return opt; + }()); + auto dense_row = std::unique_ptr(new tatami::DenseRowMatrix(NR, NC, dump)); auto dense_column = tatami::convert_to_dense(dense_row.get(), false); auto sparse_row = tatami::convert_to_compressed_sparse(dense_row.get(), true); @@ -100,7 +116,12 @@ TEST(ComputingDimSums, ColumnSums) { TEST(ComputingDimSums, ColumnSumsWithNan) { size_t NR = 82, NC = 33; - auto dump = tatami_test::simulate_sparse_vector(NR * NC, 0.1); + auto dump = tatami_test::simulate_vector(NR * NC, []{ + tatami_test::SimulateVectorOptions opt; + opt.density = 0.1; + opt.seed = 8768372; + return opt; + }()); for (size_t c = 0; c < NC; ++c) { // Injecting an NaN at the start. dump[c] = std::numeric_limits::quiet_NaN(); } @@ -132,10 +153,18 @@ TEST(ComputingDimSums, ColumnSumsWithNan) { TEST(ComputingDimSums, NewType) { size_t NR = 198, NC = 52; - auto dump = tatami_test::simulate_sparse_vector(NR * NC, 0.1, /* lower = */ 1, /* upper = */ 100); + auto dump = tatami_test::simulate_vector(NR * NC, []{ + tatami_test::SimulateVectorOptions opt; + opt.density = 0.1; + opt.lower = 1; + opt.upper = 100; + opt.seed = 8768372; + return opt; + }()); for (auto& d : dump) { d = std::round(d); } + auto ref = std::unique_ptr(new tatami::DenseRowMatrix(NR, NC, dump)); auto rexpected = tatami_stats::sums::by_row(ref.get()); auto cexpected = tatami_stats::sums::by_column(ref.get()); @@ -159,7 +188,13 @@ TEST(ComputingDimSums, NewType) { TEST(ComputingDimSums, DirtyOutput) { size_t NR = 99, NC = 152; - auto dump = tatami_test::simulate_sparse_vector(NR * NC, 0.1); + auto dump = tatami_test::simulate_vector(NR * NC, []{ + tatami_test::SimulateVectorOptions opt; + opt.density = 0.1; + opt.seed = 5936893; + return opt; + }()); + auto dense_row = std::unique_ptr(new tatami::DenseRowMatrix(NR, NC, dump)); auto dense_column = tatami::convert_to_dense(dense_row.get(), false); auto sparse_row = tatami::convert_to_compressed_sparse(dense_row.get(), true); diff --git a/tests/src/variances.cpp b/tests/src/variances.cpp index 70505a4..b7aaa33 100644 --- a/tests/src/variances.cpp +++ b/tests/src/variances.cpp @@ -8,7 +8,13 @@ TEST(ComputingDimVariances, RowVariances) { size_t NR = 109, NC = 82; - auto dump = tatami_test::simulate_sparse_vector(NR * NC, 0.1); + auto dump = tatami_test::simulate_vector(NR * NC, []{ + tatami_test::SimulateVectorOptions opt; + opt.density = 0.1; + opt.seed = 1987191; + return opt; + }()); + auto dense_row = std::unique_ptr(new tatami::DenseRowMatrix(NR, NC, dump)); auto dense_column = tatami::convert_to_dense(dense_row.get(), false); auto sparse_row = tatami::convert_to_compressed_sparse(dense_row.get(), true); @@ -45,7 +51,12 @@ TEST(ComputingDimVariances, RowVariances) { TEST(ComputingDimVariances, RowVariancesWithNan) { size_t NR = 52, NC = 83; - auto dump = tatami_test::simulate_sparse_vector(NR * NC, 0.1); + auto dump = tatami_test::simulate_vector(NR * NC, []{ + tatami_test::SimulateVectorOptions opt; + opt.density = 0.1; + opt.seed = 44982197; + return opt; + }()); for (size_t r = 0; r < NR; ++r) { // Injecting an NaN at the start. dump[r * NC] = std::numeric_limits::quiet_NaN(); } @@ -85,7 +96,13 @@ TEST(ComputingDimVariances, RowVariancesWithNan) { TEST(ComputingDimVariances, ColumnVariances) { size_t NR = 99, NC = 92; - auto dump = tatami_test::simulate_sparse_vector(NR * NC, 0.1); + auto dump = tatami_test::simulate_vector(NR * NC, []{ + tatami_test::SimulateVectorOptions opt; + opt.density = 0.1; + opt.seed = 21823818; + return opt; + }()); + auto dense_row = std::unique_ptr(new tatami::DenseRowMatrix(NR, NC, dump)); auto dense_column = tatami::convert_to_dense(dense_row.get(), false); auto sparse_row = tatami::convert_to_compressed_sparse(dense_row.get(), true); @@ -122,7 +139,12 @@ TEST(ComputingDimVariances, ColumnVariances) { TEST(ComputingDimVariances, ColumnVariancesWithNan) { size_t NR = 82, NC = 33; - auto dump = tatami_test::simulate_sparse_vector(NR * NC, 0.1); + auto dump = tatami_test::simulate_vector(NR * NC, []{ + tatami_test::SimulateVectorOptions opt; + opt.density = 0.1; + opt.seed = 191353; + return opt; + }()); for (size_t c = 0; c < NC; ++c) { // Injecting an NaN at the start. dump[c] = std::numeric_limits::quiet_NaN(); } @@ -162,10 +184,18 @@ TEST(ComputingDimVariances, ColumnVariancesWithNan) { TEST(ComputingDimVariances, NewType) { size_t NR = 198, NC = 52; - auto dump = tatami_test::simulate_sparse_vector(NR * NC, 0.1, /* lower = */ 1, /* upper = */ 100); + auto dump = tatami_test::simulate_vector(NR * NC, []{ + tatami_test::SimulateVectorOptions opt; + opt.density = 0.1; + opt.lower = 1; + opt.upper = 100; + opt.seed = 1982719; + return opt; + }()); for (auto& d : dump) { d = std::round(d); } + auto ref = std::unique_ptr(new tatami::DenseRowMatrix(NR, NC, dump)); auto rexpected = tatami_stats::variances::by_row(ref.get()); auto cexpected = tatami_stats::variances::by_column(ref.get()); @@ -189,7 +219,13 @@ TEST(ComputingDimVariances, NewType) { TEST(ComputingDimVariances, DirtyOutput) { size_t NR = 99, NC = 152; - auto dump = tatami_test::simulate_sparse_vector(NR * NC, 0.1); + auto dump = tatami_test::simulate_vector(NR * NC, []{ + tatami_test::SimulateVectorOptions opt; + opt.density = 0.1; + opt.seed = 457633478; + return opt; + }()); + auto dense_row = std::unique_ptr(new tatami::DenseRowMatrix(NR, NC, dump)); auto dense_column = tatami::convert_to_dense(dense_row.get(), false); auto sparse_row = tatami::convert_to_compressed_sparse(dense_row.get(), true);