From 036ea1b4eba030b35448151f60f67e1791eae1f5 Mon Sep 17 00:00:00 2001 From: LTLA Date: Wed, 24 Jul 2024 14:09:53 -0700 Subject: [PATCH] Added some tests for the exported utilities. --- tests/CMakeLists.txt | 1 + tests/src/utils.cpp | 71 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 tests/src/utils.cpp diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 8310ce3..349ff8c 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -33,6 +33,7 @@ macro(create_test target) src/grouped_medians.cpp src/grouped_sums.cpp src/grouped_variances.cpp + src/utils.cpp ) target_link_libraries( diff --git a/tests/src/utils.cpp b/tests/src/utils.cpp new file mode 100644 index 0000000..ae25be8 --- /dev/null +++ b/tests/src/utils.cpp @@ -0,0 +1,71 @@ +#include + +#include "tatami_stats/utils.hpp" + +TEST(Utilities, Groups) { + { + std::vector groups { 0, 0, 1, 1, 2, 2 }; + EXPECT_EQ(3, tatami_stats::total_groups(groups.data(), groups.size())); + EXPECT_EQ(0, tatami_stats::total_groups(groups.data(), 0)); + } + + { + std::vector groups { 5, 3, 2 }; + EXPECT_EQ(6, tatami_stats::total_groups(groups.data(), groups.size())); + } + + { + std::vector groups { 0, 0, 0, 1, 1, 2 }; + std::vector expected { 3, 2, 1 }; + auto observed = tatami_stats::tabulate_groups(groups.data(), groups.size()); + EXPECT_EQ(expected, observed); + } + + { + std::vector groups { 5 }; + std::vector expected { 0, 0, 0, 0, 0, 1 }; + auto observed = tatami_stats::tabulate_groups(groups.data(), groups.size()); + EXPECT_EQ(expected, observed); + } +} + +TEST(Utilities, OutputBuffer) { + { + std::vector foo(10); + tatami_stats::LocalOutputBuffer buffer(0, 5, 3, foo.data(), 1); + EXPECT_EQ(foo[4], 0); // directly uses the buffer. + EXPECT_EQ(foo[5], 1); + EXPECT_EQ(foo[6], 1); + EXPECT_EQ(foo[7], 1); + EXPECT_EQ(foo[8], 0); + + buffer.data()[0] = 3; + EXPECT_EQ(foo[5], 3); + const auto& cbuffer = buffer; // check the const overload. + EXPECT_EQ(cbuffer.data()[0], 3); + + buffer.transfer(); + EXPECT_EQ(foo[4], 0); // directly uses the buffer. + EXPECT_EQ(foo[5], 3); + EXPECT_EQ(foo[6], 1); + EXPECT_EQ(foo[7], 1); + EXPECT_EQ(foo[8], 0); + } + + { + std::vector foo(10); + tatami_stats::LocalOutputBuffer buffer(1, 5, 3, foo.data(), 2); + + buffer.data()[0] = 3; + const auto& cbuffer = buffer; // checking the const overload. + EXPECT_EQ(cbuffer.data()[0], 3); + EXPECT_EQ(foo, std::vector(10)); // buffer is not used yet... + + buffer.transfer(); + EXPECT_EQ(foo[4], 0); + EXPECT_EQ(foo[5], 3); + EXPECT_EQ(foo[6], 2); + EXPECT_EQ(foo[7], 2); + EXPECT_EQ(foo[8], 0); + } +}